User.php 2.04 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
    /**
     * @return \Illuminate\Database\Eloquent\Relations\HasMany
     */
    public function requests(): HasMany
    {
        return $this->hasMany(Request::class);
    }

57 58 59 60 61 62 63 64 65 66
    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 */
67
            foreach ($user->tokens()->get() as $token) {
68 69
                $token->delete();
            }
70 71

            /** @var \App\Entry $entry */
72
            foreach ($user->entries()->get() as $entry) {
73 74
                $entry->delete();
            }
75 76

            /** @var \App\Override $override */
77
            foreach ($user->overrides()->get() as $override) {
78 79
                $override->delete();
            }
80 81

            /** @var \App\Request $request */
82
            foreach ($user->requests()->get() as $request) {
83 84
                $request->delete();
            }
85 86
        });
    }
87
}