Completed
Push — master ( 4f3de5...9b9e7a )
by Pavel
05:51
created

User::findUserByAccessToken()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 2
nc 1
nop 1
dl 0
loc 4
rs 10
c 1
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
    protected $table = 'users';
39
40
    protected $fillable = [
41
        'full_name',
42
        'email',
43
        'role_id',
44
        'status'
45
    ];
46
47
    protected $hidden = [
48
        'password',
49
        'password_reset_token',
50
    ];
51
52
    public static $rules = [
53
        'create' => [
54
            'email'    => 'required|email',
55
            'role_id'  => 'required',
56
            'password' => 'required',
57
        ],
58
        'update' => [
59
            'email'   => 'required|email',
60
            'role_id' => 'required',
61
        ]
62
    ];
63
64
    public function role()
65
    {
66
        return $this->hasOne('App\Model\Role', 'id', 'role_id');
67
    }
68
69
    public function access_tokens(){
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
70
        return $this->hasMany('App\Model\AccessToken', 'user_id', 'id');
71
    }
72
73
    public function refresh_tokens(){
0 ignored issues
show
Coding Style introduced by
This method is not in camel caps format.

This check looks for method names that are not written in camelCase.

In camelCase names are written without any punctuation, the start of each new word being marked by a capital letter. Thus the name database connection seeker becomes databaseConnectionSeeker.

Loading history...
74
        return $this->hasMany('App\Model\RefreshToken', 'user_id', 'id');
75
    }
76
77
    public function scopeCurrentUser($query)
0 ignored issues
show
Documentation introduced by
The return type could not be reliably inferred; please add a @return annotation.

Our type inference engine in quite powerful, but sometimes the code does not provide enough clues to go by. In these cases we request you to add a @return annotation as described here.

Loading history...
78
    {
79
        $user = Auth::getUser();
80
81
        if ($user) {
82
            if ($user->role_id == self::ROLE_ADMIN) {
83
                return $query;
84
            }
85
86
            $query->where('id', $user->id);
87
        } else {
88
            $query->where('id', 0);
89
        }
90
91
        return $query;
92
    }
93
94
    /**
95
     * @param $email
96
     *
97
     * @return bool
98
     */
99
    public static function exist($email)
100
    {
101
        return self::where('email', $email)->count() > 0;
102
    }
103
104
    /**
105
     * @param string $email
106
     *
107
     * @return User|null
108
     */
109
    public static function findUserByEmail($email)
110
    {
111
        return self::where('email', $email)->where('status', self::STATUS_ACTIVE)->first();
112
    }
113
114
    /**
115
     * @param string $resetToken
116
     *
117
     * @return User|null
118
     */
119
    public static function findByPasswordResetToken($resetToken)
120
    {
121
        if (!self::isPasswordResetTokenValid($resetToken)) {
122
            return null;
123
        }
124
125
        return self::where('password_reset_token', $resetToken)->where('status', self::STATUS_ACTIVE)->first();
126
    }
127
128
    /**
129
     * @param string $token
130
     *
131
     * @return bool
132
     */
133
    public static function isPasswordResetTokenValid($token)
134
    {
135
        if (empty($token)) {
136
            return false;
137
        }
138
139
        $timestamp = (int) substr($token, strrpos($token, '_') + 1);
140
        $expire    = 3600;
141
        return $timestamp + $expire >= time();
142
    }
143
144
    /**
145
     * @void
146
     */
147
    public function generatePasswordResetToken()
148
    {
149
        $this->password_reset_token = Helper::generateRandomString() . '_' . time();
150
    }
151
152
    /**
153
     * @void
154
     */
155
    public function removePasswordResetToken()
156
    {
157
        $this->password_reset_token = null;
158
    }
159
160
    /**
161
     * @param string $password
162
     */
163
    public function setPassword($password)
164
    {
165
        $this->access_token = null;
0 ignored issues
show
Documentation introduced by
The property access_token does not exist on object<App\Model\User>. Since you implemented __set, maybe consider adding a @property annotation.

Since your code implements the magic setter _set, this function will be called for any write access on an undefined variable. You can add the @property annotation to your class or interface to document the existence of this variable.

<?php

/**
 * @property int $x
 * @property int $y
 * @property string $text
 */
class MyLabel
{
    private $properties;

    private $allowedProperties = array('x', 'y', 'text');

    public function __get($name)
    {
        if (isset($properties[$name]) && in_array($name, $this->allowedProperties)) {
            return $properties[$name];
        } else {
            return null;
        }
    }

    public function __set($name, $value)
    {
        if (in_array($name, $this->allowedProperties)) {
            $properties[$name] = $value;
        } else {
            throw new \LogicException("Property $name is not defined.");
        }
    }

}

Since the property has write access only, you can use the @property-write annotation instead.

Of course, you may also just have mistyped another name, in which case you should fix the error.

See also the PhpDoc documentation for @property.

Loading history...
166
        $this->password = password_hash($password, PASSWORD_DEFAULT, ['cost' => 13]);
167
    }
168
}
169