|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Siak\Tontine\Service; |
|
4
|
|
|
|
|
5
|
|
|
use Siak\Tontine\Exception\MessageException; |
|
6
|
|
|
use Siak\Tontine\Model\Round; |
|
7
|
|
|
use Siak\Tontine\Model\Guild; |
|
8
|
|
|
use Siak\Tontine\Model\User; |
|
9
|
|
|
|
|
10
|
|
|
class TenantService |
|
11
|
|
|
{ |
|
12
|
|
|
/** |
|
13
|
|
|
* @var User|null |
|
14
|
|
|
*/ |
|
15
|
|
|
protected ?User $user = null; |
|
16
|
|
|
|
|
17
|
|
|
/** |
|
18
|
|
|
* @var Guild|null |
|
19
|
|
|
*/ |
|
20
|
|
|
protected ?Guild $guild = null; |
|
21
|
|
|
|
|
22
|
|
|
/** |
|
23
|
|
|
* @var Round|null |
|
24
|
|
|
*/ |
|
25
|
|
|
protected ?Round $round = null; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* @var int |
|
29
|
|
|
*/ |
|
30
|
|
|
protected int $limit = 10; |
|
31
|
|
|
|
|
32
|
|
|
/** |
|
33
|
|
|
* @param LocaleService $localeService |
|
34
|
|
|
*/ |
|
35
|
|
|
public function __construct(protected LocaleService $localeService) |
|
36
|
|
|
{} |
|
37
|
|
|
|
|
38
|
|
|
/** |
|
39
|
|
|
* @param User $user |
|
40
|
|
|
* |
|
41
|
|
|
* @return void |
|
42
|
|
|
*/ |
|
43
|
|
|
public function setUser(User $user): void |
|
44
|
|
|
{ |
|
45
|
|
|
$this->user = $user; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @param Guild $guild |
|
50
|
|
|
* |
|
51
|
|
|
* @return void |
|
52
|
|
|
*/ |
|
53
|
|
|
public function setGuild(Guild $guild): void |
|
54
|
|
|
{ |
|
55
|
|
|
$this->guild = $guild; |
|
56
|
|
|
// Set the currency for locales. |
|
57
|
|
|
$this->localeService->setCurrency($guild->currency_code); |
|
58
|
|
|
// Save as latest guild id if it has changed. |
|
59
|
|
|
$guildId = $this->user?->properties['latest']['guild'] ?? 0; |
|
|
|
|
|
|
60
|
|
|
if(!$this->user || $guildId === $guild->id) |
|
61
|
|
|
{ |
|
62
|
|
|
return; |
|
63
|
|
|
} |
|
64
|
|
|
|
|
65
|
|
|
$properties = $this->user->properties; |
|
66
|
|
|
$properties['latest']['guild'] = $guild->id; |
|
67
|
|
|
$this->user->saveProperties($properties); |
|
68
|
|
|
$this->resetRound(); |
|
69
|
|
|
} |
|
70
|
|
|
|
|
71
|
|
|
/** |
|
72
|
|
|
* @param Round $round |
|
73
|
|
|
* |
|
74
|
|
|
* @return void |
|
75
|
|
|
*/ |
|
76
|
|
|
public function setRound(Round $round): void |
|
77
|
|
|
{ |
|
78
|
|
|
$this->round = $round; |
|
79
|
|
|
// Save as latest round id if it has changed. |
|
80
|
|
|
$roundId = $this->user->properties['latest']['round'] ?? 0; |
|
|
|
|
|
|
81
|
|
|
if($roundId === $round->id) |
|
82
|
|
|
{ |
|
83
|
|
|
return; |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$properties = $this->user->properties; |
|
87
|
|
|
$properties['latest']['round'] = $round->id; |
|
88
|
|
|
$this->user->saveProperties($properties); |
|
|
|
|
|
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
/** |
|
92
|
|
|
* @return void |
|
93
|
|
|
*/ |
|
94
|
|
|
public function resetRound(): void |
|
95
|
|
|
{ |
|
96
|
|
|
$this->round = null; |
|
97
|
|
|
} |
|
98
|
|
|
|
|
99
|
|
|
/** |
|
100
|
|
|
* @return User|null |
|
101
|
|
|
*/ |
|
102
|
|
|
public function user(): ?User |
|
103
|
|
|
{ |
|
104
|
|
|
return $this->user; |
|
105
|
|
|
} |
|
106
|
|
|
|
|
107
|
|
|
/** |
|
108
|
|
|
* @return Guild|null |
|
109
|
|
|
*/ |
|
110
|
|
|
public function guild(): ?Guild |
|
111
|
|
|
{ |
|
112
|
|
|
return $this->guild; |
|
113
|
|
|
} |
|
114
|
|
|
|
|
115
|
|
|
/** |
|
116
|
|
|
* @return Round|null |
|
117
|
|
|
*/ |
|
118
|
|
|
public function round(): ?Round |
|
119
|
|
|
{ |
|
120
|
|
|
return $this->round; |
|
121
|
|
|
} |
|
122
|
|
|
|
|
123
|
|
|
/** |
|
124
|
|
|
* @param int $roundId The round id |
|
125
|
|
|
* |
|
126
|
|
|
* @return Round|null |
|
127
|
|
|
*/ |
|
128
|
|
|
public function getRound(int $roundId): ?Round |
|
129
|
|
|
{ |
|
130
|
|
|
return $this->guild->rounds()->find($roundId); |
|
|
|
|
|
|
131
|
|
|
} |
|
132
|
|
|
|
|
133
|
|
|
/** |
|
134
|
|
|
* @return Round|null |
|
135
|
|
|
*/ |
|
136
|
|
|
public function getFirstRound(): ?Round |
|
137
|
|
|
{ |
|
138
|
|
|
return $this->guild->rounds()->first(); |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
/** |
|
142
|
|
|
* @return int |
|
143
|
|
|
*/ |
|
144
|
|
|
public function getLimit(): int |
|
145
|
|
|
{ |
|
146
|
|
|
return $this->limit; |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
/** |
|
150
|
|
|
* @return bool |
|
151
|
|
|
*/ |
|
152
|
|
|
private function userIsGuest(): bool |
|
153
|
|
|
{ |
|
154
|
|
|
return $this->guild !== null && $this->guild->isGuest; |
|
155
|
|
|
} |
|
156
|
|
|
|
|
157
|
|
|
/** |
|
158
|
|
|
* @return array |
|
159
|
|
|
*/ |
|
160
|
|
|
private function getHostAccess(): array |
|
161
|
|
|
{ |
|
162
|
|
|
if(!$this->guild || !$this->user) |
|
163
|
|
|
{ |
|
164
|
|
|
return []; |
|
165
|
|
|
} |
|
166
|
|
|
$userInvite = $this->guild->invites() |
|
167
|
|
|
->where('guest_id', $this->user->id) |
|
168
|
|
|
->first(); |
|
169
|
|
|
if(!$userInvite) |
|
170
|
|
|
{ |
|
171
|
|
|
return []; |
|
172
|
|
|
} |
|
173
|
|
|
|
|
174
|
|
|
return $userInvite->options->access; |
|
175
|
|
|
} |
|
176
|
|
|
|
|
177
|
|
|
/** |
|
178
|
|
|
* Check guest user access to a menu entry in a section |
|
179
|
|
|
* |
|
180
|
|
|
* @param string $section |
|
181
|
|
|
* @param string $entry |
|
182
|
|
|
* @param bool $return |
|
183
|
|
|
* |
|
184
|
|
|
* @return bool |
|
185
|
|
|
* @throws MessageException |
|
186
|
|
|
*/ |
|
187
|
|
|
public function checkHostAccess(string $section, string $entry, bool $return = false): bool |
|
188
|
|
|
{ |
|
189
|
|
|
if(!$this->userIsGuest()) |
|
190
|
|
|
{ |
|
191
|
|
|
return true; |
|
192
|
|
|
} |
|
193
|
|
|
|
|
194
|
|
|
$guestAccess = $this->getHostAccess(); |
|
195
|
|
|
if(!($guestAccess[$section][$entry] ?? false)) |
|
196
|
|
|
{ |
|
197
|
|
|
if($return) |
|
198
|
|
|
{ |
|
199
|
|
|
return false; |
|
200
|
|
|
} |
|
201
|
|
|
throw new MessageException(trans('tontine.invite.errors.access_denied')); |
|
202
|
|
|
} |
|
203
|
|
|
return true; |
|
204
|
|
|
} |
|
205
|
|
|
} |
|
206
|
|
|
|
Checks if undeclared accessed properties appear in database migrations and if the creating migration is correct.