Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

How do I add permissions from another plugin? #11

Closed
jeffreyroberts opened this issue Aug 29, 2017 · 5 comments
Closed

How do I add permissions from another plugin? #11

jeffreyroberts opened this issue Aug 29, 2017 · 5 comments

Comments

@jeffreyroberts
Copy link

I tried to create a permissions.php file in the plugins/SharpAgent/config directory, however it wasn't loaded...

How can I add in my plugin bootstrap the following?

return [
    'Users.SimpleRbac.permissions' => [
        [
            'role' => ['user', 'member', 'admin'],
            'plugin' => 'SharpAgent',
            'controller' => 'Agent',
            'action' => '*',
            'allowed' => true,
        ]
]];
@jeffreyroberts
Copy link
Author

I tried plugins/Plugin/config/permissions.php but it didn't work, any ideas? should that work?

@steinkel
Copy link
Member

steinkel commented Sep 1, 2017

You could do a Configure::read, apply your rules, then Configure::write inside your plugin's bootstrap file...
Let me know if that works :)

@steinkel steinkel closed this as completed Sep 1, 2017
@jeffreyroberts
Copy link
Author

Do I read it like so?

 $rbac = Cake\Core\Configure::read('Users.SimpleRbac.permissions');

I am doing a

print_r($rbac);die('rbac');

right after the read, but it is outputting blank...

@jeffreyroberts
Copy link
Author

Ok, I figured out that if I put the following in the plugin bootstrap, it gets overwritten, however, if I put it in an action, it doesn't get overwritten, but also doesn't do me any good at that point, since I cant put it in the bootstrap, where should I put it at?

    $rbac = Configure::read('Users.SimpleRbac.permissions');
    $rbac[] = [
        'role' => ['user', 'member', 'admin'],
        'plugin' => 'SharpAgent',
        'controller' => '*',
        'action' => '*',
        'allowed' => true,
    ];
    Configure::write('Users.SimpleRbac.permissions', $rbac);

@steinkel
Copy link
Member

steinkel commented Sep 7, 2017

Hi @jeffreyroberts I've answered in SO, let me paste it here too for reference

https://stackoverflow.com/a/46034035/365877

You can load permissions.php file from the Plugin as it is doing now, but change the contents of permissions.php to preserve existing permissions defined in configuration, for example:

config/permissions.php

$permissions = [
// add your app permissions here
[
// ...
],
];

// there are more permissions in this config key, defined across your plugins
$morePermissions = \Cake\Core\Configure::read('MyPermissions');
$allPerms = array_merge($permissions, $morePermissions);

return ['CakeDC/Auth.permissions' => $allPerms];

Then inside each plugin you could have:

YOUR_PLUGIN/config/bootstrap.php

$permissions = \Cake\Core\Configure::read('MyPermissions');
$someMorePermissions = [
[
// permissions injected into the app from this plugin
]
];

$permissions = array_merge((array)$permissions, $someMorePermissions);
\Cake\Core\Configure::write('MyPermissions', $permissions);

Allowing each plugin to dynamically inject/manage permissions into the app.

I've created a c9.io environment with this code here https://ide.c9.io/steinkel/users-35-custom-permissions

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

No branches or pull requests

2 participants