Completed
Push — widgets-pivot ( dc1e33 )
by Tony
02:59
created

User::widgets()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
rs 10
c 0
b 0
f 0
1
<?php
2
3
namespace App\Models;
4
5
use Illuminate\Foundation\Auth\User as Authenticatable;
6
use Illuminate\Notifications\Notifiable;
7
use Tymon\JWTAuth\Contracts\JWTSubject;
8
9
/**
10
 * App\Models\User
11
 *
12
 * @property integer $user_id
13
 * @property string $username
14
 * @property string $password
15
 * @property string $realname
16
 * @property string $email
17
 * @property string $descr
18
 * @property boolean $level
19
 * @property boolean $can_modify_passwd
20
 * @property string $twofactor
21
 * @property integer $dashboard
22
 * @property \Carbon\Carbon $created_at
23
 * @property \Carbon\Carbon $updated_at
24
 * @property string $remember_token
25
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Device[] $devices
26
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Port[] $ports
27
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dashboard[] $dashboards
28
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUserId($value)
29
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUsername($value)
30
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User wherePassword($value)
31
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRealname($value)
32
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereEmail($value)
33
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDescr($value)
34
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereLevel($value)
35
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCanModifyPasswd($value)
36
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereTwofactor($value)
37
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDashboard($value)
38
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCreatedAt($value)
39
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUpdatedAt($value)
40
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRememberToken($value)
41
 * @mixin \Eloquent
42
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $notifications
43
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $readNotifications
44
 * @property-read \Illuminate\Notifications\DatabaseNotificationCollection|\Illuminate\Notifications\DatabaseNotification[] $unreadNotifications
45
 */
46
class User extends Authenticatable implements JWTSubject
47
{
48
    use Notifiable;
49
50
    /**
51
     * The attributes that are mass assignable.
52
     *
53
     * @var array
54
     */
55
    protected $fillable = [
56
        'realname', 'username', 'password', 'email', 'level', 'descr',
57
    ];
58
    /**
59
     * The primary key column name.
60
     *
61
     * @var string
62
     */
63
    protected $primaryKey = 'user_id';
64
    /**
65
     * The attributes excluded from the model's JSON form.
66
     *
67
     * @var array
68
     */
69
    protected $hidden = [
70
        'password', 'remember_token',
71
    ];
72
73
74
    // ---- Helper Functions ----
75
76
    /**
77
     * Test if this user has global read access
78
     * these users have a level of 5, 10 or 11 (demo).
79
     *
80
     * @return boolean
81
     */
82
    public function hasGlobalRead()
83
    {
84
        return $this->isAdmin() || $this->level == 5;
85
    }
86
87
    /**
88
     * Test if the User is an admin or demo.
89
     *
90
     * @return boolean
91
     */
92
    public function isAdmin()
93
    {
94
        return $this->level >= 10;
95
    }
96
97
    /**
98
     * Get the identifier that will be stored in the subject claim of the JWT.
99
     *
100
     * @return mixed
101
     */
102
    public function getJWTIdentifier()
103
    {
104
        return $this->user_id;
105
        // TODO: Implement getJWTIdentifier() method.
106
    }
107
108
    /**
109
     * Return a key value array, containing any custom claims to be added to the JWT.
110
     *
111
     * @return array
112
     */
113
    public function getJWTCustomClaims()
114
    {
115
        // TODO: Implement getJWTCustomClaims() method.
116
        return ['app' => 'LibreNMS', 'username' => $this->username];
117
    }
118
119
    // ---- Accessors/Mutators ----
120
121
    /**
122
     * Encrypt passwords before saving
123
     *
124
     * @param $password
125
     */
126
    public function setPasswordAttribute($password)
127
    {
128
        $this->attributes['password'] = bcrypt($password);
129
    }
130
131
    // ---- Define Relationships ----
132
133
    /**
134
     * Returns a list of devices this user has access to
135
     */
136
    public function devices()
137
    {
138
        return $this->belongsToMany('App\Models\Device', 'devices_perms', 'user_id', 'device_id');
139
    }
140
141
    /**
142
     * Returns a list of ports this user has access to
143
     */
144
    public function ports()
145
    {
146
        //FIXME we should return all ports for a device if the user has been given access to the whole device.
147
        return $this->belongsToMany('App\Models\Port', 'ports_perms', 'user_id', 'port_id');
148
    }
149
150
    /**
151
     * Returns a list of dashboards this user has
152
     */
153
    public function dashboards()
154
    {
155
        return $this->hasMany('App\Models\Dashboard');
156
    }
157
158
    public function widgets()
159
    {
160
        return $this->hasMany('App\Models\UsersWidgets');
161
    }
162
}
163