Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
5 / 5
CRAP
100.00% covered (success)
100.00%
1 / 1
UsersTable
100.00% covered (success)
100.00%
63 / 63
100.00% covered (success)
100.00%
5 / 5
5
100.00% covered (success)
100.00%
1 / 1
 initialize
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 validationDefault
100.00% covered (success)
100.00%
29 / 29
100.00% covered (success)
100.00%
1 / 1
1
 validationResetPassword
100.00% covered (success)
100.00%
15 / 15
100.00% covered (success)
100.00%
1 / 1
1
 buildRules
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 findAuth
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2declare(strict_types=1);
3
4namespace App\Model\Table;
5
6use App\Model\Behavior\ImageValidationTrait;
7use Cake\Log\LogTrait;
8use Cake\ORM\Query;
9use Cake\ORM\RulesChecker;
10use Cake\ORM\Table;
11use Cake\Validation\Validator;
12
13/**
14 * Users Model
15 *
16 * @property \App\Model\Table\ArticlesTable&\Cake\ORM\Association\HasMany $Articles
17 * @property \App\Model\Table\CommentsTable&\Cake\ORM\Association\HasMany $Comments
18 * @method \App\Model\Entity\User newEmptyEntity()
19 * @method \App\Model\Entity\User newEntity(array $data, array $options = [])
20 * @method \App\Model\Entity\User get(mixed $primaryKey, array|string $finder = 'all', \Psr\SimpleCache\CacheInterface|string|null $cache = null, \Closure|string|null $cacheKey = null, mixed ...$args)
21 * @method \App\Model\Entity\User findOrCreate($search, ?callable $callback = null, array $options = [])
22 * @method \App\Model\Entity\User|false save(\Cake\Datasource\EntityInterface $entity, array $options = [])
23 * @method \Cake\ORM\Query\SelectQuery findByEmail(string $email)
24 * @method \Cake\ORM\Query\SelectQuery findByUsername(string $username)
25 * @mixin \Cake\ORM\Behavior\TimestampBehavior
26 * @mixin \App\Model\Behavior\QueueableImageBehavior
27 */
28class UsersTable extends Table
29{
30    use ImageValidationTrait;
31    use LogTrait;
32    use QueueableJobsTrait;
33
34    /**
35     * Initialize method
36     *
37     * @param array<string, mixed> $config The configuration for the Table.
38     * @return void
39     */
40    public function initialize(array $config): void
41    {
42        parent::initialize($config);
43
44        $this->setTable('users');
45        $this->setDisplayField('username');
46        $this->setPrimaryKey('id');
47
48        $this->addBehavior('QueueableImage', [
49            'folder_path' => 'files/Users/image/',
50            'field' => 'image',
51        ]);
52
53        $this->addBehavior('Timestamp');
54
55        $this->hasMany('Articles', [
56            'foreignKey' => 'user_id',
57        ]);
58
59        $this->hasMany('Comments', [
60            'foreignKey' => 'user_id',
61        ]);
62    }
63
64    /**
65     * Default validation rules.
66     *
67     * @param \Cake\Validation\Validator $validator Validator instance.
68     * @return \Cake\Validation\Validator
69     */
70    public function validationDefault(Validator $validator): Validator
71    {
72        $validator
73            ->scalar('username')
74            ->maxLength('username', 50)
75            ->requirePresence('username', 'create')
76            ->notEmptyString('username');
77
78        $validator
79            ->scalar('password')
80            ->minLength('password', 8, __('Password must be at least 8 characters long'))
81            ->maxLength('password', 255)
82            ->requirePresence('password', 'create')
83            ->allowEmptyString('password', __('Required'))
84            ->notEmptyString('password', null, 'create');
85
86        $validator
87            ->scalar('confirm_password')
88            ->maxLength('confirm_password', 255)
89            ->requirePresence('confirm_password', 'create')
90            ->allowEmptyString('confirm_password', __('Required'))
91            ->notEmptyString('confirm_password', null, 'create')
92            ->sameAs('confirm_password', 'password', 'Passwords do not match');
93
94        $validator
95            ->email('email')
96            ->notEmptyString('email');
97
98        $this->addOptionalImageValidation($validator, 'image', [
99            'messages' => [
100                'mimeType' => 'Please upload only images (jpeg, png, gif).',
101                'fileSize' => 'Image must be less than 10MB.',
102            ],
103        ]);
104
105        return $validator;
106    }
107
108    /**
109     * Validation method for resetting passwords.
110     *
111     * This method defines validation rules for the password reset process.
112     * It ensures that the password meets the minimum length requirement
113     * and that the password confirmation matches the password.
114     *
115     * @param \Cake\Validation\Validator $validator The validator instance to which rules will be added.
116     * @return \Cake\Validation\Validator The modified validator instance with the added rules.
117     */
118    public function validationResetPassword(Validator $validator): Validator
119    {
120        $validator
121            ->scalar('password')
122            ->minLength('password', 8, __('Password must be at least 8 characters long'))
123            ->maxLength('password', 255)
124            ->requirePresence('password', 'create')
125            ->allowEmptyString('password', 'update')
126            ->notEmptyString('password', null, 'create');
127
128        $validator
129            ->scalar('confirm_password')
130            ->maxLength('confirm_password', 255)
131            ->requirePresence('confirm_password', 'create')
132            ->allowEmptyString('confirm_password', 'update')
133            ->notEmptyString('confirm_password', null, 'create')
134            ->sameAs('confirm_password', 'password', 'Passwords do not match');
135
136        return $validator;
137    }
138
139    /**
140     * Returns a rules checker object that will be used for validating
141     * application integrity.
142     *
143     * @param \Cake\ORM\RulesChecker $rules The rules object to be modified.
144     * @return \Cake\ORM\RulesChecker
145     */
146    public function buildRules(RulesChecker $rules): RulesChecker
147    {
148        $rules->add($rules->isUnique(['username']), ['errorField' => 'username']);
149        $rules->add($rules->isUnique(['email']), ['errorField' => 'email']);
150
151        return $rules;
152    }
153
154    /**
155     * Custom finder method to retrieve only enabled records.
156     *
157     * This method modifies the query to filter out any records where the 'active'
158     * field is set to 1, effectively returning only those records that are enabled.
159     *
160     * @param \Cake\ORM\Query $query The query object to modify.
161     * @param array $options An array of options that can be used to customize the query.
162     * @return \Cake\ORM\Query The modified query object with the 'active' condition applied.
163     */
164    public function findAuth(Query $query, array $options): Query
165    {
166        return $query->where(['active' => 1]);
167    }
168}