@@ -53,16 +53,56 @@ public function getClusterSectionPermissions(): array
5353
5454 public function getResourcePermissions (): array
5555 {
56- return collect ($ this ->getResourceData ())
56+ $ resourcesInConfig = InspireCmsConfig::getFilamentResources ();
57+
58+ $ panel = filament ()->getPanel (InspireCmsConfig::getPanelId ());
59+ $ resourcesFromDiscover = collect ($ panel ?->getResourceNamespaces() ?? [])
60+ ->combine ($ panel ?->getResourceDirectories() ?? [])
61+ // get all resource from the discover directories
62+ ->flatMap (function ($ dir , $ namespace ) {
63+ // get the fully qualified class names of the pages
64+ $ files = glob ($ dir . '/*.php ' );
65+ return collect ($ files )
66+ ->map (fn ($ file ) => str_replace (['/ ' , '.php ' ], ['\\' , '' ], $ file ))
67+ ->map (fn ($ fqcn ) => $ namespace . '\\' . class_basename ($ fqcn ));
68+ })
69+ ->all ();
70+
71+ $ resourcePermissions = collect ($ resourcesInConfig )
72+ ->merge ($ resourcesFromDiscover )
73+ ->unique () // ensure no duplicates
74+ ->where (
75+ fn (string $ fqcn ): bool => is_subclass_of ($ fqcn , \Filament \Resources \Resource::class) &&
76+ in_array (ClusterSectionResource::class, class_implements ($ fqcn ))
77+ )
78+ ->map (fn (string $ fqcn ) => [
79+ 'model ' => $ fqcn ::getModel (),
80+ 'customModelName ' => $ fqcn ::getCustomModelPermissionPrefix (),
81+ 'customModelDisplay ' => $ fqcn ::getCustomModelPermissionDisplay (),
82+ 'permissionPrefixes ' => $ fqcn ::getPermissionPrefixes (),
83+ ])
84+ // add MediaAsset model permissions
85+ ->merge ([
86+ [
87+ 'model ' => \SolutionForest \InspireCms \Support \Facades \ModelRegistry::get (MediaAsset::class),
88+ 'permissionPrefixes ' => ['view ' , 'create ' , 'update ' , 'delete ' , 'delete_any ' ],
89+ ],
90+ ])
91+ ->all ();
92+
93+ return collect ($ resourcePermissions )
5794 ->map (function (array $ data ) {
5895
5996 $ model = $ data ['model ' ];
60- $ modelShortName = class_basename ($ model );
97+ $ customModelPermissionPrefix = $ data ['customModelName ' ] ?? null ;
98+ $ modelShortName = $ data ['customModelDisplay ' ] ?? class_basename ($ model );
6199
62100 $ permissionNames = collect ($ data ['permissionPrefixes ' ])
63- ->mapWithKeys (function (string $ suffix ) use ($ model ) {
101+ ->mapWithKeys (callback: function (string $ suffix ) use ($ model, $ customModelPermissionPrefix ) {
64102
65- $ permissionName = $ this ->getPermissionNameForModel ($ suffix , $ model );
103+ $ permissionName = is_string ($ customModelPermissionPrefix ) && filled ($ customModelPermissionPrefix )
104+ ? $ this ->getPermissionNameForModelShort ($ suffix , $ customModelPermissionPrefix )
105+ : $ this ->getPermissionNameForModel ($ suffix , $ model );
66106
67107 $ permissionLabel = str ($ suffix )
68108 ->kebab ()
@@ -115,7 +155,24 @@ public function getActionPermissions(): array
115155
116156 public function getPagePermissions (): array
117157 {
118- return collect (InspireCmsConfig::getFilamentPages ())
158+ $ pagesInConfig = InspireCmsConfig::getFilamentPages ();
159+
160+ $ panel = filament ()->getPanel (InspireCmsConfig::getPanelId ());
161+ $ pagesFromDiscover = collect ($ panel ?->getPageNamespaces() ?? [])
162+ ->combine ($ panel ?->getPageDirectories() ?? [])
163+ // get all pages from the discover directories
164+ ->flatMap (function ($ dir , $ namespace ) {
165+ // get the fully qualified class names of the pages
166+ $ files = glob ($ dir . '/*.php ' );
167+ return collect ($ files )
168+ ->map (fn ($ file ) => str_replace (['/ ' , '.php ' ], ['\\' , '' ], $ file ))
169+ ->map (fn ($ fqcn ) => $ namespace . '\\' . class_basename ($ fqcn ));
170+ })
171+ ->all ();
172+
173+ return collect ($ pagesInConfig )
174+ ->merge ($ pagesFromDiscover )
175+ ->unique () // ensure no duplicates
119176 ->where (fn ($ fqcn ) => in_array (GuardPage::class, class_implements ($ fqcn )))
120177 ->mapWithKeys (fn ($ fqcn ) => [$ fqcn ::getPermissionName () => $ fqcn ::getPermissionDisplayName ()])
121178 ->sortKeys ()
@@ -162,7 +219,12 @@ public function getPermissionNameForModel(string $ability, string $model): strin
162219 {
163220 $ modelShortName = class_basename ($ model );
164221
165- return str ($ modelShortName )
222+ return $ this ->getPermissionNameForModelShort ($ ability , $ modelShortName );
223+ }
224+
225+ protected function getPermissionNameForModelShort (string $ ability , string $ model ): string
226+ {
227+ return str ($ model )
166228 ->lower ()
167229 ->snake ('_ ' )
168230 ->finish ('. ' )
@@ -259,26 +321,6 @@ protected function getDefaultPermissions(): array
259321 ->toArray ();
260322 }
261323
262- protected function getResourceData (): array
263- {
264- return collect (InspireCmsConfig::getFilamentResources ())
265- ->where (
266- fn (string $ fqcn ): bool => is_subclass_of ($ fqcn , \Filament \Resources \Resource::class) &&
267- in_array (ClusterSectionResource::class, class_implements ($ fqcn ))
268- )
269- ->map (fn (string $ fqcn ) => [
270- 'model ' => $ fqcn ::getModel (),
271- 'permissionPrefixes ' => $ fqcn ::getPermissionPrefixes (),
272- ])
273- ->merge ([
274- [
275- 'model ' => \SolutionForest \InspireCms \Support \Facades \ModelRegistry::get (MediaAsset::class),
276- 'permissionPrefixes ' => ['view ' , 'create ' , 'update ' , 'delete ' , 'delete_any ' ],
277- ],
278- ])
279- ->toArray ();
280- }
281-
282324 protected function isAbilityGrantedWithTier ($ ability ): bool
283325 {
284326 return ! Str::endsWith ($ ability , '_any ' ) && $ ability != 'create ' ;
0 commit comments