This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.

desk.abhimanyu19081
desk.abhimanyu19081

I have 2 groups in the User Groups table called Registered and Guest. I wan to add the user to the Guest group upon registration and to the Registered group upon successful activation. Both of these events should happen automatically. I can't figure out how to achieve this and I don't want to use any external plugin and use the roles and permissions as that is absolutely unnecessary in my case. Can someone please help me?

baomartin
baomartin

Hello, my answer is based partially on the answer here:

http://stackoverflow.com/questions/40730841/update-user-group-octobercms-rainlab-user-plugin

What you have to do:

  1. create a simple plugin
  2. in it's boot method, extend the user model by adding addUserGroup method (This is not absolutely necessary, but is cleaner in my opinion. Moreover, you can add groups that do not exist yet).
  3. Listen to the rainlab.user.activate event and simply add the newly activated user to a desired group

Codewise, step 2 and step 3 would look something like this:

public function boot()
{
    // extend user model with addUserGroup method
    UserModel::extend(function($model) {
        $model->addDynamicMethod('addUserGroup', function($group) use ($model) {
            if ($group instanceof Collection) {
                return $model->groups()->saveMany($group);
            }

            if (is_string($group)) {
                $group = UserGroup::whereCode($group)->first();

                return $model->groups()->save($group);
            }

            if ($group instanceof UserGroup) {
                return $model->groups()->save($group);
            }
        });
    });

    // assign desired group to newly activated user
    Event::listen('rainlab.user.activate', function($user) {
        $user->addUserGroup(UserGroup::whereCode('participant')->first());
    });
}

Hope it helps.

Last updated

adeerwin1422544
adeerwin1422544

ask

how if i want to delete user and delete relation group too,?

corymillz40435183
corymillz40435183

baomartin said:

Hello, my answer is based partially on the answer here:

http://stackoverflow.com/questions/40730841/update-user-group-octobercms-rainlab-user-plugin

What you have to do:

  1. create a simple plugin
  2. in it's boot method, extend the user model by adding addUserGroup method (This is not absolutely necessary, but is cleaner in my opinion. Moreover, you can add groups that do not exist yet).
  3. Listen to the rainlab.user.activate event and simply add the newly activated user to a desired group

Codewise, step 2 and step 3 would look something like this:

public function boot() { // extend user model with addUserGroup method UserModel::extend(function($model) { $model->addDynamicMethod('addUserGroup', function($group) use ($model) { if ($group instanceof Collection) { return $model->groups()->saveMany($group); }

           if (is_string($group)) {
               $group = UserGroup::whereCode($group)->first();

               return $model->groups()->save($group);
           }

           if ($group instanceof UserGroup) {
               return $model->groups()->save($group);
           }
       });
   });

   // assign desired group to newly activated user
   Event::listen('rainlab.user.activate', function($user) {
       $user->addUserGroup(UserGroup::whereCode('participant')->first());
   });

}

Hope it helps.

How do two registration forms for two user groups know which group it belongs to?

baomartin
baomartin

corymillz40435183: That depends on the implementation ... but you could for example check for a current path in the plugin. It would not be very reusable, but for a one-time use it should be fine.

corymillz40435183
corymillz40435183

baomartin said:

corymillz40435183: That depends on the implementation ... but you could for example check for a current path in the plugin. It would not be very reusable, but for a one-time use it should be fine.

How exactly do I do that. I checked up request::url() but don't know how to add it up. Places i have tried broke the code

nolybom
nolybom

For everyone facing this, here is how:

https://github.com/rainlab/user-plugin/issues/352#issuecomment-486762313

Last updated

1-7 of 7

You cannot edit posts or make replies: the forum has moved to talk.octobercms.com.