Completed
Pull Request — develop (#98)
by Neil
17:48
created

User::dashboards()   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 1
Bugs 0 Features 1
Metric Value
c 1
b 0
f 1
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
7
/**
8
 * App\Models\User
9
 *
10
 * @property string $username
11
 * @property string $realname
12
 * @property string $password
13
 * @property string $email
14
 * @property int $level
15
 * @property integer $user_id
16
 * @property string $descr
17
 * @property boolean $can_modify_passwd
18
 * @property string $twofactor
19
 * @property integer $dashboard
20
 * @property string $remember_token
21
 * @property \Carbon\Carbon $created_at
22
 * @property \Carbon\Carbon $updated_at
23
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Device[] $devices
24
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Port[] $ports
25
 * @property-read \Illuminate\Database\Eloquent\Collection|\App\Models\Dashboard[] $dashboards
26
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUserId($value)
27
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUsername($value)
28
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User wherePassword($value)
29
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRealname($value)
30
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereEmail($value)
31
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDescr($value)
32
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereLevel($value)
33
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCanModifyPasswd($value)
34
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereTwofactor($value)
35
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereDashboard($value)
36
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereRememberToken($value)
37
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereCreatedAt($value)
38
 * @method static \Illuminate\Database\Query\Builder|\App\Models\User whereUpdatedAt($value)
39
 * @mixin \Eloquent
40
 */
41
class User extends Authenticatable
42
{
43
    /**
44
     * The attributes that are mass assignable.
45
     *
46
     * @var array
47
     */
48
    protected $fillable = [
49
        'realname', 'username', 'password', 'email', 'level',
50
    ];
51
    /**
52
     * The primary key column name.
53
     *
54
     * @var string
55
     */
56
    protected $primaryKey = 'user_id';
57
    /**
58
     * The attributes excluded from the model's JSON form.
59
     *
60
     * @var array
61
     */
62
    protected $hidden = [
63
        'password', 'remember_token',
64
    ];
65
66
67
    // ---- Define Convience Functions ----
68
69
    /**
70
     * Test if this user has global read access
71
     * these users have a level of 5, 10 or 11 (demo).
72
     *
73
     * @return boolean
74
     */
75 11
    public function hasGlobalRead()
76
    {
77 11
        return $this->isAdmin() || $this->level == 5;
78
    }
79
80
    /**
81
     * Test if the User is an admin or demo.
82
     *
83
     * @return boolean
84
     */
85 37
    public function isAdmin()
86
    {
87 37
        return $this->level >= 10;
88
    }
89
90
91
    // ---- Define Reletionships ----
92
93
    /**
94
     * Returns a list of devices this user has access to
95
     */
96 4
    public function devices()
97
    {
98 4
        return $this->belongsToMany('App\Models\Device', 'devices_perms', 'user_id', 'device_id');
99
    }
100
101
    /**
102
     * Returns a list of ports this user has access to
103
     */
104 2
    public function ports()
105
    {
106 2
        return $this->belongsToMany('App\Models\Port', 'ports_perms', 'user_id', 'port_id');
107
    }
108
109
    /**
110
     * Returns a list of dashboards this user has
111
     */
112 1
    public function dashboards()
113
    {
114 1
        return $this->hasMany('App\Models\Dashboard');
115
    }
116
}
117