Completed
Push — master ( f9bacd...4c4c7f )
by Cheren
01:57
created

UsersTable::findAuth()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 2
1
<?php
2
/**
3
 * CakeCMS Community
4
 *
5
 * This file is part of the of the simple cms based on CakePHP 3.
6
 * For the full copyright and license information, please view the LICENSE
7
 * file that was distributed with this source code.
8
 *
9
 * @package     Community
10
 * @license     MIT
11
 * @copyright   MIT License http://www.opensource.org/licenses/mit-license.php
12
 * @link        https://github.com/CakeCMS/Community".
13
 * @author      Sergey Kalistratov <[email protected]>
14
 */
15
16
namespace Community\Model\Table;
17
18
use Cake\ORM\Query;
19
use Core\ORM\Table;
20
use Core\Event\EventManager;
21
use Cake\Validation\Validator;
22
use Community\Model\Entity\User;
23
use Cake\Datasource\EntityInterface;
24
25
/**
26
 * Class UsersTable
27
 *
28
 * @method      filterParams(array $query = [])
29
 * @method      User get($primaryKey, $options = [])
30
 * @property    GroupsTable $Groups
31
 *
32
 * @package     Community\Model\Table
33
 */
34
class UsersTable extends Table
35
{
36
37
    /**
38
     * Initialize a table instance. Called after the constructor.
39
     *
40
     * @param   array $config Configuration options passed to the constructor.
41
     * @return  void
42
     *
43
     * @throws  \RuntimeException
44
     */
45
    public function initialize(array $config)
46
    {
47
        parent::initialize($config);
48
49
        $this
50
            ->setPrimaryKey('id')
51
            ->setTable(CMS_TABLE_USERS)
52
            ->setDisplayField('name');
53
54
        $this->addBehavior('Timestamp');
55
        $this->addBehavior('Search.Search');
56
57
        $this->addAssociations([
58
            'belongsTo' => [
59
                'Groups' => [
60
                    'foreignKey' => 'group_id',
61
                    'className'  => 'Community.Groups'
62
                ]
63
            ]
64
        ]);
65
    }
66
67
    /**
68
     * Persists an entity based on the fields that are marked as dirty and
69
     * returns the same entity after a successful save or false in case
70
     * of any error.
71
     *
72
     * @param   \Cake\Datasource\EntityInterface $entity the entity to be saved
73
     * @param   array|\ArrayAccess $options The options to use when saving.
74
     * @return  \Cake\Datasource\EntityInterface|false
75
     */
76
    public function save(EntityInterface $entity, $options = [])
77
    {
78
        EventManager::trigger('Model.User.beforeSave', $this, [
79
            'user' => $entity
80
        ]);
81
82
        $success = parent::save($entity, $options);
0 ignored issues
show
Bug introduced by
It seems like $options defined by parameter $options on line 76 can also be of type object<ArrayAccess>; however, Cake\ORM\Table::save() does only seem to accept array, maybe add an additional type check?

This check looks at variables that have been passed in as parameters and are passed out again to other methods.

If the outgoing method call has stricter type requirements than the method itself, an issue is raised.

An additional type check may prevent trouble.

Loading history...
83
84
        EventManager::trigger('Model.User.afterSave', $this, [
85
            'user'    => $entity,
86
            'success' => $success
87
        ]);
88
89
        return $success;
90
    }
91
92
    /**
93
     * Default validation rules.
94
     *
95
     * @param   Validator $validator The validator that can be modified to add some rules to it.
96
     * @return  Validator
97
     *
98
     * @throws  \Aura\Intl\Exception
99
     */
100
    public function validationDefault(Validator $validator)
101
    {
102
        $validator
103
            ->requirePresence('login', 'create')
104
            ->notEmpty('login', __d('community', 'Login could not be empty.'))
105
            ->add('login', 'unique', [
106
                'provider' => 'table',
107
                'rule'     => 'validateUnique',
108
                'message'  => __d('community', 'User with this login already exists.')
109
            ])
110
            ->add('login', 'length', [
111
                'rule'    => ['minLength', MIN_LENGTH_LOGIN],
112
                'message' => __d('community', 'The minimum login length must be {0} characters', MIN_LENGTH_LOGIN)
113
            ]);
114
115
        $validator
116
            ->requirePresence('slug', 'create')
117
            ->notEmpty('slug', __d('community', 'Alias could not be empty.'))
118
            ->add('slug', 'unique', [
119
                'provider' => 'table',
120
                'rule'     => 'validateUnique',
121
                'message'  => __d('community', 'User with this alias already exists.')
122
            ])
123
            ->add('slug', 'length', [
124
                'rule'    => ['minLength', MIN_LENGTH_LOGIN],
125
                'message' => __d('community', 'The minimum alias length must be {0} characters', MIN_LENGTH_LOGIN)
126
            ]);
127
128
        $validator
129
            ->requirePresence('group_id', 'create')
130
            ->notEmpty('group_id', __d('community', 'Please, choose user group.'))
131
            ->notEmpty('name', __d('community', 'Please, enter you full name.'));
132
133
        $validator
134
            ->notEmpty('email', __d('community', 'Please, enter you email.'))
135
            ->add('email', 'unique', [
136
                'provider' => 'table',
137
                'rule'     => 'validateUnique',
138
                'message'  => __d('community', 'User with this email already exists.')
139
            ])
140
            ->add('email', 'valid', [
141
                'rule'    => 'email',
142
                'message' => __d('community', 'Please enter valid email.')
143
            ]);
144
145
        $validator
146
            ->notEmpty('password', __d('community', 'Please, enter you password.'))
147
            ->add('password', 'minLength', [
148
                'rule'    => ['minLength', MIN_LENGTH_PASS],
149
                'message' => __d('community', 'The minimum password length is {0}', MIN_LENGTH_PASS)
150
            ]);
151
152
        $validator
153
            ->notEmpty('password_confirm', __d('community', 'Please, confirm you password.'))
154
            ->add('password_confirm', 'no-misspelling', [
155
                'rule'    => ['compareWith', 'password'],
156
                'message' => __d('community', 'Passwords are not equal')
157
            ]);
158
159
        return $validator;
160
    }
161
162
    /**
163
     * Find auth user.
164
     *
165
     * @param   Query $query
166
     * @param   array $options
167
     *
168
     * @return  \Cake\ORM\Query
169
     */
170
    public function findAuth(Query $query, array $options)
0 ignored issues
show
Unused Code introduced by
The parameter $options is not used and could be removed.

This check looks from parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
171
    {
172
        return $query->where(['Users.status' => 1]);
173
    }
174
}
175