Manol Posted January 3, 2014 Posted January 3, 2014 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.
Craig Posted January 3, 2014 Posted January 3, 2014 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 [+]. 3
Manol Posted January 3, 2014 Author Posted January 3, 2014 How about setting the language? $user = new User(); $user->name= "me"; $user->pass = "me"; $user->addRole("guest"); $user->addRole("myrole"); $user->language=1;
Martijn Geerts Posted January 3, 2014 Posted January 3, 2014 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. 1
Manol Posted January 3, 2014 Author Posted January 3, 2014 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.
Martijn Geerts Posted January 3, 2014 Posted January 3, 2014 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
Martijn Geerts Posted January 3, 2014 Posted January 3, 2014 // never done this, but I think this may work $member_role = $roles->get('member'); $member_role->addPermission('page-edit'); $member_role->save();
Manol Posted January 3, 2014 Author Posted January 3, 2014 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.
Martijn Geerts Posted January 3, 2014 Posted January 3, 2014 Think you have to set: $t->useRoles = 1; $t->set("roles", array('1234')); // where 1234 is the ID of the role you want to add. 1
Soma Posted January 3, 2014 Posted January 3, 2014 Why do you want to create and set all this things by API? Can't you just use admin UI?
Manol Posted January 3, 2014 Author Posted January 3, 2014 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. 1
apeisa Posted January 3, 2014 Posted January 3, 2014 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. 2
Soma Posted January 3, 2014 Posted January 3, 2014 I just was thinking if you possibly create new templates, roles and permission for every new user which may wouldn't scale well.
Manol Posted January 3, 2014 Author Posted January 3, 2014 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. 2
benbyf Posted September 19, 2016 Posted September 19, 2016 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();
adrian Posted September 19, 2016 Posted September 19, 2016 $newrole = new Role(); $newrole->name= $userrole; $newrole->save(); 2
benbyf Posted September 19, 2016 Posted September 19, 2016 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"); 2
Recommended Posts
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 accountSign in
Already have an account? Sign in here.
Sign In Now