|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Encore\Admin\Auth\Database; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Auth\Authenticatable; |
|
6
|
|
|
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract; |
|
7
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
8
|
|
|
|
|
9
|
|
|
class Administrator extends Model implements AuthenticatableContract |
|
10
|
|
|
{ |
|
11
|
|
|
use Authenticatable; |
|
12
|
|
|
|
|
13
|
|
|
protected $fillable = ['username', 'password', 'name']; |
|
14
|
|
|
|
|
15
|
|
|
/** |
|
16
|
|
|
* Create a new Eloquent model instance. |
|
17
|
|
|
* |
|
18
|
|
|
* @param array $attributes |
|
19
|
|
|
*/ |
|
20
|
|
|
public function __construct(array $attributes = []) |
|
21
|
|
|
{ |
|
22
|
|
|
$this->table = config('admin.database.users_table'); |
|
23
|
|
|
|
|
24
|
|
|
parent::__construct($attributes); |
|
25
|
|
|
} |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* A User belongs to many roles. |
|
29
|
|
|
* |
|
30
|
|
|
* @return \Illuminate\Database\Eloquent\Relations\BelongsToMany |
|
31
|
|
|
*/ |
|
32
|
|
|
public function roles() |
|
33
|
|
|
{ |
|
34
|
|
|
$pivotTable = config('admin.database.role_users_table'); |
|
35
|
|
|
|
|
36
|
|
|
return $this->belongsToMany(Role::class, $pivotTable, 'user_id', 'role_id'); |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* Check if user has permission. |
|
41
|
|
|
* |
|
42
|
|
|
* @param $permission |
|
43
|
|
|
* |
|
44
|
|
|
* @return bool |
|
45
|
|
|
*/ |
|
46
|
|
|
public function can($permission) |
|
47
|
|
|
{ |
|
48
|
|
|
foreach ($this->roles as $role) { |
|
|
|
|
|
|
49
|
|
|
if ($role->can($permission)) { |
|
50
|
|
|
return true; |
|
51
|
|
|
} |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return false; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Check if user has no permission. |
|
59
|
|
|
* |
|
60
|
|
|
* @param $permission |
|
61
|
|
|
* |
|
62
|
|
|
* @return bool |
|
63
|
|
|
*/ |
|
64
|
|
|
public function cannot($permission) |
|
65
|
|
|
{ |
|
66
|
|
|
return !$this->can($permission); |
|
67
|
|
|
} |
|
68
|
|
|
|
|
69
|
|
|
/** |
|
70
|
|
|
* Check if user is $roles. |
|
71
|
|
|
* |
|
72
|
|
|
* @param $roles |
|
73
|
|
|
* |
|
74
|
|
|
* @return mixed |
|
75
|
|
|
*/ |
|
76
|
|
|
public function isRole($roles) |
|
77
|
|
|
{ |
|
78
|
|
|
if (is_string($roles)) { |
|
79
|
|
|
$roles = [$roles]; |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
return $this->roles()->whereIn('slug', $roles)->exists(); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
/** |
|
86
|
|
|
* If visible for roles. |
|
87
|
|
|
* |
|
88
|
|
|
* @param $roles |
|
89
|
|
|
* |
|
90
|
|
|
* @return bool |
|
91
|
|
|
*/ |
|
92
|
|
|
public function visible($roles) |
|
93
|
|
|
{ |
|
94
|
|
|
if (empty($roles)) { |
|
95
|
|
|
return true; |
|
96
|
|
|
} |
|
97
|
|
|
|
|
98
|
|
|
$roles = array_column($roles, 'slug'); |
|
99
|
|
|
|
|
100
|
|
|
if ($this->isRole($roles) || $this->isRole('administrator')) { |
|
101
|
|
|
return true; |
|
102
|
|
|
} |
|
103
|
|
|
|
|
104
|
|
|
return false; |
|
105
|
|
|
} |
|
106
|
|
|
} |
|
107
|
|
|
|
Since your code implements the magic getter
_get, this function will be called for any read access on an undefined variable. You can add the@propertyannotation to your class or interface to document the existence of this variable.If the property has read access only, you can use the @property-read annotation instead.
Of course, you may also just have mistyped another name, in which case you should fix the error.
See also the PhpDoc documentation for @property.