RoleRepository.php
7.14 KB
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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
<?php
namespace App\Access\Repository\Role;
use App\Access\Model\Role\Role;
use Illuminate\Support\Facades\DB;
use App\Exceptions\GeneralException;
use App\Repositories\BaseRepository;
use Illuminate\Database\Eloquent\Model;
use App\Events\Backend\Access\Role\RoleCreated;
use App\Events\Backend\Access\Role\RoleDeleted;
use App\Events\Backend\Access\Role\RoleUpdated;
/**
* Class RoleRepository.
*/
class RoleRepository extends BaseRepository
{
/**
* Associated Repository Model.
*/
const MODEL = Role::class;
/**
* @param string $order_by
* @param string $sort
*
* @return mixed
*/
public function getAll($order_by = 'sort', $sort = 'asc')
{
return $this->query()
->with('users', 'permissions')
->orderBy($order_by, $sort)
->get();
}
public function getscience($order_by = 'sort', $sort = 'asc')
{
//迁移文件李要加个 3 表示景区创建
return $this->query()->where(['id'=>3])
->with('users', 'permissions')
->orderBy($order_by, $sort)
->first()
;
}
/**
* @return mixed
*/
public function getForDataTable()
{
return $this->query()
->with('users', 'permissions')
->select([
config('access.roles_table').'.id',
config('access.roles_table').'.name',
config('access.roles_table').'.all',
config('access.roles_table').'.sort',
]);
}
/**
* @param array $input
*
* @throws GeneralException
*
* @return bool
*/
public function create(array $input)
{
if ($this->query()->where('name', $input['name'])->first()) {
throw new GeneralException(trans('exceptions.backend.access.roles.already_exists'));
}
//See if the role has all access
$all = $input['associated-permissions'] == 'all' ? true : false;
if (! isset($input['permissions'])) {
$input['permissions'] = [];
}
//This config is only required if all is false
if (! $all) {
//See if the role must contain a permission as per config
if (config('access.roles.role_must_contain_permission') && count($input['permissions']) == 0) {
throw new GeneralException(trans('exceptions.backend.access.roles.needs_permission'));
}
}
DB::transaction(function () use ($input, $all) {
$role = self::MODEL;
$role = new $role();
$role->name = $input['name'];
$role->sort = isset($input['sort']) && strlen($input['sort']) > 0 && is_numeric($input['sort']) ? (int) $input['sort'] : 0;
//See if this role has all permissions and set the flag on the role
$role->all = $all;
if ($role->save()) {
if (! $all) {
$permissions = [];
if (is_array($input['permissions']) && count($input['permissions'])) {
foreach ($input['permissions'] as $perm) {
if (is_numeric($perm)) {
array_push($permissions, $perm);
}
}
}
$role->attachPermissions($permissions);
}
event(new RoleCreated($role));
return true;
}
throw new GeneralException(trans('exceptions.backend.access.roles.create_error'));
});
}
/**
* @param Model $role
* @param $input
*
* @throws GeneralException
*
* @return bool
*/
public function update(Model $role, array $input)
{
//See if the role has all access, administrator always has all access
if ($role->id == 1) {
$all = true;
} else {
$all = $input['associated-permissions'] == 'all' ? true : false;
}
if (! isset($input['permissions'])) {
$input['permissions'] = [];
}
//This config is only required if all is false
if (! $all) {
//See if the role must contain a permission as per config
if (config('access.roles.role_must_contain_permission') && count($input['permissions']) == 0) {
throw new GeneralException(trans('exceptions.backend.access.roles.needs_permission'));
}
}
$role->name = $input['name'];
$role->sort = isset($input['sort']) && strlen($input['sort']) > 0 && is_numeric($input['sort']) ? (int) $input['sort'] : 0;
//See if this role has all permissions and set the flag on the role
$role->all = $all;
DB::transaction(function () use ($role, $input, $all) {
if ($role->save()) {
//If role has all access detach all permissions because they're not needed
if ($all) {
$role->permissions()->sync([]);
} else {
//Remove all roles first
$role->permissions()->sync([]);
//Attach permissions if the role does not have all access
$permissions = [];
if (is_array($input['permissions']) && count($input['permissions'])) {
foreach ($input['permissions'] as $perm) {
if (is_numeric($perm)) {
array_push($permissions, $perm);
}
}
}
$role->attachPermissions($permissions);
}
event(new RoleUpdated($role));
return true;
}
throw new GeneralException(trans('exceptions.backend.access.roles.update_error'));
});
}
/**
* @param Model $role
*
* @throws GeneralException
*
* @return bool
*/
public function delete(Model $role)
{
//Would be stupid to delete the administrator role
if ($role->id == 1) { //id is 1 because of the seeder
throw new GeneralException(trans('exceptions.backend.access.roles.cant_delete_admin'));
}
//Don't delete the role is there are users associated
if ($role->users()->count() > 0) {
throw new GeneralException(trans('exceptions.backend.access.roles.has_users'));
}
DB::transaction(function () use ($role) {
//Detach all associated roles
$role->permissions()->sync([]);
if ($role->delete()) {
event(new RoleDeleted($role));
return true;
}
throw new GeneralException(trans('exceptions.backend.access.roles.delete_error'));
});
}
/**
* @return mixed
*/
public function getDefaultUserRole()
{
if (is_numeric(config('access.users.default_role'))) {
return $this->query()->where('id', (int) config('access.users.default_role'))->first();
}
return $this->query()->where('name', config('access.users.default_role'))->first();
}
}