@@ -30,9 +30,10 @@ public function resource(Resource $resource)
3030 });
3131 }
3232
33- protected function buildGatesForModel (Model $ model , Resource $ resource , array $ gates )
33+ protected function buildGatesForModel (Model | string $ model , Resource $ resource , array $ gates )
3434 {
3535 $ nameMap = [
36+ 'create ' => config ('rest.gates.names.authorized_to_create ' ),
3637 'view ' => config ('rest.gates.names.authorized_to_view ' ),
3738 'update ' => config ('rest.gates.names.authorized_to_update ' ),
3839 'delete ' => config ('rest.gates.names.authorized_to_delete ' ),
@@ -41,10 +42,23 @@ protected function buildGatesForModel(Model $model, Resource $resource, array $g
4142 ];
4243
4344 $ result = [];
44- foreach ($ gates as $ gate ) {
45- if (isset ($ nameMap [$ gate ])) {
46- $ auth = $ resource ->authorizedTo ($ gate , $ model );
47- $ result [$ nameMap [$ gate ]] = $ auth ->message () ?? $ auth ->allowed ();
45+
46+ if (config ('rest.gates.message.enabled ' , false )) {
47+ foreach ($ gates as $ gate ) {
48+ if (isset ($ nameMap [$ gate ])) {
49+ $ authorizedTo = $ resource ->authorizedTo ($ gate , $ model );
50+ $ result [$ nameMap [$ gate ]]['allowed ' ] = $ authorizedTo ->allowed ();
51+ $ result [$ nameMap [$ gate ]]['message ' ] = $ authorizedTo ->message ();
52+ }
53+ }
54+ } else {
55+ // TODO: put the good anchor to link to the new method (link in the string)
56+ trigger_deprecation ('lomkit/laravel-rest-api ' , '2.17.0 ' , 'In Laravel Rest Api 3 it won \'t be possible to use the old gate schema, please upgrade as quickly as possible. See: https://laravel-rest-api.lomkit.com/digging-deeper/gates#policy-message-in-gates ' );
57+ foreach ($ gates as $ gate ) {
58+ if (isset ($ nameMap [$ gate ])) {
59+ $ authorizedTo = $ resource ->authorizedTo ($ gate , $ model );
60+ $ result [$ nameMap [$ gate ]] = $ authorizedTo ->allowed ();
61+ }
4862 }
4963 }
5064
@@ -87,9 +101,10 @@ public function modelToResponse(Model $model, Resource $resource, array $request
87101 )
88102 )
89103 ->when ($ resource ->isGatingEnabled () && isset ($ currentRequestArray ['gates ' ]), function ($ attributes ) use ($ currentRequestArray , $ resource , $ model ) {
104+ $ currentRequestArrayWithoutCreate = collect ($ currentRequestArray ['gates ' ])->reject (fn ($ value ) => $ value === 'create ' )->toArray ();
90105 return $ attributes ->put (
91106 config ('rest.gates.key ' ),
92- $ this ->buildGatesForModel ($ model , $ resource , $ currentRequestArray [ ' gates ' ] )
107+ $ this ->buildGatesForModel ($ model , $ resource , $ currentRequestArrayWithoutCreate )
93108 );
94109 })
95110 ->toArray (),
@@ -136,20 +151,14 @@ public function modelToResponse(Model $model, Resource $resource, array $request
136151 public function toResponse ($ request )
137152 {
138153 if ($ this ->responsable instanceof LengthAwarePaginator) {
139- if ($ this ->resource ->isGatingEnabled () && in_array ('create ' , $ request ->input ('search.gates ' , []))) {
140- $ authorizedToCreate = $ this ->resource ->authorizedTo ('create ' , $ this ->resource ::newModel ()::class);
141- }
142-
143154 $ restLengthAwarePaginator = new \Lomkit \Rest \Pagination \LengthAwarePaginator (
144155 $ this ->responsable ->items (),
145156 $ this ->responsable ->total (),
146157 $ this ->responsable ->perPage (),
147158 $ this ->responsable ->currentPage (),
148159 $ this ->responsable ->getOptions (),
149- isset ($ authorizedToCreate ) ? [
150- config ('rest.gates.key ' ) => [
151- config ('rest.gates.names.authorized_to_create ' ) => $ authorizedToCreate ->message () ?? $ authorizedToCreate ->allowed (),
152- ],
160+ $ this ->resource ->isGatingEnabled () && in_array ('create ' , $ request ->input ('search.gates ' , [])) ? [
161+ config ('rest.gates.key ' ) => $ this ->buildGatesForModel ($ this ->resource ::newModel ()::class, $ this ->resource , ['create ' ]),
153162 ] : []
154163 );
155164
@@ -168,9 +177,7 @@ public function toResponse($request)
168177 'data ' => $ data ?? $ this ->map ($ this ->responsable , $ this ->modelToResponse ($ this ->responsable , $ this ->resource , $ request ->input ('search ' , []))),
169178 'meta ' => array_merge (
170179 $ this ->resource ->isGatingEnabled () && in_array ('create ' , $ request ->input ('search.gates ' , [])) ? [
171- config ('rest.gates.key ' ) => [
172- config ('rest.gates.names.authorized_to_create ' ) => $ this ->resource ->authorizedTo ('create ' , $ this ->resource ::newModel ()::class),
173- ],
180+ config ('rest.gates.key ' ) => $ this ->buildGatesForModel ($ this ->resource ::newModel ()::class, $ this ->resource , ['create ' ]),
174181 ] : []
175182 ),
176183 ];
0 commit comments