1 <?php namespace BookStack\Repos;
13 protected $entityRepo;
16 * UserRepo constructor.
19 * @param EntityRepo $entityRepo
21 public function __construct(User $user, Role $role, EntityRepo $entityRepo)
25 $this->entityRepo = $entityRepo;
29 * @param string $email
32 public function getByEmail($email)
34 return $this->user->where('email', '=', $email)->first();
41 public function getById($id)
43 return $this->user->findOrFail($id);
47 * Get all the users with their permissions.
48 * @return \Illuminate\Database\Eloquent\Builder|static
50 public function getAllUsers()
52 return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get();
56 * Get all the users with their permissions in a paginated format.
59 * @return \Illuminate\Database\Eloquent\Builder|static
61 public function getAllUsersPaginatedAndSorted($count = 20, $sortData)
63 $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
65 if ($sortData['search']) {
66 $term = '%' . $sortData['search'] . '%';
67 $query->where(function($query) use ($term) {
68 $query->where('name', 'like', $term)
69 ->orWhere('email', 'like', $term);
73 return $query->paginate($count);
77 * Creates a new user and attaches a role to them.
81 public function registerNew(array $data)
83 $user = $this->create($data);
84 $this->attachDefaultRole($user);
86 // Get avatar from gravatar and save
87 if (!config('services.disable_services')) {
89 $avatar = \Images::saveUserGravatar($user);
90 $user->avatar()->associate($avatar);
92 } catch (Exception $e) {
94 \Log::error('Failed to save user gravatar image');
102 * Give a user the default role. Used when creating a new user.
105 public function attachDefaultRole($user)
107 $roleId = setting('registration-role');
108 if ($roleId === false) $roleId = $this->role->first()->id;
109 $user->attachRoleId($roleId);
113 * Checks if the give user is the only admin.
117 public function isOnlyAdmin(User $user)
119 if (!$user->roles->pluck('name')->contains('admin')) return false;
121 $adminRole = $this->role->getRole('admin');
122 if ($adminRole->users->count() > 1) return false;
127 * Create a new basic instance of user.
131 public function create(array $data)
133 return $this->user->forceCreate([
134 'name' => $data['name'],
135 'email' => $data['email'],
136 'password' => bcrypt($data['password']),
137 'email_confirmed' => false
142 * Remove the given user from storage, Delete all related content.
145 public function destroy(User $user)
147 $user->socialAccounts()->delete();
152 * Get the latest activity for a user.
158 public function getActivity(User $user, $count = 20, $page = 0)
160 return \Activity::userActivity($user, $count, $page);
164 * Get the recently created content for this given user.
169 public function getRecentlyCreated(User $user, $count = 20)
172 'pages' => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) {
173 $query->where('created_by', '=', $user->id);
175 'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) {
176 $query->where('created_by', '=', $user->id);
178 'books' => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) {
179 $query->where('created_by', '=', $user->id);
185 * Get asset created counts for the give user.
189 public function getAssetCounts(User $user)
192 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
193 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
194 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
199 * Get the roles in the system that are assignable to a user.
202 public function getAllRoles()
204 return $this->role->all();
208 * Get all the roles which can be given restricted access to
209 * other entities in the system.
212 public function getRestrictableRoles()
214 return $this->role->where('system_name', '!=', 'admin')->get();