File tree Expand file tree Collapse file tree 4 files changed +76
-10
lines changed Expand file tree Collapse file tree 4 files changed +76
-10
lines changed Original file line number Diff line number Diff line change @@ -21,4 +21,11 @@ public function getPermissionTitle(): string;
2121 * @return array
2222 */
2323 public function getExcludeMethods (): array ;
24+
25+ /**
26+ * Append permission to another permission
27+ *
28+ * @return string
29+ */
30+ public function getAppendTo (): string ;
2431}
Original file line number Diff line number Diff line change @@ -104,9 +104,11 @@ protected function customPermissions(): Permissions
104104 protected function getCachedPermissions ()
105105 {
106106 if (!$ this ->hasCachedPermissions ()) {
107+ ksort ($ this ->permissions );
108+
107109 return $ this ->permissions ;
108110 }
109-
111+
110112 return Cache::get (Constant::CACHE_PERMISSIONS );
111113 }
112114
Original file line number Diff line number Diff line change @@ -80,10 +80,7 @@ public function generate()
8080 continue ;
8181 }
8282
83- // permission group title
84- $ title = $ this ->generatePermissionTitle ($ controllerInstance );
85-
86- $ key = strtolower (Str::slug ($ title , "- " ));
83+ $ key = $ this ->generateKey ($ controllerInstance );
8784
8885 $ permissions [$ key ][] = [
8986 'name ' => $ routeName , // permission name
@@ -101,6 +98,39 @@ public function generate()
10198 ];
10299 }
103100
101+ protected function generateKey ($ controllerInstance )
102+ {
103+ $ key = $ this ->appendPermissionKey ($ controllerInstance );
104+
105+ if (empty ($ key )) {
106+ $ title = $ this ->generatePermissionTitle ($ controllerInstance );
107+
108+ return strtolower (Str::slug ($ title , "- " ));
109+ }
110+
111+ return $ key ;
112+ }
113+
114+ protected function appendPermissionKey ($ currentControllerInstance )
115+ {
116+ if ($ currentControllerInstance instanceof WithPermissionGenerator) {
117+ $ appendTo = $ currentControllerInstance ->getAppendTo ();
118+
119+ // generate key
120+ if (!empty ($ appendTo ) && class_exists ($ appendTo )) {
121+ $ appendControllerClass = app ("\\" . $ appendTo );
122+
123+ $ title = $ this ->generatePermissionTitle ($ appendControllerClass );
124+
125+ return strtolower (Str::slug ($ title , "- " ));
126+ }
127+
128+ return $ appendTo ;
129+ }
130+
131+ return '' ;
132+ }
133+
104134 protected function generatePermissionTitle ($ controllerInstance )
105135 {
106136 // if the controller use the WithPermissible interface then get the title
Original file line number Diff line number Diff line change @@ -10,36 +10,55 @@ trait PermissionGenerator
1010
1111 private $ excludeMethods = [];
1212
13+ private $ appendTo = '' ;
14+
1315 /**
1416 * Set the permission group title
1517 *
1618 * @param string $title
1719 *
1820 * @return $this
1921 */
20- protected function permissionGroupTitle (string $ title ){
22+ protected function permissionGroupTitle (string $ title )
23+ {
2124 $ this ->title = $ title ;
2225
2326 return $ this ;
2427 }
2528
2629 /**
27- * Set excluded routes by controller method
30+ * Excluded routes by controller method,
31+ * so that it can not generate as permission name
2832 *
2933 * @param ...$methods
3034 *
3135 * @return $this
3236 */
33- protected function permissionExcludeMethods (...$ methods ){
37+ protected function permissionExcludeMethods (...$ methods )
38+ {
3439 $ this ->excludeMethods = $ methods ;
3540
3641 return $ this ;
3742 }
3843
44+ /**
45+ * Permission names append to another permission group
46+ *
47+ * @param string $key // SomeController::class | permission-group-key
48+ * @return void
49+ */
50+ protected function permissionAppendTo (string $ key )
51+ {
52+ $ this ->appendTo = $ key ;
53+
54+ return $ this ;
55+ }
56+
3957 /**
4058 * @return string
4159 */
42- public function getPermissionTitle (): string {
60+ public function getPermissionTitle (): string
61+ {
4362 return $ this ->title ;
4463 }
4564
@@ -50,4 +69,12 @@ public function getExcludeMethods(): array
5069 {
5170 return $ this ->excludeMethods ;
5271 }
53- }
72+
73+ /**
74+ * @return string
75+ */
76+ public function getAppendTo (): string
77+ {
78+ return $ this ->appendTo ;
79+ }
80+ }
You can’t perform that action at this time.
0 commit comments