1 <?php namespace BookStack\Auth;
4 use BookStack\Entities\EntityRepo;
5 use BookStack\Exceptions\NotFoundException;
6 use BookStack\Uploads\Image;
7 use BookStack\Auth\Role;
8 use BookStack\Auth\User;
17 protected $entityRepo;
20 * UserRepo constructor.
23 * @param EntityRepo $entityRepo
25 public function __construct(User $user, Role $role, EntityRepo $entityRepo)
29 $this->entityRepo = $entityRepo;
33 * @param string $email
36 public function getByEmail($email)
38 return $this->user->where('email', '=', $email)->first();
45 public function getById($id)
47 return $this->user->findOrFail($id);
51 * Get all the users with their permissions.
52 * @return \Illuminate\Database\Eloquent\Builder|static
54 public function getAllUsers()
56 return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get();
60 * Get all the users with their permissions in a paginated format.
63 * @return \Illuminate\Database\Eloquent\Builder|static
65 public function getAllUsersPaginatedAndSorted($count, $sortData)
67 $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
69 if ($sortData['search']) {
70 $term = '%' . $sortData['search'] . '%';
71 $query->where(function ($query) use ($term) {
72 $query->where('name', 'like', $term)
73 ->orWhere('email', 'like', $term);
77 return $query->paginate($count);
81 * Creates a new user and attaches a role to them.
83 * @param boolean $verifyEmail
84 * @return \BookStack\Auth\User
86 public function registerNew(array $data, $verifyEmail = false)
88 $user = $this->create($data, $verifyEmail);
89 $this->attachDefaultRole($user);
91 // Get avatar from gravatar and save
92 $this->downloadGravatarToUserAvatar($user);
98 * Give a user the default role. Used when creating a new user.
101 public function attachDefaultRole(User $user)
103 $roleId = setting('registration-role');
104 if ($roleId !== false && $user->roles()->where('id', '=', $roleId)->count() === 0) {
105 $user->attachRoleId($roleId);
110 * Assign a user to a system-level role.
112 * @param $systemRoleName
113 * @throws NotFoundException
115 public function attachSystemRole(User $user, $systemRoleName)
117 $role = $this->role->newQuery()->where('system_name', '=', $systemRoleName)->first();
118 if ($role === null) {
119 throw new NotFoundException("Role '{$systemRoleName}' not found");
121 $user->attachRole($role);
125 * Checks if the give user is the only admin.
126 * @param \BookStack\Auth\User $user
129 public function isOnlyAdmin(User $user)
131 if (!$user->hasSystemRole('admin')) {
135 $adminRole = $this->role->getSystemRole('admin');
136 if ($adminRole->users->count() > 1) {
143 * Create a new basic instance of user.
145 * @param boolean $verifyEmail
146 * @return \BookStack\Auth\User
148 public function create(array $data, $verifyEmail = false)
151 return $this->user->forceCreate([
152 'name' => $data['name'],
153 'email' => $data['email'],
154 'password' => bcrypt($data['password']),
155 'email_confirmed' => $verifyEmail
160 * Remove the given user from storage, Delete all related content.
161 * @param \BookStack\Auth\User $user
164 public function destroy(User $user)
166 $user->socialAccounts()->delete();
169 // Delete user profile images
170 $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
171 foreach ($profileImages as $image) {
172 Images::destroy($image);
177 * Get the latest activity for a user.
178 * @param \BookStack\Auth\User $user
183 public function getActivity(User $user, $count = 20, $page = 0)
185 return Activity::userActivity($user, $count, $page);
189 * Get the recently created content for this given user.
190 * @param \BookStack\Auth\User $user
194 public function getRecentlyCreated(User $user, $count = 20)
197 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
198 $query->where('created_by', '=', $user->id);
200 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
201 $query->where('created_by', '=', $user->id);
203 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
204 $query->where('created_by', '=', $user->id);
210 * Get asset created counts for the give user.
211 * @param \BookStack\Auth\User $user
214 public function getAssetCounts(User $user)
217 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
218 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
219 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
224 * Get the roles in the system that are assignable to a user.
227 public function getAllRoles()
229 return $this->role->all();
233 * Get all the roles which can be given restricted access to
234 * other entities in the system.
237 public function getRestrictableRoles()
239 return $this->role->where('system_name', '!=', 'admin')->get();
243 * Get a gravatar image for a user and set it as their avatar.
244 * Does not run if gravatar disabled in config.
248 public function downloadGravatarToUserAvatar(User $user)
250 // Get avatar from gravatar and save
251 if (!config('services.gravatar')) {
256 $avatar = Images::saveUserGravatar($user);
257 $user->avatar()->associate($avatar);
260 } catch (Exception $e) {
261 \Log::error('Failed to save user gravatar image');