]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Increased LDAP testing and fixed any Auth-based bugs found
[bookstack] / app / Repos / UserRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use BookStack\Role;
5 use BookStack\User;
6 use Setting;
7
8 class UserRepo
9 {
10
11     protected $user;
12     protected $role;
13
14     /**
15      * UserRepo constructor.
16      * @param $user
17      */
18     public function __construct(User $user, Role $role)
19     {
20         $this->user = $user;
21         $this->role = $role;
22     }
23
24     /**
25      * @param string $email
26      * @return User|null
27      */
28     public function getByEmail($email)
29     {
30         return $this->user->where('email', '=', $email)->first();
31     }
32
33     /**
34      * @param int $id
35      * @return User
36      */
37     public function getById($id)
38     {
39         return $this->user->findOrFail($id);
40     }
41
42     /**
43      * Creates a new user and attaches a role to them.
44      * @param array $data
45      * @return User
46      */
47     public function registerNew(array $data)
48     {
49         $user = $this->create($data);
50         $this->attachDefaultRole($user);
51
52         // Get avatar from gravatar and save
53         if (!config('services.disable_services')) {
54             $avatar = \Images::saveUserGravatar($user);
55             $user->avatar()->associate($avatar);
56             $user->save();
57         }
58
59         return $user;
60     }
61
62     /**
63      * Give a user the default role. Used when creating a new user.
64      * @param $user
65      */
66     public function attachDefaultRole($user)
67     {
68         $roleId = Setting::get('registration-role');
69         if ($roleId === false) $roleId = $this->role->getDefault()->id;
70         $user->attachRoleId($roleId);
71     }
72
73     /**
74      * Checks if the give user is the only admin.
75      * @param User $user
76      * @return bool
77      */
78     public function isOnlyAdmin(User $user)
79     {
80         if ($user->role->name != 'admin') {
81             return false;
82         }
83
84         $adminRole = $this->role->where('name', '=', 'admin')->first();
85         if (count($adminRole->users) > 1) {
86             return false;
87         }
88
89         return true;
90     }
91
92     /**
93      * Create a new basic instance of user.
94      * @param array $data
95      * @return User
96      */
97     public function create(array $data)
98     {
99         return $this->user->forceCreate([
100             'name'     => $data['name'],
101             'email'    => $data['email'],
102             'password' => bcrypt($data['password'])
103         ]);
104     }
105
106     /**
107      * Remove the given user from storage, Delete all related content.
108      * @param User $user
109      */
110     public function destroy(User $user)
111     {
112         $user->socialAccounts()->delete();
113         $user->delete();
114     }
115 }