Completed
Pull Request — dev (#235)
by Alies
07:13
created

User::getFormattedRolesAttribute()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 2
eloc 3
nc 2
nop 0
dl 0
loc 5
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Support\Facades\Gate;
6
use Illuminate\Notifications\Notifiable;
7
use Illuminate\Database\Eloquent\Builder;
8
use Illuminate\Foundation\Auth\User as Authenticatable;
9
use App\Notifications\ResetPassword as ResetPasswordNotification;
10
11
/**
12
 * App\Models\User.
13
 *
14
 * @property int                                                                                                       $id
15
 * @property string                                                                                                    $name
16
 * @property string                                                                                                    $email
17
 * @property \Carbon\Carbon|null                                                                                       $email_verified_at
18
 * @property string|null                                                                                               $password
19
 * @property bool                                                                                                      $active
20
 * @property string|null                                                                                               $remember_token
21
 * @property string                                                                                                    $locale
22
 * @property string                                                                                                    $timezone
23
 * @property string                                                                                                    $slug
24
 * @property \Carbon\Carbon|null                                                                                       $last_access_at
25
 * @property \Carbon\Carbon|null                                                                                       $created_at
26
 * @property \Carbon\Carbon|null                                                                                       $updated_at
27
 * @property mixed                                                                                                     $avatar
28
 * @property mixed                                                                                                     $can_delete
29
 * @property mixed                                                                                                     $can_edit
30
 * @property mixed                                                                                                     $can_impersonate
31
 * @property mixed                                                                                                     $is_super_admin
32
 * @property \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
33
 * @property \Illuminate\Database\Eloquent\Collection|\App\Models\SocialLogin[]                                        $providers
34
 *
35
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User actives()
36
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User findSimilarSlugs($attribute, $config, $slug)
37
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereActive($value)
38
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereCreatedAt($value)
39
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereEmail($value)
40
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereEmailVerifiedAt($value)
41
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereId($value)
42
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLastAccessAt($value)
43
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereLocale($value)
44
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereName($value)
45
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User wherePassword($value)
46
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereRememberToken($value)
47
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereSlug($value)
48
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereTimezone($value)
49
 * @method static \Illuminate\Database\Eloquent\Builder|\App\Models\User whereUpdatedAt($value)
50
 * @mixin \Eloquent
51
 */
52
class User extends Authenticatable
53
{
54
    use Notifiable;
0 ignored issues
show
Bug introduced by
The trait Illuminate\Notifications\Notifiable requires the property $phone_number which is not provided by App\Models\User.
Loading history...
55
56
    /**
57
     * The attributes that should be mutated to dates.
58
     *
59
     * @var array
60
     */
61
    protected $dates = [
62
        'last_access_at',
63
    ];
64
65
    /**
66
     * The attributes that are mass assignable.
67
     *
68
     * @var array
69
     */
70
    protected $fillable = [
71
        'name',
72
        'email',
73
        'active',
74
        'locale',
75
        'timezone',
76
    ];
77
78
    /**
79
     * The attributes that should be hidden for arrays.
80
     *
81
     * @var array
82
     */
83
    protected $hidden = [
84
        'password',
85
        'remember_token',
86
    ];
87
88
    /**
89
     * The attributes that should be cast to native types.
90
     *
91
     * @var array
92
     */
93
    protected $casts = [
94
        'active' => 'boolean',
95
    ];
96
97
    /**
98
     * The accessors to append to the model's array form.
99
     *
100
     * @var array
101
     */
102
    protected $appends = [
103
        'avatar',
104
        'can_edit',
105
        'can_delete',
106
        'can_impersonate',
107
    ];
108
109
    public static function boot()
110
    {
111
        parent::boot();
112
113
        static::saving(function (self $model) {
114
            $model->slug = str_slug($model->name);
115
        });
116
    }
117
118
    public function getCanEditAttribute()
119
    {
120
        return ! $this->is_super_admin || 1 === auth()->id();
121
    }
122
123
    public function getCanDeleteAttribute()
124
    {
125
        return ! $this->is_super_admin && $this->id !== auth()->id() && (
126
            Gate::check('delete users')
127
        );
128
    }
129
130
    public function getCanImpersonateAttribute()
131
    {
132
        if (Gate::check('impersonate users')) {
133
            return ! $this->is_super_admin
134
                && session()->get('admin_user_id') !== $this->id
135
                && $this->id !== auth()->id();
136
        }
137
138
        return false;
139
    }
140
141
    public function scopeActives(Builder $query)
142
    {
143
        return $query->where('active', '=', true);
144
    }
145
146
    public function getIsSuperAdminAttribute()
147
    {
148
        return 1 === $this->id;
149
    }
150
151
    /**
152
     * Send the password reset notification.
153
     *
154
     * @param string $token
155
     */
156
    public function sendPasswordResetNotification($token)
157
    {
158
        $this->notify(new ResetPasswordNotification($token));
159
    }
160
161
    /**
162
     * @param $provider
163
     *
164
     * @return bool
165
     */
166
    public function getProvider($provider)
167
    {
168
        return $this->providers->first(function (SocialLogin $item) use ($provider) {
169
            return $item->provider === $provider;
170
        });
171
    }
172
173
    /**
174
     * @return mixed
175
     */
176
    public function providers()
177
    {
178
        return $this->hasMany(SocialLogin::class);
179
    }
180
181
    /**
182
     * Get user avatar from gravatar.
183
     */
184
    public function getAvatarAttribute()
185
    {
186
        $hash = md5($this->email);
187
188
        return "https://secure.gravatar.com/avatar/{$hash}?size=100&d=mm&r=g";
189
    }
190
191
    public function posts()
192
    {
193
        return $this->hasMany(Post::class);
194
    }
195
196
    /**
197
     * @return string
198
     */
199
    public function __toString()
200
    {
201
        return $this->name;
202
    }
203
}
204