PHP code example of ykozhemiaka / simple-permission
1. Go to this page and download the library: Download ykozhemiaka/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/ */
ykozhemiaka / simple-permission example snippets
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'simple_permissions',
'username' => 'root',
'password' => '',
'charset' => 'utf8mb4'
]);
$capsule->setAsGlobal();
$capsule->bootEloquent();
use SimplePermission\Database\Schemas\UsersSchema;
$userSchema = new UsersSchema($capsule);
$userSchema->createTable();
use SimplePermission\Database\Schemas\PermissionSchemas;
$permissionSchemas = new PermissionSchemas($capsule);
$permissionSchemas->createTable();
namespace SimplePermission\Models;
use Illuminate\Database\Eloquent\Model;
class User extends Model
{
use HasPermission;
protected $table = 'custom-table-name';
...
use SimplePermission\Repositories\PermissionRepository;
$permissionRepo = new PermissionRepository();
$permission = $permissionRepo->create('Edit article');
$user = User::find(1);
$role = Role::find(1);
//or you can use role id $role = 1;
$permission = Permission::find(1);
//or you can use permission id $permission = 1;
$user->allowTo($permission);
$user->grantRole($role);
$user->ableTo('Edit posts');
$user->hasRole('Admin');
$user->terminateToPermission($permission);
$user->terminateToRole($role);
$roleRepo = new RoleRepository();
$role = $roleRepo->create('Admin');