]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Fixed image tests after amends to url system
[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      * Get all the users with their permissions in a paginated format.
56      * @param int $count
57      * @param $sortData
58      * @return \Illuminate\Database\Eloquent\Builder|static
59      */
60     public function getAllUsersPaginatedAndSorted($count = 20, $sortData)
61     {
62         $query = $this->user->with('roles', 'avatar')->orderBy($sortData['sort'], $sortData['order']);
63
64         if ($sortData['search']) {
65             $term = '%' . $sortData['search'] . '%';
66             $query->where(function($query) use ($term) {
67                 $query->where('name', 'like', $term)
68                     ->orWhere('email', 'like', $term);
69             });
70         }
71
72         return $query->paginate($count);
73     }
74
75     /**
76      * Creates a new user and attaches a role to them.
77      * @param array $data
78      * @return User
79      */
80     public function registerNew(array $data)
81     {
82         $user = $this->create($data);
83         $this->attachDefaultRole($user);
84
85         // Get avatar from gravatar and save
86         if (!config('services.disable_services')) {
87             $avatar = \Images::saveUserGravatar($user);
88             $user->avatar()->associate($avatar);
89             $user->save();
90         }
91
92         return $user;
93     }
94
95     /**
96      * Give a user the default role. Used when creating a new user.
97      * @param $user
98      */
99     public function attachDefaultRole($user)
100     {
101         $roleId = setting('registration-role');
102         if ($roleId === false) $roleId = $this->role->first()->id;
103         $user->attachRoleId($roleId);
104     }
105
106     /**
107      * Checks if the give user is the only admin.
108      * @param User $user
109      * @return bool
110      */
111     public function isOnlyAdmin(User $user)
112     {
113         if (!$user->roles->pluck('name')->contains('admin')) return false;
114
115         $adminRole = $this->role->getRole('admin');
116         if ($adminRole->users->count() > 1) return false;
117         return true;
118     }
119
120     /**
121      * Create a new basic instance of user.
122      * @param array $data
123      * @return User
124      */
125     public function create(array $data)
126     {
127         return $this->user->forceCreate([
128             'name'     => $data['name'],
129             'email'    => $data['email'],
130             'password' => bcrypt($data['password']),
131             'email_confirmed' => false
132         ]);
133     }
134
135     /**
136      * Remove the given user from storage, Delete all related content.
137      * @param User $user
138      */
139     public function destroy(User $user)
140     {
141         $user->socialAccounts()->delete();
142         $user->delete();
143     }
144
145     /**
146      * Get the latest activity for a user.
147      * @param User $user
148      * @param int $count
149      * @param int $page
150      * @return array
151      */
152     public function getActivity(User $user, $count = 20, $page = 0)
153     {
154         return \Activity::userActivity($user, $count, $page);
155     }
156
157     /**
158      * Get the recently created content for this given user.
159      * @param User $user
160      * @param int $count
161      * @return mixed
162      */
163     public function getRecentlyCreated(User $user, $count = 20)
164     {
165         return [
166             'pages'    => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) {
167                 $query->where('created_by', '=', $user->id);
168             }),
169             'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) {
170                 $query->where('created_by', '=', $user->id);
171             }),
172             'books'    => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) {
173                 $query->where('created_by', '=', $user->id);
174             })
175         ];
176     }
177
178     /**
179      * Get asset created counts for the give user.
180      * @param User $user
181      * @return array
182      */
183     public function getAssetCounts(User $user)
184     {
185         return [
186             'pages'    => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
187             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
188             'books'    => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
189         ];
190     }
191
192     /**
193      * Get the roles in the system that are assignable to a user.
194      * @return mixed
195      */
196     public function getAssignableRoles()
197     {
198         return $this->role->visible();
199     }
200
201     /**
202      * Get all the roles which can be given restricted access to
203      * other entities in the system.
204      * @return mixed
205      */
206     public function getRestrictableRoles()
207     {
208         return $this->role->where('hidden', '=', false)->where('system_name', '=', '')->get();
209     }
210
211 }