1 <?php namespace BookStack\Repos;
6 use BookStack\Services\EntityService;
16 protected $entityService;
19 * UserRepo constructor.
22 * @param EntityService $entityService
24 public function __construct(User $user, Role $role, EntityService $entityService)
28 $this->entityService = $entityService;
32 * @param string $email
35 public function getByEmail($email)
37 return $this->user->where('email', '=', $email)->first();
44 public function getById($id)
46 return $this->user->findOrFail($id);
50 * Creates a new user and attaches a role to them.
54 public function registerNew(array $data)
56 $user = $this->create($data);
57 $this->attachDefaultRole($user);
59 // Get avatar from gravatar and save
60 if (!config('services.disable_services')) {
61 $avatar = \Images::saveUserGravatar($user);
62 $user->avatar()->associate($avatar);
70 * Give a user the default role. Used when creating a new user.
73 public function attachDefaultRole($user)
75 $roleId = Setting::get('registration-role');
76 if ($roleId === false) $roleId = $this->role->getDefault()->id;
77 $user->attachRoleId($roleId);
81 * Checks if the give user is the only admin.
85 public function isOnlyAdmin(User $user)
87 if ($user->role->name != 'admin') {
91 $adminRole = $this->role->where('name', '=', 'admin')->first();
92 if (count($adminRole->users) > 1) {
100 * Create a new basic instance of user.
104 public function create(array $data)
106 return $this->user->forceCreate([
107 'name' => $data['name'],
108 'email' => $data['email'],
109 'password' => bcrypt($data['password'])
114 * Remove the given user from storage, Delete all related content.
117 public function destroy(User $user)
119 $user->socialAccounts()->delete();
124 * Get the latest activity for a user.
130 public function getActivity(User $user, $count = 20, $page = 0)
132 return \Activity::userActivity($user, $count, $page);
136 * Get the recently created content for this given user.
141 public function getRecentlyCreated(User $user, $count = 20)
144 'pages' => $this->entityService->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
145 ->take($count)->get(),
146 'chapters' => $this->entityService->chapter->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
147 ->take($count)->get(),
148 'books' => $this->entityService->book->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
149 ->take($count)->get()
154 * Get asset created counts for the give user.
158 public function getAssetCounts(User $user)
161 'pages' => $this->entityService->page->where('created_by', '=', $user->id)->count(),
162 'chapters' => $this->entityService->chapter->where('created_by', '=', $user->id)->count(),
163 'books' => $this->entityService->book->where('created_by', '=', $user->id)->count(),