Test Setup Failed
Push — vue-test ( c87041...4c6bf5 )
by Tony
04:34
created

User::canAccessDevice()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 6

Importance

Changes 0
Metric Value
cc 2
eloc 2
nc 2
nop 1
dl 0
loc 4
ccs 0
cts 0
cp 0
crap 6
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',
71
        'remember_token',
72
        'pivot',
73
    ];
74
75
76
    // ---- Helper Functions ----
77
78
    /**
79
     * Test if this user has global read access
80
     * these users have a level of 5, 10 or 11 (demo).
81
     *
82 26
     * @return boolean
83
     */
84 26
    public function hasGlobalRead()
85
    {
86
        return $this->isAdmin() || $this->level == 5;
87
    }
88
89
    /**
90
     * Test if the User is an admin or demo.
91
     *
92 60
     * @return boolean
93
     */
94 60
    public function isAdmin()
95
    {
96
        return $this->level >= 10;
97
    }
98
99
    /**
100
     * Check if this user has access to a device
101
     *
102 37
     * @param Device|int $device can be a device Model or device id
103
     * @return bool
104 37
     */
105
    public function canAccessDevice($device)
106
    {
107
        return $this->hasGlobalRead() || $this->devices->contains($device);
108
    }
109
110
    /**
111
     * Get the identifier that will be stored in the subject claim of the JWT.
112
     *
113 37
     * @return mixed
114
     */
115
    public function getJWTIdentifier()
116 37
    {
117
        return $this->user_id;
118
        // TODO: Implement getJWTIdentifier() method.
119
    }
120
121
    /**
122
     * Return a key value array, containing any custom claims to be added to the JWT.
123
     *
124
     * @return array
125
     */
126 83
    public function getJWTCustomClaims()
127
    {
128 83
        // TODO: Implement getJWTCustomClaims() method.
129 83
        return ['app' => 'LibreNMS', 'username' => $this->username];
130
    }
131
132
    // ---- Accessors/Mutators ----
133
134
    /**
135
     * Encrypt passwords before saving
136 6
     *
137
     * @param $password
138 6
     */
139
    public function setPasswordAttribute($password)
140
    {
141
        $this->attributes['password'] = bcrypt($password);
142
    }
143
144 5
    // ---- Define Relationships ----
145
146
    /**
147 5
     * Returns a list of devices this user has access to
148
     */
149
    public function devices()
150
    {
151
        return $this->belongsToMany('App\Models\Device', 'devices_perms', 'user_id', 'device_id');
152
    }
153 3
154
    /**
155 3
     * Returns a list of ports this user has access to
156
     */
157
    public function ports()
158
    {
159
        //FIXME we should return all ports for a device if the user has been given access to the whole device.
160
        return $this->belongsToMany('App\Models\Port', 'ports_perms', 'user_id', 'port_id');
161
    }
162
163
    /**
164
     * Returns a list of dashboards this user has
165
     */
166
    public function dashboards()
167
    {
168
        return $this->hasMany('App\Models\Dashboard', 'user_id');
169
    }
170
171
    /**
172
     * Returns a list of dashboards this user has
173
     */
174
    public function widgets()
175
    {
176
        return $this->hasMany('App\Models\UsersWidgets', 'user_id');
177
    }
178
}
179