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 = 20, $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) $roleId = $this->role->first()->id;
111 $user->attachRoleId($roleId);
115 * Checks if the give user is the only admin.
119 public function isOnlyAdmin(User $user)
121 if (!$user->hasSystemRole('admin')) return false;
123 $adminRole = $this->role->getSystemRole('admin');
124 if ($adminRole->users->count() > 1) return false;
129 * Create a new basic instance of user.
133 public function create(array $data)
135 return $this->user->forceCreate([
136 'name' => $data['name'],
137 'email' => $data['email'],
138 'password' => bcrypt($data['password']),
139 'email_confirmed' => false
144 * Remove the given user from storage, Delete all related content.
148 public function destroy(User $user)
150 $user->socialAccounts()->delete();
153 // Delete user profile images
154 $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
155 foreach ($profileImages as $image) {
156 Images::destroyImage($image);
161 * Get the latest activity for a user.
167 public function getActivity(User $user, $count = 20, $page = 0)
169 return Activity::userActivity($user, $count, $page);
173 * Get the recently created content for this given user.
178 public function getRecentlyCreated(User $user, $count = 20)
181 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
182 $query->where('created_by', '=', $user->id);
184 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
185 $query->where('created_by', '=', $user->id);
187 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
188 $query->where('created_by', '=', $user->id);
194 * Get asset created counts for the give user.
198 public function getAssetCounts(User $user)
201 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
202 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
203 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
208 * Get the roles in the system that are assignable to a user.
211 public function getAllRoles()
213 return $this->role->all();
217 * Get all the roles which can be given restricted access to
218 * other entities in the system.
221 public function getRestrictableRoles()
223 return $this->role->where('system_name', '!=', 'admin')->get();