PHP code example of lcloss / simple-permission

1. Go to this page and download the library: Download lcloss/simple-permission 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/ */

    

lcloss / simple-permission example snippets


    use Lcloss\SimplePermission\Models\Traits\HasRoles;
   
    class User extends Authenticatable
    {
        use HasRoles;
    }
    

    protected $middlewareGroups = [
        'web' => [
            // ...
            \Lcloss\SimplePermission\Http\Middleware\AuthGates::class,
        ],

        'api' => [
            // ...
            \Lcloss\SimplePermission\Http\Middleware\AuthGates::class,
        ],
    ];
    

    use Lcloss\SimplePermission\Models\Role;
   
    class CreateNewUser
    {
        // ...
        public function create(array $input)
        {
            // ...
            // If you are getting the first and last names:
            $name = trim($input['first_name'] . ' ' . $input['last_name']);
   
            DB::beginTransaction();
   
            $user = User::create([
                'name'      => $name,
                'email'     => $input['email'],
                'password'  => Hash::make($input['password']),
            ]);

            $countUsers = User::count();
   
            if ( $countUsers == 1 ) {
                $role = Role::where('slug', 'sysadmin')->first();
            } else {
                $role = Role::where('slug', 'user')->first();
            }
            $user->roles()->attach($role);

            DB::commit();
   
            return $user;
        }
    }
    

Route::get('/users', [UserController::class, 'index'])->middleware('can:users_list');

use Gate;

class UserController extends Controller
{
    public function index()
    {
        Gate::authorize('users_list');
        
        // ...
    }
}
bash
    php artisan vendor:publish --provider="Lcloss\SimplePermission\SimplePermissionServiceProvider"
    
bash
    php artisan migrate
    
bash
php artisan db:seed --class=SimplePermissionSeeder
bash
php artisan db:seed --class=SimplePermissionRoleSeeder
php artisan db:seed --class=SimplePermissionPermissionSeeder