Jump to content

Adding permission to the Template doesnt reflect when user logs in


madhu1
 Share

Recommended Posts

I m very new to Processwire, I have worked in PHP but not much experience with Processwire. I have created a template 'school_acvitity'. Now, at runtime i create a new role 'school1' and assign that role edit, create and add permission by using Processwire API.

It shows the fields(edit, create , new) marked in the template Access tab for the role school1.

But when the user with the role school1 login it doesnt get the option for edit new.

After this if I manually deselect the permission to create new pages through admin, the user gets the option to create new pages.

the code is :

    $editRoles = $template_->get('editRoles');
    $addRoles = $template_->get('addRoles');
    $createRoles = $template_->get('createRoles');
  
       
    $editRoles[] = $id;
    $addRoles[] = $id;
    $createRoles[] = $id;
 

    $template_->set('editRoles',$editRoles);
    $template_->set('addRoles',$addRoles);
    $template_->set('createRoles',$createRoles);

      
    $template_->save();

Appreciate any help for the above issue.

Link to comment
Share on other sites

Hi madhu1 and welcome to PW.

I noticed you just posted this in another board. You'll find we're a pretty responsive bunch here, so no need to post the same thing multiple times.

I am having trouble understanding exactly what your code is trying to accomplish, especially the $editRoles[] = $id section - What is $id being defined as? I also have no idea what $template_ is. Are you defining this somewhere else too?

Here is an idea of how to add a new role:

$newrole = new Role();
$newrole->name= "school1";
$newrole->save();

I think you have managed to do this already, but it's not in your code.

This post from Soma explains how to add a new role to the list of editRoles on a template:

https://processwire.com/talk/topic/2374-batch-allow-template-edit-access-to-new-role/?p=22366

Maybe you are already attempting to do this though with the $editRoles[] = $id

Please post all your code - it will make it easier to figure out where things are going wrong.

  • Like 1
Link to comment
Share on other sites

Thanks  :)

I thought I have posted my issue at the wrong location. Apologies for that. :)

I must admit that the people reply to the queries quicker.

Coming to the issue:

I get the variable $template_ as follows

$template_ = $templates->get('school_acvitity');

then I pass it to the function and $id is the ID of Role that should have permission

setPermission($id, $template_);

this Function have the above code.

After applying the permissions when I check it in the Access tab of the template I get the checked boxes for create, new, edit.

But when user login he doesn't get the option creating new page.

Now, If i uncheck the permission for that role through admin it works. Its Strange..

the entire code is:

$page_role = $page->get("page_role");

$id = $page_role->id;

$template_ = $templates->get('school_acvitity');
setPermission($id, $template_);

function setPermission($id,$template_){

    $editRoles = $template_->get('editRoles');
    $addRoles = $template_->get('addRoles');
    $createRoles = $template_->get('createRoles');

    $editRoles[] = $id;
    $addRoles[] = $id;
    $createRoles[] = $id;
  

    $template_->set('editRoles',$editRoles);
    $template_->set('addRoles',$addRoles);
    $template_->set('createRoles',$createRoles);
  
      
    $template_->save();
}

Link to comment
Share on other sites

I don't understand the assigning of $id to the id of a page named "page_role". Are you trying to get the id of an access role? That is what you would need to then assign to the template's edit/add/create/Roles.

EDIT: You can get the id of school1 with:

$id = $roles->get("school1")->id;
  • Like 1
Link to comment
Share on other sites

I guess I don't understand your setup, but it sounds like you have a field called page_role in a template that allows users to select a role when viewing a particular page? I think I also need to see a schematic of your page, template and field structure to understand what is going on. Maybe someone else can see what I am missing?

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