Passed
Push — develop ( 0fc46b...6b7c8d )
by Tony
04:19
created

User::devices()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 4
ccs 2
cts 2
cp 1
rs 10
cc 1
eloc 2
nc 1
nop 0
crap 1
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 26
    public function hasGlobalRead()
83
    {
84 26
        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 60
    public function isAdmin()
93
    {
94 60
        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 37
    public function getJWTIdentifier()
103
    {
104 37
        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 37
    public function getJWTCustomClaims()
114
    {
115
        // TODO: Implement getJWTCustomClaims() method.
116 37
        return ['app' => 'LibreNMS', 'username' => $this->username];
117
    }
118
119
    // ---- Accessors/Mutators ----
120
121
    /**
122
     * Encrypt passwords before saving
123
     *
124
     * @param $password
125
     */
126 83
    public function setPasswordAttribute($password)
127
    {
128 83
        $this->attributes['password'] = bcrypt($password);
129 83
    }
130
131
    // ---- Define Relationships ----
132
133
    /**
134
     * Returns a list of devices this user has access to
135
     */
136 6
    public function devices()
137
    {
138 6
        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 5
    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 5
        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 3
    public function dashboards()
154
    {
155 3
        return $this->hasMany('App\Models\Dashboard', 'user_id');
156
    }
157
158
    /**
159
     * Returns a list of dashboards this user has
160
     */
161
    public function widgets()
162
    {
163
        return $this->hasMany('App\Models\UserWidgets', 'user_id');
164
    }
165
}
166