]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Added basic system tests for markdown editor, Added extra test helpers
[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         ]);
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->getRecentlyCreatedPages($count, 0, function ($query) use ($user) {
145                 $query->where('created_by', '=', $user->id);
146             }),
147             'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) {
148                 $query->where('created_by', '=', $user->id);
149             }),
150             'books'    => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) {
151                 $query->where('created_by', '=', $user->id);
152             })
153         ];
154     }
155
156     /**
157      * Get asset created counts for the give user.
158      * @param User $user
159      * @return array
160      */
161     public function getAssetCounts(User $user)
162     {
163         return [
164             'pages'    => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
165             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
166             'books'    => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
167         ];
168     }
169
170     /**
171      * Get all the roles which can be given restricted access to
172      * other entities in the system.
173      * @return mixed
174      */
175     public function getRestrictableRoles()
176     {
177         return $this->role->where('name', '!=', 'admin')->get();
178     }
179
180 }