Jump to content

LoginRegister: Set custom roles


LAPS
 Share

Recommended Posts

I am using the LoginRegister module and I would like to set custom roles for new registered users.

I read the documentation and the LoginRegister.module source code. In the latter there is:

* CONFIGURATION PROPERTIES
* [...]
* @property array $registerRoles Name of roles to add to users created from register form.
* [...]

I am not sure how to set roles, but I am pretty sure it should be made in a hook (maybe, the 'createUserReady'). Can you help me in setting custom roles for new registered users?

Link to comment
Share on other sites

I needed something similar today, still working on it.

To get you started (it require more check in the hook), in ready.php :

<?php

wire()->addHookBefore('LoginRegister::createdUser', function($event) {
    $u = $event->arguments[0];
    $u->roles->add(wire('roles')->get("custom-role"));
    $u->save();
});

 

Edit:  used LoginRegister::createdUser instead because the role was added but not took into account.

Edited by flydev
hook change
  • Like 2
Link to comment
Share on other sites

  • 8 months later...

Hi @flydev!

I try to use this hook, but doesn't work for me.

If I use $u->roles->add(wire('roles ')->get ("bs-user")) give me an error "unexpected $u (T_VARIABLE)

I try to use $u->addRole('bs-user')  like is in the documentation, but also this not work.

I try also to use $u->of(false) ... $u->of(true)  for see if need a new formatting before assigning values to a user. But the user does not register.

How can I add a specific role from a hook?

 

UPDATE:

 

The problem not was 

  wire()->addHookBefore('LoginRegister::createdUser', function($event) {
      $u = $event->arguments[0]; 
      $u->addRole('bs-user');
      $u->save();
  });

But in the way I rendering the registration form: 

If I use  $input->get->register = 1 for rendering only the registration form the hook doesn't work.

I have to call also the login form

 <?php

  $login = $modules->get('LoginRegister');
  echo $login->execute();

  $Register = $modules->get('LoginRegister');
  $input->get->register = 1;
  echo $Register->execute();

?>

 

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