]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
380eb24c98c2f691fc1d3d5cd3b6732c9f1f59f1
[bookstack] / app / Repos / UserRepo.php
1 <?php namespace BookStack\Repos;
2
3
4 use BookStack\Page;
5 use BookStack\Role;
6 use BookStack\Services\EntityService;
7 use BookStack\User;
8 use Setting;
9
10 class UserRepo
11 {
12
13     protected $user;
14     protected $role;
15     protected $entityService;
16
17     /**
18      * UserRepo constructor.
19      * @param User $user
20      * @param Role $role
21      * @param EntityService $entityService
22      */
23     public function __construct(User $user, Role $role, EntityService $entityService)
24     {
25         $this->user = $user;
26         $this->role = $role;
27         $this->entityService = $entityService;
28     }
29
30     /**
31      * @param string $email
32      * @return User|null
33      */
34     public function getByEmail($email)
35     {
36         return $this->user->where('email', '=', $email)->first();
37     }
38
39     /**
40      * @param int $id
41      * @return User
42      */
43     public function getById($id)
44     {
45         return $this->user->findOrFail($id);
46     }
47
48     /**
49      * Creates a new user and attaches a role to them.
50      * @param array $data
51      * @return User
52      */
53     public function registerNew(array $data)
54     {
55         $user = $this->create($data);
56         $this->attachDefaultRole($user);
57
58         // Get avatar from gravatar and save
59         if (!config('services.disable_services')) {
60             $avatar = \Images::saveUserGravatar($user);
61             $user->avatar()->associate($avatar);
62             $user->save();
63         }
64
65         return $user;
66     }
67
68     /**
69      * Give a user the default role. Used when creating a new user.
70      * @param $user
71      */
72     public function attachDefaultRole($user)
73     {
74         $roleId = Setting::get('registration-role');
75         if ($roleId === false) $roleId = $this->role->getDefault()->id;
76         $user->attachRoleId($roleId);
77     }
78
79     /**
80      * Checks if the give user is the only admin.
81      * @param User $user
82      * @return bool
83      */
84     public function isOnlyAdmin(User $user)
85     {
86         if ($user->role->name != 'admin') {
87             return false;
88         }
89
90         $adminRole = $this->role->where('name', '=', 'admin')->first();
91         if (count($adminRole->users) > 1) {
92             return false;
93         }
94
95         return true;
96     }
97
98     /**
99      * Create a new basic instance of user.
100      * @param array $data
101      * @return User
102      */
103     public function create(array $data)
104     {
105         return $this->user->forceCreate([
106             'name'     => $data['name'],
107             'email'    => $data['email'],
108             'password' => bcrypt($data['password'])
109         ]);
110     }
111
112     /**
113      * Remove the given user from storage, Delete all related content.
114      * @param User $user
115      */
116     public function destroy(User $user)
117     {
118         $user->socialAccounts()->delete();
119         $user->delete();
120     }
121
122     /**
123      * Get the latest activity for a user.
124      * @param User $user
125      * @param int $count
126      * @param int $page
127      * @return array
128      */
129     public function getActivity(User $user, $count = 20, $page = 0)
130     {
131         return \Activity::userActivity($user, $count, $page);
132     }
133
134     /**
135      * Get the pages the the given user has created.
136      * @param User $user
137      * @param int $count
138      * @param int $page
139      * @return mixed
140      */
141     public function getCreatedPages(User $user, $count = 20, $page = 0)
142     {
143         return $this->entityService->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
144             ->skip($page * $count)->take($count)->get();
145     }
146
147     /**
148      * Get asset created counts for the give user.
149      * @return array
150      */
151     public function getAssetCounts(User $user)
152     {
153         return [
154             'pages' => $this->entityService->page->where('created_by', '=', $user->id)->count(),
155             'chapters' => $this->entityService->chapter->where('created_by', '=', $user->id)->count(),
156             'books' => $this->entityService->book->where('created_by', '=', $user->id)->count(),
157         ];
158     }
159 }