This forum has moved to a new location and is in read-only mode. Please visit talk.octobercms.com to access the new location.
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?
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:
- create a simple plugin
- 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).
- 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
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:
- create a simple plugin
- 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).
- Listen to the
rainlab.user.activate
event and simply add the newly activated user to a desired groupCodewise, 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?
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.
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
For everyone facing this, here is how:
https://github.com/rainlab/user-plugin/issues/352#issuecomment-486762313
Last updated
1-7 of 7