]> BookStack Code Mirror - bookstack/blob - app/User.php
Added view count tracking with personalised lists
[bookstack] / app / User.php
1 <?php
2
3 namespace BookStack;
4
5 use Illuminate\Auth\Authenticatable;
6 use Illuminate\Database\Eloquent\Model;
7 use Illuminate\Auth\Passwords\CanResetPassword;
8 use Illuminate\Contracts\Auth\Authenticatable as AuthenticatableContract;
9 use Illuminate\Contracts\Auth\CanResetPassword as CanResetPasswordContract;
10
11 class User extends Model implements AuthenticatableContract, CanResetPasswordContract
12 {
13     use Authenticatable, CanResetPassword;
14
15     /**
16      * The database table used by the model.
17      *
18      * @var string
19      */
20     protected $table = 'users';
21
22     /**
23      * The attributes that are mass assignable.
24      *
25      * @var array
26      */
27     protected $fillable = ['name', 'email', 'password'];
28
29     /**
30      * The attributes excluded from the model's JSON form.
31      *
32      * @var array
33      */
34     protected $hidden = ['password', 'remember_token'];
35
36     /**
37      * Returns a default guest user.
38      */
39     public static function getDefault()
40     {
41         return new static([
42             'email' => 'guest',
43             'name'  => 'Guest'
44         ]);
45     }
46
47     /**
48      * Permissions and roles
49      */
50
51     /**
52      * The roles that belong to the user.
53      */
54     public function roles()
55     {
56         return $this->belongsToMany('BookStack\Role');
57     }
58
59     public function getRoleAttribute()
60     {
61         return $this->roles()->first();
62     }
63
64     /**
65      * Check if the user has a particular permission.
66      * @param $permissionName
67      * @return bool
68      */
69     public function can($permissionName)
70     {
71         if($this->email == 'guest') {
72             return false;
73         }
74         $permissions = $this->role->permissions()->get();
75         $permissionSearch = $permissions->search(function ($item, $key) use ($permissionName) {
76             return $item->name == $permissionName;
77         });
78         return $permissionSearch !== false;
79     }
80
81     /**
82      * Attach a role to this user.
83      * @param Role $role
84      */
85     public function attachRole(Role $role)
86     {
87         $this->attachRoleId($role->id);
88     }
89
90     /**
91      * Attach a role id to this user.
92      * @param $id
93      */
94     public function attachRoleId($id)
95     {
96         $this->roles()->sync([$id]);
97     }
98
99     /**
100      * Get the social account associated with this user.
101      *
102      * @return \Illuminate\Database\Eloquent\Relations\HasMany
103      */
104     public function socialAccounts()
105     {
106         return $this->hasMany('BookStack\SocialAccount');
107     }
108
109     /**
110      * Check if the user has a social account,
111      * If a driver is passed it checks for that single account type.
112      * @param bool|string $socialDriver
113      * @return bool
114      */
115     public function hasSocialAccount($socialDriver = false)
116     {
117         if($socialDriver === false) {
118             return $this->socialAccounts()->count() > 0;
119         }
120
121         return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
122     }
123
124     /**
125      * Returns the user's avatar,
126      * Uses Gravatar as the avatar service.
127      *
128      * @param int $size
129      * @return string
130      */
131     public function getAvatar($size = 50)
132     {
133         $emailHash = md5(strtolower(trim($this->email)));
134         return '//www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
135     }
136
137     /**
138      * Get the url for editing this user.
139      * @return string
140      */
141     public function getEditUrl()
142     {
143         return '/users/' . $this->id;
144     }
145 }