1 <?php namespace BookStack\Auth;
4 use BookStack\Entities\Repos\EntityRepo;
5 use BookStack\Exceptions\NotFoundException;
6 use BookStack\Uploads\Image;
15 protected $entityRepo;
18 * UserRepo constructor.
21 * @param EntityRepo $entityRepo
23 public function __construct(User $user, Role $role, EntityRepo $entityRepo)
27 $this->entityRepo = $entityRepo;
31 * @param string $email
34 public function getByEmail($email)
36 return $this->user->where('email', '=', $email)->first();
43 public function getById($id)
45 return $this->user->findOrFail($id);
49 * Get all the users with their permissions.
50 * @return \Illuminate\Database\Eloquent\Builder|static
52 public function getAllUsers()
54 return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get();
58 * Get all the users with their permissions in a paginated format.
61 * @return \Illuminate\Database\Eloquent\Builder|static
63 public function getAllUsersPaginatedAndSorted($count, $sortData)
65 $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
67 if ($sortData['search']) {
68 $term = '%' . $sortData['search'] . '%';
69 $query->where(function ($query) use ($term) {
70 $query->where('name', 'like', $term)
71 ->orWhere('email', 'like', $term);
75 return $query->paginate($count);
79 * Creates a new user and attaches a role to them.
81 * @param boolean $verifyEmail
82 * @return \BookStack\Auth\User
84 public function registerNew(array $data, $verifyEmail = false)
86 $user = $this->create($data, $verifyEmail);
87 $this->attachDefaultRole($user);
89 // Get avatar from gravatar and save
90 $this->downloadGravatarToUserAvatar($user);
96 * Give a user the default role. Used when creating a new user.
99 public function attachDefaultRole(User $user)
101 $roleId = setting('registration-role');
102 if ($roleId !== false && $user->roles()->where('id', '=', $roleId)->count() === 0) {
103 $user->attachRoleId($roleId);
108 * Assign a user to a system-level role.
110 * @param $systemRoleName
111 * @throws NotFoundException
113 public function attachSystemRole(User $user, $systemRoleName)
115 $role = $this->role->newQuery()->where('system_name', '=', $systemRoleName)->first();
116 if ($role === null) {
117 throw new NotFoundException("Role '{$systemRoleName}' not found");
119 $user->attachRole($role);
123 * Checks if the give user is the only admin.
124 * @param \BookStack\Auth\User $user
127 public function isOnlyAdmin(User $user)
129 if (!$user->hasSystemRole('admin')) {
133 $adminRole = $this->role->getSystemRole('admin');
134 if ($adminRole->users->count() > 1) {
141 * Create a new basic instance of user.
143 * @param boolean $verifyEmail
144 * @return \BookStack\Auth\User
146 public function create(array $data, $verifyEmail = false)
149 return $this->user->forceCreate([
150 'name' => $data['name'],
151 'email' => $data['email'],
152 'password' => bcrypt($data['password']),
153 'email_confirmed' => $verifyEmail
158 * Remove the given user from storage, Delete all related content.
159 * @param \BookStack\Auth\User $user
162 public function destroy(User $user)
164 $user->socialAccounts()->delete();
167 // Delete user profile images
168 $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
169 foreach ($profileImages as $image) {
170 Images::destroy($image);
175 * Get the latest activity for a user.
176 * @param \BookStack\Auth\User $user
181 public function getActivity(User $user, $count = 20, $page = 0)
183 return Activity::userActivity($user, $count, $page);
187 * Get the recently created content for this given user.
188 * @param \BookStack\Auth\User $user
192 public function getRecentlyCreated(User $user, $count = 20)
195 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
196 $query->where('created_by', '=', $user->id);
198 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
199 $query->where('created_by', '=', $user->id);
201 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
202 $query->where('created_by', '=', $user->id);
208 * Get asset created counts for the give user.
209 * @param \BookStack\Auth\User $user
212 public function getAssetCounts(User $user)
215 'pages' => $this->entityRepo->getUserTotalCreated('page', $user),
216 'chapters' => $this->entityRepo->getUserTotalCreated('chapter', $user),
217 'books' => $this->entityRepo->getUserTotalCreated('book', $user),
222 * Get the roles in the system that are assignable to a user.
225 public function getAllRoles()
227 return $this->role->all();
231 * Get all the roles which can be given restricted access to
232 * other entities in the system.
235 public function getRestrictableRoles()
237 return $this->role->where('system_name', '!=', 'admin')->get();
241 * Get a gravatar image for a user and set it as their avatar.
242 * Does not run if gravatar disabled in config.
246 public function downloadGravatarToUserAvatar(User $user)
248 // Get avatar from gravatar and save
249 if (!config('services.gravatar')) {
254 $avatar = Images::saveUserGravatar($user);
255 $user->avatar()->associate($avatar);
258 } catch (Exception $e) {
259 \Log::error('Failed to save user gravatar image');