@@ -18,11 +18,7 @@ class ParseCustomPolicyActions
1818{
1919 public function handle (Request $ request , Closure $ next ): Response
2020 {
21- if (! cache ()->has ('governor.registered-custom-actions ' )) {
22- $ this ->registerCustomPolicyActions ();
23-
24- cache ()->forever ('governor.registered-custom-actions ' , true );
25- }
21+ $ this ->registerCustomPolicyActions ();
2622
2723 return $ next ($ request );
2824 }
@@ -34,15 +30,34 @@ protected function registerCustomPolicyActions(): void
3430 ->map (function (string $ policyClass , string $ modelClass ): Collection {
3531 return $ this ->getCustomActionMethods ($ policyClass )
3632 ->map (function (string $ method ) use ($ modelClass ): Action {
37- $ action = (new Action )->firstOrCreate ([
38- "name " => "{$ modelClass }: {$ method }" ,
39- ]);
40- (new Permission )->firstOrCreate ([
41- "role_name " => "SuperAdmin " ,
42- "entity_name " => $ action ->entity ,
43- "action_name " => $ action ->name ,
44- "ownership_name " => "any " ,
45- ]);
33+ $ action = app ("governor-actions " )
34+ ->where ("name " , "{$ modelClass }: {$ method }" )
35+ ->first ();
36+
37+ if (! $ action ) {
38+ $ actionClass = app (config ('genealabs-laravel-governor.models.action ' ));
39+ $ action = (new $ actionClass )
40+ ->firstOrCreate ([
41+ "name " => "{$ modelClass }: {$ method }" ,
42+ ]);
43+ }
44+
45+ $ permission = app ("governor-permissions " )
46+ ->where ("role_name " , "SuperAdmin " )
47+ ->where ("entity_name " , $ action ->entity )
48+ ->where ("action_name " , $ action ->name )
49+ ->where ("ownership_name " , "any " )
50+ ->first ();
51+
52+ if (! $ permission ) {
53+ $ permissionClass = config ("genealabs-laravel-governor.models.permission " );
54+ (new $ permissionClass )->create ([
55+ "role_name " => "SuperAdmin " ,
56+ "entity_name " => $ action ->entity ,
57+ "action_name " => $ action ->name ,
58+ "ownership_name " => "any " ,
59+ ]);
60+ }
4661
4762 return $ action ;
4863 });
0 commit comments