Zend_Auth and Firebird DB - firebird

A short question for the PROs.
Is it possible to use Zend_Auth_Adapter_DbTable with ZendX_Db_Adapter?
I've tried this:
$adapter = new Zend_Auth_Adapter_DbTable(
Zend_Registry::get('db')
);
$adapter->setTableName('USERS')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setIdentity('FOO')
->setCredential('BAR');
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate();
But it doesn't work.
ErrorMsg:
Catchable fatal error: Argument 1 passed to Zend_Auth::authenticate() must implement interface Zend_Auth_Adapter_Interface, none given, called in D:\xampp\htdocs\liquisales-online\application\controllers\IndexController.php on line 35 and defined in D:\xampp\htdocs\liquisales-online\library\Zend\Auth.php on line 115
Any hints?
Btw. ZendX_Db_Adapter ist registerd in application.ini
resources.db.adapter = Firebird
resources.db.params.dbname = "/var/db/liquisales.FDB"
resources.db.params.host = "127.0.0.1"
resources.db.params.username = sysdba
resources.db.params.password = masterkey
resources.db.params.adapterNamespace = "ZendX_Db_Adapter"

Okay, here is the right way to use Firebird DB with Zend_Auth.
At first we had to use capitol letters for column names.
Furthermore I've forgotten to pass the adapter.
Here's the right code.
$adapter = new Zend_Auth_Adapter_DbTable(
Zend_Registry::get('db')
);
$adapter->setTableName('USERS')
->setIdentityColumn('USERNAME')
->setCredentialColumn('PASSWORD')
->setIdentity($loginForm->getValue('kunummer'))
->setCredential($loginForm->getValue('passwd'));
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);

it seems your adapter doesn't implement all the methods needed .... so you would have to code the authentication yourself.

Related

Zend authenticate returns white screen

This is the most frustrating ever. It is nearly impossible to find errors when all you get is a white screen!!!
This code is used on other projects and it works fine there so syntactically, it is correct. But SOMETHING must be wrong in the configuration...
Here is the code:
protected function _process($values)
{
// Get our authentication adapter and check credentials
$adapter = $this->_getAuthAdapter();
$adapter->setIdentity($values['username']);
$adapter->setCredential($values['password']);
$auth = Zend_Auth::getInstance();
$result = $auth->authenticate($adapter);
if ($result->isValid()) {
$user = $adapter->getResultRowObject();
$auth->getStorage()->write($user);
return true;
}
return false;
}
protected function _getAuthAdapter()
{
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
$authAdapter->setTableName('Users')
->setIdentityColumn('username')
->setCredentialColumn('password')
->setCredentialTreatment('md5(?)');
return $authAdapter;
}
This is in my auth controller and gets called after I set up the adapter, etc. If I put a die("foo"); right before the $result line, I see it. If I put it right after the $result line, I get a WSOD and the system stops. I know there is not enough here for anyone to debug my code but I was hoping someone else had had this problem and could give me a hint as to what to try to fix this??? I have double checked the database, the column names, etc. I need to know what kinds of things may make the line:
$result = $auth->authenticate($adapter);
result in a white screen of death??? Any ideas? I have all error display turned on in application.ini.
I am running Zend 1.11.12 on this server. Does that make a difference? The server where it is working is running is running 1.12.0-9
Thanks for any ideas you might have.
EDIT::: I added code for my _getAuthAdapter.
Enable the error reporting for your app. Set all error reporting to 1 in your configs/application.ini file -
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
Also, instead of returning true or false, try to print a message, or redirect to a different page to know.
Try a var_dump on the $adapter to see the resulting object.
I've never used Zend_Auth to authenticate. You should be able to do that with your adapter. (assuming your $adapter is an instance of Zend_Auth_Adapter_DbTable for example)
$adapter = $this->_getAuthAdapter();
// this will tell you if there's something wrong with your adapter
if (!($adapter instanceof Zend_Auth_Adapter_DbTable)) {
throw new Exception('invalid adapter');
}
$adapter->setIdentity($values['username']);
$adapter->setCredential($values['password']);
$result = $adapter->authenticate();
if ($result->isValid()) {
}
I tried like below,
public function loginAction() {
$this->_helper->layout->disableLayout();
$users = new Application_Model_DbTable_Admin();
$this->_redirector = $this->_helper->getHelper('Redirector');
$form = new Application_Form_LoginForm();
$this->view->form = $form;
if ($this->getRequest()->isPost()) {
if ($form->isValid($_POST)) {
$consumer = new Zend_OpenId_Consumer();
$username = $this->_request->getPost('username');
$password = $this->_request->getPost('password');
if ($username <> '' && $password <> '') {
$auth = Zend_Auth::getInstance();
$authAdapter = new Zend_Auth_Adapter_DbTable($users->getAdapter(), 'admin');
$authAdapter->setIdentityColumn('username')
->setCredentialColumn('password');
$authAdapter->setIdentity($username)
->setCredential(md5($password));
$result = $auth->authenticate($authAdapter);
if ($result->isValid()) {
$storage = new Zend_Auth_Storage_Session();
$storage->write($authAdapter->getResultRowObject());
$this->_auth_user = $auth->getStorage()->read();
$this->_redirect('admin/index/');
}
else
$this->view->errorMessage = "Invalid username or password. Please try again.";
}
else
$this->view->errorMessage = "Username or password should not be empty!!!.";
}
}
}
If your Adapter doesn't work try it:
public function _getAuthAdapter() {
$authAdapter = new Zend_Auth_Adapter_DbTable(Zend_Db_Table::getDefaultAdapter());
$authAdapter->setTableName('table_name')
->setIdentityColumn('user_name')
->setCredentialColumn('password');
return $authAdapter;
}
And in application.ini
resources.db.isDefaultTableAdapter = true

Zend session and zend auth

I have made a login system through zend auth here is the code
// userAuthentication
public function authAction(){
$request = $this->getRequest();
$registry = Zend_Registry::getInstance();
$auth = Zend_Auth::getInstance();
$DB = $registry['DB'];
$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
$authAdapter->setTableName('user')
->setIdentityColumn('user_name')
->setCredentialColumn('user_password');
$username = $request->getParam('username');
$password = $request->getParam('password');
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$data = $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);
$this->_redirect('/login/controlpannel');
}else{
$this->_redirect('/login/login');
}
}
This work fine now. There is user_id (column) in user (table) where there are username and password too. I need to get that specific user_id from this table which just login and put it in session through
$user_session = new Zend_Session_Namespace('user_session');
$user_session->username = $username;
$user_id->user_id = $user_id;
so that I can query some info against this $user_id and pass the result into view (name) controlpanel
Get user id from storage :
$userInfo = Zend_Auth::getInstance()->getStorage()->read();
echo $userInfo->user_id;
While this was already answered, I tend to use the getIdentity() function more frequently than the getStorage()->read() chain. Examples below.
// to check if authenticated
Zend_Auth::getInstance()->hasIdentity();
// to actually get the details from storage
Zend_Auth::getInstance()->getIdentity()->user_id;
// if I need to use the identity over and over
$identity = Zend_Auth::getInstance()->getIdentity();
$userId = $identity->user_id;
You can access the data the way Teez suggest or just pull it from Zend_Session_Namespace.
15.1.3.1. Default Persistence in the PHP Session By default, Zend_Auth provides persistent storage of the identity from a successful
authentication attempt using the PHP session. Upon a successful
authentication attempt, Zend_Auth::authenticate() stores the identity
from the authentication result into persistent storage. Unless
configured otherwise, Zend_Auth uses a storage class named
Zend_Auth_Storage_Session, which, in turn, uses Zend_Session. A custom
class may instead be used by providing an object that implements
Zend_Auth_Storage_Interface to Zend_Auth::setStorage().
Zend_Auth_Storage_Session uses a session namespace of 'Zend_Auth'.
This namespace may be overridden by passing a different value to the
constructor of Zend_Auth_Storage_Session, and this value is internally
passed along to the constructor of Zend_Session_Namespace. This should
occur before authentication is attempted, since
Zend_Auth::authenticate() performs the automatic storage of the
identity.
assigning an array to a session, you must provide a name to the session you area creating, i.e. you must do setStorage before you do getStorage.
you must write your code like this:
// userAuthentication
public function authAction(){
$request = $this->getRequest();
$registry = Zend_Registry::getInstance();
$auth = Zend_Auth::getInstance();
$DB = $registry['DB'];
$authAdapter = new Zend_Auth_Adapter_DbTable($DB);
$authAdapter->setTableName('user')
->setIdentityColumn('user_name')
->setCredentialColumn('user_password');
$username = $request->getParam('username');
$password = $request->getParam('password');
$authAdapter->setIdentity($username);
$authAdapter->setCredential($password);
$authAdapter->setStorage(new Zend_Auth_Storage_Session('User_Auth'));
$result = $auth->authenticate($authAdapter);
if($result->isValid()){
$data = $authAdapter->getResultRowObject(null,'password');
$auth->getStorage()->write($data);
$this->_redirect('/login/controlpannel');
}else{
$this->_redirect('/login/login');
}
}
and then to get your storage value, you must use this:
$x = new Zend_Auth_Storage_Session('User_Auth');
$y = $x->read();
and you get everything in $y as an object.
Enjoy!
This is my approach and it s working nice:
1-i start by defining an init function in the bootstrap
protected function _initSession()
{
$UserSession = new Zend_Session_Namespace('UserSession');
$UserSession->setExpirationSeconds(/* you may fix a limit */);
Zend_Registry::set('UserSession', $UserSession);
}
/* in the Login action,after correct username & pwd */
// Create session
$UserSession = Zend_Registry::get('UserSession');
// Get the user from database
$db = Zend_Db_Table::getDefaultAdapter();
$user = $db->fetchRow("SELECT * FROM user_table WHERE user_email = '".$user_email."'");
//then you assign to $user to $UserSession variable :
$UserSession->user = $user;
//finaly don't forget to unset session variable in the Logout action ...
user = Zend_Auth::getInstance()->getIdentity();
if(!#$this->user){
$objSession->errorMsg = " Please Login First .. ! ";
$this->_redirect('/user/login');
}
?>

Zend config inheritence

I have these values in my application.ini
[production]
; Database;
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user1"
resources.db.params.password = "password1"
resources.db.params.dbname = "projects__01"
;Names
website.settings.websiteName = "My website 1"
website.settings.websiteUrl = "http://www.mydomain1.com"
website.settings.title = "mydomain.com - mydomain"
website.settings.titleSeperator = " - "
[staging : production]
; Database;
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user2"
resources.db.params.password = "password2"
resources.db.params.dbname = "projects__02"
;Exceptions
phpSettings.display_startup_errors = 1
phpSettings.display_errors = 1
resources.frontController.params.displayExceptions = 1
;Title and url
website.settings.websiteName = "My website 2"
website.settings.websiteUrl = "http://www.mydomain2.com"
[development : staging]
;Database
resources.db.adapter = "pdo_mysql"
resources.db.params.host = "localhost"
resources.db.params.username = "user3"
resources.db.params.password = "password3"
resources.db.params.dbname = "projects__03"
;Title and url
website.settings.websiteName = "My website 3"
website.settings.websiteUrl = "http://www.mydomain3.com"
The problem is all database and exception values work properly, meaning that they are inheriting properly as they are supposed to
But the values I have set for Title and url do not inherit properly, only the first defined ones are used.
Why is this? is this by design? are only predefined/standard environment values such as database and exceptions are inherited?
Or am I making a mistake somewhere?
Okay, from your comment it sounds like you are creating a new Zend_Config object in the bootstrap, putting this in the registry, and it's this that's not returning what you'd expect. If this is the case I'm guessing that you've omitted the second parameter on the config object, so you have something like this:
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini');
but what you should have is more like this:
$config = new Zend_Config_Ini(APPLICATION_PATH.'/configs/application.ini', APPLICATION_ENV);
the second param tells it what section of the config file to use, without that it will always get the same value.
However, you don't actually need to re-parse the config file since Zend Application has done this already. Instead you can access the options from the bootstrap class and use these to create an object (or just store the options in their existing array format):
protected function _initConfig()
{
$config = new Zend_Config($this->getOptions());
Zend_Registry::set('config', $config);
return $config;
}
and this should work as you expect.
If my guess was wrong, please can you edit your question to include the relevant part of your bootstrap where the config object is created and stored in the registry.

Using a Zend_Db Database connection

I'm trying to teach myself the Zend Framework. I have some extensive experience using custom-frameworks but have never used Zend. It's like trying to use a knife and fork with mittens on.
I've got a system up and running. A database connection is being created in the applition.ini file without errors.
The point I'm at is trying to use this connection to the database to execute basic SQL. The application.ini has the lines:
db.adapter = PDO_MYSQL
db.params.host = localhost
db.params.username = cpanel_dbuser
db.params.password = 123456
db.params.dbname = cpanel_dbname
I'm trying to connect to the database in my /public/index.php
$config = new Zend_Config_Ini(APPLICATION_PATH .
'/configs/application.ini', 'production');
$application->db = Zend_Db::factory($config->db);
Zend_Db_Table::setDefaultAdapter($db);
The error I get is:
Fatal error: Uncaught exception 'Zend_Db_Exception' with message 'Adapter name must be specified in a string' in /home/path/library/Zend/Db.php:226
Stack trace:
#0 /home/path/public/index.php(32): Zend_Db::factory(Array)
#1 {main} thrown in /home/path/library/Zend/Db.php on line 226
And if I print_r the Config Object
Zend_Config Object
(
[_allowModifications:protected] =>
[_index:protected] => 0
[_count:protected] => 2
[_data:protected] => Array
(
[adapter] => PDO_MYSQL
/* ... more stuff ... */
From my understanding of the tutorials & PDF's I'm working from, if I get this connection working I'll be able to do such fantastical amazements as the following from within a controller's indexAction
$data = $this->db->fetchAll(‘SELECT * FROM table’);
foreach ($data as $row) {
echo $row[‘table_fieldname’];
}
And then start writing models for the tables I have.
At this point, there's a pretty serious temptation to cut-the-corner and do this the way I already know how, but that completely defeats the purpose of learning to work within the framework.
Can anyone bridge the gap for me (or point me to a resource that can clear this up)?
In application.ini file inside config directory do
resources.db.adapter = PDO_MYSQL
resources.db.isDefaultAdapter = true
resources.db.params.host = localhost
resources.db.params.username = username
resources.db.params.password = pwd
resources.db.params.dbname = mydatabase
and whenever/wherever you need $db just do
$db = Zend_Db_Table::getDefaultAdapter()
this is the best way to do it.
public function ActionnameAction()
{
$params=array(
'host' =>'localhost',
'username' =>'username',
'password' =>'password',
'dbname' =>'database_name');
$db = Zend_Db::Factory('PDO_MYSQL',$params);
$sql="select * from table_name";
$result=$db->fetchAll($sql);
}
If I don't remember wrong, the adapters are case sensitive. You should use Pdo_Mysql. The exception you are receiving doesn't seems to be related to this though.

Registering Zend Database Adapter in Registry

I am looking to register a reference to the main Database Adapter in the Registry during Bootstrapping so it can be used elsewhere in my site (specifically the Authorisation action).
I have implemented an ugly fix where i create a Database Table object and call the getAdapter() method on it and pass through that. However, this is a bad way of doing it and I would like it to be available via the registry.
Does anyone know how to do this? Any help or pointers in the right direction are appreciated!
I'm using Zend Framework 1.8.
If you're using Zend Framework 1.8+, and created your project with the command line tool, then it's as simple as registering your database settings in your application.ini config file.
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "your.database.host"
resources.db.params.dbname = "database_name"
resources.db.params.username = "username"
resources.db.params.password = "password"
resources.db.isDefaultTableAdapter = true
If your database settings are preceded by resources.db you won't even need to do anything in your Bootstrap.php file because it will do it for you. Also, by setting the isDefaultTableAdapter setting to true, you can get an instance of your database adapter anywhere in your application.
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
$authAdapter = new Zend_Auth_Adapter_DbTable($dbAdapter);
Thanks for the replies. Ive decided to change the accepted answer and post the solution I finally used - which is insanely simple in the end!!
This is basically based on Dcaunt's comment...
In the bootstrap class..
protected function _initDb()
{
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();
Zend_Registry::set("db", $db);
}
Then access that elsewhere with...
$dbAdapter = Zend_Registry::get("db");
Thanks for the help and hopefully this helps someone else.
Your missing the best thing :)
If you use the Zend_Db_Table models (you should be) etc then you can set up a default adaptor - this way when you instantiate a model the DB connection it taken care off - this way you dont really need to save it in the registry or bother about connection before running a query through the model.
I do save it in the registry for later use if needed though - but I may remove this
protected function _initDB()
{
// Check that the config contains the correct database array.
if ($this->_config->db) {
// Instantiate the DB factory
$dbAdapter = Zend_Db::factory($this->_config->db);
// Set the DB Table default adaptor for auto connection in the models
Zend_Db_Table::setDefaultAdapter($dbAdapter);
// Add the DB Adaptor to the registry if we need to call it outside of the modules.
Zend_Registry::set('dbAdapter', $dbAdapter);
}
}
My 2 cents...
How to grab the default DB Adapter:
From Bootstrap:
<?php
$dbResource = $this->getPluginResource('db');
db = $dbResource->getDbAdapter();
var_dump($db);
?>
From a Controller there are two methods:
<?php
// Method 1
$bootstrap = $this->getInvokeArg('bootstrap');
$dbResource = $bootstrap->getPluginResource('db');
$dbAdapter = $dbResource->getDbAdapter();
var_dump($dbAdapter);
// Method 2
$dbAdapter = Zend_Db_Table::getDefaultAdapter();
var_dump($dbAdapter);
?>
Check the zend-documentation at :
15.5.3.3. Storing a Database Adapter in the Registry
http://framework.zend.com/manual/en/zend.db.table.html
$db = Zend_Db::factory('PDO_MYSQL', $options);
Zend_Registry::set('my_db', $db);
// Later...
$table = new Bugs(array('db' => 'my_db'));
something like that you're looking for?
Edit:
to load your configuration from an ini-file, use:
parse_ini_file($inifile)
;configuration.ini
host = 127.0.0.1
user = username
password = blabla
;yourfile.php
$options = parse_ini_file('configuration.ini');
$db = Zend_Db::factory('PDO_MYSQL', $options);
I have a method in my bootstrap to add the adapter to the registry. I'd prefer a cleaner solution, but it works:
protected function _initRegistry(){
$this->bootstrap('db');
$db = $this->getResource('db');
$db->setFetchMode(Zend_Db::FETCH_OBJ);
Zend_Registry::set('db', $db);
}
Here is what i do:
Inside the bootstrap:
define('CONFIG_FILE', '../config/general.ini');
define('APP_MODE', 'development');
Inside the Initializer:
/**
* Initialize data bases
*
* #return void
*/
public function initDb ()
{
$options = Zend_Registry::get('conf');
$db = Zend_Db::factory($options->database);
$db->query(new Zend_Db_Expr('SET NAMES utf8'));
Zend_Registry::set('db', $db);
}
public function initConfig ()
{
if (file_exists(CONFIG_FILE) && is_readable(CONFIG_FILE)) {
$conf = new Zend_Config_Ini(CONFIG_FILE, APP_MODE);
Zend_Registry::set('conf', $conf);
} else {
throw new Zend_Config_Exception('Unable to load config file');
}
}
And finaly my config file looks like this:
[production]
database.adapter = pdo_Mysql
database.params.host = db.example.com
database.params.username = dbuser
database.params.password = secret
database.params.dbname = dbname
; Overloaded configuration from production
[development : production]
database.params.host = localhost
database.params.username = root
database.params.password =
Take a look at:
Zend_Db::Factory()
Zend_Config_Ini
Zend_Registry
If you are using Zend Framework 1.8 just do something like this in your controller/action:
class CreateorderController extends Zend_Controller_Action
{
public function testAction()
{
//more code
$users_obj = new Default_Model_Users(); //this would load the model using the Default namespace
//more code
}
}
My Defaul_Model_Users class would look something like this:
<?php
/**
* application/models/Users.php
*/
class Default_Model_Users extends Zend_Db_Table
{
protected $_table;
public function getTable()
{
if(null === $this->_table) {
$this->_table = new Default_Model_DbTable_Users();
}
return $this->_table;
}
public function fetchAll()
{
$result = $this->getTable()->fetchAll();
return $result;
}
}
And the part of the model which "interacts" directly with the database tables is found inside DbTable directory will look like this:
<?php
/**
* application/models/DbTable/Users.php
*/
class Default_Model_DbTable_Users extends Zend_Db_Table_Abstract
{
/** Table name */
protected $_name = 'users';
public function init()
{
$this->_db->setFetchMode(Zend_Db::FETCH_OBJ);
}
}
Then I would have the same application.ini generated by Zend Framework with this small addition:
resources.db.adapter = "PDO_MYSQL"
resources.db.params.host = "localhost"
resources.db.params.dbname = "mydb"
resources.db.params.username = "root"
resources.db.params.password = "password"
That is how I did without without having to change the bootstrap files.
I didn't want to use the registry to store an object that I should be able to access, so I did a little digging. It turns out that the bootstrap is registered as the front controller parameter "bootstrap", which is accessible from any of your controllers as explained in this manual page for Zend_Application.
So in your controller classes you can get the db adapter that has been defined in your ini file like this:
$bootstrap = $this->getInvokeArg('bootstrap');
$resource = $bootstrap->getPluginResource('db');
$db = $resource->getDbAdapter();