]> BookStack Code Mirror - bookstack/blob - app/User.php
Added indexes, Reduced queries on pages
[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      * This holds the user's permissions when loaded.
38      * @var array
39      */
40     protected $permissions;
41
42     /**
43      * Returns a default guest user.
44      */
45     public static function getDefault()
46     {
47         return new static([
48             'email' => 'guest',
49             'name' => 'Guest'
50         ]);
51     }
52
53     /**
54      * Permissions and roles
55      */
56
57     /**
58      * The roles that belong to the user.
59      */
60     public function roles()
61     {
62         return $this->belongsToMany('BookStack\Role');
63     }
64
65     public function getRoleAttribute()
66     {
67         return $this->roles()->with('permissions')->first();
68     }
69
70     /**
71      * Loads the user's permissions from thier role.
72      */
73     private function loadPermissions()
74     {
75         if (isset($this->permissions)) return;
76         $this->load('roles.permissions');
77         $permissions = $this->roles[0]->permissions;
78         $permissionsArray = $permissions->pluck('name')->all();
79         $this->permissions = $permissionsArray;
80     }
81
82     /**
83      * Check if the user has a particular permission.
84      * @param $permissionName
85      * @return bool
86      */
87     public function can($permissionName)
88     {
89         if ($this->email == 'guest') {
90             return false;
91         }
92         $this->loadPermissions();
93         return array_search($permissionName, $this->permissions) !== false;
94     }
95
96     /**
97      * Attach a role to this user.
98      * @param Role $role
99      */
100     public function attachRole(Role $role)
101     {
102         $this->attachRoleId($role->id);
103     }
104
105     /**
106      * Attach a role id to this user.
107      * @param $id
108      */
109     public function attachRoleId($id)
110     {
111         $this->roles()->sync([$id]);
112     }
113
114     /**
115      * Get the social account associated with this user.
116      *
117      * @return \Illuminate\Database\Eloquent\Relations\HasMany
118      */
119     public function socialAccounts()
120     {
121         return $this->hasMany('BookStack\SocialAccount');
122     }
123
124     /**
125      * Check if the user has a social account,
126      * If a driver is passed it checks for that single account type.
127      * @param bool|string $socialDriver
128      * @return bool
129      */
130     public function hasSocialAccount($socialDriver = false)
131     {
132         if ($socialDriver === false) {
133             return $this->socialAccounts()->count() > 0;
134         }
135
136         return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
137     }
138
139     /**
140      * Returns the user's avatar,
141      * Uses Gravatar as the avatar service.
142      *
143      * @param int $size
144      * @return string
145      */
146     public function getAvatar($size = 50)
147     {
148         $emailHash = md5(strtolower(trim($this->email)));
149         return '//www.gravatar.com/avatar/' . $emailHash . '?s=' . $size . '&d=identicon';
150     }
151
152     /**
153      * Get the url for editing this user.
154      * @return string
155      */
156     public function getEditUrl()
157     {
158         return '/users/' . $this->id;
159     }
160 }