Passed
Pull Request — master (#2)
by
unknown
01:52
created

UserController::notFoundError()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
rs 10
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 (!$form->validate()) {
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.SENT_INVITATION_VALIDATION_FAILED',
73
                    'At least one error occured while trying to save your invite: {error}',
74
                    array('error' => $form->getValidator()->getErrors()[0]['fieldName'])
75
                ),
76
                'bad'
77
            );
78
            return $this->redirectBack();
79
        }
80
81
        $invite = UserInvitation::create();
82
        if (!Permission::check('ACCESS_USER_INVITATIONS')){
83
            $form->saveInto($invite);
84
            try {
85
                $invite->write();
86
            } 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...
87
                $form->sessionMessage(
88
                    $e->getMessage(),
89
                    'bad'
90
                );
91
                return $this->redirectBack();
92
            }
93
            $invite->sendInvitation();
94
95
            $form->sessionMessage(
96
                _t(
97
                    'UserController.SENT_INVITATION',
98
                    'An invitation was sent to {email}.',
99
                    array('email' => $data['Email'])
100
                ),
101
                'good'
102
            );
103
            return $this->redirectBack();
104
        } else {
105
            $form->sessionMessage(
106
                "You don't have permission to create user invitations",
107
                'bad'
108
            );
109
            return $this->redirectBack();
110
        }
111
    }
112
113
114
    public function accept()
115
    {
116
        if (!$hash = $this->getRequest()->param('ID')) {
117
            return $this->forbiddenError();
118
        }
119
        if ($invite = UserInvitation::get()->filter('TempHash', $hash)->first()) {
120
            if ($invite->isExpired()) {
121
                return $this->redirect($this->Link('expired'));
122
            }
123
        } else {
124
            return $this->redirect($this->Link('notfound'));
125
        }
126
        return $this->renderWith(array('UserController_accept', 'Page'), array('Invite' => $invite));
127
    }
128
129
    public function AcceptForm()
130
    {
131
        $hash = $this->getRequest()->param('ID');
132
        $invite = UserInvitation::get()->filter('TempHash', $hash)->first();
133
        $firstName = ($invite) ? $invite->FirstName : '';
134
135
        $fields = FieldList::create(
136
            TextField::create(
137
                'FirstName',
138
                _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

138
                /** @scrutinizer ignore-call */ _t('UserController.ACCEPTFORM_FIRSTNAME', 'First name:'),
Loading history...
139
                $firstName
140
            ),
141
            TextField::create('Surname', _t('UserController.ACCEPTFORM_SURNAME', 'Surname:')),
142
            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...
143
            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...
144
        );
145
        $actions = FieldList::create(
146
            FormAction::create('saveInvite', _t('UserController.ACCEPTFORM_REGISTER', 'Register'))
147
        );
148
        $requiredFields = RequiredFields::create('FirstName', 'Surname');
149
        $form = new  Form($this, 'AcceptForm', $fields, $actions, $requiredFields);
150
        Session::set('UserInvitation.accepted', true);
151
        $this->extend('updateAcceptForm', $form);
152
        return $form;
153
    }
154
155
    /**
156
     * @param $data
157
     * @param Form $form
158
     * @return bool|SS_HTTPResponse
159
     */
160
    public function saveInvite($data, Form $form)
161
    {
162
        if (!$invite = UserInvitation::get()->filter('TempHash', $data['HashID'])->first()) {
163
            return $this->notFoundError();
164
        }
165
        if ($form->validate()) {
166
            $member = Member::create(array('Email' => $invite->Email));
167
            $form->saveInto($member);
168
            try {
169
                if ($member->validate()) {
170
                    $member->write();
171
                    // Add user group info
172
                    $groups = explode(',', $invite->Groups);
173
                    foreach ($groups as $groupCode) {
174
                        $member->addToGroupByCode($groupCode);
175
                    }
176
                }
177
            } catch (ValidationException $e) {
178
                $form->sessionMessage(
179
                    $e->getMessage(),
180
                    'bad'
181
                );
182
                return $this->redirectBack();
183
            }
184
            // Delete invitation
185
            $invite->delete();
186
            return $this->redirect($this->Link('success'));
187
        } else {
188
            $form->sessionMessage(
189
                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...
190
                'bad'
191
            );
192
            return $this->redirectBack();
193
        }
194
    }
195
196
    public function success()
197
    {
198
        return $this->renderWith(
199
            array('UserController_success', 'Page'),
200
            array('BaseURL' => Director::absoluteBaseURL())
201
        );
202
    }
203
204
    public function expired()
205
    {
206
        return $this->renderWith(array('UserController_expired', 'Page'));
207
    }
208
209
    public function notfound()
210
    {
211
        return $this->renderWith(array('UserController_notfound', 'Page'));
212
    }
213
214
    private function forbiddenError()
215
    {
216
        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

216
        return $this->httpError(403, /** @scrutinizer ignore-call */ _t('UserController.403_NOTICE', 'You must be logged in to access this page.'));
Loading history...
217
    }
218
219
    private function notFoundError()
220
    {
221
        return $this->redirect($this->Link('notfound'));
222
    }
223
224
    /**
225
     * Ensure that links for this controller use the customised route.
226
     * Searches through the rules set up for the class and returns the first route.
227
     *
228
     * @param  string $action
229
     * @return string
230
     */
231
    public function Link($action = null)
232
    {
233
        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...
234
            // Check for slashes and drop them
235
            if ($indexOf = stripos($url, '/')) {
236
                $url = substr($url, 0, $indexOf);
237
            }
238
            return $this->join_links($url, $action);
239
        }
240
    }
241
}
242