|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace A17\Twill\Models; |
|
4
|
|
|
|
|
5
|
|
|
use A17\Twill\Models\User; |
|
6
|
|
|
use A17\Twill\Models\Behaviors\HasMedias; |
|
7
|
|
|
use A17\Twill\Models\Behaviors\HasPermissions; |
|
8
|
|
|
use A17\Twill\Models\Behaviors\IsTranslatable; |
|
9
|
|
|
use A17\Twill\Models\Behaviors\HasPosition; |
|
10
|
|
|
use A17\Twill\Models\Behaviors\Sortable; |
|
11
|
|
|
use Illuminate\Database\Eloquent\Builder; |
|
12
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
13
|
|
|
use Illuminate\Database\Eloquent\SoftDeletes; |
|
14
|
|
|
use Illuminate\Database\Eloquent\Model as BaseModel; |
|
15
|
|
|
use Illuminate\Database\Eloquent\Relations\BelongsToMany; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* Role model |
|
19
|
|
|
* |
|
20
|
|
|
* @property-read string $createdAt Date of creation |
|
21
|
|
|
* @property-read string $usersCount Formatted number of users in this role (ie. '123 users') |
|
22
|
|
|
* @method static Builder published() Get published roles |
|
23
|
|
|
* @method static Builder draft() Get draft roles |
|
24
|
|
|
* @method static Builder onlyTrashed() Get trashed roles |
|
25
|
|
|
*/ |
|
26
|
|
|
class Role extends BaseModel implements Sortable |
|
27
|
|
|
{ |
|
28
|
|
|
use HasMedias, SoftDeletes, HasPermissions, IsTranslatable, HasPosition; |
|
|
|
|
|
|
29
|
|
|
|
|
30
|
|
|
public $timestamps = true; |
|
31
|
|
|
|
|
32
|
|
|
protected $fillable = [ |
|
33
|
|
|
'name', |
|
34
|
|
|
'published', |
|
35
|
|
|
'in_everyone_group', |
|
36
|
|
|
'position', |
|
37
|
|
|
]; |
|
38
|
|
|
|
|
39
|
|
|
protected $dates = [ |
|
40
|
|
|
'deleted_at', |
|
41
|
|
|
]; |
|
42
|
|
|
|
|
43
|
|
|
public $checkboxes = ['published']; |
|
44
|
|
|
|
|
45
|
|
|
protected $casts = [ |
|
46
|
|
|
'in_everyone_group' => 'boolean', |
|
47
|
|
|
]; |
|
48
|
|
|
|
|
49
|
|
|
/** |
|
50
|
|
|
* Scope accessible roles for the current user. |
|
51
|
|
|
* |
|
52
|
|
|
* @param Builder $query |
|
53
|
|
|
* @return Builder |
|
54
|
|
|
*/ |
|
55
|
|
|
public function scopeAccessible($query) |
|
56
|
|
|
{ |
|
57
|
|
|
$currentUser = auth('twill_users')->user(); |
|
58
|
|
|
|
|
59
|
|
|
if ($currentUser->isSuperAdmin()) { |
|
|
|
|
|
|
60
|
|
|
return $query; |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$accessibleRoleIds = $currentUser->role->accessibleRoles->pluck('id')->toArray(); |
|
|
|
|
|
|
64
|
|
|
|
|
65
|
|
|
return $query->whereIn('id', $accessibleRoleIds); |
|
66
|
|
|
} |
|
67
|
|
|
|
|
68
|
|
|
/** |
|
69
|
|
|
* Scope published roles. |
|
70
|
|
|
* |
|
71
|
|
|
* @param Builder $query |
|
72
|
|
|
* @return Builder |
|
73
|
|
|
*/ |
|
74
|
|
|
public function scopePublished($query) |
|
75
|
|
|
{ |
|
76
|
|
|
return $query->wherePublished(true); |
|
77
|
|
|
} |
|
78
|
|
|
|
|
79
|
|
|
/** |
|
80
|
|
|
* Scope unpublished (draft) roles. |
|
81
|
|
|
* |
|
82
|
|
|
* @param Builder $query |
|
83
|
|
|
* @return Builder |
|
84
|
|
|
*/ |
|
85
|
|
|
public function scopeDraft($query) |
|
86
|
|
|
{ |
|
87
|
|
|
return $query->wherePublished(false); |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* Scope trashed roles. |
|
92
|
|
|
* |
|
93
|
|
|
* @param Builder $query |
|
94
|
|
|
* @return Builder |
|
95
|
|
|
*/ |
|
96
|
|
|
public function scopeOnlyTrashed($query) |
|
97
|
|
|
{ |
|
98
|
|
|
return $query->whereNotNull('deleted_at'); |
|
99
|
|
|
} |
|
100
|
|
|
|
|
101
|
|
|
/** |
|
102
|
|
|
* User model relationship |
|
103
|
|
|
* |
|
104
|
|
|
* @return BelongsToMany|Collection|User[] |
|
105
|
|
|
*/ |
|
106
|
|
|
public function users() |
|
107
|
|
|
{ |
|
108
|
|
|
return $this->hasMany(User::class); |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
/** |
|
112
|
|
|
* Return the formatted created date |
|
113
|
|
|
* |
|
114
|
|
|
* @return string |
|
115
|
|
|
*/ |
|
116
|
|
|
public function getCreatedAtAttribute($value) |
|
117
|
|
|
{ |
|
118
|
|
|
return \Carbon\Carbon::parse($value)->format('d M Y'); |
|
119
|
|
|
} |
|
120
|
|
|
|
|
121
|
|
|
/** |
|
122
|
|
|
* Return the formatted number of users in this group |
|
123
|
|
|
* |
|
124
|
|
|
* @return string |
|
125
|
|
|
*/ |
|
126
|
|
|
public function getUsersCountAttribute($value) |
|
|
|
|
|
|
127
|
|
|
{ |
|
128
|
|
|
return $this->users->count() . ' users'; |
|
129
|
|
|
} |
|
130
|
|
|
|
|
131
|
|
|
/** |
|
132
|
|
|
* Return the roles that are considered accessible for this role |
|
133
|
|
|
* |
|
134
|
|
|
* @return \Illuminate\Database\Eloquent\Collection |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getAccessibleRolesAttribute() |
|
137
|
|
|
{ |
|
138
|
|
|
return static::where('position', '>=', $this->position)->get(); |
|
139
|
|
|
} |
|
140
|
|
|
} |
|
141
|
|
|
|