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

UserController::providePermissions()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 4

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 4
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 6
rs 9.4285
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 (UserInvitation::create()->canCreate(Member::CurrentUser()) == false){
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
        } 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(
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...
40
            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

40
            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...
41
            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...
42
            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...
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'))
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...
48
        );
49
        $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...
50
51
        if (UserInvitation::config()->get('force_require_group')) {
52
            $requiredFields->addRequiredField('Groups');
53
        }
54
55
        $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...
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
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...
65
     */
66
    public function sendInvite($data, Form $form)
67
    {
68
        if (!$form->validate()) {
69
            $form->sessionMessage(
70
                _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

70
                /** @scrutinizer ignore-call */ _t(
Loading history...
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) {
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...
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:'),
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

137
                /** @scrutinizer ignore-call */ _t('UserController.ACCEPTFORM_FIRSTNAME', 'First name:'),
Loading history...
138
                $firstName
139
            ),
140
            TextField::create('Surname', _t('UserController.ACCEPTFORM_SURNAME', 'Surname:')),
141
            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...
142
            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...
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()),
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...
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.'));
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

215
        return $this->httpError(403, /** @scrutinizer ignore-call */ _t('UserController.403_NOTICE', 'You must be logged in to access this page.'));
Loading history...
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'))) {
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...
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