Wednesday, January 2, 2013

Re: queries on HABTM relationships

This simply is solvable with:

$this->Auth->userScope = array('User.can_access_admin' => 1);

Put that in you beforeFilter function inside AppController, and your problems are solved. If you want to redirect them, than you shoukd do something like this:

if ($this->Auth->user()) {
        if(isset($this->params['admin']) && $this->Auth->user('admin') != 1) {
          $this->redirect('/');
        }
}

Hope this helps. 
Cheers



On Wed, Jan 2, 2013 at 7:59 PM, lowpass <zijn.digital@gmail.com> wrote:
When running a find('count') the contain param is useless. This is
because the main query is fetching a sum, not a set of records from
which to fetch the associated 'contain' records.

Are you using AuthComponent? You can use that to fetch the User and
associated records. However, I can't remember how deep it goes. To see
what records it has, put this in your UsersController login method:

die(debug($this->Auth->user()));

http://book.cakephp.org/2.0/en/core-libraries/components/authentication.html#accessing-the-logged-in-user

On Tue, Jan 1, 2013 at 5:23 PM, Stefano Campanella
<leonardo@guildofmessengers.com> wrote:
> Hello all,
> this is my first time posting here. I recently started to study how to use
> CakePHP and I'm trying to develop a website.
> In my website I am writing a custom authorize component where each user get
> one or more groups and each group has one or more permissions associated to
> it.
> I have problems in using the find() method.
> This is my structure:
> User hasAndBelongsToMany Group
> Group hasAndBelongsToMany Perm
>
> I have attached Containable to all the Models (in AppModel)
>
> now in my authorization component I need to check if a group has a specific
> permission, and for this I use find():
>
>> if(!isset($this->controller()->Group)){
>>   $this->controller()->loadModel('Group');
>> }
>> $n_perm=$this->controller()->Group->find('count',array(
>>   'conditions'=>array('id'=>1),
>>   'contain'=>array(
>>     'Perm'=>array(
>>       'conditions' => array('id'=>'can_access_admin')
>>     )
>>   )
>>
>> ));
>
>
> I would expect this to give me a result >=1 if group 1 has the
> 'can_access_admin' permission, and =0 if the group has no such permission.
> This is not what actually happens, the only query that cakePHP shows is
> this:
>
> SELECT COUNT(*) AS `count` FROM `groups` AS `Group` WHERE `id` = 1
>
> And it is obviously not enough to find what I requested.
>
> Can anyone help me?
>
> Thanks
>
> --
> Like Us on FaceBook https://www.facebook.com/CakePHP
> Find us on Twitter http://twitter.com/CakePHP
>
> ---
> You received this message because you are subscribed to the Google Groups
> "CakePHP" group.
> To post to this group, send email to cake-php@googlegroups.com.
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com.
> Visit this group at http://groups.google.com/group/cake-php?hl=en.
>
>

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.





--
Ivan Rimac
tel: +385 95 555 99 66

--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP
 
---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com.
To unsubscribe from this group, send email to cake-php+unsubscribe@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php?hl=en.
 
 

No comments: