1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class UserController extends Controller implements PermissionProvider |
|
|
|
|
4
|
|
|
{ |
5
|
|
|
|
6
|
|
|
private static $allowed_actions = array( |
|
|
|
|
7
|
|
|
'index', |
8
|
|
|
'accept', |
9
|
|
|
'success', |
10
|
|
|
'InvitationForm', |
11
|
|
|
'AcceptForm', |
12
|
|
|
'expired', |
13
|
|
|
'notfound' |
14
|
|
|
); |
15
|
|
|
|
16
|
|
|
public function providePermissions() |
17
|
|
|
{ |
18
|
|
|
return array( |
19
|
|
|
'ACCESS_USER_INVITATIONS' => array( |
20
|
|
|
'name' => _t('UserController.ACCESS_PERMISSIONS', 'Allow user invitations'), |
|
|
|
|
21
|
|
|
'category' => _t('UserController.CMS_ACCESS_CATEGORY', 'User Invitations') |
22
|
|
|
) |
23
|
|
|
); |
24
|
|
|
} |
25
|
|
|
|
26
|
|
|
|
27
|
|
|
public function index() |
28
|
|
|
{ |
29
|
|
|
if (!Member::currentUserID()) { |
|
|
|
|
30
|
|
|
return $this->redirect('/Security/login'); |
31
|
|
|
} else if (!Permission::check('ACCESS_USER_INVITATIONS')){ |
|
|
|
|
32
|
|
|
return Security::permissionFailure(); |
|
|
|
|
33
|
|
|
|
34
|
|
|
} else return $this->renderWith(array('UserController', 'Page')); |
35
|
|
|
} |
36
|
|
|
|
37
|
|
|
public function InvitationForm() |
38
|
|
|
{ |
39
|
|
|
$groups = Member::currentUser()->Groups()->map('Code', 'Title')->toArray(); |
40
|
|
|
$fields = FieldList::create( |
|
|
|
|
41
|
|
|
TextField::create('FirstName', _t('UserController.INVITE_FIRSTNAME', 'First name:')), |
|
|
|
|
42
|
|
|
EmailField::create('Email', _t('UserController.INVITE_EMAIL', 'Invite email:')), |
|
|
|
|
43
|
|
|
ListboxField::create('Groups', _t('UserController.INVITE_GROUP', 'Add to group'), $groups) |
|
|
|
|
44
|
|
|
->setMultiple(true) |
45
|
|
|
->setRightTitle(_t('UserController.INVITE_GROUP_RIGHTTITLE', 'Ctrl + click to select multiple')) |
46
|
|
|
); |
47
|
|
|
$actions = FieldList::create( |
48
|
|
|
FormAction::create('sendInvite', _t('UserController.SEND_INVITATION', 'Send Invitation')) |
|
|
|
|
49
|
|
|
); |
50
|
|
|
$requiredFields = RequiredFields::create('FirstName', 'Email'); |
|
|
|
|
51
|
|
|
|
52
|
|
|
if (UserInvitation::config()->get('force_require_group')) { |
53
|
|
|
$requiredFields->addRequiredField('Groups'); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
$form = new Form($this, 'InvitationForm', $fields, $actions, $requiredFields); |
|
|
|
|
57
|
|
|
$this->extend('updateInvitationForm', $form); |
58
|
|
|
return $form; |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
/** |
62
|
|
|
* Records and sends the user's invitation |
63
|
|
|
* @param $data |
64
|
|
|
* @param Form $form |
65
|
|
|
* @return bool|SS_HTTPResponse |
|
|
|
|
66
|
|
|
*/ |
67
|
|
|
public function sendInvite($data, Form $form) |
68
|
|
|
{ |
69
|
|
|
if (!Permission::check('ACCESS_USER_INVITATIONS')){ |
70
|
|
|
$form->sessionMessage( |
71
|
|
|
_t( |
|
|
|
|
72
|
|
|
'UserController.PERMISSION_FAILURE', |
73
|
|
|
"You don't have permission to create user invitations" |
74
|
|
|
), |
75
|
|
|
'bad' |
76
|
|
|
); |
77
|
|
|
return $this->redirectBack(); |
78
|
|
|
} |
79
|
|
|
if (!$form->validate()) { |
80
|
|
|
$form->sessionMessage( |
81
|
|
|
_t( |
82
|
|
|
'UserController.SENT_INVITATION_VALIDATION_FAILED', |
83
|
|
|
'At least one error occured while trying to save your invite: {error}', |
84
|
|
|
array('error' => $form->getValidator()->getErrors()[0]['fieldName']) |
85
|
|
|
), |
86
|
|
|
'bad' |
87
|
|
|
); |
88
|
|
|
return $this->redirectBack(); |
89
|
|
|
} |
90
|
|
|
|
91
|
|
|
$invite = UserInvitation::create(); |
92
|
|
|
$form->saveInto($invite); |
93
|
|
|
try { |
94
|
|
|
$invite->write(); |
95
|
|
|
} catch (ValidationException $e) { |
|
|
|
|
96
|
|
|
$form->sessionMessage( |
97
|
|
|
$e->getMessage(), |
98
|
|
|
'bad' |
99
|
|
|
); |
100
|
|
|
return $this->redirectBack(); |
101
|
|
|
} |
102
|
|
|
$invite->sendInvitation(); |
103
|
|
|
|
104
|
|
|
$form->sessionMessage( |
105
|
|
|
_t( |
106
|
|
|
'UserController.SENT_INVITATION', |
107
|
|
|
'An invitation was sent to {email}.', |
108
|
|
|
array('email' => $data['Email']) |
109
|
|
|
), |
110
|
|
|
'good' |
111
|
|
|
); |
112
|
|
|
return $this->redirectBack(); |
113
|
|
|
|
114
|
|
|
} |
115
|
|
|
|
116
|
|
|
|
117
|
|
|
public function accept() |
118
|
|
|
{ |
119
|
|
|
if (!$hash = $this->getRequest()->param('ID')) { |
120
|
|
|
return $this->forbiddenError(); |
121
|
|
|
} |
122
|
|
|
if ($invite = UserInvitation::get()->filter('TempHash', $hash)->first()) { |
123
|
|
|
if ($invite->isExpired()) { |
124
|
|
|
return $this->redirect($this->Link('expired')); |
125
|
|
|
} |
126
|
|
|
} else { |
127
|
|
|
return $this->redirect($this->Link('notfound')); |
128
|
|
|
} |
129
|
|
|
return $this->renderWith(array('UserController_accept', 'Page'), array('Invite' => $invite)); |
130
|
|
|
} |
131
|
|
|
|
132
|
|
|
public function AcceptForm() |
133
|
|
|
{ |
134
|
|
|
$hash = $this->getRequest()->param('ID'); |
135
|
|
|
$invite = UserInvitation::get()->filter('TempHash', $hash)->first(); |
136
|
|
|
$firstName = ($invite) ? $invite->FirstName : ''; |
137
|
|
|
|
138
|
|
|
$fields = FieldList::create( |
139
|
|
|
TextField::create( |
140
|
|
|
'FirstName', |
141
|
|
|
_t('UserController.ACCEPTFORM_FIRSTNAME', 'First name:'), |
|
|
|
|
142
|
|
|
$firstName |
143
|
|
|
), |
144
|
|
|
TextField::create('Surname', _t('UserController.ACCEPTFORM_SURNAME', 'Surname:')), |
145
|
|
|
ConfirmedPasswordField::create('Password'), |
|
|
|
|
146
|
|
|
HiddenField::create('HashID')->setValue($hash) |
|
|
|
|
147
|
|
|
); |
148
|
|
|
$actions = FieldList::create( |
149
|
|
|
FormAction::create('saveInvite', _t('UserController.ACCEPTFORM_REGISTER', 'Register')) |
150
|
|
|
); |
151
|
|
|
$requiredFields = RequiredFields::create('FirstName', 'Surname'); |
152
|
|
|
$form = new Form($this, 'AcceptForm', $fields, $actions, $requiredFields); |
153
|
|
|
Session::set('UserInvitation.accepted', true); |
154
|
|
|
$this->extend('updateAcceptForm', $form); |
155
|
|
|
return $form; |
156
|
|
|
} |
157
|
|
|
|
158
|
|
|
/** |
159
|
|
|
* @param $data |
160
|
|
|
* @param Form $form |
161
|
|
|
* @return bool|SS_HTTPResponse |
162
|
|
|
*/ |
163
|
|
|
public function saveInvite($data, Form $form) |
164
|
|
|
{ |
165
|
|
|
if (!$invite = UserInvitation::get()->filter('TempHash', $data['HashID'])->first()) { |
166
|
|
|
return $this->notFoundError(); |
167
|
|
|
} |
168
|
|
|
if ($form->validate()) { |
169
|
|
|
$member = Member::create(array('Email' => $invite->Email)); |
170
|
|
|
$form->saveInto($member); |
171
|
|
|
try { |
172
|
|
|
if ($member->validate()) { |
173
|
|
|
$member->write(); |
174
|
|
|
// Add user group info |
175
|
|
|
$groups = explode(',', $invite->Groups); |
176
|
|
|
foreach ($groups as $groupCode) { |
177
|
|
|
$member->addToGroupByCode($groupCode); |
178
|
|
|
} |
179
|
|
|
} |
180
|
|
|
} catch (ValidationException $e) { |
181
|
|
|
$form->sessionMessage( |
182
|
|
|
$e->getMessage(), |
183
|
|
|
'bad' |
184
|
|
|
); |
185
|
|
|
return $this->redirectBack(); |
186
|
|
|
} |
187
|
|
|
// Delete invitation |
188
|
|
|
$invite->delete(); |
189
|
|
|
return $this->redirect($this->Link('success')); |
190
|
|
|
} else { |
191
|
|
|
$form->sessionMessage( |
192
|
|
|
Convert::array2json($form->getValidator()->getErrors()), |
|
|
|
|
193
|
|
|
'bad' |
194
|
|
|
); |
195
|
|
|
return $this->redirectBack(); |
196
|
|
|
} |
197
|
|
|
} |
198
|
|
|
|
199
|
|
|
public function success() |
200
|
|
|
{ |
201
|
|
|
return $this->renderWith( |
202
|
|
|
array('UserController_success', 'Page'), |
203
|
|
|
array('BaseURL' => Director::absoluteBaseURL()) |
204
|
|
|
); |
205
|
|
|
} |
206
|
|
|
|
207
|
|
|
public function expired() |
208
|
|
|
{ |
209
|
|
|
return $this->renderWith(array('UserController_expired', 'Page')); |
210
|
|
|
} |
211
|
|
|
|
212
|
|
|
public function notfound() |
213
|
|
|
{ |
214
|
|
|
return $this->renderWith(array('UserController_notfound', 'Page')); |
215
|
|
|
} |
216
|
|
|
|
217
|
|
|
private function forbiddenError() |
218
|
|
|
{ |
219
|
|
|
return $this->httpError(403, _t('UserController.403_NOTICE', 'You must be logged in to access this page.')); |
|
|
|
|
220
|
|
|
} |
221
|
|
|
|
222
|
|
|
private function notFoundError() |
223
|
|
|
{ |
224
|
|
|
return $this->redirect($this->Link('notfound')); |
225
|
|
|
} |
226
|
|
|
|
227
|
|
|
/** |
228
|
|
|
* Ensure that links for this controller use the customised route. |
229
|
|
|
* Searches through the rules set up for the class and returns the first route. |
230
|
|
|
* |
231
|
|
|
* @param string $action |
232
|
|
|
* @return string |
233
|
|
|
*/ |
234
|
|
|
public function Link($action = null) |
235
|
|
|
{ |
236
|
|
|
if ($url = array_search(get_called_class(), (array)Config::inst()->get('Director', 'rules'))) { |
|
|
|
|
237
|
|
|
// Check for slashes and drop them |
238
|
|
|
if ($indexOf = stripos($url, '/')) { |
239
|
|
|
$url = substr($url, 0, $indexOf); |
240
|
|
|
} |
241
|
|
|
return $this->join_links($url, $action); |
242
|
|
|
} |
243
|
|
|
} |
244
|
|
|
} |
245
|
|
|
|
The issue could also be caused by a filter entry in the build configuration. If the path has been excluded in your configuration, e.g.
excluded_paths: ["lib/*"]
, you can move it to the dependency path list as follows:For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths