+
+ /**
+ * Remove the given user from storage, Delete all related content.
+ * @param User $user
+ * @throws Exception
+ */
+ public function destroy(User $user)
+ {
+ $user->socialAccounts()->delete();
+ $user->delete();
+
+ // Delete user profile images
+ $profileImages = $images = Image::where('type', '=', 'user')->where('created_by', '=', $user->id)->get();
+ foreach ($profileImages as $image) {
+ Images::destroyImage($image);
+ }
+ }
+
+ /**
+ * Get the latest activity for a user.
+ * @param User $user
+ * @param int $count
+ * @param int $page
+ * @return array
+ */
+ public function getActivity(User $user, $count = 20, $page = 0)
+ {
+ return Activity::userActivity($user, $count, $page);
+ }
+
+ /**
+ * Get the recently created content for this given user.
+ * @param User $user
+ * @param int $count
+ * @return mixed
+ */
+ public function getRecentlyCreated(User $user, $count = 20)
+ {
+ return [
+ 'pages' => $this->entityRepo->getRecentlyCreated('page', $count, 0, function ($query) use ($user) {
+ $query->where('created_by', '=', $user->id);
+ }),
+ 'chapters' => $this->entityRepo->getRecentlyCreated('chapter', $count, 0, function ($query) use ($user) {
+ $query->where('created_by', '=', $user->id);
+ }),
+ 'books' => $this->entityRepo->getRecentlyCreated('book', $count, 0, function ($query) use ($user) {
+ $query->where('created_by', '=', $user->id);
+ })
+ ];
+ }
+
+ /**
+ * Get asset created counts for the give user.
+ * @param User $user
+ * @return array
+ */
+ public function getAssetCounts(User $user)
+ {
+ return [
+ 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
+ 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
+ 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
+ ];
+ }
+
+ /**
+ * Get the roles in the system that are assignable to a user.
+ * @return mixed
+ */
+ public function getAllRoles()
+ {
+ return $this->role->all();
+ }
+
+ /**
+ * Get all the roles which can be given restricted access to
+ * other entities in the system.
+ * @return mixed
+ */
+ public function getRestrictableRoles()
+ {
+ return $this->role->where('system_name', '!=', 'admin')->get();
+ }
+
+ /**
+ * Get a gravatar image for a user and set it as their avatar.
+ * Does not run if gravatar disabled in config.
+ * @param User $user
+ * @return bool
+ */
+ public function downloadGravatarToUserAvatar(User $user)
+ {
+ // Get avatar from gravatar and save
+ if (!config('services.gravatar')) {
+ return false;
+ }
+
+ try {
+ $avatar = Images::saveUserGravatar($user);
+ $user->avatar()->associate($avatar);
+ $user->save();
+ return true;
+ } catch (Exception $e) {
+ \Log::error('Failed to save user gravatar image');
+ return false;
+ }
+ }
+}