-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathGroups.php
169 lines (146 loc) · 4.88 KB
/
Groups.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
<?php
namespace App\controllers;
use Minz\Request;
use Minz\Response;
use App\auth;
use App\models;
/**
* @author Marien Fressinaud <[email protected]>
* @license http://www.gnu.org/licenses/agpl-3.0.en.html AGPL
*/
class Groups
{
/**
* @request_param string id
* @request_param string from
*
* @response 302 /login?redirect_to=:from
* if not connected
* @response 404
* if the group doesn't exist or is inaccessible
* @response 200
* on success
*/
public function edit(Request $request): Response
{
$user = auth\CurrentUser::get();
$group_id = $request->param('id', '');
$from = $request->param('from', '');
if (!$user) {
return Response::redirect('login', ['redirect_to' => $from]);
}
$group = models\Group::find($group_id);
if ($group && auth\GroupsAccess::canUpdate($user, $group)) {
return Response::ok('groups/edit.phtml', [
'group' => $group,
'name' => $group->name,
'from' => $from,
'name_max_length' => models\Group::NAME_MAX_LENGTH,
]);
} else {
return Response::notFound('not_found.phtml');
}
}
/**
* @request_param string id
* @request_param string name
* @request_param string from
* @request_param string csrf
*
* @response 302 /login?redirect_to=:from
* if not connected
* @response 404
* if the group doesn't exist or is inaccessible
* @response 400
* if the CSRF token is invalid, if the name is invalid or already used
* @response 302 :from
* on success
*/
public function update(Request $request): Response
{
$user = auth\CurrentUser::get();
$group_id = $request->param('id', '');
$name = $request->param('name', '');
$from = $request->param('from', '');
$csrf = $request->param('csrf', '');
if (!$user) {
return Response::redirect('login', ['redirect_to' => $from]);
}
$group = models\Group::find($group_id);
if (!$group || !auth\GroupsAccess::canUpdate($user, $group)) {
return Response::notFound('not_found.phtml');
}
if (!\Minz\Csrf::validate($csrf)) {
return Response::badRequest('groups/edit.phtml', [
'group' => $group,
'name' => $name,
'from' => $from,
'name_max_length' => models\Group::NAME_MAX_LENGTH,
'error' => _('A security verification failed: you should retry to submit the form.'),
]);
}
$group->name = trim($name);
$errors = $group->validate();
if ($errors) {
return Response::badRequest('groups/edit.phtml', [
'group' => $group,
'name' => $name,
'from' => $from,
'name_max_length' => models\Group::NAME_MAX_LENGTH,
'errors' => $errors,
]);
}
$existing_group = models\Group::findBy([
'user_id' => $user->id,
'name' => $group->name,
]);
if ($existing_group && $existing_group->id !== $group->id) {
return Response::badRequest('groups/edit.phtml', [
'group' => $group,
'name' => $name,
'from' => $from,
'name_max_length' => models\Group::NAME_MAX_LENGTH,
'errors' => [
'name' => _('You already have a group with this name.'),
],
]);
}
$group->save();
return Response::found($from);
}
/**
* @request_param string id
* @request_param string from
* @request_param string csrf
*
* @response 302 /login?redirect_to=:from
* if not connected
* @response 404
* if the group doesn't exist or is inaccessible
* @response 302 :from
* @flash error
* if the CSRF token is invalid
* @response 302 :from
* on success
*/
public function delete(Request $request): Response
{
$user = auth\CurrentUser::get();
$group_id = $request->param('id', '');
$from = $request->param('from', '');
$csrf = $request->param('csrf', '');
if (!$user) {
return Response::redirect('login', ['redirect_to' => $from]);
}
$group = models\Group::find($group_id);
if (!$group || !auth\GroupsAccess::canDelete($user, $group)) {
return Response::notFound('not_found.phtml');
}
if (!\Minz\Csrf::validate($csrf)) {
\Minz\Flash::set('error', _('A security verification failed.'));
return Response::found($from);
}
models\Group::delete($group->id);
return Response::found($from);
}
}