summaryrefslogtreecommitdiff
path: root/src/Models/User/User.php
blob: d04693c8134ed297f7699ae9e25acb3edf871cd0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
<?php

namespace Engelsystem\Models\User;

use Carbon\Carbon;
use Engelsystem\Models\Auth\Permission;
use Engelsystem\Models\Auth\Role;
use Engelsystem\Models\BaseModel;
use Engelsystem\Models\Team;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Database\Eloquent\Relations\BelongsToMany;
use Illuminate\Database\Eloquent\Relations\HasOne;
use Illuminate\Database\Query\Builder as QueryBuilder;
use Staudenmeir\EloquentHasManyDeep\HasManyDeep;

/**
 * @property integer                                   $id
 * @property string                                    $name
 * @property string                                    $email
 * @property string                                    $password
 * @property string                                    $api_key
 * @property Carbon|null                               $last_login_at
 * @property Carbon                                    $created_at
 * @property Carbon                                    $updated_at
 *
 * @property-read QueryBuilder|Contact                 $contact
 * @property-read QueryBuilder|PersonalData            $personalData
 * @property-read QueryBuilder|Settings                $settings
 * @property-read QueryBuilder|State                   $state
 * @property-read QueryBuilder|Collection|Permission[] $permissions
 * @property-read QueryBuilder|Collection|Role[]       $roles
 * @property-read QueryBuilder|Collection|Team[]       $supports
 * @property-read QueryBuilder|Collection|Team[]       $teams
 *
 * @method static QueryBuilder|User              whereId($value)
 * @method static QueryBuilder|Collection|User[] whereName($value)
 * @method static QueryBuilder|Collection|User[] whereEmail($value)
 * @method static QueryBuilder|Collection|User[] wherePassword($value)
 * @method static QueryBuilder|Collection|User[] whereApiKey($value)
 * @method static QueryBuilder|Collection|User[] whereLastLoginAt($value)
 * @method static QueryBuilder|Collection|User[] whereCreatedAt($value)
 * @method static QueryBuilder|Collection|User[] whereUpdatedAt($value)
 */
class User extends BaseModel
{
    /** @var bool enable timestamps */
    public $timestamps = true;

    /** The attributes that are mass assignable */
    protected $fillable = [
        'name',
        'password',
        'email',
        'api_key',
        'last_login_at',
    ];

    /** @var array The attributes that should be hidden for serialization */
    protected $hidden = [
        'api_key',
        'password',
    ];

    /** @var array The attributes that should be mutated to dates */
    protected $dates = [
        'last_login_at',
    ];

    /**
     * @return HasOne
     */
    public function contact()
    {
        return $this
            ->hasOne(Contact::class)
            ->withDefault();
    }

    /**
     * @return HasOne
     */
    public function personalData()
    {
        return $this
            ->hasOne(PersonalData::class)
            ->withDefault();
    }

    /**
     * The permissions that the user has
     *
     * @return HasManyDeep
     */
    public function permissions()
    {
        return $this->hasManyDeepFromRelations($this->roles(), (new Role)->permissions());
    }

    /**
     * The roles that belong to the user
     *
     * @return HasManyDeep
     */
    public function roles()
    {
        return $this->hasManyDeepFromRelations($this->teams(), (new Team)->roles());
    }

    /**
     * The teams that are supported by the user
     *
     * @return BelongsToMany
     */
    public function supports()
    {
        return $this
            ->belongsToMany(Team::class, 'supporter_team')
            ->withTimestamps();
    }

    /**
     * The teams that belong to the user
     *
     * @return BelongsToMany
     */
    public function teams()
    {
        return $this
            ->belongsToMany(Team::class)
            ->withPivot(['confirmed'])
            ->withTimestamps();
    }

    /**
     * @return HasOne
     */
    public function settings()
    {
        return $this
            ->hasOne(Settings::class)
            ->withDefault();
    }

    /**
     * @return HasOne
     */
    public function state()
    {
        return $this
            ->hasOne(State::class)
            ->withDefault();
    }
}