]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Merge branch 'custom_role_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      * 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::get('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         ]);
111     }
112
113     /**
114      * Remove the given user from storage, Delete all related content.
115      * @param User $user
116      */
117     public function destroy(User $user)
118     {
119         $user->socialAccounts()->delete();
120         $user->delete();
121     }
122
123     /**
124      * Get the latest activity for a user.
125      * @param User $user
126      * @param int $count
127      * @param int $page
128      * @return array
129      */
130     public function getActivity(User $user, $count = 20, $page = 0)
131     {
132         return \Activity::userActivity($user, $count, $page);
133     }
134
135     /**
136      * Get the recently created content for this given user.
137      * @param User $user
138      * @param int $count
139      * @return mixed
140      */
141     public function getRecentlyCreated(User $user, $count = 20)
142     {
143         return [
144             'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
145                 ->take($count)->get(),
146             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
147                 ->take($count)->get(),
148             'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
149                 ->take($count)->get()
150         ];
151     }
152
153     /**
154      * Get asset created counts for the give user.
155      * @param User $user
156      * @return array
157      */
158     public function getAssetCounts(User $user)
159     {
160         return [
161             'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
162             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
163             'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
164         ];
165     }
166
167     /**
168      * Get all the roles which can be given restricted access to
169      * other entities in the system.
170      * @return mixed
171      */
172     public function getRestrictableRoles()
173     {
174         return $this->role->where('name', '!=', 'admin')->get();
175     }
176
177 }