1 <?php namespace BookStack\Repos;
6 use BookStack\Services\EntityService;
15 protected $entityService;
18 * UserRepo constructor.
21 * @param EntityService $entityService
23 public function __construct(User $user, Role $role, EntityService $entityService)
27 $this->entityService = $entityService;
31 * @param string $email
34 public function getByEmail($email)
36 return $this->user->where('email', '=', $email)->first();
43 public function getById($id)
45 return $this->user->findOrFail($id);
49 * Creates a new user and attaches a role to them.
53 public function registerNew(array $data)
55 $user = $this->create($data);
56 $this->attachDefaultRole($user);
58 // Get avatar from gravatar and save
59 if (!config('services.disable_services')) {
60 $avatar = \Images::saveUserGravatar($user);
61 $user->avatar()->associate($avatar);
69 * Give a user the default role. Used when creating a new user.
72 public function attachDefaultRole($user)
74 $roleId = Setting::get('registration-role');
75 if ($roleId === false) $roleId = $this->role->getDefault()->id;
76 $user->attachRoleId($roleId);
80 * Checks if the give user is the only admin.
84 public function isOnlyAdmin(User $user)
86 if ($user->role->name != 'admin') {
90 $adminRole = $this->role->where('name', '=', 'admin')->first();
91 if (count($adminRole->users) > 1) {
99 * Create a new basic instance of user.
103 public function create(array $data)
105 return $this->user->forceCreate([
106 'name' => $data['name'],
107 'email' => $data['email'],
108 'password' => bcrypt($data['password'])
113 * Remove the given user from storage, Delete all related content.
116 public function destroy(User $user)
118 $user->socialAccounts()->delete();
123 * Get the latest activity for a user.
129 public function getActivity(User $user, $count = 20, $page = 0)
131 return \Activity::userActivity($user, $count, $page);
135 * Get the pages the the given user has created.
141 public function getCreatedPages(User $user, $count = 20, $page = 0)
143 return $this->entityService->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
144 ->skip($page * $count)->take($count)->get();
148 * Get asset created counts for the give user.
151 public function getAssetCounts(User $user)
154 'pages' => $this->entityService->page->where('created_by', '=', $user->id)->count(),
155 'chapters' => $this->entityService->chapter->where('created_by', '=', $user->id)->count(),
156 'books' => $this->entityService->book->where('created_by', '=', $user->id)->count(),