]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Update PageRepo.php
[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 Carbon\Carbon;
9 use Setting;
10
11 class UserRepo
12 {
13
14     protected $user;
15     protected $role;
16     protected $entityService;
17
18     /**
19      * UserRepo constructor.
20      * @param User $user
21      * @param Role $role
22      * @param EntityService $entityService
23      */
24     public function __construct(User $user, Role $role, EntityService $entityService)
25     {
26         $this->user = $user;
27         $this->role = $role;
28         $this->entityService = $entityService;
29     }
30
31     /**
32      * @param string $email
33      * @return User|null
34      */
35     public function getByEmail($email)
36     {
37         return $this->user->where('email', '=', $email)->first();
38     }
39
40     /**
41      * @param int $id
42      * @return User
43      */
44     public function getById($id)
45     {
46         return $this->user->findOrFail($id);
47     }
48
49     /**
50      * Creates a new user and attaches a role to them.
51      * @param array $data
52      * @return User
53      */
54     public function registerNew(array $data)
55     {
56         $user = $this->create($data);
57         $this->attachDefaultRole($user);
58
59         // Get avatar from gravatar and save
60         if (!config('services.disable_services')) {
61             $avatar = \Images::saveUserGravatar($user);
62             $user->avatar()->associate($avatar);
63             $user->save();
64         }
65
66         return $user;
67     }
68
69     /**
70      * Give a user the default role. Used when creating a new user.
71      * @param $user
72      */
73     public function attachDefaultRole($user)
74     {
75         $roleId = Setting::get('registration-role');
76         if ($roleId === false) $roleId = $this->role->getDefault()->id;
77         $user->attachRoleId($roleId);
78     }
79
80     /**
81      * Checks if the give user is the only admin.
82      * @param User $user
83      * @return bool
84      */
85     public function isOnlyAdmin(User $user)
86     {
87         if ($user->role->name != 'admin') {
88             return false;
89         }
90
91         $adminRole = $this->role->where('name', '=', 'admin')->first();
92         if (count($adminRole->users) > 1) {
93             return false;
94         }
95
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->entityService->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
145                 ->take($count)->get(),
146             'chapters' => $this->entityService->chapter->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
147                 ->take($count)->get(),
148             'books' => $this->entityService->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->entityService->page->where('created_by', '=', $user->id)->count(),
162             'chapters' => $this->entityService->chapter->where('created_by', '=', $user->id)->count(),
163             'books' => $this->entityService->book->where('created_by', '=', $user->id)->count(),
164         ];
165     }
166
167 }