Failed Conditions
Pull Request — master (#321)
by Anton
23:35 queued 08:33
created

application/models/Users/Row.php (2 issues)

Labels
Severity
1
<?php
2
/**
3
 * @copyright Bluz PHP Team
4
 * @link      https://github.com/bluzphp/skeleton
5
 */
6
7
declare(strict_types=1);
8
9
namespace Application\Users;
10
11
use Application\Privileges;
12
use Application\Roles;
13
use Bluz\Auth\AbstractIdentity;
0 ignored issues
show
The type Bluz\Auth\AbstractIdentity 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...
14
use Bluz\Validator\Traits\Validator;
0 ignored issues
show
The type Bluz\Validator\Traits\Validator 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...
15
16
/**
17
 * Row of User
18
 *
19
 * @package  Application\Users
20
 *
21
 * @property integer $id
22
 * @property string $login
23
 * @property string $email
24
 * @property string $created
25
 * @property string $updated
26
 * @property string $status
27
 *
28
 * @OA\Schema(schema="user", title="user", required={"id", "login", "status"})
29
 * @OA\Property(property="id", type="integer", description="User UID", example=2)
30
 * @OA\Property(property="login", type="string", description="Login", example="admin")
31
 * @OA\Property(property="email", type="string", description="Email", example="[email protected]")
32
 * @OA\Property(property="created", type="string", format="date-time", example="2017-01-01 20:17:01")
33
 * @OA\Property(property="updated", type="string", format="date-time", example="2017-01-01 20:17:01")
34
 * @OA\Property(property="status", type="string", enum={"pending", "active", "disabled", "deleted"})
35
 */
36
class Row extends AbstractIdentity
37
{
38
    use Validator;
39
40
    /**
41
     * Small cache of user privileges
42
     *
43
     * @var array
44
     */
45
    protected $privileges;
46
47
    /**
48
     * {@inheritdoc}
49
     *
50
     * @throws \Bluz\Validator\Exception\ComponentException
51
     */
52
    public function beforeSave(): void
53
    {
54
        $this->email = strtolower($this->email ?? '');
55
56
        $this->addValidator('login')
57
            ->required()
58
            ->latin()
59
            ->length(3, 255)
60
            ->callback(
61
                function ($login) {
62
                    $selector = static::getTable()
63
                        ::select()
64
                        ->where('login = ?', $login);
65
66
                    if ($this->id) {
67
                        $selector->andWhere('id != ?', $this->id);
68
                    }
69
70
                    $user = $selector->execute();
71
                    return !$user;
72
                },
73
                'User with this login is already exists'
74
            );
75
76
        $this->addValidator('email')
77
            ->required()
78
            ->email(true)
79
            ->callback(
80
                function ($email) {
81
                    $selector = static::getTable()
82
                        ::select()
83
                        ->where('email = ?', $email);
84
85
                    if ($this->id) {
86
                        $selector->andWhere('id != ?', $this->id);
87
                    }
88
89
                    $user = $selector->execute();
90
                    return !$user;
91
                },
92
                'User with this email is already exists'
93
            );
94
    }
95
96
    /**
97
     * @return void
98
     */
99
    public function beforeInsert(): void
100
    {
101
        $this->created = gmdate('Y-m-d H:i:s');
102
    }
103
104
    /**
105
     * @return void
106
     */
107
    public function beforeUpdate(): void
108
    {
109
        $this->updated = gmdate('Y-m-d H:i:s');
110
    }
111
112
    /**
113
     * Get user roles
114
     */
115
    public function getRoles()
116
    {
117
        return Roles\Table::getInstance()->getUserRoles($this->id);
118
    }
119
120
    /**
121
     * {@inheritdoc}
122
     */
123 6
    public function getPrivileges(): array
124
    {
125 6
        if (!$this->privileges) {
126 6
            $this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id);
127
        }
128 6
        return $this->privileges;
129
    }
130
131
    /**
132
     * Check user role
133
     *
134
     * @param integer $roleId
135
     *
136
     * @return boolean
137
     */
138 1
    public function hasRole($roleId): bool
139
    {
140 1
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id);
141
142 1
        return in_array($roleId, $roles, false);
143
    }
144
}
145