Jump to content

how to set role permissions though api


Manol
 Share

Recommended Posts

Each role object has some additional functions to help with checking, adding and removing permissions:

// Add
$role->addPermission("page-edit");

// Remove
$role->removePermission("page-edit");

// Don't forget to save
$role->save();
 

The parameter (here, "page-edit"), can be a permission ID, name, or instance of a permission (i.e. doing $permissions ->get() or ->find())

This was taken from the reference on the cheatsheet - $roles - click [+].

  • Like 3
Link to comment
Share on other sites

I've got that:

	<?php 
		// fieldgroup
		$fg = new Fieldgroup();
		$fg->name = "myfg";
		$fg->add("title"); // add some fields
		$fg->save();

		// template
		$t = new Template();
		$t->name = "mytpl";
		$t->fieldgroup = $fg;
		$t->save();  

	?>

and wish to add access ( view, edit, create,a add children to this template ) to certain roles, and remove access to this template to other roles?

When I create a template it gives view access to all roles but I'm interested in getting something like the image down.

post-1191-0-94069600-1388763484_thumb.pn

Link to comment
Share on other sites

Hello Martijn.

    Your code is working I used

		$t->useRoles = 1;
		$t->set("roles", array('1316','2006')); // where 1234 is the ID of the role you want to add.

It allows those roles to view pages using this templates but edit, create and add.



Hi Soma.

    This page will have many roles and users I prefer do to it automatically with the api.

  • Like 1
Link to comment
Share on other sites

This code that I just wrote might help:

// Lets clone the "mallitoimipiste" that has all the required pages
$fc = $this->pages->clone($model);
$fc->title = $this->input->post->name;
$fc->name = $name;
$fc->location->address = $this->input->post->address;
$fc->removeStatus(Page::statusLocked);
$fc->save();

// create role for this fitness-center
$fcrole = $this->roles->add("fitness-center-" . $name);
$fcrole->addPermission("page-view");
$fcrole->addPermission("page-edit");
$fcrole->addPermission("page-sort");
$fcrole->addPermission("profile-edit");
$fcrole->save();

// add template permissions for the new role in fitness-center template
$fctemplate = $this->templates->get("fitness-center");

$addRoles = $fctemplate->get("addRoles");
$addRoles[] =$fcrole->id;

$editRoles = $fctemplate->get("editRoles");
$editRoles[] =$fcrole->id;

$createRoles = $fctemplate->get("createRoles");
$createRoles[] =$fcrole->id;

$fctemplate->roles->add($fcrole);
$fctemplate->set("addRoles", $addRoles);
$fctemplate->set("editRoles", $editRoles);
$fctemplate->set("createRoles", $createRoles);
$fctemplate->save();

// add the current role as a edit_role for the page
$fc->edit_roles->add($fcrole);
$fc->save();


Not sure if that set("createRoles", $createRoles) is even required, probably works with ->add just like "roles" does.

  • Like 2
Link to comment
Share on other sites

Marvelous, easy when you now how to.

		$t->useRoles = 1;
		$t->set("roles", array('1316','2006')); //view
		$t->set("editRoles", array('1316','2006'));  //edit
		$t->set("createRoles", array('1316','2006')); //create
		$t->set("addRoles", array('1316','2006')); //add children

The array contains as Martijn said the roles's ids.

Thanks to you three.

  • Like 2
Link to comment
Share on other sites

  • 2 years later...

having trouble adding a role permission on module install. But role my custom permission $roleName = "subscriber"; never gets set on the new role. Any help would be appreciated.

// add new role name
$role = new Page();
$roleParent = $this->pages->get('id=30');
$role->parent = $roleParent;
$role->name = $roleName;
$role->template = "role";
$role->set($roleName, true);

$role->save();

 

Link to comment
Share on other sites

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...