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