Last active
December 28, 2019 12:19
-
-
Save programarivm/d9d1ebb699a1cdf36a9dc21516b947f0 to your computer and use it in GitHub Desktop.
Acl model
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
namespace App; | |
use App\User; | |
use Illuminate\Database\Eloquent\Model; | |
class Acl extends Model | |
{ | |
protected $fillable = ['resource', 'role']; | |
const CHOICE_PERMISSIONS = [ | |
User::CHOICE_ROLE_BASIC => [ | |
'RestaurantController@index', | |
'ReviewController@store', | |
], | |
User::CHOICE_ROLE_EDITOR => [ | |
'RestaurantController@index', | |
'RestaurantController@show', | |
'RestaurantController@update', | |
'RestaurantController@delete', | |
'ReviewController@delete', | |
'UserController@index', | |
], | |
User::CHOICE_ROLE_ADMIN => [ | |
'RestaurantController@index', | |
'RestaurantController@show', | |
'RestaurantController@store', | |
'RestaurantController@update', | |
'RestaurantController@delete', | |
'ReviewController@delete', | |
'UserController@index', | |
'UserController@show', | |
'UserController@store', | |
'UserController@update', | |
'UserController@delete', | |
'UserController@delete', | |
], | |
]; | |
public static function getChoices() | |
{ | |
return [ | |
'permissions' => self::CHOICE_PERMISSIONS, | |
]; | |
} | |
public static function grantedRoles(string $resource) | |
{ | |
$roles = []; | |
foreach (self::CHOICE_PERMISSIONS as $key => $val) { | |
in_array($resource, $val) ? $roles[] = $key : false; | |
} | |
return $roles; | |
} | |
public static function isAuthorized(string $role, string $resource) | |
{ | |
return in_array($role, self::grantedRoles($resource)); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment