Completed
Pull Request — master (#19)
by Pavel
02:27
created

User::access_tokens()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
namespace App\Model;
3
4
use Illuminate\Database\Eloquent\SoftDeletes;
5
use App\Common\Helper;
6
use App\Common\Auth;
7
8
/**
9
 * Class User
10
 *
11
 * @property integer        $id
12
 * @property string         $email
13
 * @property string         $full_name
14
 * @property string         $password
15
 * @property string         $password_reset_token
16
 * @property integer        $role_id
17
 * @property integer        $created_by
18
 * @property integer        $updated_by
19
 * @property \Carbon\Carbon $created_at
20
 * @property \Carbon\Carbon $updated_at
21
 * @property \Carbon\Carbon $deleted_at
22
 * @property integer        $status
23
 * @property-read Role      $role
24
 *
25
 * @package App\Model
26
 */
27
final class User extends BaseModel
28
{
29
    use SoftDeletes;
30
31
    const STATUS_BLOCKED     = 0;
32
    const STATUS_ACTIVE      = 1;
33
    const STATUS_WAIT        = 2;
34
35
    const ROLE_ADMIN         = 1;
36
    const ROLE_USER          = 2;
37
38
    const EXPIRE_RESET_TOKEN = 3600;
39
40
    protected $table = 'users';
41
42
    protected $fillable = [
43
        'full_name',
44
        'email',
45
        'role_id',
46
        'status'
47
    ];
48
49
    protected $hidden = [
50
        'password',
51
        'password_reset_token',
52
    ];
53
54
    public static $rules = [
55
        'create' => [
56
            'email'    => 'required|email',
57
            'role_id'  => 'required',
58
            'password' => 'required',
59
        ],
60
        'update' => [
61
            'email'   => 'required|email',
62
            'role_id' => 'required',
63
        ]
64
    ];
65
66
    /**
67
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
68
     */
69
    public function role()
70
    {
71
        return $this->hasOne('App\Model\Role', 'id', 'role_id');
72
    }
73
74
    /**
75
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
76
     */
77
    public function accessTokens()
78
    {
79
        return $this->hasMany('App\Model\AccessToken', 'user_id', 'id');
80
    }
81
82
    /**
83
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
84
     */
85
    public function refreshTokens()
86
    {
87
        return $this->hasMany('App\Model\RefreshToken', 'user_id', 'id');
88
    }
89
90
    /**
91
     * @param $query
92
     *
93
     * @return mixed
94
     */
95
    public function scopeCurrentUser($query)
96
    {
97
        $user = Auth::getUser();
98
99
        if ($user) {
100
            if ($user->role_id == self::ROLE_ADMIN) {
101
                return $query;
102
            }
103
104
            $query->where('id', $user->id);
105
        } else {
106
            $query->where('id', 0);
107
        }
108
109
        return $query;
110
    }
111
112
    /**
113
     * @param $email
114
     *
115
     * @return bool
116
     */
117
    public static function exist($email)
118
    {
119
        return self::where('email', $email)->count() > 0;
120
    }
121
122
    /**
123
     * @param string $email
124
     *
125
     * @return User|null
126
     */
127
    public static function findUserByEmail($email)
128
    {
129
        return self::where('email', $email)->where('status', self::STATUS_ACTIVE)->first();
130
    }
131
132
    /**
133
     * @param string $resetToken
134
     *
135
     * @return User|null
136
     */
137
    public static function findByPasswordResetToken($resetToken)
138
    {
139
        if (!self::isPasswordResetTokenValid($resetToken)) {
140
            return null;
141
        }
142
143
        return self::where('password_reset_token', $resetToken)->where('status', self::STATUS_ACTIVE)->first();
144
    }
145
146
    /**
147
     * @param string $token
148
     *
149
     * @return bool
150
     */
151
    public static function isPasswordResetTokenValid($token)
152
    {
153
        if (empty($token)) {
154
            return false;
155
        }
156
157
        $timestamp = (int)substr($token, strrpos($token, '_') + 1);
158
        $expire    = self::EXPIRE_RESET_TOKEN;
159
        return $timestamp + $expire >= time();
160
    }
161
162
    /**
163
     * @void
164
     */
165
    public function generatePasswordResetToken()
166
    {
167
        $this->password_reset_token = Helper::generateRandomString().'_'.time();
168
    }
169
170
    /**
171
     * @void
172
     */
173
    public function removePasswordResetToken()
174
    {
175
        $this->password_reset_token = null;
176
    }
177
178
    /**
179
     * @param string $password
180
     */
181
    public function setPassword($password)
182
    {
183
        // we need to invalidate tokens when changing password
184
        AccessToken::where('user_id', $this->id)->delete();
185
        RefreshToken::where('user_id', $this->id)->delete();
186
187
        $this->password = password_hash($password, PASSWORD_DEFAULT, ['cost' => 13]);
188
    }
189
}
190