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')) {
87 $avatar = \Images::saveUserGravatar($user);
88 $user->avatar()->associate($avatar);
96 * Give a user the default role. Used when creating a new user.
99 public function attachDefaultRole($user)
101 $roleId = setting('registration-role');
102 if ($roleId === false) $roleId = $this->role->first()->id;
103 $user->attachRoleId($roleId);
107 * Checks if the give user is the only admin.
111 public function isOnlyAdmin(User $user)
113 if (!$user->roles->pluck('name')->contains('admin')) return false;
115 $adminRole = $this->role->getRole('admin');
116 if ($adminRole->users->count() > 1) return false;
121 * Create a new basic instance of user.
125 public function create(array $data)
127 return $this->user->forceCreate([
128 'name' => $data['name'],
129 'email' => $data['email'],
130 'password' => bcrypt($data['password']),
131 'email_confirmed' => false
136 * Remove the given user from storage, Delete all related content.
139 public function destroy(User $user)
141 $user->socialAccounts()->delete();
146 * Get the latest activity for a user.
152 public function getActivity(User $user, $count = 20, $page = 0)
154 return \Activity::userActivity($user, $count, $page);
158 * Get the recently created content for this given user.
163 public function getRecentlyCreated(User $user, $count = 20)
166 'pages' => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) {
167 $query->where('created_by', '=', $user->id);
169 'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) {
170 $query->where('created_by', '=', $user->id);
172 'books' => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) {
173 $query->where('created_by', '=', $user->id);
179 * Get asset created counts for the give user.
183 public function getAssetCounts(User $user)
186 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
187 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
188 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
193 * Get the roles in the system that are assignable to a user.
196 public function getAssignableRoles()
198 return $this->role->visible();
202 * Get all the roles which can be given restricted access to
203 * other entities in the system.
206 public function getRestrictableRoles()
208 return $this->role->where('hidden', '=', false)->where('system_name', '=', '')->get();