/
User.php
120 lines (101 loc) · 3.32 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
<?php
namespace app;
use Illuminate\Auth\Authenticatable;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Auth\Passwords\CanResetPassword;
use Illuminate\Foundation\Auth\Access\Authorizable;
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
class User extends Model implements
AuthenticatableContract,
CanResetPasswordContract
{
use Authenticatable, Authorizable, CanResetPassword;
/**
* The database table used by the model.
*
* @var string
*/
protected $table = 'users';
/**
* The attributes that are mass assignable.
*
* @var array
*/
protected $fillable = ['name', 'email', 'password'];
/**
* The attributes excluded from the model's JSON form.
*
* @var array
*/
protected $hidden = ['password', 'remember_token'];
/*
|--------------------------------------------------------------------------
| ACL Methods
|--------------------------------------------------------------------------
*/
/**
* Checks a Permission.
*
* @param String $permission Slug of a permission (i.e: manage_user)
*
* @return Boolean true if has permission, otherwise false
*/
public function can($permission = null)
{
$array_permission = explode('|', $permission);
return !is_null($permission) && $this->checkPermission($array_permission);
}
/**
* Check if the permission matches with any permission user has.
*
* @param String $perm permission slug of a permission
*
* @return Boolean true if permission exists, otherwise false
*/
protected function checkPermission($perm)
{
$permissions = $this->getAllPernissionsFormAllRoles();
// dd($permissions);
$permissionArray = is_array($perm) ? $perm : [$perm];
return count(array_intersect($permissions, $permissionArray));
}
/**
* Get all permission slugs from all permissions of all roles.
*
* @return Array of permission slugs
*/
protected function getAllPernissionsFormAllRoles()
{
$permissions = $this->roles->load('permissions')->fetch('permissions')->toArray();
return array_map('strtolower', array_unique(array_flatten(array_map(function ($permission) {
return array_fetch($permission, 'permission_slug');
}, $permissions))));
}
/**
* Get all access module from all permissions of all roles.
*
* @return Array of permission slugs
*/
public function hasAccess()
{
$permissions = $this->roles->load('permissions')->fetch('permissions')->toArray();
return array_map('strtolower', array_unique(array_flatten(array_map(function ($permission) {
return array_fetch($permission, 'permission_slug');
}, $permissions))));
}
/*
|--------------------------------------------------------------------------
| Relationship Methods
|--------------------------------------------------------------------------
*/
/**
* Many-To-Many Relationship Method for accessing the User->roles.
*
* @return QueryBuilder Object
*/
public function roles()
{
return $this->belongsToMany('App\Admin\Role');
}
}