1 <?php namespace BookStack\Repos;
12 protected $entityRepo;
15 * UserRepo constructor.
18 * @param EntityRepo $entityRepo
20 public function __construct(User $user, Role $role, EntityRepo $entityRepo)
24 $this->entityRepo = $entityRepo;
28 * @param string $email
31 public function getByEmail($email)
33 return $this->user->where('email', '=', $email)->first();
40 public function getById($id)
42 return $this->user->findOrFail($id);
46 * Get all the users with their permissions.
47 * @return \Illuminate\Database\Eloquent\Builder|static
49 public function getAllUsers()
51 return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get();
55 * Get all the users with their permissions in a paginated format.
58 * @return \Illuminate\Database\Eloquent\Builder|static
60 public function getAllUsersPaginatedAndSorted($count = 20, $sortData)
62 $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
64 if ($sortData['search']) {
65 $term = '%' . $sortData['search'] . '%';
66 $query->where(function($query) use ($term) {
67 $query->where('name', 'like', $term)
68 ->orWhere('email', 'like', $term);
72 return $query->paginate($count);
76 * Creates a new user and attaches a role to them.
80 public function registerNew(array $data)
82 $user = $this->create($data);
83 $this->attachDefaultRole($user);
85 // Get avatar from gravatar and save
86 if (!config('services.disable_services')) {
88 $avatar = \Images::saveUserGravatar($user);
89 $user->avatar()->associate($avatar);
91 } catch (Exception $e) {
93 \Log::error('Failed to save user gravatar image');
101 * Give a user the default role. Used when creating a new user.
104 public function attachDefaultRole($user)
106 $roleId = setting('registration-role');
107 if ($roleId === false) $roleId = $this->role->first()->id;
108 $user->attachRoleId($roleId);
112 * Checks if the give user is the only admin.
116 public function isOnlyAdmin(User $user)
118 if (!$user->roles->pluck('name')->contains('admin')) return false;
120 $adminRole = $this->role->getRole('admin');
121 if ($adminRole->users->count() > 1) return false;
126 * Create a new basic instance of user.
130 public function create(array $data)
132 return $this->user->forceCreate([
133 'name' => $data['name'],
134 'email' => $data['email'],
135 'password' => bcrypt($data['password']),
136 'email_confirmed' => false
141 * Remove the given user from storage, Delete all related content.
144 public function destroy(User $user)
146 $user->socialAccounts()->delete();
151 * Get the latest activity for a user.
157 public function getActivity(User $user, $count = 20, $page = 0)
159 return \Activity::userActivity($user, $count, $page);
163 * Get the recently created content for this given user.
168 public function getRecentlyCreated(User $user, $count = 20)
171 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
172 $query->where('created_by', '=', $user->id);
174 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
175 $query->where('created_by', '=', $user->id);
177 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
178 $query->where('created_by', '=', $user->id);
184 * Get asset created counts for the give user.
188 public function getAssetCounts(User $user)
191 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
192 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
193 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
198 * Get the roles in the system that are assignable to a user.
201 public function getAllRoles()
203 return $this->role->all();
207 * Get all the roles which can be given restricted access to
208 * other entities in the system.
211 public function getRestrictableRoles()
213 return $this->role->where('system_name', '!=', 'admin')->get();