55use Illuminate \Contracts \Support \Responsable ;
66use Illuminate \Database \Eloquent \Model ;
77use Illuminate \Database \Eloquent \Relations \Pivot ;
8+ use Illuminate \Http \Resources \Json \PaginatedResourceResponse ;
89use Illuminate \Pagination \LengthAwarePaginator ;
910use Illuminate \Support \Collection ;
11+ use Illuminate \Support \Facades \Gate ;
1012use Illuminate \Support \Str ;
1113use Lomkit \Rest \Http \Requests \RestRequest ;
1214use Lomkit \Rest \Relations \Relation ;
@@ -28,6 +30,16 @@ public function resource(Resource $resource) {
2830 });
2931 }
3032
33+ protected function buildGatesForModel (Model $ model ) {
34+ return [
35+ config ('rest.automatic_gates.names.authorized_to_view ' ) => Gate::allows ('view ' , $ model ),
36+ config ('rest.automatic_gates.names.authorized_to_update ' ) => Gate::allows ('update ' , $ model ),
37+ config ('rest.automatic_gates.names.authorized_to_delete ' ) => Gate::allows ('delete ' , $ model ),
38+ config ('rest.automatic_gates.names.authorized_to_restore ' ) => Gate::allows ('restore ' , $ model ),
39+ config ('rest.automatic_gates.names.authorized_to_force_delete ' ) => Gate::allows ('forceDelete ' , $ model ),
40+ ];
41+ }
42+
3143 public function modelToResponse (Model $ model , Resource $ resource , array $ requestArray , Relation $ relation = null ) {
3244 return array_merge (
3345 // toArray to take advantage of Laravel's logic
@@ -45,6 +57,12 @@ public function modelToResponse(Model $model, Resource $resource, array $request
4557 ->toArray ()
4658 )
4759 )
60+ ->when ($ resource ->isAutomaticGatingEnabled (), function ($ attributes ) use ($ model ) {
61+ return $ attributes ->put (
62+ config ('rest.automatic_gates.key ' ),
63+ $ this ->buildGatesForModel ($ model )
64+ );
65+ })
4866 ->toArray (),
4967 collect ($ model ->getRelations ())
5068 ->mapWithKeys (function ($ modelRelation , $ relationName ) use ($ requestArray , $ relation , $ resource ) {
@@ -94,19 +112,38 @@ public function modelToResponse(Model $model, Resource $resource, array $request
94112
95113 public function toResponse ($ request ) {
96114 if ($ this ->responsable instanceof LengthAwarePaginator) {
97- return $ this ->responsable ->through (function ($ model ) use ($ request ) {
115+ $ restLengthAwarePaginator = new \Lomkit \Rest \Pagination \LengthAwarePaginator (
116+ $ this ->responsable ->items (),
117+ $ this ->responsable ->total (),
118+ $ this ->responsable ->perPage (),
119+ $ this ->responsable ->currentPage (),
120+ $ this ->responsable ->getOptions (),
121+ $ this ->resource ->isAutomaticGatingEnabled () ? [
122+ config ('rest.automatic_gates.key ' ) => [
123+ config ('rest.automatic_gates.names.authorized_to_create ' ) => Gate::allows ('create ' , $ this ->responsable ->first ()::class),
124+ ]
125+ ] : []
126+ );
127+
128+ $ restLengthAwarePaginator ->through (function ($ model ) use ($ request ) {
98129 return $ this ->map ($ model , $ this ->modelToResponse ($ model , $ this ->resource , $ request ->input ()));
99130 });
131+
132+ return $ restLengthAwarePaginator ;
133+
100134 } elseif ($ this ->responsable instanceof Collection) {
101- return [
102- 'data ' => $ this ->responsable ->map (function ($ model ) use ($ request ) {
103- return $ this ->map ($ model , $ this ->modelToResponse ($ model , $ this ->resource , $ request ->input ()));
104- })
105- ];
135+ $ data = $ this ->responsable ->map (function ($ model ) use ($ request ) {
136+ return $ this ->map ($ model , $ this ->modelToResponse ($ model , $ this ->resource , $ request ->input ()));
137+ });
106138 }
107139
108140 return [
109- 'data ' => $ this ->map ($ this ->responsable , $ this ->modelToResponse ($ this ->responsable , $ this ->resource , $ request ->input ()))
141+ 'data ' => $ data ?? $ this ->map ($ this ->responsable , $ this ->modelToResponse ($ this ->responsable , $ this ->resource , $ request ->input ())),
142+ 'meta ' => [
143+ config ('rest.automatic_gates.key ' ) => [
144+ config ('rest.automatic_gates.names.authorized_to_create ' ) => Gate::allows ('create ' , $ this ->responsable ->first ()::class),
145+ ]
146+ ]
110147 ];
111148 }
112149
@@ -121,4 +158,7 @@ public function toResponse($request) {
121158 protected function map (\Illuminate \Database \Eloquent \Model $ model , array $ responseModel ) : array {
122159 return $ responseModel ;
123160 }
161+
162+ // @TODO: this class needs a refactor because it has grown a lot since the beginning also with the "lengthAwarePaginator"
163+ // @TODO: recursive response for those gates ?
124164 }
0 commit comments