-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathGroupInvitationsPresenter.php
More file actions
193 lines (174 loc) · 6.09 KB
/
GroupInvitationsPresenter.php
File metadata and controls
193 lines (174 loc) · 6.09 KB
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
<?php
namespace App\V1Module\Presenters;
use App\Helpers\MetaFormats\Attributes\Post;
use App\Helpers\MetaFormats\Attributes\Path;
use App\Helpers\MetaFormats\Validators\VMixed;
use App\Helpers\MetaFormats\Validators\VString;
use App\Helpers\MetaFormats\Validators\VTimestamp;
use App\Helpers\MetaFormats\Validators\VUuid;
use App\Exceptions\ForbiddenRequestException;
use App\Model\Repository\GroupInvitations;
use App\Model\Repository\Groups;
use App\Model\Entity\GroupInvitation;
use App\Model\View\GroupViewFactory;
use App\Security\ACL\IGroupPermissions;
use DateTime;
/**
* Group invitations - links that allow users to join a group.
*/
class GroupInvitationsPresenter extends BasePresenter
{
/**
* @var GroupInvitations
* @inject
*/
public $groupInvitations;
/**
* @var Groups
* @inject
*/
public $groups;
/**
* @var IGroupPermissions
* @inject
*/
public $groupAcl;
/**
* @var GroupViewFactory
* @inject
*/
public $groupViewFactory;
public function checkDefault($id)
{
$invitation = $this->groupInvitations->findOrThrow($id);
if (!$invitation->getGroup() || !$this->groupAcl->canViewInvitations($invitation->getGroup())) {
throw new ForbiddenRequestException();
}
}
/**
* Return invitation details including all relevant group entities (so a name can be constructed).
* @GET
*/
#[Path("id", new VUuid(), "Identifier of the group invitation", required: true)]
public function actionDefault($id)
{
$invitation = $this->groupInvitations->findOrThrow($id);
$groups = $this->groups->groupsAncestralClosure([$invitation->getGroup()]);
$this->sendSuccessResponse([
"invitation" => $invitation,
"groups" => $this->groupViewFactory->getGroups($groups),
]);
}
public function checkUpdate($id)
{
$invitation = $this->groupInvitations->findOrThrow($id);
if (!$invitation->getGroup() || !$this->groupAcl->canEditInvitations($invitation->getGroup())) {
throw new ForbiddenRequestException();
}
}
/**
* Edit the invitation.
* @POST
*/
#[Post("expireAt", new VTimestamp(), "When the invitation expires.", nullable: true)]
#[Post("note", new VMixed(), "Note for the students who wish to use the invitation link.", nullable: true)]
#[Path("id", new VUuid(), "Identifier of the group invitation", required: true)]
public function actionUpdate($id)
{
$req = $this->getRequest();
$invitation = $this->groupInvitations->findOrThrow($id);
$expireAt = $req->getPost("expireAt");
$invitation->setExpireAt($expireAt ? new DateTime("@" . (int)$expireAt) : null);
$invitation->setNote($req->getPost("note"));
$this->groupInvitations->persist($invitation);
$this->sendSuccessResponse($invitation);
}
public function checkRemove($id)
{
$invitation = $this->groupInvitations->findOrThrow($id);
if (!$invitation->getGroup() || !$this->groupAcl->canEditInvitations($invitation->getGroup())) {
throw new ForbiddenRequestException();
}
}
/**
* Remove the invitation.
* @DELETE
*/
#[Path("id", new VUuid(), "Identifier of the group invitation", required: true)]
public function actionRemove($id)
{
$invitation = $this->groupInvitations->findOrThrow($id);
$this->groupInvitations->remove($invitation);
$this->sendSuccessResponse("OK");
}
public function checkAccept($id)
{
$invitation = $this->groupInvitations->findOrThrow($id);
if (
$invitation->hasExpired()
|| !$invitation->getGroup()
|| !$this->groupAcl->canAcceptInvitation($invitation->getGroup())
|| $invitation->getGroup()->isOrganizational()
) {
throw new ForbiddenRequestException();
}
}
/**
* Allow the current user to join the corresponding group using the invitation.
* @POST
*/
#[Path("id", new VUuid(), "Identifier of the group invitation", required: true)]
public function actionAccept($id)
{
$invitation = $this->groupInvitations->findOrThrow($id);
$group = $invitation->getGroup();
$user = $this->getCurrentUser();
if ($group->isStudentOf($user) === false) {
$user->makeStudentOf($group);
$this->groups->flush();
}
$this->sendSuccessResponse($this->groupViewFactory->getGroup($group));
}
public function checkList($groupId)
{
$group = $this->groups->findOrThrow($groupId);
if (!$this->groupAcl->canViewDetail($group)) {
throw new ForbiddenRequestException();
}
}
/**
* List all invitations of a group.
* @GET
*/
#[Path("groupId", new VUuid(), required: true)]
public function actionList($groupId)
{
$group = $this->groups->findOrThrow($groupId);
$this->sendSuccessResponse($group->getInvitations()->toArray());
}
public function checkCreate($groupId)
{
$group = $this->groups->findOrThrow($groupId);
if (!$this->groupAcl->canEditInvitations($group)) {
throw new ForbiddenRequestException();
}
}
/**
* Create a new invitation for given group.
* @POST
*/
#[Post("expireAt", new VTimestamp(), "When the invitation expires.", nullable: true)]
#[Post("note", new VMixed(), "Note for the students who wish to use the invitation link.", nullable: true)]
#[Path("groupId", new VUuid(), required: true)]
public function actionCreate($groupId)
{
$req = $this->getRequest();
$group = $this->groups->findOrThrow($groupId);
$host = $this->getCurrentUser();
$expireAt = $req->getPost("expireAt");
$expireAt = $expireAt ? new DateTime("@" . (int)$expireAt) : null;
$invitation = new GroupInvitation($group, $host, $expireAt, $req->getPost("note"));
$this->groupInvitations->persist($invitation);
$this->sendSuccessResponse($invitation);
}
}