|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Gameap\Repositories; |
|
4
|
|
|
|
|
5
|
|
|
use Bouncer; |
|
6
|
|
|
use DB; |
|
7
|
|
|
use Gameap\Helpers\ServerPermissionHelper; |
|
8
|
|
|
use Gameap\Models\User; |
|
9
|
|
|
use Gameap\Models\Server; |
|
10
|
|
|
|
|
11
|
|
|
class UserRepository extends Repository |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* UserRepository constructor. |
|
15
|
|
|
* @param User $model |
|
16
|
|
|
*/ |
|
17
|
27 |
|
public function __construct(User $model) |
|
18
|
|
|
{ |
|
19
|
27 |
|
$this->model = $model; |
|
20
|
27 |
|
} |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @param int $perPage |
|
24
|
|
|
* @return mixed |
|
25
|
|
|
*/ |
|
26
|
|
|
public function getAll($perPage = 20) |
|
27
|
|
|
{ |
|
28
|
|
|
return User::orderBy('id')->paginate($perPage); |
|
29
|
|
|
} |
|
30
|
|
|
|
|
31
|
|
|
/** |
|
32
|
|
|
* @param array $attributes |
|
33
|
|
|
*/ |
|
34
|
|
|
public function store(array $attributes) |
|
35
|
|
|
{ |
|
36
|
|
|
$user = User::create($attributes); |
|
37
|
|
|
|
|
38
|
|
|
if (isset($attributes['roles'])) { |
|
39
|
|
|
foreach ($attributes['roles'] as &$role) { |
|
40
|
|
|
if (Bouncer::role()->where(['name' => $role])->exists()) { |
|
41
|
|
|
$user->assign($role); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
Bouncer::refresh(); |
|
45
|
|
|
} |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param array $fields |
|
50
|
|
|
* @param User $user |
|
51
|
|
|
* @return bool |
|
52
|
|
|
*/ |
|
53
|
12 |
|
public function update(User $user, array $fields) |
|
54
|
|
|
{ |
|
55
|
12 |
|
if (!$user->update($fields)) { |
|
56
|
|
|
return false; |
|
57
|
|
|
} |
|
58
|
|
|
|
|
59
|
12 |
|
if (isset($fields['servers'])) { |
|
60
|
9 |
|
$user->servers()->sync($fields['servers']); |
|
61
|
9 |
|
DB::table('permissions') |
|
62
|
9 |
|
->where('entity_id', '=', $user->id) |
|
63
|
|
|
->whereIn('ability_id', function ($query) use ($fields) { |
|
64
|
9 |
|
$query->select('id') |
|
65
|
9 |
|
->from('abilities') |
|
66
|
9 |
|
->where('entity_type', '=', Server::class) |
|
67
|
9 |
|
->whereNotIn('entity_id', $fields['servers']); |
|
68
|
9 |
|
}) |
|
69
|
9 |
|
->delete(); |
|
70
|
|
|
} else { |
|
71
|
3 |
|
$user->servers()->detach(); |
|
72
|
3 |
|
DB::table('permissions') |
|
73
|
3 |
|
->where('entity_id', '=', $user->id) |
|
74
|
|
|
->whereIn('ability_id', function ($query) { |
|
75
|
3 |
|
$query->select('id') |
|
76
|
3 |
|
->from('abilities') |
|
77
|
3 |
|
->where('entity_type', '=', Server::class); |
|
78
|
3 |
|
}) |
|
79
|
3 |
|
->delete(); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
12 |
|
if (isset($fields['roles'])) { |
|
83
|
|
|
$user->assign($fields['roles']); |
|
84
|
|
|
} else { |
|
85
|
12 |
|
$user->retract(Bouncer::role()->all()); |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
12 |
|
Bouncer::refresh(); |
|
89
|
|
|
|
|
90
|
12 |
|
return true; |
|
91
|
|
|
} |
|
92
|
|
|
|
|
93
|
27 |
|
public function updateServerPermission(User $user, Server $server, ?array $disabledPermissions) |
|
94
|
|
|
{ |
|
95
|
27 |
|
foreach (ServerPermissionHelper::getAllPermissions() as $pname) { |
|
96
|
27 |
|
if (isset($disabledPermissions[$pname]) && $disabledPermissions[$pname]) { |
|
97
|
18 |
|
Bouncer::forbid($user)->to($pname, $server); |
|
98
|
18 |
|
$user->disallow($pname, $server); |
|
99
|
|
|
} else { |
|
100
|
27 |
|
Bouncer::unforbid($user)->to($pname, $server); |
|
101
|
27 |
|
$user->allow($pname, $server); |
|
102
|
|
|
} |
|
103
|
|
|
} |
|
104
|
|
|
|
|
105
|
27 |
|
$user->servers()->syncWithoutDetaching([$server->id]); |
|
106
|
|
|
|
|
107
|
27 |
|
Bouncer::refresh(); |
|
108
|
|
|
} |
|
109
|
|
|
} |