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
|
36 |
|
public function __construct(User $model) |
18
|
|
|
{ |
19
|
36 |
|
$this->model = $model; |
20
|
36 |
|
} |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* @param int $perPage |
24
|
|
|
* @return mixed |
25
|
|
|
*/ |
26
|
3 |
|
public function getAll($perPage = 20) |
27
|
|
|
{ |
28
|
3 |
|
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
|
15 |
|
public function update(User $user, array $fields) |
54
|
|
|
{ |
55
|
15 |
|
if (!$user->update($fields)) { |
56
|
|
|
return false; |
57
|
|
|
} |
58
|
|
|
|
59
|
15 |
|
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
|
6 |
|
$user->servers()->detach(); |
72
|
6 |
|
DB::table('permissions') |
73
|
6 |
|
->where('entity_id', '=', $user->id) |
74
|
|
|
->whereIn('ability_id', function ($query) { |
75
|
6 |
|
$query->select('id') |
76
|
6 |
|
->from('abilities') |
77
|
6 |
|
->where('entity_type', '=', Server::class); |
78
|
6 |
|
}) |
79
|
6 |
|
->delete(); |
80
|
|
|
} |
81
|
|
|
|
82
|
15 |
|
$user->retract(Bouncer::role()->all()); |
83
|
|
|
|
84
|
15 |
|
if (isset($fields['roles'])) { |
85
|
|
|
$user->assign($fields['roles']); |
86
|
|
|
} |
87
|
|
|
|
88
|
15 |
|
Bouncer::refresh(); |
89
|
|
|
|
90
|
15 |
|
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
|
|
|
} |