Skip to content

Conversation

@martinoak
Copy link
Contributor

Implement wildcard permission support in Statamic CMS to allow using * as a placeholder in permission strings, enabling simplified role configuration.

In nutshell, I have extended hasPermission() function and created a new matchesWildcard() function to use a regex and parse the wildcard entries in roles.yaml file.

What is allowed now:
roles.yaml:

- view * entries #instead of 'view pages entries', 'view news entries', 'view team entries' etc.
- view * terms #instead of 'view tags terms'
- view * assets #instead of 'view pictures assets'

What led me to this change:
The current roles.yaml file can contain 11 times the same, repetitive setup. For example, the "editor" role lists the same permissions for 11 different collections, resulting in 99 nearly identical permission entries.

Example Transformations

Wildcard Pattern Regex Pattern Matches Doesn't Match
view * entries /^view .* entries$/ view courses entries
view news entries
edit courses entries
view courses terms
* * terms /^.* .* terms$/ view machines terms
edit regions terms
view machines entries
edit other authors * entries /^edit other authors .* entries$/ edit other authors courses entries delete other authors courses entries

Testing
I tried to write some tests for that, but I'm not sure, if its OK, havent practiced much in writing tests.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant