--- role_delegation.module.back 2009-10-07 21:18:44.000000000 -0700 +++ role_delegation.module 2009-10-07 22:21:50.000000000 -0700 @@ -162,71 +162,60 @@ function _role_delegation_make_perm($rol return "assign $role role"; } -/** - * Implementation of hook_form_alter(). - */ -function role_delegation_form_alter(&$form, $form_state, $form_id) { - // Only alter user form when user can't assign permissions without Role Delegation. - if ($form_id != 'user_register' && $form_id != 'user_profile_form') { - return; - } - if (user_access('administer permissions')) { - return; - } - // Split up roles based on whether they can be delegated or not. - $current_roles = (isset($form['#uid']) && $user = user_load(array('uid' => $form['#uid']))) ? $user->roles : array(); - $rids_default = array(); - $rids_preserve = array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID); - $roles_preserve = array('authenticated user'); - $roles_options = array(); - $roles = _role_delegation_roles(); - foreach ($roles as $rid => $role) { - if (user_access('assign all roles') || user_access(_role_delegation_make_perm($role))) { - if (array_key_exists($rid, $current_roles)) { - $rids_default[] = $rid; - } - $roles_options[$rid] = $role; - } - else { - if (array_key_exists($rid, $current_roles)) { - $rids_preserve[$rid] = $rid; - $roles_preserve[] = $role; - } - } - } - if (empty($roles_options)) { - // No role can be assigned. - return; - } - // Generate the form items. - $form['roles_preserve'] = array( - '#type' => 'value', - '#value' => $rids_preserve, - ); - $assign_item = array( - '#type' => 'checkboxes', - '#title' => t('Roles'), - '#description' => t('The user receives the combined permissions of the %roles role(s), and all roles selected here. ', array('%roles' => implode(', ', $roles_preserve))), - '#options' => $roles_options, - '#default_value' => $rids_default, - ); - if (isset($form['account'])) { - $form['account']['roles_assign'] = $assign_item; - } - else { - $form['roles_assign'] = $assign_item; - } -} /** * Implementation of hook_user(). */ function role_delegation_user($op, &$edit, &$account, $category = NULL) { - if ($op != 'insert' && $op != 'submit') { - return; - } - if (!isset($edit['roles_assign'])) { - return; - } - $edit['roles'] = $edit['roles_preserve'] + array_filter($edit['roles_assign']); + switch ($op) { + case 'insert': + case 'submit': + if (!isset($edit['roles_assign'])) { + return; + } + $edit['roles'] = $edit['roles_preserve'] + array_filter($edit['roles_assign']); + break; + case 'form': + if ($category == 'account') { + // Split up roles based on whether they can be delegated or not. + $current_roles = $account->roles; + $rids_default = array(); + $rids_preserve = array(DRUPAL_AUTHENTICATED_RID => DRUPAL_AUTHENTICATED_RID); + $roles_preserve = array('authenticated user'); + $roles_options = array(); + $roles = _role_delegation_roles(); + foreach ($roles as $rid => $role) { + if (user_access('assign all roles') || user_access(_role_delegation_make_perm($role))) { + if (array_key_exists($rid, $current_roles)) { + $rids_default[] = $rid; + } + $roles_options[$rid] = $role; + } + else { + if (array_key_exists($rid, $current_roles)) { + $rids_preserve[$rid] = $rid; + $roles_preserve[] = $role; + } + } + } + if (empty($roles_options)) { + // No role can be assigned. + return; + } + // Generate the form items. + $form['roles_preserve'] = array( + '#type' => 'value', + '#value' => $rids_preserve, + ); + $assign_item = array( + '#type' => 'checkboxes', + '#title' => t('Roles'), + '#description' => t('The user receives the combined permissions of the %roles role(s), and all roles selected here. ', array('%roles' => implode(', ', $roles_preserve))), + '#options' => $roles_options, + '#default_value' => $rids_default, + ); + $form['roles_assign'] = $assign_item; + return $form; + } + } }