Completed
Push — master ( 08ae01...6d5582 )
by Fèvre
13s
created

User::rolePermissions()   B

Complexity

Conditions 1
Paths 1

Size

Total Lines 27
Code Lines 22

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 27
rs 8.8571
c 0
b 0
f 0
cc 1
eloc 22
nc 1
nop 0
1
<?php
2
namespace Xetaravel\Models;
3
4
use Eloquence\Behaviours\Sluggable;
5
use Illuminate\Auth\Authenticatable;
6
use Illuminate\Auth\Passwords\CanResetPassword;
7
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
8
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
9
use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
10
use Illuminate\Database\Eloquent\Builder;
11
use Illuminate\Foundation\Auth\Access\Authorizable;
12
use Illuminate\Notifications\DatabaseNotification;
13
use Illuminate\Notifications\Notifiable;
14
use Spatie\MediaLibrary\HasMedia\HasMediaTrait;
15
use Spatie\MediaLibrary\HasMedia\Interfaces\HasMediaConversions;
16
use Ultraware\Roles\Contracts\HasRoleAndPermission as HasRoleAndPermissionContract;
17
use Ultraware\Roles\Traits\HasRoleAndPermission;
18
use Xetaravel\Models\Entities\UserEntity;
19
use Xetaravel\Models\Presenters\UserPresenter;
20
use Xetaravel\Notifications\ResetPasswordNotification;
21
22
class User extends Model implements
23
    AuthenticatableContract,
24
    AuthorizableContract,
25
    CanResetPasswordContract,
26
    HasRoleAndPermissionContract,
27
    HasMediaConversions
28
{
29
    use Authenticatable,
30
        Authorizable,
31
        CanResetPassword,
32
        Notifiable,
33
        Sluggable,
34
        HasRoleAndPermission,
35
        HasMediaTrait,
36
        UserEntity,
37
        UserPresenter;
38
39
    /**
40
     * The attributes that are mass assignable.
41
     *
42
     * @var array
43
     */
44
    protected $fillable = [
45
        'username',
46
        'email',
47
        'password',
48
        'slug',
49
        'register_ip',
50
        'last_login_ip',
51
        'last_login'
52
    ];
53
54
    /**
55
     * The attributes that should be hidden for arrays.
56
     *
57
     * @var array
58
     */
59
    protected $hidden = [
60
        'password',
61
        'remember_token'
62
    ];
63
64
    /**
65
     * The accessors to append to the model's array form.
66
     *
67
     * @var array
68
     */
69
    protected $appends = [
70
        'profile_background',
71
72
        // Media Model
73
        'avatar_small',
74
        'avatar_medium',
75
        'avatar_big',
76
77
        // Account Model
78
        'first_name',
79
        'last_name',
80
        'biography',
81
        'signature',
82
        'facebook',
83
        'twitter'
84
    ];
85
86
    /**
87
     * The "booting" method of the model.
88
     *
89
     * @return void
90
     */
91
    protected static function boot()
92
    {
93
        parent::boot();
94
95
        // Generated the slug before updating.
96
        static::updating(function ($model) {
97
            $model->generateSlug();
98
        });
99
    }
100
101
    /**
102
     * Return the field to slug.
103
     *
104
     * @return string
105
     */
106
    public function slugStrategy(): string
107
    {
108
        return 'username';
109
    }
110
111
    /**
112
     * Register the related to the Model.
113
     *
114
     * @return void
115
     */
116
    public function registerMediaConversions()
117
    {
118
        $this->addMediaConversion('thumbnail.small')
119
                ->width(100)
120
                ->height(100)
121
                ->keepOriginalImageFormat();
122
123
        $this->addMediaConversion('thumbnail.medium')
124
                ->width(200)
125
                ->height(200)
126
                ->keepOriginalImageFormat();
127
128
        $this->addMediaConversion('thumbnail.big')
129
                ->width(300)
130
                ->height(300)
131
                ->keepOriginalImageFormat();
132
133
        $this->addMediaConversion('original')
134
                ->keepOriginalImageFormat();
135
    }
136
137
    /**
138
     * Get the comments for the user.
139
     *
140
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
141
     */
142
    public function comments()
143
    {
144
        return $this->hasMany(Comment::class);
145
    }
146
147
    /**
148
     * Get the articles for the user.
149
     *
150
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
151
     */
152
    public function articles()
153
    {
154
        return $this->hasMany(Article::class);
155
    }
156
157
    /**
158
     * Get the account for the user.
159
     *
160
     * @return \Illuminate\Database\Eloquent\Relations\HasOne
161
     */
162
    public function account()
163
    {
164
        return $this->hasOne(Account::class);
165
    }
166
167
    /**
168
     * Get the roles for the user.
169
     *
170
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
171
     */
172
    public function roles()
173
    {
174
        return $this->belongsToMany(Role::class)->withTimestamps();
175
    }
176
177
    /**
178
     * Get the badges for the user.
179
     *
180
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
181
     */
182
    public function badges()
183
    {
184
        return $this->belongsToMany(Badge::class)->withTimestamps();
185
    }
186
187
    /**
188
     * Get the notifications for the user.
189
     *
190
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
191
     */
192
    public function notifications()
193
    {
194
        return $this->morphMany(DatabaseNotification::class, 'notifiable')
195
                        ->orderBy('read_at', 'asc')
196
                        ->orderBy('created_at', 'desc');
197
    }
198
199
    /**
200
     * Send the password reset notification.
201
     *
202
     * @param string $token
203
     *
204
     * @return void
205
     */
206
    public function sendPasswordResetNotification($token)
207
    {
208
        $this->notify(new ResetPasswordNotification($token));
209
    }
210
211
    /**
212
     * Get all permissions from roles.
213
     *
214
     * @return \Illuminate\Database\Eloquent\Builder
215
     */
216
    public function rolePermissions(): Builder
217
    {
218
        $permissionModel = app(config('roles.models.permission'));
219
220
        return $permissionModel
221
            ::select([
222
                'permissions.*',
223
                'permission_role.created_at as pivot_created_at',
224
                'permission_role.updated_at as pivot_updated_at'
225
            ])
226
            ->join('permission_role', 'permission_role.permission_id', '=', 'permissions.id')
227
            ->join('roles', 'roles.id', '=', 'permission_role.role_id')
228
            ->whereIn('roles.id', $this->getRoles()->pluck('id')->toArray())
229
            ->orWhere('roles.level', '<', $this->level())
230
            ->groupBy([
231
                'permissions.id',
232
                'permissions.name',
233
                'permissions.slug',
234
                'permissions.description',
235
                'permissions.model',
236
                'permissions.created_at',
237
                'permissions.updated_at',
238
                'permissions.is_deletable',
239
                'pivot_created_at',
240
                'pivot_updated_at'
241
            ]);
242
    }
243
}
244