-
Notifications
You must be signed in to change notification settings - Fork 0
/
CompanyPolicy.php
254 lines (231 loc) · 7.48 KB
/
CompanyPolicy.php
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
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
<?php
namespace App\Policies;
use App\Models\Company;
use App\Models\Team;
use App\Models\User;
use Illuminate\Auth\Access\Response;
class CompanyPolicy
{
/**
* Determine if the user can see the list of companies
* @param User $user
* @return bool
*/
public function viewAny(User $user): bool
{
return $user->can('viewAny company');
}
/**
* Determine whether the user can view the model.
*/
public function view(User $user, Company $company): bool
{
return ($user->is_crew || $user->isMemberOf($company))
&& $user->can('view company');
}
/**
* Determine if the user can create a new entity
*
* @param User $user
* @return bool
*/
public function create(User $user): bool
{
return $user->can('create company');
}
/**
* Determine if the user can edit company details.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function update(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->can('update company');
}
/**
* Determine whether the user can delete the model.
*/
public function delete(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->can('delete company');
}
/**
* Determine if the user can view the company's approval status
* @param User $user
* @param Company $company
* @return bool
*/
public function viewRequest(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company))
&& $user->can('view company request');
}
/**
* Determine if the user can approve the company
* @param User $user
* @param Company $company
* @return bool
*/
public function approveRequest(User $user, Company $company): bool
{
return $user->is_crew && $user->can('update company request');
}
/**
* Determine if the user can view company members.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function viewAnyMember(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->hasPermissionTo('viewAny company member');
}
/**
* Determine if the user can edit company members.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function updateMember(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->hasPermissionTo('update company member');
}
/**
* Determine if the user can delete company member.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function deleteMember(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->hasPermissionTo('delete company member');
}
/**
* Determine if the user can create company invitation.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function createMemberInvitation(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->hasPermissionTo('create company member invitation');
}
/**
* Determine if the user can view company invitations.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function viewAnyMemberInvitation(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->hasPermissionTo('viewAny company member invitation');
}
/**
* Determine if the user can delete company invitation.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function deleteMemberInvitation(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->hasPermissionTo('delete company member invitation');
}
/**
* Determine if the user can request to delete company.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function requestDelete(User $user, Company $company): bool
{
return ($user->is_crew
|| $user->isMemberOf($company) && $company->is_approved)
&& $user->hasPermissionTo('create company delete request');
}
/**
* Determine if the user can view company requests.
*
* @param User $user The user instance.
* @param Company $company The company instance.
* @return bool
*/
public function viewRequests(User $user, Company $company): bool
{
// View if the company member hasn't decided their role
if ($user->isMemberOf($company) && $user->isDefaultCompanyMember) {
return true;
}
$isCrewOrCompanyMember = $user->is_crew || ($user->isMemberOf($company) && $company->is_approved);
$hasRequiredPermissions = $user->hasAllPermissions([
'view booth request',
'view sponsorship request',
'view company delete request'
]) || $user->hasRole('booth owner');
return $isCrewOrCompanyMember && $hasRequiredPermissions;
}
/**
* Determines whether the user can request a booth
* @param User $user
* @param Company $company
* @return bool
*/
public function requestBooth(User $user, Company $company): bool
{
return $user->isMemberOf($company)
&& !$company->booth
&& ($user->isDefaultCompanyMember() || $user->hasRole('company representative', 'web'))
&& $user->hasPermissionTo('create booth request');
}
/**
* Determine whether the user can request sponsorship
*
* @param User $user
* @param Company $company
* @return bool
*/
public function requestSponsorship(User $user, Company $company)
{
return $user->isMemberOf($company)
&& $user->hasPermissionTo('create sponsorship request')
&& !$company->sponsorship;
}
/**
* Determines whether the user can join the booth owners
* @param User $user
* @param Company $company
* @return bool
*/
public function becomeBoothOwner(User $user, Company $company): bool
{
return $user->isMemberOf($company)
&& $company->booth
&& ($user->isDefaultCompanyMember() || $user->hasRole('company representative', 'web'))
&& $user->hasPermissionTo('create booth request');
}
}