PHP code example of thinkstudeo / laravel-rakshak

1. Go to this page and download the library: Download thinkstudeo/laravel-rakshak library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

thinkstudeo / laravel-rakshak example snippets




return [
    /*
    |--------------------------------------------------------------------------
    | Route Prefix
    |--------------------------------------------------------------------------
    |
    | This value will be used as route prefix for all rakshak routes.
    |
    */
    'route_prefix' => 'rakshak',
    /*
    |--------------------------------------------------------------------------
    | Application's User Model
    |--------------------------------------------------------------------------
    |
    | Mainly to indicate the primary key type for the User model in your 
    | application - whether its the default bigIncrements or uuid.
    |
    */
    'users' => [
        'pk_type' => 'unsignedBigInteger'
    ],
    /*
    |--------------------------------------------------------------------------
    | Two Factor Authentication
    |--------------------------------------------------------------------------
    |
    | Use the below keys to configure the two factor authentication for app.
    | Switch to enable or disable two factor authentication - enable_2fa. 
    | Notification classes and sms templates for otp and welcome message.
    | Remember to use approved templates for sms messages in countries
    | where there are DND restrictions for transactional messaging.
    |
    */
    'enable_2fa' => false,
    'login'      => [
        'email'                      => ['App\Notifications\Rakshak\LoginOtpMail'],
        'sms'                        => ['App\Notifications\Rakshak\LoginOtpSms'],
        'verify_mobile'              => ['App\Notifications\Rakshak\VerifyMobileOtpSms'],
        'otp_template'               => 'Your OTP for ' . config('app.name') . ' is 234567. It is valid for the next 10 minutes only.',
        'verify_mobile_sms_template' => '%s: Confirmation code to verify your mobile number is %s.'
    ],
    'register' => [
        'welcome_email'    => ['App\Notifications\Rakshak\RegistrationWelcomeEmail'],
        'welcome_sms'      => ['App\Notifications\Rakshak\RegistrationWelcomeSms'],
        'welcome_template' => 'Welcome %s! We are happy to have you onboard. Team %s',
    ],
    /*
    |--------------------------------------------------------------------------
    | Authorisation Roles
    |--------------------------------------------------------------------------
    |
    | Define the name for the super user role.
    | Define the name for the authorizer user role.
    | User with authorizer roler can perform crud ops for Role & Ability
    |
    */
    'roles' => [
        'super_user' => 'super',
        'authorizer' => 'rakshak'
    ],
    /*
    |--------------------------------------------------------------------------
    | Sender
    |--------------------------------------------------------------------------
    |
    | Define the sender for email and sms messages.
    |
    */
    'sender' => [
        'email' => [
            'from' => env('RAKSHAK_EMAIL_FROM', config('mail.from.address')),
            'name' => env('RAKSHAK_EMAIL_FROM_NAME', config('mail.from.address'))
        ],
        'sms' => [
            'from' => env('TEXTLOCAL_TRANSACTIONAL_SENDER'),
            'number' => env('TEXTLOCAL_TRANSACTIONAL_FROM')
        ]
    ]
];

$user = User::first();
$role = Role::whereName('hr_manager')->first();
$ability = Ability::whereName('manage_users')->first();

// You can add an existing ability to a role
//By passing the Ability model instance
$role->addAbility($ability);

//Or by passing an Ability name string
$role->addAbility('manage_users');

//To retract an ability form a role
//By passing the Ability name string
$role->retractAbility('manage_users');

//Or - by passing the model instance
$role->retractAbility($ability);

//By passing the Role model instance
$user->assignRole($role);

//Or - by passing the Role name string

$user->assignRole('hr_manager');

//Retract a role from the user
//By passing the Role model instance
$user->retractRole($role);

//Or by passing the Role name string
$user->retractRole('hr_manager');

//Passing the Role model instance
$user->hasRole($role);

//Passing the Role name string
$user->hasRole('hr_manager');

//Passing an array of multiple Role model instances
$user->hasAnyRole([$role, $role2]);

//Passing array of multiple Role name strings
$user->hasAnyRole(['hr_manager', 'content_manager']);

//Passing the Ability model instance
$user->hasAbility($ability);

//Passing the Ability name string
$user->hasAbility('manage_users');

//Passing an array of multiple Ability model instances
$user->hasAnyAbility([$ability, $ability2]);

//Passing array of multiple Ability name strings
$user->hasAnyAbility(['manage_users', 'manage_content']);

//Protect the route and make it accessible only to users having hr_manager role.
Route::get('/some-route', 'SomeController@action')->middleware('role:hr_manager');

//Protect the route and make it accessible only to users having either of hr_manager, content_manager or super role.
Route::get('/some-route', 'SomeController@action')->middleware('role:hr_manager|content_manager|super');

//Protect the route to check for valid otp token.
Route::post('/another-route', 'AnotherController@action')->middleware('rakshak.2fa');

@role('hr_manager')
    User has the role of hr_manager
@elserole('super')
    User has the role of super
@else
    User does not have the role of hr_manager or super
@endrole

@anyrole('hr_manager|super')
    Visible to user having role of hr_manager or super
@else
    User does not have the role of hr_manager or super
@endanyrole
bash
$ php artisan rakshak:install