1 <?php namespace BookStack\Repos;
4 use BookStack\Exceptions\NotFoundException;
16 protected $entityRepo;
19 * UserRepo constructor.
22 * @param EntityRepo $entityRepo
24 public function __construct(User $user, Role $role, EntityRepo $entityRepo)
28 $this->entityRepo = $entityRepo;
32 * @param string $email
35 public function getByEmail($email)
37 return $this->user->where('email', '=', $email)->first();
44 public function getById($id)
46 return $this->user->findOrFail($id);
50 * Get all the users with their permissions.
51 * @return \Illuminate\Database\Eloquent\Builder|static
53 public function getAllUsers()
55 return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get();
59 * Get all the users with their permissions in a paginated format.
62 * @return \Illuminate\Database\Eloquent\Builder|static
64 public function getAllUsersPaginatedAndSorted($count, $sortData)
66 $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
68 if ($sortData['search']) {
69 $term = '%' . $sortData['search'] . '%';
70 $query->where(function ($query) use ($term) {
71 $query->where('name', 'like', $term)
72 ->orWhere('email', 'like', $term);
76 return $query->paginate($count);
80 * Creates a new user and attaches a role to them.
84 public function registerNew(array $data)
86 $user = $this->create($data);
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)
101 $roleId = setting('registration-role');
102 if ($roleId === false) {
103 $roleId = $this->role->first()->id;
105 $user->attachRoleId($roleId);
109 * Assign a user to a system-level role.
111 * @param $systemRoleName
112 * @throws NotFoundException
114 public function attachSystemRole(User $user, $systemRoleName)
116 $role = $this->role->newQuery()->where('system_name', '=', $systemRoleName)->first();
117 if ($role === null) {
118 throw new NotFoundException("Role '{$systemRoleName}' not found");
120 $user->attachRole($role);
124 * Checks if the give user is the only admin.
128 public function isOnlyAdmin(User $user)
130 if (!$user->hasSystemRole('admin')) {
134 $adminRole = $this->role->getSystemRole('admin');
135 if ($adminRole->users->count() > 1) {
142 * Create a new basic instance of user.
146 public function create(array $data)
148 return $this->user->forceCreate([
149 'name' => $data['name'],
150 'email' => $data['email'],
151 'password' => bcrypt($data['password']),
152 'email_confirmed' => false
157 * Remove the given user from storage, Delete all related content.
161 public function destroy(User $user)
163 $user->socialAccounts()->delete();
166 // Delete user profile images
167 $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
168 foreach ($profileImages as $image) {
169 Images::destroyImage($image);
174 * Get the latest activity for a user.
180 public function getActivity(User $user, $count = 20, $page = 0)
182 return Activity::userActivity($user, $count, $page);
186 * Get the recently created content for this given user.
191 public function getRecentlyCreated(User $user, $count = 20)
194 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
195 $query->where('created_by', '=', $user->id);
197 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
198 $query->where('created_by', '=', $user->id);
200 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
201 $query->where('created_by', '=', $user->id);
207 * Get asset created counts for the give user.
211 public function getAssetCounts(User $user)
214 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
215 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
216 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
221 * Get the roles in the system that are assignable to a user.
224 public function getAllRoles()
226 return $this->role->all();
230 * Get all the roles which can be given restricted access to
231 * other entities in the system.
234 public function getRestrictableRoles()
236 return $this->role->where('system_name', '!=', 'admin')->get();
240 * Get a gravatar image for a user and set it as their avatar.
241 * Does not run if gravatar disabled in config.
245 public function downloadGravatarToUserAvatar(User $user)
247 // Get avatar from gravatar and save
248 if (!config('services.gravatar')) {
253 $avatar = Images::saveUserGravatar($user);
254 $user->avatar()->associate($avatar);
257 } catch (Exception $e) {
258 \Log::error('Failed to save user gravatar image');