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

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