1 <?php namespace BookStack\Repos;
15 * UserRepo constructor.
18 public function __construct(User $user, Role $role)
25 * @param string $email
28 public function getByEmail($email)
30 return $this->user->where('email', '=', $email)->first();
37 public function getById($id)
39 return $this->user->findOrFail($id);
43 * Creates a new user and attaches a role to them.
47 public function registerNew(array $data)
49 $user = $this->create($data);
50 $this->attachDefaultRole($user);
52 // Get avatar from gravatar and save
53 if (!config('services.disable_services')) {
54 $avatar = \Images::saveUserGravatar($user);
55 $user->avatar()->associate($avatar);
63 * Give a user the default role. Used when creating a new user.
66 public function attachDefaultRole($user)
68 $roleId = Setting::get('registration-role');
69 if ($roleId === false) $roleId = $this->role->getDefault()->id;
70 $user->attachRoleId($roleId);
74 * Checks if the give user is the only admin.
78 public function isOnlyAdmin(User $user)
80 if ($user->role->name != 'admin') {
84 $adminRole = $this->role->where('name', '=', 'admin')->first();
85 if (count($adminRole->users) > 1) {
93 * Create a new basic instance of user.
97 public function create(array $data)
99 return $this->user->forceCreate([
100 'name' => $data['name'],
101 'email' => $data['email'],
102 'password' => bcrypt($data['password'])
107 * Remove the given user from storage, Delete all related content.
110 public function destroy(User $user)
112 $user->socialAccounts()->delete();