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 * Creates a new user and attaches a role to them.
50 public function registerNew(array $data)
52 $user = $this->create($data);
53 $this->attachDefaultRole($user);
55 // Get avatar from gravatar and save
56 if (!config('services.disable_services')) {
57 $avatar = \Images::saveUserGravatar($user);
58 $user->avatar()->associate($avatar);
66 * Give a user the default role. Used when creating a new user.
69 public function attachDefaultRole($user)
71 $roleId = Setting::get('registration-role');
72 if ($roleId === false) $roleId = $this->role->getDefault()->id;
73 $user->attachRoleId($roleId);
77 * Checks if the give user is the only admin.
81 public function isOnlyAdmin(User $user)
83 if ($user->role->name != 'admin') {
87 $adminRole = $this->role->where('name', '=', 'admin')->first();
88 if (count($adminRole->users) > 1) {
96 * Create a new basic instance of user.
100 public function create(array $data)
102 return $this->user->forceCreate([
103 'name' => $data['name'],
104 'email' => $data['email'],
105 'password' => bcrypt($data['password'])
110 * Remove the given user from storage, Delete all related content.
113 public function destroy(User $user)
115 $user->socialAccounts()->delete();
120 * Get the latest activity for a user.
126 public function getActivity(User $user, $count = 20, $page = 0)
128 return \Activity::userActivity($user, $count, $page);
132 * Get the recently created content for this given user.
137 public function getRecentlyCreated(User $user, $count = 20)
140 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
141 ->take($count)->get(),
142 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
143 ->take($count)->get(),
144 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
145 ->take($count)->get()
150 * Get asset created counts for the give user.
154 public function getAssetCounts(User $user)
157 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
158 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
159 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),