Yes you're right, jeremy, I made this example up (my real User model
doesn't have an Address, I just added this to show that there are
cases where there's more than one key that isn't another array
itself).

The reason why I'd like to know this is the following.

I have implemented a simple ACL component that delivers an ActiveUser
object to the views which can be asked for permissions for certain
things:

// In the view...
<li><?php if($usr->allowed('Users', 'edit')) echo
$this->Html->link(__('Edit User', true), array('action' => 'edit',
$post['User']['id'])); ?> </li>

This works great so far, but it only works on the controller/action level.

Every user is in a group which provides an ACL access string like
"!*:*,*:index,*:view" for guests or "!*:*,Users:*" for users editors
or "*:*" for administrators. So with the functionality above, only
administrators can edit users. But I'd also like my users to be able
to edit their own user data.

So I thought of an extended $usr->allowed(...) version which also
accepts a model instance like the following:

$usr->allowed('edit', $userInstance) // $userInstance is not the data
array, but a real "new User()" instance

Then in the User::allowed(...) method I can ask the model instance
itself whether it wants to allow something or not:

function allowed
  if(last param is model object)
    return modelObject->allows($action, $this->id)
  else
    do normal ACL string stuff...
}

So I can implement an allows(...) method in every model I'd like to
have authorisation functionality on a model instance level:

// In the model...
function allows($action, $userId)
  return $this->user_id == $userId // If the user is the same like the
one who owns the record, then let him edit it!

This works great so far, too. But because CakePHP doesn't deliver
views with real model instances but only data arrays, I'd like to be
able to pass them to the allowed(...) method, too. There I only have
to re-create the model instance, so I can ask it for allowance. And
here's the problem: it seems there can't be known what model a data
array originated from... Or can there? :-)

Thanks for help... (and sorry for the long explanation...)

On Thu, Nov 18, 2010 at 4:53 PM, Jeremy Burns | Class Outfit
<jeremybu...@classoutfit.com> wrote:
> Interesting question: why do you need to know? The User key contains user 
> data, the Address key contains address data, and the two are related...no? I 
> think your example array is missing some elements as there would normally be 
> a user_id key in the Address section...I think.
>
> Jeremy Burns
> Class Outfit
>
> jeremybu...@classoutfit.com
> http://www.classoutfit.com
>
> On 18 Nov 2010, at 15:45, psybear83 wrote:
>
>> Hi everybody
>>
>> I have a User model which when loaded results in something like the
>> following array:
>>
>> Array
>> (
>>    [User] => Array
>>        (
>>            [id] => 3
>>            [name] => posts_editor
>>            [password] => 9fb00060ce49a5d5fab91b350b1529fda941a1de
>>            [superuser] => 0
>>        )
>>    [Post] => Array
>>        (
>>            [0] => Array
>>                (
>>                    [id] => 3
>>                    [user_id] => 3
>>                    [title] => Comment from comments editor
>>                    [body] => I like editing comments :-)
>>                )
>>        )
>>    [Group] => Array
>>        (
>>            [0] => Array
>>                (
>>                    [id] => 1
>>                    [name] => Posts Editors
>>                    [GroupsUser] => Array
>>                        (
>>                            [id] => 1
>>                            [group_id] => 1
>>                            [user_id] => 3
>>                        )
>>                )
>>        )
>> )
>>
>>
>> This is nice, but it seems that this array structure doesn't tell
>> itself where it came from, right? I could guess that it's from the
>> User model because there is only one key in the array that isn't
>> another array of data itself, but what if my model looked the
>> following (if my User model belonged to one Address):
>>
>> Array
>> (
>>    [User] => Array
>>        (
>>            [id] => 3
>>            [name] => posts_editor
>>            [password] => 9fb00060ce49a5d5fab91b350b1529fda941a1de
>>            [superuser] => 0
>>        )
>>    [Address] => Array
>>        (
>>            [street] => Some nice street 123
>>            [city] => Ducktown
>>        )
>> )
>>
>> Then it could be a User or an Address model, right? Or can we tell
>> from the order that it's a User and not an Address (i.e. maybe always
>> the first key ist the model, could that be?)? Or is there any other
>> way to determine the source of a model data array?
>>
>> Thanks for help, guys
>> Josh
>>
>> Check out the new CakePHP Questions site http://cakeqs.org and help others 
>> with their CakePHP related questions.
>>
>> 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+unsubscr...@googlegroups.com For more options, visit this group at 
>> http://groups.google.com/group/cake-php?hl=en
>
> Check out the new CakePHP Questions site http://cakeqs.org and help others 
> with their CakePHP related questions.
>
> 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+unsubscr...@googlegroups.com For more options, visit this group at 
> http://groups.google.com/group/cake-php?hl=en
>

Check out the new CakePHP Questions site http://cakeqs.org and help others with 
their CakePHP related questions.

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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en

Reply via email to