]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Got registration process working with social accounts
[bookstack] / app / Repos / UserRepo.php
1 <?php namespace Oxbow\Repos;
2
3
4 use Oxbow\Role;
5 use Oxbow\User;
6
7 class UserRepo
8 {
9
10     protected $user;
11     protected $role;
12
13     /**
14      * UserRepo constructor.
15      * @param $user
16      */
17     public function __construct(User $user, Role $role)
18     {
19         $this->user = $user;
20         $this->role = $role;
21     }
22
23     /**
24      * @param string $email
25      * @return User|null
26      */
27     public function getByEmail($email)
28     {
29         return $this->user->where('email', '=', $email)->first();
30     }
31
32     /**
33      * @param int $id
34      * @return User
35      */
36     public function getById($id)
37     {
38         return $this->user->findOrFail($id);
39     }
40
41     /**
42      * Creates a new user and attaches a role to them.
43      * @param array $data
44      * @return User
45      */
46     public function registerNew(array $data)
47     {
48         $user = $this->create($data);
49         $roleId = \Setting::get('registration-role');
50
51         if ($roleId === false) {
52             $roleId = $this->role->getDefault()->id;
53         }
54
55         $user->attachRoleId($roleId);
56         return $user;
57     }
58
59     /**
60      * Checks if the give user is the only admin.
61      * @param User $user
62      * @return bool
63      */
64     public function isOnlyAdmin(User $user)
65     {
66         if ($user->role->name != 'admin') {
67             return false;
68         }
69
70         $adminRole = $this->role->where('name', '=', 'admin')->first();
71         if (count($adminRole->users) > 1) {
72             return false;
73         }
74
75         return true;
76     }
77
78     /**
79      * Create a new basic instance of user.
80      * @param array $data
81      * @return User
82      */
83     public function create(array $data)
84     {
85         return $this->user->create([
86             'name'     => $data['name'],
87             'email'    => $data['email'],
88             'password' => bcrypt($data['password'])
89         ]);
90     }
91 }