Is there an action which assigns role/s to users?
i'm using workflow module to create an applycation form for a "membership", that way the administrator should be able to change among the workflows (aprove, pending, unaprove). i need that if the administrator changes the workflow to "aproved" then the user who filled that form (the author) would automatically be assign to a specific role which will grant him with new privilegies.

Comments

Soren Jones’s picture

/**
 * Implementation of a Drupal action.
 * This action assigns a role to the node author.
 */
function action_role_to_author($op, $edit = array(), &$node) {
  switch($op) {
    case 'metadata':
      return array(
        'description' => t('Assign a role to the current user'),
        'type' => t('Role'),
        'batchable' => TRUE,
        'configurable' => TRUE,
      );

    case 'do':
      $rolelist = user_roles(TRUE);

      $added = array();

      $uid = $node->author;
      
      global $user;
      $name = $user->name;

      foreach ($edit['roles'] as $rid) {
        // Why is role '0' in the $edit['roles'] array?
        if (!empty($rid)) {
          if (!db_num_rows(db_query('SELECT * FROM {users_roles} WHERE uid = %d AND rid = %d', $uid, $rid))) {
            if (db_affected_rows(db_query('INSERT INTO {users_roles} (uid, rid) VALUES (%d, %d)', $uid, $rid))) {
              $added[] = $rolelist[$rid];
            }
            else {
              watchdog('error', t('Could not add role "%role" to %user.', array('%role' => $rolelist[$rid], '%user' => $name)));
            }
          }
        }
      }
      if (!empty($added)) {
        watchdog('notice', t('Roles added to %user: %roles', array('%user' => $name, '%roles' => implode(', ', $added))));
      }
      
      cache_clear_all("{$uid}:", 'cache_menu', TRUE);

    case 'form':
    
      // we need to capture the following:
      // - roles to assign

      // default values for form
      if (!isset($edit['roles'])) $edit['roles'] = array();
      $form = array();

      // add form components
       $form['roles'] = array(
        '#type' => 'checkboxes',
        '#title' => t('Roles'),
        '#default_value' => $edit['roles'],
        '#options' => user_roles(TRUE),
        '#required' => TRUE,
        '#description' => t('The selected role or roles will be assigned to the current user.'),
      );
      return $form;

    // validate the HTML form
    case 'validate':
      return TRUE;

    // process the HTML form to store configuration
    case 'submit':
      $params = array(
        'roles' => $edit['roles'],
      );
      return $params;
  }
}

This is an idea that borrows from #2 in 156705 and ec_roles.module. There's probably a better way to do this, but this seems to work.

newmediaist’s picture

I just tried this code and can't seem to get it to work - has anyone had success testing it?
Thanks -

leanazulyoro’s picture

Status: Active » Closed (fixed)

i used the workflow-ng module to achive this (http://drupal.org/project/workflow_ng), good luck

newmediaist’s picture

Can you elaborate? I have workflow_ng installed but can't figure out how to assign a role based on an action, as the supplied PHP code doesn't seem to work for me :(

moshe weitzman’s picture

there is a user operation already - maybe reuse that code.

egbertb’s picture

Did you achieve this functionality?
I need something similar for D6: an action to change the role of all users belonging to a group, when a specified field (I think best thing is a workflow-state) in the group node changes.