Completed
Push — develop ( c763e2...508664 )
by Abdelrahman
16:47
created

Guardian::boot()   A

Complexity

Conditions 4
Paths 1

Size

Total Lines 12
Code Lines 6

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 12
rs 9.2
c 0
b 0
f 0
cc 4
eloc 6
nc 1
nop 0
1
<?php
2
3
declare(strict_types=1);
4
5
namespace Cortex\Auth\Models;
6
7
use Illuminate\Auth\Authenticatable;
8
use Illuminate\Support\Facades\Hash;
9
use Rinvex\Auth\Traits\HasHashables;
10
use Cortex\Foundation\Traits\Auditable;
11
use Illuminate\Database\Eloquent\Model;
12
use Rinvex\Cacheable\CacheableEloquent;
13
use Rinvex\Support\Traits\ValidatingTrait;
14
use Spatie\Activitylog\Traits\LogsActivity;
15
use Illuminate\Foundation\Auth\Access\Authorizable;
16
use Illuminate\Database\Eloquent\Relations\MorphMany;
17
use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
18
use Illuminate\Contracts\Auth\Access\Authorizable as AuthorizableContract;
19
20
class Guardian extends Model implements AuthenticatableContract, AuthorizableContract
21
{
22
    // @TODO: Strangely, this issue happens only here!!!
0 ignored issues
show
Coding Style Best Practice introduced by
Comments for TODO tasks are often forgotten in the code; it might be better to use a dedicated issue tracker.
Loading history...
23
    // Duplicate trait usage to fire attached events for cache
24
    // flush before other events in other traits specially LogsActivity,
25
    // otherwise old cached queries used and no changelog recorded on update.
26
    use CacheableEloquent;
27
    use Auditable;
28
    use LogsActivity;
29
    use Authorizable;
30
    use HasHashables;
31
    use Authenticatable;
32
    use ValidatingTrait;
33
34
    /**
35
     * {@inheritdoc}
36
     */
37
    protected $fillable = [
38
        'username',
39
        'password',
40
        'email',
41
        'is_active',
42
        'last_activity',
43
    ];
44
45
    /**
46
     * {@inheritdoc}
47
     */
48
    protected $casts = [
49
        'username' => 'string',
50
        'password' => 'string',
51
        'email' => 'string',
52
        'is_active' => 'boolean',
53
        'last_activity' => 'datetime',
54
        'deleted_at' => 'datetime',
55
    ];
56
57
    /**
58
     * {@inheritdoc}
59
     */
60
    protected $hidden = [
61
        'password',
62
        'remember_token',
63
    ];
64
65
    /**
66
     * {@inheritdoc}
67
     */
68
    protected $observables = [
69
        'validating',
70
        'validated',
71
    ];
72
73
    /**
74
     * The attributes to be encrypted before saving.
75
     *
76
     * @var array
77
     */
78
    protected $hashables = [
79
        'password',
80
    ];
81
82
    /**
83
     * The default rules that the model will validate against.
84
     *
85
     * @var array
86
     */
87
    protected $rules = [];
88
89
    /**
90
     * Whether the model should throw a
91
     * ValidationException if it fails validation.
92
     *
93
     * @var bool
94
     */
95
    protected $throwValidationExceptions = true;
96
97
    /**
98
     * Indicates whether to log only dirty attributes or all.
99
     *
100
     * @var bool
101
     */
102
    protected static $logOnlyDirty = true;
103
104
    /**
105
     * The attributes that are logged on change.
106
     *
107
     * @var array
108
     */
109
    protected static $logFillable = true;
110
111
    /**
112
     * The attributes that are ignored on change.
113
     *
114
     * @var array
115
     */
116
    protected static $ignoreChangedAttributes = [
117
        'password',
118
        'last_activity',
119
        'created_at',
120
        'updated_at',
121
        'deleted_at',
122
    ];
123
124
    /**
125
     * Get the route key for the model.
126
     *
127
     * @return string
128
     */
129
    public function getRouteKeyName(): string
130
    {
131
        return 'username';
132
    }
133
134
    /**
135
     * {@inheritdoc}
136
     */
137
    protected static function boot()
138
    {
139
        parent::boot();
140
141
        static::saving(function (self $user) {
142
            foreach (array_intersect($user->getHashables(), array_keys($user->getAttributes())) as $hashable) {
143
                if ($user->isDirty($hashable) && Hash::needsRehash($user->$hashable)) {
144
                    $user->$hashable = Hash::make($user->$hashable);
145
                }
146
            }
147
        });
148
    }
149
150
    /**
151
     * The user may have many sessions.
152
     *
153
     * @return \Illuminate\Database\Eloquent\Relations\MorphMany
154
     */
155
    public function sessions(): MorphMany
156
    {
157
        return $this->morphMany(config('cortex.auth.models.session'), 'user');
158
    }
159
160
    /**
161
     * Activate the user.
162
     *
163
     * @return $this
164
     */
165
    public function activate()
166
    {
167
        $this->update(['is_active' => true]);
168
169
        return $this;
170
    }
171
172
    /**
173
     * Deactivate the user.
174
     *
175
     * @return $this
176
     */
177
    public function deactivate()
178
    {
179
        $this->update(['is_active' => false]);
180
181
        return $this;
182
    }
183
}
184