]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Added Italian language
[bookstack] / app / Repos / UserRepo.php
1 <?php namespace BookStack\Repos;
2
3 use BookStack\Role;
4 use BookStack\User;
5 use Exception;
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             try {
88                 $avatar = \Images::saveUserGravatar($user);
89                 $user->avatar()->associate($avatar);
90                 $user->save();
91             } catch (Exception $e) {
92                 $user->save();
93                 \Log::error('Failed to save user gravatar image');
94             }
95         }
96
97         return $user;
98     }
99
100     /**
101      * Give a user the default role. Used when creating a new user.
102      * @param $user
103      */
104     public function attachDefaultRole($user)
105     {
106         $roleId = setting('registration-role');
107         if ($roleId === false) $roleId = $this->role->first()->id;
108         $user->attachRoleId($roleId);
109     }
110
111     /**
112      * Checks if the give user is the only admin.
113      * @param User $user
114      * @return bool
115      */
116     public function isOnlyAdmin(User $user)
117     {
118         if (!$user->roles->pluck('name')->contains('admin')) return false;
119
120         $adminRole = $this->role->getRole('admin');
121         if ($adminRole->users->count() > 1) return false;
122         return true;
123     }
124
125     /**
126      * Create a new basic instance of user.
127      * @param array $data
128      * @return User
129      */
130     public function create(array $data)
131     {
132         return $this->user->forceCreate([
133             'name'     => $data['name'],
134             'email'    => $data['email'],
135             'password' => bcrypt($data['password']),
136             'email_confirmed' => false
137         ]);
138     }
139
140     /**
141      * Remove the given user from storage, Delete all related content.
142      * @param User $user
143      */
144     public function destroy(User $user)
145     {
146         $user->socialAccounts()->delete();
147         $user->delete();
148     }
149
150     /**
151      * Get the latest activity for a user.
152      * @param User $user
153      * @param int $count
154      * @param int $page
155      * @return array
156      */
157     public function getActivity(User $user, $count = 20, $page = 0)
158     {
159         return \Activity::userActivity($user, $count, $page);
160     }
161
162     /**
163      * Get the recently created content for this given user.
164      * @param User $user
165      * @param int $count
166      * @return mixed
167      */
168     public function getRecentlyCreated(User $user, $count = 20)
169     {
170         return [
171             'pages'    => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
172                 $query->where('created_by', '=', $user->id);
173             }),
174             'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
175                 $query->where('created_by', '=', $user->id);
176             }),
177             'books'    => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
178                 $query->where('created_by', '=', $user->id);
179             })
180         ];
181     }
182
183     /**
184      * Get asset created counts for the give user.
185      * @param User $user
186      * @return array
187      */
188     public function getAssetCounts(User $user)
189     {
190         return [
191             'pages'    => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
192             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
193             'books'    => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
194         ];
195     }
196
197     /**
198      * Get the roles in the system that are assignable to a user.
199      * @return mixed
200      */
201     public function getAllRoles()
202     {
203         return $this->role->all();
204     }
205
206     /**
207      * Get all the roles which can be given restricted access to
208      * other entities in the system.
209      * @return mixed
210      */
211     public function getRestrictableRoles()
212     {
213         return $this->role->where('system_name', '!=', 'admin')->get();
214     }
215
216 }
Morty Proxy This is a proxified and sanitized view of the page, visit original site.