]> BookStack Code Mirror - bookstack/blob - app/Repos/UserRepo.php
Closes #55. Allows you to set the primary color.
[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      * Creates a new user and attaches a role to them.
47      * @param array $data
48      * @return User
49      */
50     public function registerNew(array $data)
51     {
52         $user = $this->create($data);
53         $this->attachDefaultRole($user);
54
55         // Get avatar from gravatar and save
56         if (!config('services.disable_services')) {
57             $avatar = \Images::saveUserGravatar($user);
58             $user->avatar()->associate($avatar);
59             $user->save();
60         }
61
62         return $user;
63     }
64
65     /**
66      * Give a user the default role. Used when creating a new user.
67      * @param $user
68      */
69     public function attachDefaultRole($user)
70     {
71         $roleId = Setting::get('registration-role');
72         if ($roleId === false) $roleId = $this->role->getDefault()->id;
73         $user->attachRoleId($roleId);
74     }
75
76     /**
77      * Checks if the give user is the only admin.
78      * @param User $user
79      * @return bool
80      */
81     public function isOnlyAdmin(User $user)
82     {
83         if ($user->role->name != 'admin') {
84             return false;
85         }
86
87         $adminRole = $this->role->where('name', '=', 'admin')->first();
88         if (count($adminRole->users) > 1) {
89             return false;
90         }
91
92         return true;
93     }
94
95     /**
96      * Create a new basic instance of user.
97      * @param array $data
98      * @return User
99      */
100     public function create(array $data)
101     {
102         return $this->user->forceCreate([
103             'name'     => $data['name'],
104             'email'    => $data['email'],
105             'password' => bcrypt($data['password'])
106         ]);
107     }
108
109     /**
110      * Remove the given user from storage, Delete all related content.
111      * @param User $user
112      */
113     public function destroy(User $user)
114     {
115         $user->socialAccounts()->delete();
116         $user->delete();
117     }
118
119     /**
120      * Get the latest activity for a user.
121      * @param User $user
122      * @param int $count
123      * @param int $page
124      * @return array
125      */
126     public function getActivity(User $user, $count = 20, $page = 0)
127     {
128         return \Activity::userActivity($user, $count, $page);
129     }
130
131     /**
132      * Get the recently created content for this given user.
133      * @param User $user
134      * @param int $count
135      * @return mixed
136      */
137     public function getRecentlyCreated(User $user, $count = 20)
138     {
139         return [
140             'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
141                 ->take($count)->get(),
142             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
143                 ->take($count)->get(),
144             'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->orderBy('created_at', 'desc')
145                 ->take($count)->get()
146         ];
147     }
148
149     /**
150      * Get asset created counts for the give user.
151      * @param User $user
152      * @return array
153      */
154     public function getAssetCounts(User $user)
155     {
156         return [
157             'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
158             'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
159             'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
160         ];
161     }
162
163 }