]> BookStack Code Mirror - bookstack/blob - app/User.php
Added basic system tests for markdown editor, Added extra test helpers
[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      * @var string
18      */
19     protected $table = 'users';
20
21     /**
22      * The attributes that are mass assignable.
23      * @var array
24      */
25     protected $fillable = ['name', 'email', 'image_id'];
26
27     /**
28      * The attributes excluded from the model's JSON form.
29      * @var array
30      */
31     protected $hidden = ['password', 'remember_token'];
32
33     /**
34      * This holds the user's permissions when loaded.
35      * @var array
36      */
37     protected $permissions;
38
39     /**
40      * Returns a default guest user.
41      */
42     public static function getDefault()
43     {
44         return new static([
45             'email' => 'guest',
46             'name' => 'Guest'
47         ]);
48     }
49
50     /**
51      * The roles that belong to the user.
52      */
53     public function roles()
54     {
55         return $this->belongsToMany('BookStack\Role');
56     }
57
58     /**
59      * Check if the user has a role.
60      * @param $role
61      * @return mixed
62      */
63     public function hasRole($role)
64     {
65         return $this->roles->pluck('name')->contains($role);
66     }
67
68     /**
69      * Get all permissions belonging to a the current user.
70      * @param bool $cache
71      * @return \Illuminate\Database\Eloquent\Relations\HasManyThrough
72      */
73     public function permissions($cache = true)
74     {
75         if(isset($this->permissions) && $cache) return $this->permissions;
76         $this->load('roles.permissions');
77         $permissions = $this->roles->map(function($role) {
78             return $role->permissions;
79         })->flatten()->unique();
80         $this->permissions = $permissions;
81         return $permissions;
82     }
83
84     /**
85      * Check if the user has a particular permission.
86      * @param $permissionName
87      * @return bool
88      */
89     public function can($permissionName)
90     {
91         if ($this->email === 'guest') return false;
92         return $this->permissions()->pluck('name')->contains($permissionName);
93     }
94
95     /**
96      * Attach a role to this user.
97      * @param Role $role
98      */
99     public function attachRole(Role $role)
100     {
101         $this->attachRoleId($role->id);
102     }
103
104     /**
105      * Attach a role id to this user.
106      * @param $id
107      */
108     public function attachRoleId($id)
109     {
110         $this->roles()->attach($id);
111     }
112
113     /**
114      * Get the social account associated with this user.
115      * @return \Illuminate\Database\Eloquent\Relations\HasMany
116      */
117     public function socialAccounts()
118     {
119         return $this->hasMany('BookStack\SocialAccount');
120     }
121
122     /**
123      * Check if the user has a social account,
124      * If a driver is passed it checks for that single account type.
125      * @param bool|string $socialDriver
126      * @return bool
127      */
128     public function hasSocialAccount($socialDriver = false)
129     {
130         if ($socialDriver === false) {
131             return $this->socialAccounts()->count() > 0;
132         }
133
134         return $this->socialAccounts()->where('driver', '=', $socialDriver)->exists();
135     }
136
137     /**
138      * Returns the user's avatar,
139      * @param int $size
140      * @return string
141      */
142     public function getAvatar($size = 50)
143     {
144         if ($this->image_id === 0 || $this->image_id === '0' || $this->image_id === null) return '/user_avatar.png';
145         return $this->avatar->getThumb($size, $size, false);
146     }
147
148     /**
149      * Get the avatar for the user.
150      * @return \Illuminate\Database\Eloquent\Relations\BelongsTo
151      */
152     public function avatar()
153     {
154         return $this->belongsTo('BookStack\Image', 'image_id');
155     }
156
157     /**
158      * Get the url for editing this user.
159      * @return string
160      */
161     public function getEditUrl()
162     {
163         return '/settings/users/' . $this->id;
164     }
165 }