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