Welcome Guest, Not a member yet? Register   Sign In
Roles and Permissions
#8

(This post was last modified: 12-26-2023, 01:53 AM by pippuccio76.)

(06-02-2023, 04:15 AM)mayurkoshti Wrote: Please provide an example of Roles and Permissions in CI4 without using session and other external library.

sorry for my poor enghlish , i try to learn shield , documentation is not complete for my own opinion.

but you must first of all add your permission to config->Authgroups :

Code:
    public array $permissions = [
        'admin.access'              => 'Can access the sites admin area',
        'admin.settings'            => 'Can access the main site settings',
        'users.access'              => 'Can access the sites user area',
        'users.create'              => 'Can create new non-admin users',
        'users.edit'                => 'Can edit existing non-admin users',
        'users.delete'              => 'Can delete existing non-admin users',

        'users.singing_in_the_rain'            => 'Can sing in the rain',

        'beta.access'              => 'Can access beta-level features',
    ];

now you must assign the permission


Code:
    public function permit_to_sing_in_the_rain($id_user){

        $users = auth()->getProvider();

        $user = $users->findById($id_user);

        $res = $user->addPermission('users.singing_in_the_rain');

        if($res){

            echo 'Permission added';

        }else{

            echo 'Add permission problem';
        }

    }

a new row is inserted on auth_permissions_users table .


Now you must simply control if user can sing in the rain for example in user controller ( or implement filter) :


Code:
    public function sing_in_the_rain()
    {
        $user = auth()->user(); //get user logged


        if ($user->can('users.singing_in_the_rain')) {

            echo 'Sing in the rain';
       
        }else{

            echo 'go Home!!!';
        }

    }
Reply


Messages In This Thread
Roles and Permissions - by mayurkoshti - 06-02-2023, 04:15 AM
RE: Roles and Permissions - by InsiteFX - 06-02-2023, 09:51 PM
RE: Roles and Permissions - by captain-sensible - 06-04-2023, 04:59 AM
RE: Roles and Permissions - by DeanE10 - 06-09-2023, 09:20 AM
RE: Roles and Permissions - by Corsari - 06-11-2023, 07:20 AM
RE: Roles and Permissions - by JustJohnQ - 06-11-2023, 11:51 AM
RE: Roles and Permissions - by Corsari - 06-12-2023, 04:08 AM
RE: Roles and Permissions - by pippuccio76 - 12-26-2023, 01:51 AM



Theme © iAndrew 2016 - Forum software by © MyBB