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.
82 * @param boolean $verifyEmail
85 public function registerNew(array $data, $verifyEmail = false)
87 $user = $this->create($data, $verifyEmail);
88 $this->attachDefaultRole($user);
90 // Get avatar from gravatar and save
91 $this->downloadGravatarToUserAvatar($user);
97 * Give a user the default role. Used when creating a new user.
100 public function attachDefaultRole($user)
102 $roleId = setting('registration-role');
103 if ($roleId === false) {
104 $roleId = $this->role->first()->id;
106 $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.
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
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.
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.
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.
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.
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');