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);
88 $this->downloadAndAssignUserAvatar($user);
94 * Give a user the default role. Used when creating a new user.
97 public function attachDefaultRole(User $user)
99 $roleId = setting('registration-role');
100 if ($roleId !== false && $user->roles()->where('id', '=', $roleId)->count() === 0) {
101 $user->attachRoleId($roleId);
106 * Assign a user to a system-level role.
108 * @param $systemRoleName
109 * @throws NotFoundException
111 public function attachSystemRole(User $user, $systemRoleName)
113 $role = $this->role->newQuery()->where('system_name', '=', $systemRoleName)->first();
114 if ($role === null) {
115 throw new NotFoundException("Role '{$systemRoleName}' not found");
117 $user->attachRole($role);
121 * Checks if the give user is the only admin.
122 * @param \BookStack\Auth\User $user
125 public function isOnlyAdmin(User $user)
127 if (!$user->hasSystemRole('admin')) {
131 $adminRole = $this->role->getSystemRole('admin');
132 if ($adminRole->users->count() > 1) {
139 * Create a new basic instance of user.
141 * @param boolean $verifyEmail
142 * @return \BookStack\Auth\User
144 public function create(array $data, $verifyEmail = false)
147 return $this->user->forceCreate([
148 'name' => $data['name'],
149 'email' => $data['email'],
150 'password' => bcrypt($data['password']),
151 'email_confirmed' => $verifyEmail
156 * Remove the given user from storage, Delete all related content.
157 * @param \BookStack\Auth\User $user
160 public function destroy(User $user)
162 $user->socialAccounts()->delete();
165 // Delete user profile images
166 $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
167 foreach ($profileImages as $image) {
168 Images::destroy($image);
173 * Get the latest activity for a user.
174 * @param \BookStack\Auth\User $user
179 public function getActivity(User $user, $count = 20, $page = 0)
181 return Activity::userActivity($user, $count, $page);
185 * Get the recently created content for this given user.
186 * @param \BookStack\Auth\User $user
190 public function getRecentlyCreated(User $user, $count = 20)
193 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
194 $query->where('created_by', '=', $user->id);
196 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
197 $query->where('created_by', '=', $user->id);
199 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
200 $query->where('created_by', '=', $user->id);
206 * Get asset created counts for the give user.
207 * @param \BookStack\Auth\User $user
210 public function getAssetCounts(User $user)
213 'pages' => $this->entityRepo->getUserTotalCreated('page', $user),
214 'chapters' => $this->entityRepo->getUserTotalCreated('chapter', $user),
215 'books' => $this->entityRepo->getUserTotalCreated('book', $user),
220 * Get the roles in the system that are assignable to a user.
223 public function getAllRoles()
225 return $this->role->all();
229 * Get all the roles which can be given restricted access to
230 * other entities in the system.
233 public function getRestrictableRoles()
235 return $this->role->where('system_name', '!=', 'admin')->get();
239 * Get an avatar image for a user and set it as their avatar.
240 * Returns early if avatars disabled or not set in config.
244 public function downloadAndAssignUserAvatar(User $user)
246 if (!Images::avatarFetchEnabled()) {
251 $avatar = Images::saveUserAvatar($user);
252 $user->avatar()->associate($avatar);
255 } catch (Exception $e) {
256 \Log::error('Failed to save user avatar image');