This repository has been archived by the owner on Jan 8, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
User.php
131 lines (109 loc) · 3.33 KB
/
User.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<?php
use Illuminate\Auth\UserTrait;
use Illuminate\Auth\UserInterface;
use Illuminate\Auth\Reminders\RemindableTrait;
use Illuminate\Auth\Reminders\RemindableInterface;
class User extends Eloquent implements UserInterface, RemindableInterface
{
use UserTrait, RemindableTrait;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token', 'email'];
protected $fillable = ['username', 'password', 'title', 'roles', 'email',
'phone'];
public function attachRole($role)
{
$origin = explode(',', $this->roles);
$origin[] = $role;
$this->roles = implode(',', $origin);
$this->save();
}
public function attachRoles($roles)
{
$origin = explode(',', $this->roles);
$origin = array_merge($origin, $roles);
$this->roles = implode(',', $origin);
$this->save();
}
public function detachRole($role)
{
$origin = explode(',', $this->roles);
$origin = array_diff($origin, [$role]);
$this->roles = implode(',', $origin);
$this->save();
}
public function detachRoles($roles)
{
$origin = explode(',', $this->roles);
$origin = array_diff($origin, $roles);
$this->roles = implode(',', $origin);
$this->save();
}
public function getRoles()
{
$roles = explode(',', $this->roles);
return is_array($roles) ? $roles : [$roles];
}
public function hasRole($role)
{
return in_array($role, $this->getRoles());
}
public function can($permission)
{
$permission_role = Config::get('role_perm.permission_role');
foreach ($this->getRoles() as $role) {
if (in_array($permission, $permission_role[$role])) {
return true;
}
}
return false;
}
public function ability(Array $roles, Array $permissions, $options = [])
{
if (!isset($options['validate_all'])) {
$options['validate_all'] = false;
}
if (!is_bool($options['validate_all'])) {
$options['validate_all'] = false;
}
if (!isset($options['return_type'])) {
$options['return_type'] = 'boolean';
}
$types = ['boolean', 'array', 'both'];
if (!in_array($options['return_type'], $types)) {
$options['return_type'] = 'boolean';
}
foreach ($roles as $role) {
$validations[$role] = $this->hasRole($role);
}
foreach ($permissions as $permission) {
$validations[$permission] = $this->can($permission);
}
if ($options['validate_all'] == true) {
$is_pass = !in_array(false, $validations);
} else {
$is_pass = in_array(true, $validations);
}
switch ($options['return_type']) {
case 'boolean':
default:
return $is_pass;
break;
case 'array':
return $validations;
break;
case 'both':
return [$is_pass, $validations];
break;
}
}
}