UsersTable::validationSettings()   B
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 30
Code Lines 21

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 30
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 21
nc 1
nop 1
1
<?php
2
namespace App\Model\Table;
3
4
use Cake\ORM\Query;
5
use Cake\ORM\Table;
6
use Cake\Validation\Validator;
7
8
class UsersTable extends Table
9
{
10
11
    /**
12
     * Initialize method.
13
     *
14
     * @param array $config The configuration for the Table.
15
     *
16
     * @return void
17
     */
18
    public function initialize(array $config)
19
    {
20
        $this->table('users');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::table() has been deprecated with message: 3.4.0 Use setTable()/getTable() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
21
        $this->displayField('username');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::displayField() has been deprecated with message: 3.4.0 Use setDisplayField()/getDisplayField() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
22
        $this->primaryKey('id');
0 ignored issues
show
Deprecated Code introduced by
The method Cake\ORM\Table::primaryKey() has been deprecated with message: 3.4.0 Use setPrimaryKey()/getPrimaryKey() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
23
24
        $this->addBehavior('Timestamp');
25
        $this->addBehavior('Xety/Cake3Upload.Upload', [
26
            'fields' => [
27
                'avatar' => [
28
                    'path' => 'upload/avatar/:id/:md5',
29
                    'overwrite' => true,
30
                    'prefix' => '../',
31
                    'defaultFile' => 'avatar.png'
32
                ]
33
            ]
34
        ]);
35
        $this->addBehavior('Acl.Acl', [
36
            'type' => 'requester',
37
            'enabled' => false
38
        ]);
39
40
        $this->hasMany('BlogArticles', [
41
            'foreignKey' => 'user_id',
42
            'dependent' => true
43
        ]);
44
        $this->hasMany('BlogArticlesComments', [
45
            'foreignKey' => 'user_id',
46
            'dependent' => true
47
        ]);
48
        $this->hasMany('BlogArticlesLikes', [
49
            'foreignKey' => 'user_id',
50
            'dependent' => true
51
        ]);
52
        $this->hasMany('BadgesUsers', [
53
            'foreignKey' => 'user_id',
54
            'dependent' => true
55
        ]);
56
        $this->belongsTo('Groups', [
57
            'foreignKey' => 'group_id'
58
        ]);
59
        $this->hasMany('Notifications', [
60
            'foreignKey' => 'user_id',
61
            'dependent' => true,
62
            'cascadeCallbacks' => true
63
        ]);
64
        $this->hasMany('Conversations', [
65
            'foreignKey' => 'user_id',
66
            'dependent' => true
67
        ]);
68
        $this->hasMany('ConversationsMessages', [
69
            'foreignKey' => 'user_id',
70
            'dependent' => true
71
        ]);
72
        $this->hasMany('UsersLogs', [
73
            'foreignKey' => 'user_id',
74
            'dependent' => true
75
        ]);
76
        $this->hasOne('UsersTwoFactorAuth', [
77
            'foreignKey' => 'user_id',
78
            'dependent' => true
79
        ]);
80
        $this->hasMany('Polls', [
81
            'foreignKey' => 'user_id',
82
            'dependent' => true
83
        ]);
84
    }
85
86
    /**
87
     * Create validation rules.
88
     *
89
     * @param \Cake\Validation\Validator $validator The Validator instance.
90
     *
91
     * @return \Cake\Validation\Validator
92
     */
93
    public function validationCreate(Validator $validator)
94
    {
95
        $validator
96
            ->notEmpty('username', __("You must set an username"))
97
            ->add('username', [
98
                'unique' => [
99
                    'rule' => 'validateUnique',
100
                    'provider' => 'table',
101
                    'message' => __("This username is already used.")
102
                ],
103
                'alphanumeric' => [
104
                    'rule' => ['custom', '#^[A-Za-z0-9]+$#'],
105
                    'message' => __("Only alphanumeric characters.")
106
                ],
107
                'lengthBetween' => [
108
                    'rule' => ['lengthBetween', 4, 20],
109
                    'message' => __("Your username must be between {0} and {1} characters.", 4, 20)
110
                ]
111
            ])
112
            ->notEmpty('password', __("You must specify your password."))
113
            ->notEmpty('password_confirm', __("You must specify your password (confirmation)."))
114
            ->add('password_confirm', [
115
                'lengthBetween' => [
116
                    'rule' => ['lengthBetween', 8, 20],
117
                    'message' => __("Your password (confirmation) must be between {0} and {1} characters.", 8, 20)
118
                ],
119
                'equalToPassword' => [
120
                    'rule' => function ($value, $context) {
121
                        return $value === $context['data']['password'];
122
                    },
123
                    'message' => __("Your password confirm must match with your password.")
124
                ]
125
            ])
126
            ->notEmpty('email')
127
            ->add('email', [
128
                'unique' => [
129
                    'rule' => 'validateUnique',
130
                    'provider' => 'table',
131
                    'message' => __("This E-mail is already used.")
132
                ],
133
                'email' => [
134
                    'rule' => 'email',
135
                    'message' => __("You must specify a valid E-mail address.")
136
                ]
137
            ]);
138
139
        return $validator;
140
    }
141
142
    /**
143
     * Account validation rules.
144
     *
145
     * @param \Cake\Validation\Validator $validator The Validator instance.
146
     *
147
     * @return \Cake\Validation\Validator
148
     */
149
    public function validationAccount(Validator $validator)
150
    {
151
        return $validator
0 ignored issues
show
Deprecated Code introduced by
The method Cake\Validation\Validator::provider() has been deprecated with message: 3.4.0 Use setProvider()/getProvider() instead.

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
152
            ->provider('upload', 'App\Model\Validation\UploadValidator')
153
            ->provider('purifier', 'App\Model\Validation\PurifierValidator')
154
            ->allowEmpty('first_name')
155
            ->add('first_name', 'maxLength', [
156
                'rule' => ['maxLength', 100],
157
                'message' => __("Your First Name can not contain more than {0} characters.", 100)
158
            ])
159
            ->allowEmpty('last_name')
160
            ->add('last_name', 'maxLength', [
161
                'rule' => ['maxLength', 100],
162
                'message' => __("Your Last Name can not contain more than {0} characters.", 100)
163
            ])
164
            ->allowEmpty('avatar_file')
165
            ->add('avatar_file', [
166
                'mimeType' => [
167
                    'rule' => ['mimeType', ['image/jpeg', 'image/png']],
168
                    'message' => __("The mimeType is not allowed."),
169
                    'on' => function ($context) {
170
                            return !empty($context['data']['avatar_file']['name']);
171
                    }
172
                ],
173
                'fileExtension' => [
174
                    'rule' => ['extension', ['jpg', 'jpeg', 'png']],
175
                    'message' => __("The extensions allowed are {0}.", '.jpg, .jpeg and .png'),
176
                    'on' => function ($context) {
177
                            return !empty($context['data']['avatar_file']['name']);
178
                    }
179
                ],
180
                'fileSize' => [
181
                    'rule' => ['fileSize', '<', '500KB'],
182
                    'message' => __("The file exceeded the max allowed size of {0}", '500KB'),
183
                    'on' => function ($context) {
184
                            return !empty($context['data']['avatar_file']['name']);
185
                    }
186
                ],
187
                'maxDimension' => [
188
                    'rule' => ['maxDimension', 230, 230],
189
                    'provider' => 'upload',
190
                    'message' => __(
191
                        "The file exceeded the max allowed dimension. Max height : {0} Max width : {1}",
192
                        230,
193
                        230
194
                    ),
195
                ]
196
            ])
197
            ->allowEmpty('facebook')
198
            ->add('facebook', 'maxLength', [
199
                'rule' => ['maxLength', 200],
200
                'message' => __("Your Facebook can not contain more than {0} characters.", 200)
201
            ])
202
            ->allowEmpty('twitter')
203
            ->add('twitter', 'maxLength', [
204
                'rule' => ['maxLength', 200],
205
                'message' => __("Your Twitter can not contain more than {0} characters.", 200)
206
            ])
207
            ->allowEmpty('biography')
208
            ->add('biography', [
209
                'purifierMaxLength' => [
210
                    'rule' => ['purifierMaxLength', 3000],
211
                    'provider' => 'purifier',
212
                    'message' => __('Your biography can not contain more than {0} characters.', 3000)
213
                ]
214
            ])
215
            ->allowEmpty('signature')
216
            ->add('signature', [
217
                'purifierMaxLength' => [
218
                    'rule' => ['purifierMaxLength', 300],
219
                    'provider' => 'purifier',
220
                    'message' => __('Your biography can not contain more than {0} characters.', 300)
221
                ]
222
            ]);
223
    }
224
225
    /**
226
     * Settings validation rules.
227
     *
228
     * @param \Cake\Validation\Validator $validator The Validator instance.
229
     *
230
     * @return \Cake\Validation\Validator
231
     */
232
    public function validationSettings(Validator $validator)
233
    {
234
        return $validator
235
            ->notEmpty('email', __("Your E-mail can not be empty."))
236
            ->add('email', [
237
                'email' => [
238
                    'rule' => 'email',
239
                    'message' => __("You must specify a valid E-mail address.")
240
                ],
241
                'unique' => [
242
                    'rule' => 'validateUnique',
243
                    'provider' => 'table',
244
                    'message' => __("This E-mail is already used, please choose another E-mail.")
245
                ],
246
            ])
247
            ->notEmpty('password', __("You must specify your new password."))
248
            ->notEmpty('password_confirm', __("You must specify your password (confirmation)."))
249
            ->add('password_confirm', [
250
                'lengthBetween' => [
251
                    'rule' => ['lengthBetween', 8, 20],
252
                    'message' => __("Your password (confirmation) must be between {0} and {1} characters.", 8, 20)
253
                ],
254
                'equalToPassword' => [
255
                    'rule' => function ($value, $context) {
256
                            return $value === $context['data']['password'];
257
                    },
258
                    'message' => __("Your password confirm must match with your new password")
259
                ]
260
            ]);
261
    }
262
263
    /**
264
     * ResetPassword validation rules.
265
     *
266
     * @param \Cake\Validation\Validator $validator The Validator instance.
267
     *
268
     * @return \Cake\Validation\Validator
269
     */
270
    public function validationResetpassword(Validator $validator)
271
    {
272
        return $validator
273
            ->notEmpty('password', __("You must specify your new password."))
274
            ->notEmpty('password_confirm', __("You must specify your password (confirmation)."))
275
            ->add('password_confirm', [
276
                'lengthBetween' => [
277
                    'rule' => ['lengthBetween', 8, 20],
278
                    'message' => __("Your password (confirmation) must be between {0} and {1} characters.", 8, 20)
279
                ],
280
                'equalToPassword' => [
281
                    'rule' => function ($value, $context) {
282
                            return $value === $context['data']['password'];
283
                    },
284
                    'message' => __("Your password confirm must match with your new password")
285
                ]
286
            ]);
287
    }
288
289
    /**
290
     * Update validation rules. (Administration)
291
     *
292
     * @param \Cake\Validation\Validator $validator The Validator instance.
293
     *
294
     * @return \Cake\Validation\Validator
295
     */
296
    public function validationUpdate(Validator $validator)
297
    {
298
        $validator
299
            ->requirePresence('username', 'update')
300
            ->notEmpty('username', __("You must set an username"))
301
            ->add('username', [
302
                'unique' => [
303
                    'rule' => 'validateUnique',
304
                    'provider' => 'table',
305
                    'message' => __("This username is already used.")
306
                ],
307
                'alphanumeric' => [
308
                    'rule' => ['custom', '#^[A-Za-z0-9]+$#'],
309
                    'message' => __("Only alphanumeric characters.")
310
                ],
311
                'lengthBetween' => [
312
                    'rule' => ['lengthBetween', 4, 20],
313
                    'message' => __("Your username must be between {0} and {1} characters.", 4, 20)
314
                ]
315
            ])
316
            ->requirePresence('email', 'update')
317
            ->notEmpty('email')
318
            ->add('email', [
319
                'unique' => [
320
                    'rule' => 'validateUnique',
321
                    'provider' => 'table',
322
                    'message' => __("This E-mail is already used.")
323
                ],
324
                'email' => [
325
                    'rule' => 'email',
326
                    'message' => __("You must specify a valid E-mail address.")
327
                ]
328
            ]);
329
330
        return $validator;
331
    }
332
333
    /**
334
     * Custom finder for select only the required fields.
335
     *
336
     * @param \Cake\ORM\Query $query The query finder.
337
     *
338
     * @return \Cake\ORM\Query
339
     */
340
    public function findShort(Query $query)
341
    {
342
        return $query->select([
343
            'id',
344
            'first_name',
345
            'last_name',
346
            'username'
347
        ]);
348
    }
349
350
    /**
351
     * Custom finder for select the required fields and avatar.
352
     *
353
     * @param \Cake\ORM\Query $query The query finder.
354
     *
355
     * @return \Cake\ORM\Query
356
     */
357
    public function findMedium(Query $query)
358
    {
359
        return $query->select([
360
            'id',
361
            'first_name',
362
            'last_name',
363
            'username',
364
            'avatar'
365
        ]);
366
    }
367
368
    /**
369
     * Custom finder for select full fields.
370
     *
371
     * @param \Cake\ORM\Query $query The query finder.
372
     *
373
     * @return \Cake\ORM\Query
374
     */
375
    public function findFull(Query $query)
376
    {
377
        return $query->select([
378
            'id',
379
            'first_name',
380
            'last_name',
381
            'username',
382
            'avatar',
383
            'group_id',
384
            'blog_articles_comment_count',
385
            'blog_article_count',
386
            'facebook',
387
            'twitter',
388
            'signature',
389
            'created',
390
            'last_login'
391
        ]);
392
    }
393
}
394