Jump to content

Recommended Posts

Posted

Need some help, how can I set view, edit, delete, move..., permissions through the api?

    $role = new Role();
    $role->name= "myrole;
    $role->save();
    $role->set("page-edit",true);

Thanks.

Posted

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
Posted

How about setting the language?

		$user = new User();
		$user->name= "me";
		$user->pass = "me";
		$user->addRole("guest");
		$user->addRole("myrole");
		$user->language=1;
Posted

I'm not sure about this, but could you try:

$u->language = $languages->get("english");

ps, I think $user is a bad variable name, because $user is the current user.

Also note that the guest role is added automagically.

  • Like 1
Posted

You're totally right about using $user, big mistake from my part.

One last question, how can I set access ( view, edit, create,a add children ) to certain roles?

Thank you.

Posted

Assumed that the role is called 'member':

click: Admin » Access » Roles » member, There you could set permissions then at template level, you can specify select them

Posted

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

Posted

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
Posted

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
Posted

I just was thinking if you possibly create new templates, roles and permission for every new user which may wouldn't scale well.

Posted

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
  • 2 years later...
Posted

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();

 

Posted

That did the trick - i was adding it as a page in the admin instead of simply creating a new Role() and using the above

$role->addPermission("permissionName");
  • Like 2

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
  • Recently Browsing   0 members

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