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