jmcneese / permissionable

A CakePHP 1.3 plugin that provides UNIX-like row-level permissions for model data.
http://jmcneese.wordpress.com/2010/01/28/rmac-is-dead-long-live-rmac/
MIT License
50 stars 8 forks source link

hasOne not automatically bound when calling $Model::find('list'); #4

Open cdvrooman opened 14 years ago

cdvrooman commented 14 years ago

I'm trying to do this:

$this->Group->find('list');

and my SQL is coming out like this:

SELECT Group.id, Group.name, GroupPermission.* FROM groups AS Group WHERE 1 = 1

In permisssionable/models/behaviors/permissionable.php, when I debugged the $Model before and after this line of the beforeFind() function:

$this->_bind($Model, array( ... ));

the $Model did not and then did have a 'hasOne' binding to GroupPermission however for some reason it isn't being applied to the ->find('list') query.

I tried to do a $this->Group->find('all'); and that did work as expected.

I'm calling $this->Group from my users_controller where I have:

var $uses = array('User', 'Group');

I have User HABTM Group and Group HABTM User defined in the appropriate model files.

cdvrooman commented 14 years ago

I did some poking around in /cake/libs/model/model.php and in the _findList() function, I found this: if (!isset($query['recursive']) || $query['recursive'] === null) { $query['recursive'] = -1; } Which could definitely be screwing things up if 'recursive' is not being explicitly set by Permissionable', perhaps only when the find is of the 'list' variety?

cdvrooman commented 14 years ago

On a related note, I'm running into a similar problem when trying to save something to the Group table, where first the ability to write to the table is verified. The SQL is appearing as: SELECT COUNT() AS count, GroupPermission. FROM groups AS Group WHERE Group.name = 'Root' AND Group.id != ' ... uuid ...'

In \models\behaviors\permissionable.php I tried adding 'recursive' => 1 to hasPermissions() here: $perm = $Model->{$alias}->find('count', array( ... )); but it didn't have any effect.

I also tried modifying $queryData['recursive'] directly in the beforeFind() function, but that caused my browser to be unable to resolve the page.

cdvrooman commented 14 years ago

Two alternatives are:

  1. Adding 'permissionable' => false to the find parameters, i.e.: $users = $this->User->find('list', array( 'permissionable' => false, 'conditions' => array('User.name <>' => 'guest') )); or, using find('all') but specifying the fields, i.e. $users = $this->User->find('all', array( 'recursive' => -1, 'conditions' => array('User.name <>' => 'guest'), 'fields' => array('User.id', 'User.name') ));
cdvrooman commented 14 years ago

Of course in the second case you would have to use Set::extract() or something similar on the results to leave only the 'user_id' => 'user_name'.

afrance commented 13 years ago

Any real solution to this? The same issue crops up when attempting to use the tree behavior.