Jump to content
cybromancy

Change user role not working

Recommended Posts

Hi there, going a bit crazy here. Trying to be a user and change the role of another user.

//get the user id to change

$changeRole = $users->get($sanitizer->username('username'))->id;

//add role

$changeRole->addRole('moderator');

//save changes

$users->save($changeRole);

 

What am I missing? Is it a user permissions setting? All I saw were permission settings for pages.

Thanks up front for the help.

Share this post


Link to post
Share on other sites

Hi @cybromancy - welcome to the forums!

You are assigning the user's ID to the $changeRole variable. You need to keep the full user object assigned to it so that addRole() can do its thing!

Let us know if you have anymore troubles.

Share this post


Link to post
Share on other sites

thanks for the reply I changed my code, but it's still throwing an error

$changeRole = $users->get($sanitizer->username('username'));
//add role
$changeRole->addRole('moderator');
//save changes
$users->save($changeRole);

Do I need to be using:

    $users->of(true);

or

  $changeRole>roles->add($roles->get('moderator));

Share this post


Link to post
Share on other sites
3 minutes ago, cybromancy said:

thanks for the reply I changed my code, but it's still throwing an error


$changeRole = $users->get($sanitizer->username('username'));
//add role
$changeRole->addRole('moderator');
//save changes
$users->save($changeRole);

Do I need to be using:

    $users->of(true);

or

  $changeRole>roles->add($roles->get('moderator));

Yeah, you will need something like this:

$u = $users->get($sanitizer->username('username'));
$u->of(false);
$u->addRole("moderator");
$u->save();

You should have received an error that mentions specifically that you need to turn of output formatting. Do you have debugmode on and/or TracyDebugger installed?

  • Like 2

Share this post


Link to post
Share on other sites

That was it THANKS.

I got this as an error:

Call $page->setOutputFormatting(false) before getting/setting values that will be modified and saved.

Is that what the 'of' stands for in of(false) ? outputformatting?

Call $page->setOutputFormatting(false) before getting/setting values that will be modified and saved.

  • Like 1

Share this post


Link to post
Share on other sites
Just now, cybromancy said:

Is that what the 'of' stands for in of(false) ? outputformatting?

Yep, of() is just a shortcut for the full setOutputFormatting() method.

  • Like 2

Share this post


Link to post
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

  • Recently Browsing   0 members

    No registered users viewing this page.

  • Similar Content

    • By VeiJari
      Hi, this is the first we are trying to make a page that has only one type of user that has access to every page. 
      The other users should only have a given access to specific pages, not to the whole template.
      My structure
      -Field -Organisation -Project -Report I want that the "measurer" role only has access to "project x" and it's children, but no view access to every project, organisation or field. I've tried to do this with https://modules.processwire.com/modules/page-edit-per-user/ but it still needs a view access to the whole tree to see the "project x" page. Or is there something I haven't figured out?
      Maybe I have to make it via the API: a select field in the "organisation" template where the admins could add the users and then I use hook to update the privileges?
      Have you done something like this and how did you accomplish it?
      Any help would be appreciated.
       
    • By Pip
      Hi everyone!
      I'm trying out the Login/Register module for my site. Noted that the module assigns the newly registered user to login-register role. 
      Once you modify the login-register role's permissions, particularly adding page-edit, the new member role will be set to guest. 
      Thing is I'd like to grant my new users the power to create their own pages. Any advice? 
      Thanks. 
    • By neonwired
      I'm hoping someone has seen this before. There doesn't appear to be an issue with the user info.

       

    • By prestoav
      Hi there,
      I have a marketplace site that used the older Fredi module for front end editing so advertisers can create and edit their adverts (in the form of pages). After upgrading to v3.0 of Processwire I'd like to change the editing method for users to the new, core front end editing methods. However, it seems that with this method any editor can edit any page (subject to template restrictions for their user role). Using Fredi I checked the user and created user were equal before displaying edit buttons (clunky but it worked).
      Is there any stock way to restrict front end editing to the creator of the page so that my advertisers can only edit their own ads or should I test for user as before?
      Thanks in advance as always 🙂
    • By DooM
      Hey guys!
      I try to achieve a possibility for the client's admin account to change other users privileges. 
      In the CMS I have superadmin account, which is for me. My client got an account with a role user, which is limited administration account. The client also got some other people, who will edit the content with role editor  
      User story would look somehow like this:
      user role can add new roles and users user role can change privileges for all users (except superadmin). Eg. When the user role gives the editor role privilege to edit only articles. The Editor role will be able to edit only them.  
      I know this functionality is possible for superadmin, but I couldn't simulate it on the user role. Is there any easy way to achieve such a feature? Otherwise, I'll have to make a module for this I guess.
      Thanks for any help!
×
×
  • Create New...