]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Made email confirmations work with LDAP auth
[bookstack] / app / Repos / UserRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Role;
4 use BookStack\User;
5 use Setting;
6
7 class UserRepo
8 {
9
10     protected $user;
11     protected $role;
12     protected $entityRepo;
13
14     /**
15      * UserRepo constructor.
16      * @param User $user
17      * @param Role $role
18      * @param EntityRepo $entityRepo
19      */
20     public function __construct(User $user, Role $role, EntityRepo $entityRepo)
21     {
22         $this->user = $user;
23         $this->role = $role;
24         $this->entityRepo = $entityRepo;
25     }
26
27     /**
28      * @param string $email
29      * @return User|null
30      */
31     public function getByEmail($email)
32     {
33         return $this->user->where('email', '=', $email)->first();
34     }
35
36     /**
37      * @param int $id
38      * @return User
39      */
40     public function getById($id)
41     {
42         return $this->user->findOrFail($id);
43     }
44
45     /**
46      * Get all the users with their permissions.
47      * @return \Illuminate\Database\Eloquent\Builder|static
48      */
49     public function getAllUsers()
50     {
51         return $this->user->with('roles', 'avatar')->orderBy('name', 'asc')->get();
52     }
53
54     /**
55      * Creates a new user and attaches a role to them.
56      * @param array $data
57      * @return User
58      */
59     public function registerNew(array $data)
60     {
61         $user = $this->create($data);
62         $this->attachDefaultRole($user);
63
64         // Get avatar from gravatar and save
65         if (!config('services.disable_services')) {
66             $avatar = \Images::saveUserGravatar($user);
67             $user->avatar()->associate($avatar);
68             $user->save();
69         }
70
71         return $user;
72     }
73
74     /**
75      * Give a user the default role. Used when creating a new user.
76      * @param $user
77      */
78     public function attachDefaultRole($user)
79     {
80         $roleId = setting('registration-role');
81         if ($roleId === false) $roleId = $this->role->first()->id;
82         $user->attachRoleId($roleId);
83     }
84
85     /**
86      * Checks if the give user is the only admin.
87      * @param User $user
88      * @return bool
89      */
90     public function isOnlyAdmin(User $user)
91     {
92         if (!$user->roles->pluck('name')->contains('admin')) return false;
93
94         $adminRole = $this->role->getRole('admin');
95         if ($adminRole->users->count() > 1) return false;
96         return true;
97     }
98
99     /**
100      * Create a new basic instance of user.
101      * @param array $data
102      * @return User
103      */
104     public function create(array $data)
105     {
106         return $this->user->forceCreate([
107             'name'     => $data['name'],
108             'email'    => $data['email'],
109             'password' => bcrypt($data['password']),
110             'email_confirmed' => false
111         ]);
112     }
113
114     /**
115      * Remove the given user from storage, Delete all related content.
116      * @param User $user
117      */
118     public function destroy(User $user)
119     {
120         $user->socialAccounts()->delete();
121         $user->delete();
122     }
123
124     /**
125      * Get the latest activity for a user.
126      * @param User $user
127      * @param int $count
128      * @param int $page
129      * @return array
130      */
131     public function getActivity(User $user, $count = 20, $page = 0)
132     {
133         return \Activity::userActivity($user, $count, $page);
134     }
135
136     /**
137      * Get the recently created content for this given user.
138      * @param User $user
139      * @param int $count
140      * @return mixed
141      */
142     public function getRecentlyCreated(User $user, $count = 20)
143     {
144         return [
145             'pages'    => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) {
146                 $query->where('created_by', '=', $user->id);
147             }),
148             'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) {
149                 $query->where('created_by', '=', $user->id);
150             }),
151             'books'    => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) {
152                 $query->where('created_by', '=', $user->id);
153             })
154         ];
155     }
156
157     /**
158      * Get asset created counts for the give user.
159      * @param User $user
160      * @return array
161      */
162     public function getAssetCounts(User $user)
163     {
164         return [
165             'pages'    => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
166             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
167             'books'    => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
168         ];
169     }
170
171     /**
172      * Get all the roles which can be given restricted access to
173      * other entities in the system.
174      * @return mixed
175      */
176     public function getRestrictableRoles()
177     {
178         return $this->role->where('name', '!=', 'admin')->get();
179     }
180
181 }