Dan Brown

Added restrictions to user profile lists

...@@ -42,13 +42,19 @@ class EntityRepo ...@@ -42,13 +42,19 @@ class EntityRepo
42 42
43 /** 43 /**
44 * Get the latest books added to the system. 44 * Get the latest books added to the system.
45 - * @param $count 45 + * @param int $count
46 - * @param $page 46 + * @param int $page
47 + * @param bool $additionalQuery
48 + * @return
47 */ 49 */
48 - public function getRecentlyCreatedBooks($count = 20, $page = 0) 50 + public function getRecentlyCreatedBooks($count = 20, $page = 0, $additionalQuery = false)
49 { 51 {
50 - return $this->restrictionService->enforceBookRestrictions($this->book) 52 + $query = $this->restrictionService->enforceBookRestrictions($this->book)
51 - ->orderBy('created_at', 'desc')->skip($page * $count)->take($count)->get(); 53 + ->orderBy('created_at', 'desc');
54 + if ($additionalQuery !== false && is_callable($additionalQuery)) {
55 + $additionalQuery($query);
56 + }
57 + return $query->skip($page * $count)->take($count)->get();
52 } 58 }
53 59
54 /** 60 /**
...@@ -65,13 +71,36 @@ class EntityRepo ...@@ -65,13 +71,36 @@ class EntityRepo
65 71
66 /** 72 /**
67 * Get the latest pages added to the system. 73 * Get the latest pages added to the system.
68 - * @param $count 74 + * @param int $count
69 - * @param $page 75 + * @param int $page
76 + * @param bool $additionalQuery
77 + * @return
70 */ 78 */
71 - public function getRecentlyCreatedPages($count = 20, $page = 0) 79 + public function getRecentlyCreatedPages($count = 20, $page = 0, $additionalQuery = false)
72 { 80 {
73 - return $this->restrictionService->enforcePageRestrictions($this->page) 81 + $query = $this->restrictionService->enforcePageRestrictions($this->page)
74 - ->orderBy('created_at', 'desc')->skip($page * $count)->take($count)->get(); 82 + ->orderBy('created_at', 'desc');
83 + if ($additionalQuery !== false && is_callable($additionalQuery)) {
84 + $additionalQuery($query);
85 + }
86 + return $query->skip($page * $count)->take($count)->get();
87 + }
88 +
89 + /**
90 + * Get the latest chapters added to the system.
91 + * @param int $count
92 + * @param int $page
93 + * @param bool $additionalQuery
94 + * @return
95 + */
96 + public function getRecentlyCreatedChapters($count = 20, $page = 0, $additionalQuery = false)
97 + {
98 + $query = $this->restrictionService->enforceChapterRestrictions($this->chapter)
99 + ->orderBy('created_at', 'desc');
100 + if ($additionalQuery !== false && is_callable($additionalQuery)) {
101 + $additionalQuery($query);
102 + }
103 + return $query->skip($page * $count)->take($count)->get();
75 } 104 }
76 105
77 /** 106 /**
...@@ -100,7 +129,7 @@ class EntityRepo ...@@ -100,7 +129,7 @@ class EntityRepo
100 foreach ($restrictions as $action => $value) { 129 foreach ($restrictions as $action => $value) {
101 $entity->restrictions()->create([ 130 $entity->restrictions()->create([
102 'role_id' => $roleId, 131 'role_id' => $roleId,
103 - 'action' => strtolower($action) 132 + 'action' => strtolower($action)
104 ]); 133 ]);
105 } 134 }
106 } 135 }
......
...@@ -141,12 +141,15 @@ class UserRepo ...@@ -141,12 +141,15 @@ class UserRepo
141 public function getRecentlyCreated(User $user, $count = 20) 141 public function getRecentlyCreated(User $user, $count = 20)
142 { 142 {
143 return [ 143 return [
144 - 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->orderBy('created_at', 'desc') 144 + 'pages' => $this->entityRepo->getRecentlyCreatedPages($count, 0, function ($query) use ($user) {
145 - ->take($count)->get(), 145 + $query->where('created_by', '=', $user->id);
146 - 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->orderBy('created_at', 'desc') 146 + }),
147 - ->take($count)->get(), 147 + 'chapters' => $this->entityRepo->getRecentlyCreatedChapters($count, 0, function ($query) use ($user) {
148 - 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->orderBy('created_at', 'desc') 148 + $query->where('created_by', '=', $user->id);
149 - ->take($count)->get() 149 + }),
150 + 'books' => $this->entityRepo->getRecentlyCreatedBooks($count, 0, function ($query) use ($user) {
151 + $query->where('created_by', '=', $user->id);
152 + })
150 ]; 153 ];
151 } 154 }
152 155
...@@ -158,9 +161,9 @@ class UserRepo ...@@ -158,9 +161,9 @@ class UserRepo
158 public function getAssetCounts(User $user) 161 public function getAssetCounts(User $user)
159 { 162 {
160 return [ 163 return [
161 - 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(), 164 + 'pages' => $this->entityRepo->page->where('created_by', '=', $user->id)->count(),
162 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(), 165 'chapters' => $this->entityRepo->chapter->where('created_by', '=', $user->id)->count(),
163 - 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(), 166 + 'books' => $this->entityRepo->book->where('created_by', '=', $user->id)->count(),
164 ]; 167 ];
165 } 168 }
166 169
......
...@@ -26,8 +26,8 @@ class ActivityService ...@@ -26,8 +26,8 @@ class ActivityService
26 * Add activity data to database. 26 * Add activity data to database.
27 * @param Entity $entity 27 * @param Entity $entity
28 * @param $activityKey 28 * @param $activityKey
29 - * @param int $bookId 29 + * @param int $bookId
30 - * @param bool $extra 30 + * @param bool $extra
31 */ 31 */
32 public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false) 32 public function add(Entity $entity, $activityKey, $bookId = 0, $extra = false)
33 { 33 {
...@@ -45,7 +45,7 @@ class ActivityService ...@@ -45,7 +45,7 @@ class ActivityService
45 /** 45 /**
46 * Adds a activity history with a message & without binding to a entity. 46 * Adds a activity history with a message & without binding to a entity.
47 * @param $activityKey 47 * @param $activityKey
48 - * @param int $bookId 48 + * @param int $bookId
49 * @param bool|false $extra 49 * @param bool|false $extra
50 */ 50 */
51 public function addMessage($activityKey, $bookId = 0, $extra = false) 51 public function addMessage($activityKey, $bookId = 0, $extra = false)
...@@ -88,7 +88,7 @@ class ActivityService ...@@ -88,7 +88,7 @@ class ActivityService
88 */ 88 */
89 public function latest($count = 20, $page = 0) 89 public function latest($count = 20, $page = 0)
90 { 90 {
91 - $activityList = $this->restrictionService 91 + $activityList = $this->restrictionService
92 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type') 92 ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
93 ->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get(); 93 ->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get();
94 94
...@@ -99,8 +99,8 @@ class ActivityService ...@@ -99,8 +99,8 @@ class ActivityService
99 * Gets the latest activity for an entity, Filtering out similar 99 * Gets the latest activity for an entity, Filtering out similar
100 * items to prevent a message activity list. 100 * items to prevent a message activity list.
101 * @param Entity $entity 101 * @param Entity $entity
102 - * @param int $count 102 + * @param int $count
103 - * @param int $page 103 + * @param int $page
104 * @return array 104 * @return array
105 */ 105 */
106 public function entityActivity($entity, $count = 20, $page = 0) 106 public function entityActivity($entity, $count = 20, $page = 0)
...@@ -121,9 +121,10 @@ class ActivityService ...@@ -121,9 +121,10 @@ class ActivityService
121 */ 121 */
122 public function userActivity($user, $count = 20, $page = 0) 122 public function userActivity($user, $count = 20, $page = 0)
123 { 123 {
124 - $activity = $this->activity->where('user_id', '=', $user->id) 124 + $activityList = $this->restrictionService
125 - ->orderBy('created_at', 'desc')->skip($count * $page)->take($count)->get(); 125 + ->filterRestrictedEntityRelations($this->activity, 'activities', 'entity_id', 'entity_type')
126 - return $this->filterSimilar($activity); 126 + ->orderBy('created_at', 'desc')->where('user_id', '=', $user->id)->skip($count * $page)->take($count)->get();
127 + return $this->filterSimilar($activityList);
127 } 128 }
128 129
129 /** 130 /**
......