We have the following use case scenario on our website:
When a new subuser is created it is assigned the role "premium subscription". When the group owner has the role "free subscription" then the subuser should ALSO receive the "free subscription" role. Its not possible to currently do this in the subuser module.
There are two approaches
Approach (1) We should have a new checkbox (in the subuser admin settings) that says: "Assign the subuser the same roles as parent on creation (except the special role which is exempt from cascading behaviour)."
What will happen in this approach will be that the subuser will be assigned exactly the same roles as the parent account (except the exempt role) during account creation.
Approach(2) We create a new hook that gives an opportunity for other modules to change the $roles that the subuser is going to be assigned.
The patch is small
diff --git a/htdocs/sites/all/modules/subuser/subuser.pages.inc b/htdocs/sites/all/modules/subuser/subuser.pages.inc
index e868431..eb59281 100644
--- a/htdocs/sites/all/modules/subuser/subuser.pages.inc
+++ b/htdocs/sites/all/modules/subuser/subuser.pages.inc
@@ -268,6 +268,16 @@ function subuser_create_form_submit($form, &$form_state) {
$roles = array();
}
+ // Custom Patch Start
+ $parent_user_id = $form_state['values']['parent_user'];
+ $parent_user = user_load($parent_user_id);
+ // Give each module a chance to change the $roles array
+ foreach (module_implements('subuser_create_alter_roles') as $module) {
+ $func = $module .'_subuser_create_alter_roles';
+ $func($roles, $parent_user);
+ }
+ // Custom Patch End
+
if (!$admin && array_intersect(array_keys($form_state['values']), array('uid', 'roles', 'init', 'session', 'status'))) {
watchdog('security', 'Detected malicious attempt to alter protected user fields.', array(), WATCHDOG_WARNING);
$form_state['redirect'] = 'user/register';
Once this hook is available achieving our use case becomes simple:
function mymodule_subuser_create_alter_roles(&$subuser_roles, $parent_account){
// Search for free subscription role in parent account
$key = array_search('free subscription', $parent_account->roles, TRUE);
// If parent has free subscription as role then assign the subuser the same role
if($key !== FALSE){
if(!array_key_exists($key, $subuser_roles)){
$subuser_roles += array($key => $key);
}
}
Now module maintainers may want to take up Approach (1) or Approach (2) or both.... Please give feedback on what makes sense and what is likely to get implemented in the module.
Thanks,
Sidharth
Comments
Comment #1
blakehall commentedLet's go with both approaches.
I think it definitely makes sense to be assigning the same roles (excluding the exempt role) for the subuser accounts.
It also makes sense to provide an alter hook, in case other developers need to do something more complicated / funky.
I'd be interested in a patch that will accomplish both, or will add the functionality myself when I have time.
Comment #2
blakehall commentedThis patch, along with code to support both approaches has been committed to the 6.x-1.x branch.
Thanks Sid!