|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace App\Services; |
|
4
|
|
|
|
|
5
|
|
|
use App\Http\Requests\TeamStoreRequest; |
|
6
|
|
|
use Spatie\Permission\Models\Role; |
|
7
|
|
|
|
|
8
|
|
|
class TeamService |
|
9
|
|
|
{ |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
/** |
|
13
|
|
|
* Create a user team (Spatie role) |
|
14
|
|
|
* |
|
15
|
|
|
* @param TeamStoreRequest $request |
|
16
|
|
|
* |
|
17
|
|
|
* @return \Illuminate\Database\Eloquent\Builder|\Illuminate\Database\Eloquent\Model |
|
18
|
|
|
*/ |
|
19
|
1 |
|
public function createTeam(TeamStoreRequest $request) |
|
20
|
|
|
{ |
|
21
|
1 |
|
$team = Role::create(['name' => $request->name, 'guard_name' => 'web']); |
|
22
|
|
|
|
|
23
|
1 |
|
return $team; |
|
24
|
|
|
} |
|
25
|
|
|
|
|
26
|
|
|
/** |
|
27
|
|
|
* Update a user team (Spatie role) |
|
28
|
|
|
* |
|
29
|
|
|
* @param \App\Http\Requests\TeamStoreRequest $request |
|
30
|
|
|
* @param int $teamId |
|
31
|
|
|
* |
|
32
|
|
|
* @return \Spatie\Permission\Contracts\Role |
|
33
|
|
|
*/ |
|
34
|
1 |
|
public function updateTeam(TeamStoreRequest $request, int $teamId) |
|
35
|
|
|
{ |
|
36
|
1 |
|
$team = Role::findById($teamId); |
|
37
|
1 |
|
$team->name = $request->name; |
|
|
|
|
|
|
38
|
1 |
|
$team->save(); |
|
39
|
|
|
|
|
40
|
1 |
|
return $team; |
|
41
|
|
|
} |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Return the team from the database |
|
45
|
|
|
* |
|
46
|
|
|
* @param int $teamId |
|
47
|
|
|
* |
|
48
|
|
|
* @return \Spatie\Permission\Contracts\Role |
|
49
|
|
|
*/ |
|
50
|
1 |
|
public function getById(int $teamId) |
|
51
|
|
|
{ |
|
52
|
1 |
|
return Role::findById($teamId); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* Get all teams. |
|
57
|
|
|
* |
|
58
|
|
|
* @return iterable |
|
59
|
|
|
*/ |
|
60
|
1 |
|
public function getAll() |
|
61
|
|
|
{ |
|
62
|
1 |
|
return Role::whereNotIn('name', ['Super Admin', 'Admin', 'Individual', 'Organisation', 'Venue'])->get(); |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
/** |
|
66
|
|
|
* Delete the team from the database |
|
67
|
|
|
* |
|
68
|
|
|
* @param int $teamId |
|
69
|
|
|
* |
|
70
|
|
|
* @throws \Exception |
|
71
|
|
|
*/ |
|
72
|
1 |
|
public function deleteTeam(int $teamId) |
|
73
|
|
|
{ |
|
74
|
1 |
|
$team = Role::findById($teamId); |
|
75
|
1 |
|
$team->delete(); |
|
76
|
1 |
|
} |
|
77
|
|
|
|
|
78
|
|
|
/** |
|
79
|
|
|
* Return all the roles related to administration |
|
80
|
|
|
* |
|
81
|
|
|
* @return \Spatie\Permission\Models\Role[] |
|
82
|
|
|
*/ |
|
83
|
1 |
|
public function getAllAdminRoles() |
|
84
|
|
|
{ |
|
85
|
1 |
|
return Role::where('name', 'like', '%Admin%')->get(); // Admin, Super Admin |
|
86
|
|
|
} |
|
87
|
|
|
|
|
88
|
|
|
/** |
|
89
|
|
|
* Return all the roles related to teams |
|
90
|
|
|
* (just admins can be assigned to teams) |
|
91
|
|
|
* |
|
92
|
|
|
* @return \Spatie\Permission\Models\Role[] |
|
93
|
|
|
*/ |
|
94
|
1 |
|
public function getAllTeamRoles() |
|
95
|
|
|
{ |
|
96
|
1 |
|
return Role::orWhere(function ($query) { |
|
97
|
1 |
|
$query->where('name', 'not like', '%Super admin%') |
|
98
|
1 |
|
->where('name', 'not like', '%Admin%') |
|
99
|
1 |
|
->where('name', 'not like', '%Registered%'); |
|
100
|
1 |
|
})->get(); |
|
101
|
|
|
} |
|
102
|
|
|
|
|
103
|
|
|
/** |
|
104
|
|
|
* Return all the roles Admins and Teams |
|
105
|
|
|
* |
|
106
|
|
|
* @param null $userId |
|
|
|
|
|
|
107
|
|
|
* |
|
108
|
|
|
* @return \Illuminate\Support\Collection |
|
109
|
|
|
*/ |
|
110
|
1 |
|
public function getAllUserRoles($userId = null) |
|
|
|
|
|
|
111
|
|
|
{ |
|
112
|
1 |
|
return Role::all()->pluck('name'); |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
|
|
116
|
|
|
|
|
117
|
|
|
|
|
118
|
|
|
} |
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.