File tree 4 files changed +76
-10
lines changed
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;
21
21
* @return array
22
22
*/
23
23
public function getExcludeMethods (): array ;
24
+
25
+ /**
26
+ * Append permission to another permission
27
+ *
28
+ * @return string
29
+ */
30
+ public function getAppendTo (): string ;
24
31
}
Original file line number Diff line number Diff line change @@ -104,9 +104,11 @@ protected function customPermissions(): Permissions
104
104
protected function getCachedPermissions ()
105
105
{
106
106
if (!$ this ->hasCachedPermissions ()) {
107
+ ksort ($ this ->permissions );
108
+
107
109
return $ this ->permissions ;
108
110
}
109
-
111
+
110
112
return Cache::get (Constant::CACHE_PERMISSIONS );
111
113
}
112
114
Original file line number Diff line number Diff line change @@ -80,10 +80,7 @@ public function generate()
80
80
continue ;
81
81
}
82
82
83
- // permission group title
84
- $ title = $ this ->generatePermissionTitle ($ controllerInstance );
85
-
86
- $ key = strtolower (Str::slug ($ title , "- " ));
83
+ $ key = $ this ->generateKey ($ controllerInstance );
87
84
88
85
$ permissions [$ key ][] = [
89
86
'name ' => $ routeName , // permission name
@@ -101,6 +98,39 @@ public function generate()
101
98
];
102
99
}
103
100
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
+
104
134
protected function generatePermissionTitle ($ controllerInstance )
105
135
{
106
136
// 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
10
10
11
11
private $ excludeMethods = [];
12
12
13
+ private $ appendTo = '' ;
14
+
13
15
/**
14
16
* Set the permission group title
15
17
*
16
18
* @param string $title
17
19
*
18
20
* @return $this
19
21
*/
20
- protected function permissionGroupTitle (string $ title ){
22
+ protected function permissionGroupTitle (string $ title )
23
+ {
21
24
$ this ->title = $ title ;
22
25
23
26
return $ this ;
24
27
}
25
28
26
29
/**
27
- * Set excluded routes by controller method
30
+ * Excluded routes by controller method,
31
+ * so that it can not generate as permission name
28
32
*
29
33
* @param ...$methods
30
34
*
31
35
* @return $this
32
36
*/
33
- protected function permissionExcludeMethods (...$ methods ){
37
+ protected function permissionExcludeMethods (...$ methods )
38
+ {
34
39
$ this ->excludeMethods = $ methods ;
35
40
36
41
return $ this ;
37
42
}
38
43
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
+
39
57
/**
40
58
* @return string
41
59
*/
42
- public function getPermissionTitle (): string {
60
+ public function getPermissionTitle (): string
61
+ {
43
62
return $ this ->title ;
44
63
}
45
64
@@ -50,4 +69,12 @@ public function getExcludeMethods(): array
50
69
{
51
70
return $ this ->excludeMethods ;
52
71
}
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