Completed
Push — master ( 273f6a...0114af )
by Abdelrahman
08:12
created

User::setPasswordAttribute()   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 1
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
/*
4
 * NOTICE OF LICENSE
5
 *
6
 * Part of the Rinvex Fort Package.
7
 *
8
 * This source file is subject to The MIT License (MIT)
9
 * that is bundled with this package in the LICENSE file.
10
 *
11
 * Package: Rinvex Fort Package
12
 * License: The MIT License (MIT)
13
 * Link:    https://rinvex.com
14
 */
15
16
namespace Rinvex\Fort\Models;
17
18
use Rinvex\Fort\Traits\CanVerifyEmail;
19
use Rinvex\Fort\Traits\Authenticatable;
20
use Illuminate\Database\Eloquent\Model;
21
use Illuminate\Notifications\Notifiable;
22
use Rinvex\Fort\Traits\CanResetPassword;
23
use Illuminate\Database\Eloquent\SoftDeletes;
24
use Rinvex\Fort\Contracts\CanVerifyEmailContract;
25
use Rinvex\Fort\Contracts\AuthenticatableContract;
26
use Illuminate\Foundation\Auth\Access\Authorizable;
27
use Rinvex\Fort\Contracts\CanResetPasswordContract;
28
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
29
30
class User extends Model implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract, CanVerifyEmailContract
0 ignored issues
show
Coding Style introduced by
This line exceeds maximum limit of 120 characters; contains 131 characters

Overly long lines are hard to read on any screen. Most code styles therefor impose a maximum limit on the number of characters in a line.

Loading history...
31
{
32
    use Notifiable, Authenticatable, Authorizable, CanResetPassword, CanVerifyEmail, SoftDeletes;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected $dates = [
38
        'email_verified_at',
39
        'deleted_at',
40
        'birthdate',
41
        'login_at',
42
    ];
43
44
    /**
45
     * {@inheritdoc}
46
     */
47
    protected $fillable = [
48
        'username',
49
        'password',
50
        'two_factor',
51
        'email',
52
        'email_verified',
53
        'email_verified_at',
54
        'phone',
55
        'phone_verified',
56
        'phone_verified_at',
57
        'prefix',
58
        'first_name',
59
        'middle_name',
60
        'last_name',
61
        'sufix',
62
        'job_title',
63
        'country',
64
        'birthdate',
65
        'gender',
66
        'active',
67
        'login_at',
68
    ];
69
70
    /**
71
     * {@inheritdoc}
72
     */
73
    protected $hidden = [
74
        'password',
75
        'two_factor',
76
        'remember_token',
77
    ];
78
79
    /**
80
     * {@inheritdoc}
81
     */
82
    protected $with = ['abilities', 'roles'];
83
84
    /**
85
     * Create a new Eloquent model instance.
86
     *
87
     * @param array $attributes
88
     *
89
     * @return void
0 ignored issues
show
Comprehensibility Best Practice introduced by
Adding a @return annotation to constructors is generally not recommended as a constructor does not have a meaningful return value.

Adding a @return annotation to a constructor is not recommended, since a constructor does not have a meaningful return value.

Please refer to the PHP core documentation on constructors.

Loading history...
90
     */
91
    public function __construct(array $attributes = [])
92
    {
93
        parent::__construct($attributes);
94
95
        $this->setTable(config('rinvex.fort.tables.users'));
96
    }
97
98
    /**
99
     * A user may have multiple direct abilities.
100
     *
101
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
102
     */
103
    public function abilities()
104
    {
105
        return $this->belongsToMany(config('rinvex.fort.models.ability'), config('rinvex.fort.tables.ability_user'))
106
                    ->withTimestamps();
107
    }
108
109
    /**
110
     * A user may have multiple roles.
111
     *
112
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
113
     */
114
    public function roles()
115
    {
116
        return $this->belongsToMany(config('rinvex.fort.models.role'), config('rinvex.fort.tables.role_user'))
117
                    ->withTimestamps();
118
    }
119
120
    /**
121
     * A user may have multiple persistences.
122
     *
123
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
124
     */
125
    public function persistences()
126
    {
127
        return $this->hasMany(config('rinvex.fort.models.persistence'));
128
    }
129
130
    /**
131
     * A user may have multiple socialites.
132
     *
133
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
134
     */
135
    public function socialites()
136
    {
137
        return $this->hasMany(config('rinvex.fort.models.socialite'));
138
    }
139
140
    /**
141
     * Get name attribute.
142
     *
143
     * @return string
144
     */
145
    public function getNameAttribute()
146
    {
147
        $segments = [$this->prefix, $this->first_name, $this->middle_name, $this->last_name, $this->suffix];
0 ignored issues
show
Documentation introduced by
The property prefix does not exist on object<Rinvex\Fort\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
Documentation introduced by
The property first_name does not exist on object<Rinvex\Fort\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
Documentation introduced by
The property middle_name does not exist on object<Rinvex\Fort\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
Documentation introduced by
The property last_name does not exist on object<Rinvex\Fort\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
Documentation introduced by
The property suffix does not exist on object<Rinvex\Fort\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
148
149
        return trim(implode(' ', $segments));
150
    }
151
152
    /**
153
     * Get all abilities of the user.
154
     *
155
     * @return \Illuminate\Support\Collection
156
     */
157
    public function getAllAbilitiesAttribute()
158
    {
159
        return $this->abilities->merge($this->roles->pluck('abilities')->collapse());
0 ignored issues
show
Documentation introduced by
The property abilities does not exist on object<Rinvex\Fort\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
Documentation introduced by
The property roles does not exist on object<Rinvex\Fort\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
160
    }
161
162
    /**
163
     * Determine if the user is super admin.
164
     *
165
     * @return bool
166
     */
167
    public function isSuperadmin()
168
    {
169
        return $this->getAllAbilitiesAttribute()
170
                    ->where('resource', 'global')
171
                    ->where('policy', null)
172
                    ->contains('action', 'superadmin');
173
    }
174
175
    /**
176
     * Determine if the user is protected.
177
     *
178
     * @return bool
179
     */
180
    public function isProtected()
181
    {
182
        return in_array($this->id, config('rinvex.fort.protected.users'));
0 ignored issues
show
Documentation introduced by
The property id does not exist on object<Rinvex\Fort\Models\User>. Since you implemented __get, maybe consider adding a @property annotation.

Since your code implements the magic getter _get, this function will be called for any read 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.");
        }
    }

}

If the property has read access only, you can use the @property-read 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...
183
    }
184
185
    /**
186
     * Set the user's password.
187
     *
188
     * @param string $value
189
     *
190
     * @return void
191
     */
192
    public function setPasswordAttribute($value)
193
    {
194
        $this->attributes['password'] = bcrypt($value);
195
    }
196
}
197