User::scopeOrderByNameAsc()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
namespace NukaCode\Users\Models;
4
5
use App\Models\BaseModel;
6
use Illuminate\Auth\Authenticatable;
7
use Illuminate\Auth\Passwords\CanResetPassword;
8
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
9
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
10
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
11
use Illuminate\Database\Eloquent\SoftDeletes;
12
use Illuminate\Foundation\Auth\Access\Authorizable;
13
use NukaCode\Users\Traits\HasRoles;
14
15
abstract class User extends BaseModel implements AuthenticatableContract, AuthorizableContract, CanResetPasswordContract
16
{
17
    use Authenticatable, Authorizable, CanResetPassword, HasRoles, SoftDeletes;
18
19
    /**
20
     * Define the SQL table for this model
21
     *
22
     * @var string
23
     */
24
    protected $table = 'users';
25
26
    /**
27
     * The attributes excluded from the model's JSON form.
28
     *
29
     * @var array
30
     */
31
    protected $hidden = [
32
        'password',
33
        'remember_token',
34
    ];
35
36
    protected $fillable = [
37
        'username',
38
        'password',
39
        'email',
40
        'first_name',
41
        'last_name',
42
        'display_name',
43
        'timezone',
44
        'location',
45
        'url',
46
    ];
47
48
    /**
49
     * Tell eloquent to set deleted_at as a carbon date.
50
     *
51
     * @var array
52
     */
53
    protected $dates = [
54
        'deleted_at',
55
    ];
56
57
    /**
58
     * Determines if the users has global rights.
59
     *
60
     * @return mixed
61
     */
62 View Code Duplication
    public function isSuperUser()
63
    {
64
        if (config('users.allow_super_user') == true
65
            && $this->super_flag == 1
66
        ) {
67
            return true;
68
        }
69
70
        return false;
71
    }
72
73
    /**
74
     * Grant a user global rights.
75
     *
76
     * @return mixed
77
     */
78 View Code Duplication
    public function makeSuperUser()
79
    {
80
        if (config('users.allow_super_user') == true) {
81
            $this->super_flag = 1;
82
            $this->save();
83
84
            return true;
85
        }
86
        
87
        return false;
88
    }
89
90
    /**
91
     * Order by name ascending scope
92
     *
93
     * @param Builder $query The current query to append to
94
     *
95
     * @return Builder
96
     */
97
    public function scopeOrderByNameAsc($query)
98
    {
99
        return $query->orderBy('username', 'asc');
100
    }
101
102
    /**
103
     * Make sure to hash the user's password on save
104
     *
105
     * @param string $value The value of the attribute (Auto Set)
106
     */
107
    public function setPasswordAttribute($value)
108
    {
109
        $this->attributes['password'] = bcrypt($value);
110
    }
111
}
112