im going through http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html <--- this tutorial and guess what? ;) it doesnt work, im Using cakephp 2.2.2... i get next error
Undefined index: id [CORE/Cake/Model/AclNode.php, line 140]
AclNode::node() - Couldn't find Aro node identified by
"Array ( [Aro0.model] => User [Aro0.foreign_key] => )
" Error: An Internal Error Has Occurred.
this error is becouse i dont get my group_id in my User model
public function bindNode($user) {
return array('model' => 'Groups', 'foreign_key' => $user['Users']['groups_id']);
}
So in this function $user var contains only username and password... and no group_id's
Please help guys...
Model name must be singular.
'model' => 'Group'
$user['Users']['group_id'] //in your DB, put "group_id" to, not "groups_id"
Make sure that the Model you've created is "Group". :)
I've just encountered this problem. Tracing the code back the issue was that my model was called 'AdminUser' and not the default, 'User'. Altering the userModel setting solved it for me.
public $components = array(
'Acl',
'Auth' => array(
'authorize' => array(
'Actions' => array(
'actionPath' => 'controllers',
'userModel' => 'AdminUser'
)
)
),
'Session'
);
Looking at your bindNode method it looks like your using a model named 'Users' rather than 'User' so setting the userModel to 'Users' might do the trick.
I hope that helps,
Pete
Related
Working in Yii, i get the error of
SystemManagementController and its behaviors do not have a method or
closure named "createReturnableUrl"
I can not find anything to solve it. it happened right after I added this:
array(
'class' => 'CButtonColumn',
//'viewButtonUrl' => '$this->grid->controller->createReturnableUrl("view",array("id"=>$data->id))',
'updateButtonUrl' => '$this->grid->controller->createReturnableUrl("update",array("id"=>$data->id))',
'deleteButtonUrl' => '$this->grid->controller->createReturnableUrl("delete",array("id"=>$data->id))',
//'deleteConfirmation' => Yii::t('app', 'Are you sure to delete this item?'),
),
to :
<?php
//The following lines needs to be moved to the controller to sepparate the view from the controllers.
$model = new CActiveDataProvider('User');
//The following line should be set by the controller. Containing the names of the colums in a chosen language.
/**/
$colums = array(
'login',
'name_first',
'name_last',
//'password',
'email',
'is_active',
//'sortorder',
array(
'class' => 'CButtonColumn',
//'viewButtonUrl' => '$this->grid->controller->createReturnableUrl("view",array("id"=>$data->id))',
'updateButtonUrl' => '$this->grid->controller->createReturnableUrl("update",array("id"=>$data->id))',
'deleteButtonUrl' => '$this->grid->controller->createReturnableUrl("delete",array("id"=>$data->id))',
//'deleteConfirmation' => Yii::t('app', 'Are you sure to delete this item?'),
),
);
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $model,
'columns' => $colums,
'filter' => $model->model)
);
?>
when I remove that piece of code, there is not problem, everything works, but when I add that line it gives me the error.
I use that line in a different class as well and there I dont get the problem, how can I get rid of this?
It looks like the controller does not know about createReturnableUrl. From what I googled that is a behavior of yii that you can attach, are you sure you attached the behavior properly for the SystemManagementController? See if all the other controller that work with the same code have that behavior attached, maybe this one does not.
CakePHP v.2.4...
I'm following this documentation trying to set up the Auth component to use my custom password hashing class:
App::uses('PHPassPasswordHasher', 'Controller/Component/Auth');
class AppController extends Controller {
// auth needed stuff
public $components = array(
'Session',
'Cookie',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array('username'=>'email', 'password'=>'password'),
'passwordHasher' => 'PHPass'
)
),
Inside my UsersController::login() I debug the return from $this->Auth->login(); and it always returns false, even when I submit the correct email / password.
(NOTE: It looks strange to me that the login() takes no parameters, but the docs seem to imply that it looks into the the request data automatically. And this would make sense if my configurations aren't correctly causing it to check the User.email field instead username.)
The post data from the submitted login form looks like this:
array(
'User' => array(
'password' => '*****',
'email' => 'whatever#example.com'
)
)
What am I missing?
Update2
I'm starting to suspect that the default hashing algorithm is getting used instead of my custom class. I tried to match the examples in the docs but they're quite vague on how to do this.
Here's the contents of app/Controller/Component/Auth/PHPassPasswordHasher.php
<?php
App::import('Vendor', 'PHPass/class-phpass'); //<--this exists and defines PasswordHash class
class PHPassPasswordHasher extends AbstractPasswordHasher {
public function hash($password) {
$hasher = new new PasswordHash( 8, true );
return $hasher->HashPassword($password);
}
public function check($password, $hashedPassword) {
debug('PHPassHasher'); die('Using custom hasher'); //<--THIS NEVER HAPPENS!
$hasher = new new PasswordHash( 8, true );
return $hasher->CheckPassword($password, $hashedPassword);
}
}
AHA! The debug() never appears... so I'm pretty sure the problem is with my custom hasher configuration(s).
Update3
Additional clue: I experimented by setting various default hashing algorithms (Ex: "Simple", "Blowfish") and creating users. The hashes which show up in the DB are all the same which tells me that my config settings are getting ignored completely.
Update4
I debugged $this->settings inside the constructor of /lib/Cake/Controller/Component/Auth/BaseAuthenticate.php and my custom hasher settings are in there:
array(
'fields' => array(
'password' => 'password',
'username' => 'email'
),
'userModel' => 'User',
'scope' => array(),
'recursive' => (int) 0,
'contain' => null,
'passwordHasher' => 'PHPass'
)
You need to rename your password hasher class to have the suffix "PasswordHasher", and only provide the non-suffixed name in the 'className' argument.
eg:
<?php
App::import('Vendor', 'PHPass/class-phpass'); //<--this exists and defines PasswordHash class
class PHPassHasherPasswordHasher extends AbstractPasswordHasher {
// functions
}
The example from the docs sets the classname to 'Simple', which then loads 'SimplePasswordHasher'.
You might find that having a name of PHPassHasherPasswordHasher is a bit silly, it's up to you what you want to call it. Perhaps PHPassPasswordHasher might be a bit more appropriate (and then use the classname argument 'PHPass').
EDIT: It seems as if Cake has issues when multiple capital letters are used one after the other (eg. PHPass), so the right way to do it is to change the password hasher class to the following:
<?php
App::import('Vendor', 'PHPass/class-phpass'); //<--this exists and defines PasswordHash class
class PhpassPasswordHasher extends AbstractPasswordHasher {
// functions
}
... and make sure the filename matches the classname: PhpassPasswordHasher.php.
Thanks to SDP for the discussion, I learnt something today!
According to the docs:
To configure different fields for user in $components array:
// Pass settings in $components array
public $components = array(
'Auth' => array(
'authenticate' => array(
'Form' => array(
'fields' => array(
'username' => 'email',
'password' => 'password'
)
)
)
)
);
Source
I finally got this working. We were on the right track by renaming the file/class to comply with Cake conventions. I had to go one step further and change the capitalization as well:
PHPassPasswordHasher.php --> PhpassPasswordHasher.php
class PHPassPasswordHasher... --> class PhpassPasswordHasher...
Phew!
ps: Many many thanks to #Ben Hitchcock for support on this.
Recently, I've been studying cake, I've seen the auth library which said to be will take care of the access control over your app, but, it seems like, you can't initialize or even use this auth library when you're not in the 'UsersController', i did not want that, what if it has some admin part wherein i want the URI to be admin/login, or just simply /login, i've been scratching my head over this one, please help.
Another question, why it seems like the functionality of the '$this->redirect' is not effective when i'm putting this one at any method that contains nothing but redirection, or even in the __construct()?
thanks guys, hoping someone could clearly explain to me those things.
you can use the Auth component inside any controller in the application. If you want it will only effect with the admin section then you can add condition in the beforeFilter funciton in you application AppController on Auth initialization like.
// for component initialization.
public $components = array(
'Auth' => array(
'authenticate' => array(
'userModel' => 'Customer', // you can also specify the differnt model instead of user
'Form' => array(
'fields' => array('username' => 'email')
)
)
)
}
and you can bind this on the admin routing like
function beforeFilter(){
// only works with admin routing.
if(isset($this->request->params['prefix']) && ($this->request->params['prefix'] == 'admin')){
$this->Auth->loginRedirect = array('admin' => true, 'controller' => 'pages', 'action' => 'index');
$this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login', 'admin' => true);
$this->Auth->loginAction = array('admin' => true, 'controller' => 'customers', 'action' => 'login');
}
}
If you're using cake 2.3.x or later then make sure you have specified the redirect action in correct format like.
return $this->redirect('action_name'); // you can also specify the array of parameters.
I read a book Pactpub Web Application Development with Yii and PHP Nov 2012. Faced with such a problem, I can not understand the logic behind the use of relations (). Here diagram tables in the database:
You need to insert code in the model:
Issue model:
...
'requester' => array(self::BELONGS_TO, 'User', 'requester_id'),
'owner' => array(self::BELONGS_TO, 'User', 'owner_id'),
'project' => array(self::BELONGS_TO, 'Project', 'project_id'),
);
...
Project model:
...
'issues' => array(self::HAS_MANY, 'Issue', 'project_id'),
'users' => array(self::MANY_MANY, 'User', 'tbl_project_user_assignment(project_id, user_id)'),
...
I can not understand that we add? If the model Issue understand everything, then the model Project - I do not understand that we are adding. Help to understand ...
If the model Issue understand everything, then the model Project - I
do not understand that we are adding
in some case, you have already had a project, and you would like to find all of issues and partner users of that project.
$project = Project::model()->findByPK(1); // get project id=1
$issues = $project->issues; // get all of issues of project id=1, the result would be array
$users = $project->issues; // get all of users of project id=1, the result would be array
$project = Project::model()->with('issues', 'users')->findAll(); // get all of projects which has issue and user
//you have a user name ABC, and you want to find all of projects which contains a issue from owner has that user name.
$projects = Project::model()->with(array(
'issues' => array(
'alias' => 'issue',
//'condition' => '',
//'params' => array(),
'with' => array(
'owner'=>array(
'alias' => 'user',
'condition' => 'username =:username',
'params' => array(':username'=>'ABC'),
)
)
),
))->findAll();
There has many ways let you mix them up with multiple relations and conditions. One of above example would generate some big SQL SELECT query that I never want to deal with on my own :)
AR Relations Details
community,
there is a problem with multiple tables which receive their entries of the same model "Post" on the same page. When clicking a paginator-number the app will change the page for both tables.
I tried it with dummy classes in the AppModel which should work but I get an error saying an internal error has occurred (error 500). I found out that there is a problem with the corresponding SQL-statement with a not found row.
In the AppModel:
class PostHood extends Post {
public $useTable = 'posts';
};
That code uses the table "cake_posts" of "class Post extends AppModel". In the controller I tried to get the PostHood like following:
$this->paginate['PostHood'] = array(
'conditions' => array('OR' => array(stuff)),
'limit' => 5
);
$this->set('postsHood', $this->paginate('PostHood'));
In the view there is a foreach-loop using the $postsHood as $post.
Maybe you have an idea, thanks in advance :)
EDIT 1:
I got some error notices after changing the code. May be you have an idea what to do.
Change of the AppModel:
class PostHood extends AppModel {
var $name = 'Post';
public $useTable = 'posts';
};
The Controller:
$this->loadModel('PostHood');
$this->paginate = array(
'conditions' => array('OR' =>
array(
array('AND' => array(
array('PostHood.ZIPCODE LIKE' => $userArea . '%'),
array('PostHood.ALTDATE >' => date("Y-m-d")),
array('PostHood.AGENT' => '0'),
array('PostHood.OWNER <>' => $this->UserAuth->getUserId()),
array('PostHood.PARENTID' => '0'),
array('PostHood.ACCEPTED' => '0')
)), [MORE AND ARRAYS]
$this->set('postsHood', $this->paginate('PostHood'));
The corresponding error in the view:
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Post.DATE' in 'order clause'
SQL Query: SELECT PostHood.id, PostHood.B/S, PostHood.H, PostHood.CITY,
PostHood.MARKET, PostHood.DATE, PostHood.ALTDATE, PostHood.TIME, PostHood.INCOME, PostHood.ZIPCODE, PostHood.ALIAS, PostHood.VEHICLE, PostHood.DELIVERYAREA, PostHood.SPACE, PostHood.STREET, PostHood.HOUSENUMBER, PostHood.NAME, PostHood.CART, PostHood.TEL, PostHood.OWNER, PostHood.created, PostHood.modified, PostHood.AGENT, PostHood.EXTRADATA, PostHood.PARENTID, PostHood.BRATED, PostHood.SRATED, PostHood.REQUESTED, PostHood.ACCEPTED FROM usr_web126986_4.cake_posts AS PostHood WHERE 1 = 1 ORDER BY Post.DATE asc LIMIT 5
Obviously Cake tries to fetch data with "PostHood" but the table "posts" which I actually want to use is listening to "Post.field". How can I fix that? Thanks :)
You should try something like this:
$this->paginate = array(
'conditions' => array('OR' => array(stuff)),
'limit' => 5
);
$this->set('postsHood', $this->paginate('PostHood'));
I am just curious as to why you want to structure your model extending another model.
Got it working with the great plugin DataTables. That overrides the cake-pagination and uses JSON to display the results.
In order to implement it into cake you need
1.) a cake component for interpreting sql-statements: https://github.com/cnizzdotcom/cakephp-datatable
2.) the plugin: http://www.datatables.net/index
3.) implementation:
$this->paginate = array(
'fields' => array('Model.field1', 'Model.field2'),
'conditions' => array( stuff )
);
$response = $this->DataTable->getResponse();
$this->set('response', $response);
$this->set('_serialize','response');
$encode = json_encode($response);
$this->set('dataTablesData', $encode);
Then you can grab the data in the view.