Completed
Pull Request — master (#295)
by Anton
06:14
created

Row::getRoles()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 2
c 0
b 0
f 0
nc 1
nop 0
dl 0
loc 4
rs 10
ccs 0
cts 2
cp 0
crap 2
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\Exception;
12
use Application\Privileges;
13
use Application\Roles;
14
use Bluz\Auth\AbstractRowEntity;
15
use Bluz\Auth\AuthException;
16
use Bluz\Proxy\Auth;
17
use Bluz\Proxy\Session;
18
use Bluz\Validator\Traits\Validator;
19
20
/**
21
 * User
22
 *
23
 * @package  Application\Users
24
 *
25
 * @property integer $id
26
 * @property string  $login
27
 * @property string  $email
28
 * @property string  $created
29
 * @property string  $updated
30
 * @property string  $status
31
 *
32
 * @SWG\Definition(definition="users", title="user", required={"id", "login", "status"})
33
 * @SWG\Property(property="id", type="integer", description="User UID", example=2)
34
 * @SWG\Property(property="login", type="string", description="Login", example="admin")
35
 * @SWG\Property(property="email", type="string", description="Email", example="[email protected]")
36
 * @SWG\Property(property="created", type="string", format="date-time", example="2017-01-01 20:17:01")
37
 * @SWG\Property(property="updated", type="string", format="date-time", example="2017-01-01 20:17:01")
38
 * @SWG\Property(property="status", type="string", enum={"pending", "active", "disabled", "deleted"})
39
 */
40
class Row extends AbstractRowEntity
41
{
42
    use Validator;
43
44
    /**
45
     * Small cache of user privileges
46
     *
47
     * @var array
48
     */
49
    protected $privileges;
50
51
    /**
52
     * @return void
53
     */
54
    public function beforeSave()
55
    {
56
        $this->email = strtolower($this->email);
57
58
        $this->addValidator('login')
59
            ->required()
60
            ->latin()
61
            ->length(3, 255)
62
            ->callback(
63
                function ($login) {
64
                    $user = $this->getTable()
65
                        ->select()
66
                        ->where('login = ?', $login)
67
                        ->andWhere('id != ?', $this->id)
68
                        ->execute();
69
                    return !$user;
70
                },
71
                'User with this login is already exists'
72
            );
73
74
        $this->addValidator('email')
75
            ->required()
76
            ->email(true)
77
            ->callback(
78
                function ($email) {
79
                    $user = $this->getTable()
80
                        ->select()
81
                        ->where('email = ?', $email)
82
                        ->andWhere('id != ?', $this->id)
83
                        ->execute();
84
                    return !$user;
85
                },
86
                'User with this email is already exists'
87
            );
88
    }
89
90
    /**
91
     * @return void
92
     */
93
    public function beforeInsert()
94
    {
95
        $this->created = gmdate('Y-m-d H:i:s');
96
    }
97
98
    /**
99
     * @return void
100
     */
101
    public function beforeUpdate()
102
    {
103
        $this->updated = gmdate('Y-m-d H:i:s');
104
    }
105
106
    /**
107
     * Can entity login
108
     *
109
     * @throws Exception
110
     * @throws AuthException
111
     * @return void
112
     */
113
    public function tryLogin()
114
    {
115
        switch ($this->status) {
116
            case (Table::STATUS_PENDING):
117
                throw new AuthException('Your account is pending activation', 403);
118
            case (Table::STATUS_DISABLED):
119
                throw new AuthException('Your account is disabled by administrator', 403);
120
            case (Table::STATUS_ACTIVE):
121
                // all ok
122
                // regenerate session
123
                if (PHP_SAPI !== 'cli') {
124
                    Session::regenerateId();
125
                }
126
                // save user to new session
127
                Auth::setIdentity($this);
128
                break;
129
            default:
130
                throw new Exception('User status is undefined in system');
131
        }
132
    }
133
134
    /**
135
     * Get user roles
136
     */
137
    public function getRoles()
138
    {
139
        return Roles\Table::getInstance()->getUserRoles($this->id);
140
    }
141
142
    /**
143
     * {@inheritdoc}
144
     */
145 6
    public function getPrivileges(): array
146
    {
147 6
        if (!$this->privileges) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $this->privileges of type array is implicitly converted to a boolean; are you sure this is intended? If so, consider using empty($expr) instead to make it clear that you intend to check for an array without elements.

This check marks implicit conversions of arrays to boolean values in a comparison. While in PHP an empty array is considered to be equal (but not identical) to false, this is not always apparent.

Consider making the comparison explicit by using empty(..) or ! empty(...) instead.

Loading history...
148 6
            $this->privileges = Privileges\Table::getInstance()->getUserPrivileges($this->id);
149
        }
150 6
        return $this->privileges;
151
    }
152
153
    /**
154
     * Check user role
155
     *
156
     * @param integer $roleId
157
     *
158
     * @return boolean
159
     */
160 1
    public function hasRole($roleId)
161
    {
162 1
        $roles = Roles\Table::getInstance()->getUserRolesIdentity($this->id);
163
164 1
        return in_array($roleId, $roles);
165
    }
166
}
167