@@ -15,10 +15,16 @@ class RestrictionService
1515 public function __construct ()
1616 {
1717 $ user = auth ()->user ();
18- $ this ->userRoles = $ user ? auth ()->user ()->roles ->pluck ('id ' ) : false ;
18+ $ this ->userRoles = $ user ? auth ()->user ()->roles ->pluck ('id ' ) : [] ;
1919 $ this ->isAdmin = $ user ? auth ()->user ()->hasRole ('admin ' ) : false ;
2020 }
2121
22+ /**
23+ * Checks if an entity has a restriction set upon it.
24+ * @param Entity $entity
25+ * @param $action
26+ * @return bool
27+ */
2228 public function checkIfEntityRestricted (Entity $ entity , $ action )
2329 {
2430 if ($ this ->isAdmin ) return true ;
@@ -93,12 +99,28 @@ private function pageRestrictionQuery($query)
9399 });
94100 });
95101 })
102+ // Page unrestricted, Has an unrestricted chapter & book has accepted restrictions
103+ ->orWhere (function ($ query ) {
104+ $ query ->where ('restricted ' , '= ' , false )
105+ ->whereExists (function ($ query ) {
106+ $ query ->select ('* ' )->from ('chapters ' )
107+ ->whereRaw ('chapters.id=pages.chapter_id ' )->where ('restricted ' , '= ' , false );
108+ })
109+ ->whereExists (function ($ query ) {
110+ $ query ->select ('* ' )->from ('books ' )
111+ ->whereRaw ('books.id=pages.book_id ' )
112+ ->whereExists (function ($ query ) {
113+ $ this ->checkRestrictionsQuery ($ query , 'books ' , 'Book ' );
114+ });
115+ });
116+ })
96117 // Page unrestricted, Has a chapter with accepted permissions
97118 ->orWhere (function ($ query ) {
98119 $ query ->where ('restricted ' , '= ' , false )
99120 ->whereExists (function ($ query ) {
100121 $ query ->select ('* ' )->from ('chapters ' )
101122 ->whereRaw ('chapters.id=pages.chapter_id ' )
123+ ->where ('restricted ' , '= ' , true )
102124 ->whereExists (function ($ query ) {
103125 $ this ->checkRestrictionsQuery ($ query , 'chapters ' , 'Chapter ' );
104126 });
@@ -183,8 +205,10 @@ private function bookRestrictionQuery($query)
183205 return $ query ->where (function ($ parentWhereQuery ) {
184206 $ parentWhereQuery
185207 ->where ('restricted ' , '= ' , false )
186- ->orWhereExists (function ($ query ) {
187- $ this ->checkRestrictionsQuery ($ query , 'books ' , 'Book ' );
208+ ->orWhere (function ($ query ) {
209+ $ query ->where ('restricted ' , '= ' , true )->whereExists (function ($ query ) {
210+ $ this ->checkRestrictionsQuery ($ query , 'books ' , 'Book ' );
211+ });
188212 });
189213 });
190214 }
0 commit comments