User.php 1.69 KB
Newer Older
1 2 3 4
<?php

namespace App;

Jacob Priddy's avatar
Jacob Priddy committed
5
use Illuminate\Database\Eloquent\Relations\HasMany;
Jacob Priddy's avatar
Jacob Priddy committed
6
use Illuminate\Foundation\Auth\User as Authenticatable;
7
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
8

Jacob Priddy's avatar
Jacob Priddy committed
9 10
class User extends Authenticatable
{
Jacob Priddy's avatar
Jacob Priddy committed
11 12 13 14
    protected $fillable = [
        '*',
    ];

15
    protected $dates = ['expires_at'];
Jacob Priddy's avatar
Jacob Priddy committed
16 17

    /**
18
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
Jacob Priddy's avatar
Jacob Priddy committed
19
     */
Jacob Priddy's avatar
Jacob Priddy committed
20 21
    public function tokens(): HasMany
    {
Jacob Priddy's avatar
Jacob Priddy committed
22 23
        return $this->hasMany(Token::class);
    }
24

25 26 27 28 29
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function entries(): HasMany
    {
30
        return $this->hasMany(Entry::class);
31 32
    }

33 34 35 36 37 38 39
    /**
     * @return \Illuminate\Database\Eloquent\Relations\BelongsToMany
     */
    public function groups(): BelongsToMany
    {
        return $this->belongsToMany(Group::class);
    }
40

41 42 43 44 45 46 47 48
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function overrides(): HasMany
    {
        return $this->hasMany(Override::class);
    }

49 50 51 52 53 54 55 56 57 58 59 60 61
    public static function boot()
    {
        parent::boot();

        static::deleting(static function (User $user) {
            // Detach all groups
            $user->groups()->detach();

            // Delete all tokens
            /** @var \App\Token $token */
            foreach ($user->tokens() as $token) {
                $token->delete();
            }
62 63 64 65 66

            /** @var \App\Entry $entry */
            foreach ($user->entries() as $entry) {
                $entry->delete();
            }
67 68 69 70 71

            /** @var \App\Override $override */
            foreach ($user->overrides() as $override) {
                $override->delete();
            }
72 73
        });
    }
74
}