Coveralls logob
Coveralls logo
  • Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

burzum / cakephp-user-tools / 318

7 Sep 2017 - 7:12 coverage: 37.811% (-0.6%) from 38.365%
318

Pull #46

travis-ci

9181eb84f9c35729a3bad740fb7f9d93?size=18&default=identiconweb-flow
Improving the code and doc blocks
Pull Request #46: Updating the plugin to CakePHP 3.4 changes

43 of 114 new or added lines in 7 files covered. (37.72%)

21 existing lines in 4 files now uncovered.

304 of 804 relevant lines covered (37.81%)

2.15 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

58.23
/src/Model/UserValidationTrait.php
1
<?php
2
namespace Burzum\UserTools\Model;
3

4
use Cake\Core\Configure;
5
use Cake\Validation\Validator;
6
use RuntimeException;
7

8
trait UserValidationTrait {
9

10
        /**
11
         * Validates the password reset.
12
         *
13
         * Override it as needed to change the rules for only that field.
14
         *
15
         * @param \Cake\Validation\Validator $validator Validator
16
         * @return \Cake\Validation\Validator
17
         */
18
        public function validationPasswordReset(Validator $validator) {
19
                return $this->validateOldPassword($validator)
!
20
                        ->validatePassword($validator)
!
21
                        ->validateConfirmPassword($validator);
!
22
        }
23

24
        /**
25
         * Validates the username field.
26
         *
27
         * Override it as needed to change the rules for only that field.
28
         *
29
         * @param \Cake\Validation\Validator $validator Validator
30
         * @return \Cake\Validation\Validator
31
         */
32
        public function validationUserName(Validator $validator) {
33
                $validator->setProvider('userTable', $this->_table);
21×
34

35
                $validator->add($this->_field('username'), [
21×
36
                        'notBlank' => [
21×
37
                                'rule' => 'notBlank',
21×
38
                                'message' => __d('user_tools', 'An username is required.')
21×
39
                        ],
40
                        'length' => [
41
                                'rule' => ['lengthBetween', 3, 32],
42
                                'message' => __d('user_tools', 'The username must be between 3 and 32 characters.')
21×
43
                        ],
44
                        'unique' => [
45
                                'rule' => ['validateUnique', ['scope' => 'username']],
46
                                'provider' => 'userTable',
21×
47
                                'message' => __d('user_tools', 'The username is already in use.')
21×
48
                        ],
49
                        'alphaNumeric' => [
50
                                'rule' => 'alphaNumeric',
21×
51
                                'message' => __d('user_tools', 'The username must be alpha numeric.')
21×
52
                        ]
53
                ]);
54

55
                return $validator;
21×
56
        }
57

58
        /**
59
         * Validates the email field.
60
         *
61
         * Override it as needed to change the rules for only that field.
62
         *
63
         * @param \Cake\Validation\Validator $validator Validator
64
         * @return \Cake\Validation\Validator
65
         */
66
        public function validationEmail(Validator $validator) {
67
                $validator->setProvider('userTable', $this->_table);
21×
68

69
                $validator->add($this->_field('email'), [
21×
70
                        'notBlank' => [
71
                                'rule' => 'notBlank',
21×
72
                                'message' => __d('user_tools', 'An email is required.')
21×
73
                        ],
74
                        'unique' => [
75
                                'rule' => ['validateUnique', [
21×
76
                                        'scope' => $this->_field('email')
21×
77
                                ]],
78
                                'provider' => 'table',
21×
79
                                'message' => __d('user_tools', 'The email is already in use.')
21×
80
                        ],
81
                        'validEmail' => [
82
                                'rule' => 'email',
21×
83
                                'message' => __d('user_tools', 'Must be a valid email address.')
21×
84
                        ]
85
                ]);
86

87
                return $validator;
21×
88
        }
89

90
        /**
91
         * Validates the password field.
92
         *
93
         * Override it as needed to change the rules for only that field.
94
         *
95
         * @param \Cake\Validation\Validator $validator Validator
96
         * @return \Cake\Validation\Validator
97
         */
98
        public function validationPassword(Validator $validator) {
99
                $validator->setProvider('userTable', $this->_table);
21×
100

101
                $validator->add($this->_field('password'), [
21×
102
                        'notBlank' => [
21×
103
                                'rule' => 'notBlank',
21×
104
                                'message' => __d('user_tools', 'A password is required.')
21×
105
                        ],
106
                        'minLength' => [
107
                                'rule' => ['minLength', $this->_config['passwordMinLength']],
21×
108
                                'message' => __d('user_tools', 'The password must have at least 6 characters.')
21×
109
                        ],
110
                        'confirmPassword' => [
111
                                'rule' => ['compareFields', 'confirm_password'],
112
                                'message' => __d('user_tools', 'The passwords don\'t match!'),
21×
113
                                'provider' => 'userTable',
21×
114
                        ]
115
                ]);
116

117
                return $validator;
21×
118
        }
119

120
        /**
121
         * Validates the confirm_password field.
122
         *
123
         * Override it as needed to change the rules for only that field.
124
         *
125
         * @param \Cake\Validation\Validator $validator Validator
126
         * @return \Cake\Validation\Validator
127
         */
128
        public function validationConfirmPassword(Validator $validator) {
129
                $validator->setProvider('userBehavior', $this);
21×
130

131
                $validator->add($this->_field('passwordCheck'), [
21×
132
                        'notBlank' => [
21×
133
                                'rule' => 'notBlank',
21×
134
                                'message' => __d('user_tools', 'A password is required.')
21×
135
                        ],
136
                        'minLength' => [
137
                                'rule' => ['minLength', $this->_config['passwordMinLength']],
21×
138
                                'message' => __d('user_tools', 'The password must have at least 6 characters.')
21×
139
                        ],
140
                        'confirmPassword' => [
141
                                'rule' => ['compareFields', 'password'],
142
                                'message' => __d('user_tools', 'The passwords don\'t match!'),
21×
143
                                'provider' => 'userBehavior',
21×
144
                        ]
145
                ]);
146

147
                return $validator;
21×
148
        }
149

150
        /**
151
         * Validation rules for the password reset request.
152
         *
153
         * @param \Cake\Validation\Validator $validator Validator
154
         * @return \Cake\Validation\Validator
155
         * @see \Burzum\UserTools\Controller\Component\UserToolComponent::requestPassword()
156
         */
157
        public function validationRequestPassword(Validator $validator) {
158
                $validator = $this->_table->validationDefault($validator);
!
159
                $validator->remove($this->_field('email'), 'unique');
!
160

UNCOV
161
                return $validator;
!
162
        }
163

164
        /**
165
         * Configures the validator with rules for the password change
166
         *
167
         * @param \Cake\Validation\Validator $validator Validator
168
         * @return \Cake\Validation\Validator
169
         */
170
        public function validationChangePassword($validator) {
171
                $validator->provider('userBehavior', $this);
!
172
                $validator = $this->validationPassword($validator);
!
173
                $validator = $this->validationConfirmPassword($validator);
!
174
                $validator = $this->validationOldPassword($validator);
!
175

UNCOV
176
                return $validator;
!
177
        }
178

179
        /**
180
         * Configures the validator with rules to check the old password
181
         *
182
         * @param \Cake\Validation\Validator $validator Validator
183
         * @return \Cake\Validation\Validator
184
         */
185
        protected function validationOldPassword($validator) {
186
                $validator->provider('userBehavior', $this);
!
187

188
                $validator->provider('userTable', $this->_table);
!
189
                $validator->add('old_password', 'notBlank', [
!
190
                        'rule' => 'notBlank',
!
191
                        'message' => __d('user_tools', 'Enter your old password.')
!
192
                ]);
193
                $validator->add('old_password', 'oldPassword', [
!
194
                        'rule' => ['validateOldPassword', 'password'],
!
195
                        'provider' => 'userBehavior',
!
196
                        'message' => __d('user_tools', 'Wrong password, please try again.')
!
197
                ]);
198

UNCOV
199
                return $validator;
!
200
        }
201

202
        /**
203
         * Validation method for the old password.
204
         *
205
         * This method will hash the old password and compare it to the stored hash
206
         * in the database. You don't have to hash it manually before validating.
207
         *
208
         * @param mixed $value Value
209
         * @param string $field Field
210
         * @param mixed $context Context
211
         * @return bool
212
         */
213
        public function validateOldPassword($value, $field, $context) {
214
                if (Configure::read('debug') > 0 && empty($context['data'][$this->_table->primaryKey()])) {
!
215
                        throw new RuntimeException('The user id is required as well to validate the old password!');
!
216
                }
217

218
                $result = $this->_table->find()
!
219
                        ->select([
!
220
                                $this->_table->aliasField($field)
!
221
                        ])
222
                        ->where([
!
223
                                $this->_table->primaryKey() => $context['data'][$this->_table->primaryKey()],
!
224
                        ])
225
                        ->first();
!
226

227
                if (!$result) {
!
228
                        return false;
!
229
                }
230

231
                return $this->getPasswordHasher()->check($value, $result->get($field));
!
232
        }
233

234
        /**
235
         * Compares the value of two fields.
236
         *
237
         * @param mixed $value Value
238
         * @param string $field Field
239
         * @param Entity $context Context
240
         * @return bool
241
         */
242
        public function compareFields($value, $field, $context) {
243
                if (!isset($context['data'][$field])) {
3×
244
                        return true;
!
245
                }
246
                if ($value === $context['data'][$field]) {
3×
247
                        return true;
3×
248
                }
249

250
                return false;
1×
251
        }
252
}
Troubleshooting · Open an Issue · Sales · Support · ENTERPRISE · CAREERS · STATUS
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2023 Coveralls, Inc