Wednesday, January 2, 2013

Re: queries on HABTM relationships

Works like a charm! Thank you so much cricket!
Now to reply to your questions:
I am using a count because I don't need any content returned. With this query I just check if the record exists, if it does then I know the Group has the specified permission. The count will always return 0 if group 1 has no can_access permission or 1 if the group has the permission. I don't need any information, I only need to know if it exists.

I didn't find any note about CakePHP supporting only integer ids, is this true? Every permission has a unique name and it is not going to change, so I thought using an integer id and another name field is useless, I just use the permission name as primary key.

As for using ACL: reading CakePHP2.x Book I understood that ACL only supports trees structures, but it is a limitation for me because I need to be able to group my users in mode than one way. A user could be a Manager but also a Moderator, and these two groups are not one child of the other. I would need ACL to support more than just trees, it should support graphs. So I decided to make my Authorization component, which just uses flat groups (there is no group hierarchy) but allows one user to have more than one group. And it isn't even very big, the Authorize class is only 70 lines long, while ACL seems really too complex.

Anyway, I am done now. Thank you to everyone who tried to help me!!

On Wednesday, January 2, 2013 11:53:44 PM UTC+1, cricket wrote:
I just realised that this also makes no sense. Why are you running a
COUNT(*) on Group when your condition is to select only the Group with
id = 1?

Perhaps you need to start from the beginning and explain what it is
you're looking for.

And I'll second the suggestion to use ACL.

On Wed, Jan 2, 2013 at 5:50 PM, lowpass <zijn.d...@gmail.com> wrote:
> I think that should be something like:
>
> $data = $this->controller()->Group->find(
>         'count',
>         array(
>                 'conditions' => array(
>                         'Group.id' => 1 // hard-coded for testing?
>                 ),
>                 'joins' => array(
>                         array(
>                                 'type' => 'INNER',
>                                 'table' => 'groups_perms',
>                                 'alias' => 'GroupPerm',
>                                 'conditions' => array(
>                                         'GroupPerm.group_id' => 'Group.id'
>                                 )
>                         ),
>                         array(
>                                 'type' => 'INNER',
>                                 'table' => 'perms',
>                                 'alias' => 'Perm',
>                                 'conditions' => array(
>                                         'GroupPerm.perm_id' => 'Perm.id'
>                                 )
>                         )
>                 )
>         )
> );
>
> However, I've left out the perm.id='can_access_admin' part because it
> makes no sense in the context of Cake. The id column should be an INT.
> I presume you mean something like 'Perm.type' => 'can_access_admin',
> in which case add it to the 2nd joins conditions array.
>
> On Wed, Jan 2, 2013 at 3:48 PM, Stefano Campanella
> <leon...@guildofmessengers.com> wrote:
>> mmh, ok. But the problem is that even if I change the find("count") into a
>> find("all") it still loads data only from Group table.
>>
>> Yes I am using the AuthComponent for logged in users. But since I want to
>> use my Authorize class also for managing what anonymous users can access I
>> am calling $this->Auth->isAuthorized() in the beforeFilter of AppController
>> with a $user parameter of array('id'=>0). Inside the authorize method I
>> recognize the empty user and make checks for permissions on an hard coded
>> group id. I checked this would work correctly, if only I could make the
>> query in the right way.
>>
>> Let's change the question. Which code would you write to make CakePHP
>> execute this query:
>>>
>>> SELECT COUNT(*) AS count FROM groups INNER JOIN groups_perms ON
>>> groups.id=groups_perms.group_id INNER JOIN perm ON
>>> groups_perms.perm_id=perm.id WHERE group.id=1 AND perm.id='can_access_admin'
>>
>> given that you have access to $this->controller()->Group?
>>
>> Thanks
>>
>> On Wednesday, January 2, 2013 7:59:24 PM UTC+1, cricket 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
>>> <leon...@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...@googlegroups.com.
>>> > To unsubscribe from this group, send email to
>>> > cake-php+u...@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...@googlegroups.com.
>> To unsubscribe from this group, send email to
>> cake-php+u...@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.
 
 

No comments: