Jump to content

[SOLVED] How to Add/Remove permissions via API?


PWaddict
 Share

Recommended Posts

Here is what I want to do:

if ($page->my_checkbox) {

//Remove permission "my-permission" from role "client"
//Remove permission "page-add" from template "my-template" from role "client"

} else {

//Add permission "my-permission" on role "client"
//Add permission "page-add" on template "my-template" on role "client"

}

I would really appreciate your help.

Link to comment
Share on other sites

Here is the first part for simply removing/adding a permission from specific role:

$client = $roles->get('client');
if ($page->my_checkbox) {
	if ($client->hasPermission('my-permission')) {
		$client->of(false);
		$client->removePermission('my-permission');
		$client->save();
	}
} else {
	if (!$client->hasPermission('my-permission')) {
		$client->of(false);
		$client->addPermission('my-permission');
		$client->save();
	}
}

@Zeka What I don't understand is how to remove permission "page-add" from template "my-template" from role "client".

  • Like 1
Link to comment
Share on other sites

I solved it ?

$client = $roles->get('client');
$s_template = $templates->get("my-template");

if ($page->my_checkbox) {
	if ($client->hasPermission('my-permission')) {

		$client->of(false);
		$client->removePermission('my-permission');
		$client->save();

		$s_template->revokePermissionByRole("page-add", $client);
		$s_template->save();

	}
} else {
	if (!$client->hasPermission('my-permission')) {

		$client->of(false);
		$client->addPermission('my-permission');
		$client->save();

		$s_template->addPermissionByRole("page-add", $client);
		$s_template->save();

	}
}

 

  • Like 2
Link to comment
Share on other sites

  • PWaddict changed the title to [SOLVED] How to Add/Remove permissions via API?

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...