Created
February 11, 2020 10:28
-
-
Save sasin91/ac126f64175bd58949d438798917a936 to your computer and use it in GitHub Desktop.
This file contains hidden or 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\Enums\RBAC; | |
use BenSampo\Enum\Traits\CastsEnums; | |
use Illuminate\Contracts\Auth\MustVerifyEmail; | |
use Illuminate\Database\Eloquent\SoftDeletes; | |
use Illuminate\Foundation\Auth\User as Authenticatable; | |
use Illuminate\Notifications\Notifiable; | |
use Illuminate\Support\Collection; | |
use Illuminate\Support\Str; | |
use function md5; | |
use function url; | |
use function ucfirst; | |
class User extends Authenticatable | |
{ | |
use Notifiable, SoftDeletes; | |
/** | |
* The attributes that are mass assignable. | |
* | |
* @var array | |
*/ | |
protected $fillable = [ | |
'role', | |
'permissions', | |
'name', | |
'email', | |
'password' | |
]; | |
/** | |
* The attributes that should be hidden for arrays. | |
* | |
* @var array | |
*/ | |
protected $hidden = [ | |
'password', 'remember_token', | |
]; | |
/** | |
* The attributes that should be cast to native types. | |
* | |
* @var array | |
*/ | |
protected $casts = [ | |
'email_verified_at' => 'datetime', | |
'permissions' => 'integer' | |
]; | |
public function getRoleAttribute(?string $role): ?string | |
{ | |
if ($role) { | |
return ucfirst(Str::lower($role)); | |
} | |
return null; | |
} | |
public function getPermissionsAttribute(?int $grants): int | |
{ | |
if ($this->role) { | |
$permissions = RBAC::getValue( | |
$this->role | |
); | |
return (int)($permissions | $grants); | |
} | |
return (int)$grants; | |
} | |
public function needsAuthorization(): bool | |
{ | |
return $this->role !== 'Admin'; | |
} | |
public function hasPermissionTo($permission): bool | |
{ | |
if ($this->needsAuthorization() === false) { | |
return true; | |
} | |
return RBAC::check($this, $permission); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment