1 <?php namespace BookStack\Repos;
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.
83 public function registerNew(array $data)
85 $user = $this->create($data);
86 $this->attachDefaultRole($user);
88 // Get avatar from gravatar and save
89 if (!config('services.disable_services')) {
91 $avatar = Images::saveUserGravatar($user);
92 $user->avatar()->associate($avatar);
94 } catch (Exception $e) {
96 \Log::error('Failed to save user gravatar image');
104 * Give a user the default role. Used when creating a new user.
107 public function attachDefaultRole($user)
109 $roleId = setting('registration-role');
110 if ($roleId === false) {
111 $roleId = $this->role->first()->id;
113 $user->attachRoleId($roleId);
117 * Checks if the give user is the only admin.
121 public function isOnlyAdmin(User $user)
123 if (!$user->hasSystemRole('admin')) {
127 $adminRole = $this->role->getSystemRole('admin');
128 if ($adminRole->users->count() > 1) {
135 * Create a new basic instance of user.
139 public function create(array $data)
141 return $this->user->forceCreate([
142 'name' => $data['name'],
143 'email' => $data['email'],
144 'password' => bcrypt($data['password']),
145 'email_confirmed' => false
150 * Remove the given user from storage, Delete all related content.
154 public function destroy(User $user)
156 $user->socialAccounts()->delete();
159 // Delete user profile images
160 $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
161 foreach ($profileImages as $image) {
162 Images::destroyImage($image);
167 * Get the latest activity for a user.
173 public function getActivity(User $user, $count = 20, $page = 0)
175 return Activity::userActivity($user, $count, $page);
179 * Get the recently created content for this given user.
184 public function getRecentlyCreated(User $user, $count = 20)
187 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
188 $query->where('created_by', '=', $user->id);
190 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
191 $query->where('created_by', '=', $user->id);
193 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
194 $query->where('created_by', '=', $user->id);
200 * Get asset created counts for the give user.
204 public function getAssetCounts(User $user)
207 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
208 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
209 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
214 * Get the roles in the system that are assignable to a user.
217 public function getAllRoles()
219 return $this->role->all();
223 * Get all the roles which can be given restricted access to
224 * other entities in the system.
227 public function getRestrictableRoles()
229 return $this->role->where('system_name', '!=', 'admin')->get();