Completed
Push — master ( fce117...68ea64 )
by Franco
12s
created

UserController::success()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 0
dl 0
loc 5
rs 9.4285
c 0
b 0
f 0
1
<?php
2
3
class UserController extends Controller implements PermissionProvider
0 ignored issues
show
Bug introduced by
The type Controller was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Bug introduced by
The type PermissionProvider was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
4
{
5
6
    private static $allowed_actions = array(
0 ignored issues
show
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
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'),
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

20
                'name' => /** @scrutinizer ignore-call */ _t('UserController.ACCESS_PERMISSIONS', 'Allow user invitations'),
Loading history...
21
                'category' => _t('UserController.CMS_ACCESS_CATEGORY', 'User Invitations')
22
            )
23
        );
24
    }
25
26
27
    public function index()
28
    {
29
        if (!Member::currentUserID()) {
0 ignored issues
show
Bug introduced by
The type Member was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
30
            return $this->redirect('/Security/login');
31
        } else if (!Permission::check('ACCESS_USER_INVITATIONS')){
0 ignored issues
show
Bug introduced by
The type Permission was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
32
                return Security::permissionFailure();
0 ignored issues
show
Bug introduced by
The type Security was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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(
0 ignored issues
show
Bug introduced by
The type FieldList was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
41
            TextField::create('FirstName', _t('UserController.INVITE_FIRSTNAME', 'First name:')),
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

41
            TextField::create('FirstName', /** @scrutinizer ignore-call */ _t('UserController.INVITE_FIRSTNAME', 'First name:')),
Loading history...
Bug introduced by
The type TextField was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
42
            EmailField::create('Email', _t('UserController.INVITE_EMAIL', 'Invite email:')),
0 ignored issues
show
Bug introduced by
The type EmailField was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
43
            ListboxField::create('Groups', _t('UserController.INVITE_GROUP', 'Add to group'), $groups)
0 ignored issues
show
Bug introduced by
The type ListboxField was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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'))
0 ignored issues
show
Bug introduced by
The type FormAction was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
49
        );
50
        $requiredFields = RequiredFields::create('FirstName', 'Email');
0 ignored issues
show
Bug introduced by
The type RequiredFields was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
51
52
        if (UserInvitation::config()->get('force_require_group')) {
53
            $requiredFields->addRequiredField('Groups');
54
        }
55
56
        $form = new Form($this, 'InvitationForm', $fields, $actions, $requiredFields);
0 ignored issues
show
Bug introduced by
The type Form was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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
0 ignored issues
show
Bug introduced by
The type SS_HTTPResponse was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
66
     */
67
    public function sendInvite($data, Form $form)
68
    {
69
        if (!Permission::check('ACCESS_USER_INVITATIONS')){
70
            $form->sessionMessage(
71
                _t(
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

71
                /** @scrutinizer ignore-call */ _t(
Loading history...
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) {
0 ignored issues
show
Bug introduced by
The type ValidationException was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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:'),
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

141
                /** @scrutinizer ignore-call */ _t('UserController.ACCEPTFORM_FIRSTNAME', 'First name:'),
Loading history...
142
                $firstName
143
            ),
144
            TextField::create('Surname', _t('UserController.ACCEPTFORM_SURNAME', 'Surname:')),
145
            ConfirmedPasswordField::create('Password'),
0 ignored issues
show
Bug introduced by
The type ConfirmedPasswordField was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
146
            HiddenField::create('HashID')->setValue($hash)
0 ignored issues
show
Bug introduced by
The type HiddenField was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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()),
0 ignored issues
show
Bug introduced by
The type Convert was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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.'));
0 ignored issues
show
Bug introduced by
The function _t was not found. Maybe you did not declare it correctly or list all dependencies? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

219
        return $this->httpError(403, /** @scrutinizer ignore-call */ _t('UserController.403_NOTICE', 'You must be logged in to access this page.'));
Loading history...
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'))) {
0 ignored issues
show
Bug introduced by
The type Config was not found. Maybe you did not declare it correctly or list all dependencies?

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:

filter:
    dependency_paths: ["lib/*"]

For further information see https://scrutinizer-ci.com/docs/tools/php/php-scrutinizer/#list-dependency-paths

Loading history...
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