Implement a config option like "allowAdminChanges" - override permissions dynamically based on APP_ENV? #6289
-
Hey everyone, Statamic newbie speaking: Technical backgroundI stumbled over Craft CMSes allowAdminChanges option while reading Craft CMS deployment docs.
Disabling all "Configure " permissions for Statamic CP on production sitesI would really love to lock down the control panel of Statamic production sites with the same approach. In the end config changes should only be possible in the local development environment ( As far as I can understand by now, it basically comes down to strip away all "Configure ..." permissions from the current user. Also "super admins" shouldn't be possible on production sites. Previous ideasI already found the possibility to load different // config/statamic/users.php
'roles' => env('APP_ENV') === 'local' ? resource_path('users/roles.yaml') : resource_path('users/roles.production.yaml'), But my current guess is that stripping away "Configure ..."-permissions in a central place would be much cleaner and easier to maintain. I also saw the My current approach - help neededFor my first quick & dirty try I therefore overwrote I added the following to delete any permissions which start with "configure": // vendor/statamic/cms/src/Auth/File/User.php
if(env('APP_ENV') === 'production'){
foreach ($permissions as $index => $permission) {
// Throw out all "Configure ..." permissions
if (strpos($permission, 'configure') !== false) {
unset($permissions[$index]);
}
if($permission == 'super'){
unset($permissions[$index]);
}
}
} Full method: public function permissions()
{
$cache = app(PermissionCache::class);
if ($cached = $cache->get($this->id)) {
return $cached;
}
$permissions = $this
->groups()
->flatMap->roles()
->merge($this->roles())
->flatMap->permissions();
if ($this->get('super', false)) {
$permissions[] = 'super';
}
$permissions = $permissions->unique()->values();
if(env('APP_ENV') === 'production'){
foreach ($permissions as $index => $permission) {
// Throw out all "Configure ..." permissions
if (strpos($permission, 'configure') !== false) {
unset($permissions[$index]);
}
if($permission == 'super'){
unset($permissions[$index]);
}
}
}
// var_dump($permissions);
$cache->put($this->id, $permissions);
return $permissions;
} Long story short, two questions:
Thanks in advance for any suggestions and hints! Cheers, Matthias |
Beta Was this translation helpful? Give feedback.
Replies: 7 comments 11 replies
-
If you are using Statamic Pro you can just create a Role that has the permissions that you want and assign this to the users you want as "limited admins". If you still want to do it your way you can probably extend the |
Beta Was this translation helpful? Give feedback.
-
Another option would be to collaborate with us on a possible new permission or setting and now have to deep track an override approach. |
Beta Was this translation helpful? Give feedback.
-
What's wrong with doing this? Seems reasonable to me that if you don't want a user to have permission to do stuff, don't give it to them.
If you don't want a super admin on production, don't create one. Locally, you can just create your own user that you don't commit. |
Beta Was this translation helpful? Give feedback.
-
Hey @mandrasch, I'm curious if you found a suitable solution for this? We're running into the same situation where I'd like to allow users to make changes locally, but—in a production environment—disallow these updates so schema/configuration changes flow upstream. |
Beta Was this translation helpful? Give feedback.
-
Popping back in here because I would definitely like to suggest this being added into core! Does this make sense to keep as a discussion? Should it be moved/recreated in https://github.com/statamic/ideas? Reason I ask is because we really like the ability to Git track our config changes, exclude content, but disallow any config changes in production. While that can be useful for some Statamic instances I'm always wary to allow changes to Blueprints, Collections, etc in a production environment before going through proper testing. Anything I could do to help here? An example PR? Write-up of the expected behavior? Happy to help in whatever way to make this possible! ❤️ |
Beta Was this translation helpful? Give feedback.
-
For what it's worth, I ended up utilizing the same model that this wonderful repo did—shoutout to @justbetter! Here's the basics of what I'm doing:
It's a simple, but effective way to enforce this behavior. Hopefully something makes its way to core to remove the need for this, but I hope it helps someone in a similar position as I! |
Beta Was this translation helpful? Give feedback.
-
Hi everyone, Has there been any development regarding adding this feature to the core? Thanks. |
Beta Was this translation helpful? Give feedback.
For what it's worth, I ended up utilizing the same model that this wonderful repo did—shoutout to @justbetter!
Here's the basics of what I'm doing:
ALLOW_ADMIN_CHANGES
environment variable is set tofalse
true
then nothing happens. But if it'sfalse
I take the next steps because all users should have reduced permissions.$this->app->bind()
to bind a custom User class I built to theStatamic\Auth\File\User
class (what @afonic suggested)isSuper()
method to always return falsehasPermission($permission)
method to returnfalse
when select permissions are requested (configure forms
,manage preferences
,edit header nav
, etc)It…