Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
ACL | ORM | Group Permission
  • Hi, I have this code:

        if(Input::post('send')){

                    $var_perms = Input::post('var_perms');
                    foreach($var_perms as $elements=>$index){
                        $group = \Model\Auth_Group::find($elements, array('related' => array('permissions','grouppermission')));

                        foreach($index as $element){
                            $arr_permissions[] = $element;

                         foreach($group->permissions as $perms){
                             print_r($perms);
                            $perms->area = $name;
                            $perms->permission = $element ;
                            $perms->actions = $arr_permissions;
                            $perms->save();
                        }

                        foreach($group->grouppermission as $group_permission){
                            print_r($group_permission);
                            $group_permission->actions = $arr_permissions;
                            $group_permission->save();
                        }
                        $group->save();
                    }
                }




    And the variable $var_perms have

     Array ( [6] => Array ( [0] => new [1] => edit [2] => view ) [3] => Array ( [0] => edit ) )

    But only save the first array  

    [6] => Array ( [0] => new [1] => edit [2] => view )

    I need save it all.

    What is my error. Anyone can see it  -_-'
  • HarroHarro
    Accepted Answer
    I've reformatted your code because I was missing a bracket, and posted it in the code bin: https://bin.fuelphp.com/snippet/view/OH (use this next time, much easier than posting it). 

    Did I do it correctly?
  • Harro Verton, Is correct.
    I was missing a bracket, but is not the problem, on the rest of my code, i have 2 lines more and the missing bracket.

    The error is that not save all arrays. can you see other error?
  • If I interpret your data correctly, you have an array in $var_perms, that contains one or more group id's, and for each id, an array of actions.

    In that case I don't understand your code, because the relation between group and permission is a many-to-many, one group can have many permissions.

    You loop over all permission records found, set the area to $name (where does that come from?), and then you do something weird with the permission records.

    I think you missed how permissions actually work.
  • Harro, this is the complete code.

    https://bin.fuelphp.com/snippet/view/OI

    Thanks for your help
  • I have groups table, permissions table, group_permissions table.


  • HarroHarro
    Accepted Answer
    A permission has three components:
    - the area
    - the permission
    - the action

    So you can have an area "admin", "forum", "clients", etc, that define high-level functionality in your application.

    Then within "admin", you could have "users", "groups", "forums", etc, specific functionality within an area. This is the permission.

    Then within a permission, you can have actions to make the system even more granular, so for example you can have "create", "edit", "view", "delete" actions.

    So a user with "admin.users.view" permission would be allowed to see the users list in the admin section of your application.

    For this permission, you would create a permission record:

    $perm = \Model\Auth_Permission::forge(array(
        'area' => 'admin',
        'name' => 'users',
        'actions' => array("create", "edit", "view", "delete"),
    ));
    $perm->save();

    If you want to assign this permission to for example a group, you would find the group:

    $group = \Model\Auth_Group::find($elements);

    And then link the two:

    $group->grouppermission[] = \Model\Auth_Grouppermission::forge(array(
        'group_id' => $group->id,
        'perms_id' => $perms->id,
    ));

    If you want to assign only some actions to this group (for example only "view" and "edit"), you would do

    $group->grouppermission[] = \Model\Auth_Grouppermission::forge(array(
        'group_id' => $group->id,
        'perms_id' => $perms->id,
        'actions' => array(1,2),
    ));

    where the 1 and 2 are the index values of the actions array as stored in the permission record.
  • Harro,
    I can understand your explication, but this not work to me. I was tries this and only can get errors.
    I create the base for this ACL with Migration of the auth package with orm drive.

    Thank you for your time, but this not solve my problem
  • One aditional Question :

    $perm = \Model\Auth_Permission::forge(array(
        'area' => 'admin',
        'name' => 'users',
        'actions' => array("create", "edit", "view", "delete"),
    ));
    $perm->save();

    Here create the permission and return own id?

  • Logical it doesn't solve your problem, as your code is completly wrong.

    ORM calls don't return values, the work with objects. So after $perm->save(), you can access $perm->id (or whatever your primary key is).

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion