? autoassignrole.admin.inc Index: autoassignrole.info =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/autoassignrole/autoassignrole.info,v retrieving revision 1.5 diff -u -p -r1.5 autoassignrole.info --- autoassignrole.info 25 Sep 2009 16:08:43 -0000 1.5 +++ autoassignrole.info 10 Oct 2010 20:22:42 -0000 @@ -1,4 +1,9 @@ ; $Id: autoassignrole.info,v 1.5 2009/09/25 16:08:43 cyberswat Exp $ -name = "Auto Assign Role" -description = "Designate a role to assign all new users to." -core = 6.x +name = Auto Assign Role +description = Automatically assign a role to new users. +core = 7.x +configure=admin/config/autoassignrole +version = VERSION +files[] = autoassignrole.module +files[] = autoassignrole.admin.inc +files[] = autoassignrole.test Index: autoassignrole.install =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/autoassignrole/autoassignrole.install,v retrieving revision 1.13 diff -u -p -r1.13 autoassignrole.install --- autoassignrole.install 10 Oct 2010 12:32:34 -0000 1.13 +++ autoassignrole.install 10 Oct 2010 20:22:42 -0000 @@ -4,238 +4,5 @@ /** * @file * - * The autoassignrole.install file - * - * Installs the {autoassignrole} table and inserts default data as well as - * provides uninstall functionality. + * Installation related functionality for the auto assign role module. */ - -/** - * Implementation of hook_install(). - */ -function autoassignrole_install() { - drupal_install_schema('autoassignrole'); - $sql = "INSERT into {autoassignrole} (arid,value) VALUES ('%s', '%s')"; - db_query($sql, 'auto_roles', serialize(array())); - db_query($sql, 'auto_active', 0); - db_query($sql, 'user_active', 0); - db_query($sql, 'user_roles', serialize(array())); - db_query($sql, 'user_required', 0); - db_query($sql, 'user_multiple', 0); - db_query($sql, 'user_description', ''); - db_query($sql, 'user_title', t("Role")); - db_query($sql, 'user_fieldset_title', t("User Roles")); - db_query($sql, 'user_sort', 'SORT_ASC'); - db_query($sql, 'user_selection', 2); - db_query($sql, 'page_active', 0); - db_query($sql, 'auto_admin_active', 0); - db_query($sql, 'node_user_register', ''); -} - -/** - * Implementation of hook_uninstall(). - */ -function autoassignrole_uninstall() { - drupal_uninstall_schema('autoassignrole'); -} - -/** - * Implementation of hook_schema(). - */ -function autoassignrole_schema() { - $schema['autoassignrole'] = array( - 'description' => t('Stores autoassignrole variables.'), - 'fields' => array( - 'arid' => array( - 'type' => 'varchar', - 'length' => 38, - 'not null' => TRUE, - 'default' => '', - 'description' => t('The id for this setting.'), - ), - 'value' => array( - 'type' => 'text', - 'not null' => FALSE, - 'description' => t('The value for this setting.'), - ), - ), - 'primary key' => array('arid'), - ); - $schema['autoassignrole_page'] = array( - 'description' => t('Stores autoassignrole page information.'), - 'fields' => array( - 'rid' => array( - 'type' => 'int', - 'not null' => TRUE, - 'description' => t('The rid for this page.'), - ), - 'display' => array( - 'type' => 'text', - 'not null' => TRUE, - 'description' => t('How to display the navigation to this page.'), - ), - 'path' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'description' => t('The path for this page.'), - ), - 'menu' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'description' => t('The default parent menu for this menu item.'), - ), - 'title' => array( - 'type' => 'text', - 'not null' => TRUE, - 'description' => t('The title for this page.'), - ), - 'description' => array( - 'type' => 'text', - 'size' => 'big', - 'not null' => FALSE, - 'description' => t('The description for this page.'), - ), - 'format' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => FALSE, - 'description' => t('The filter format to use on the description.'), - ), - 'weight' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => FALSE, - 'default' => 0, - 'description' => t('The weight of this item.'), - ), - 'registration' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => FALSE, - 'default' => 0, - 'description' => t('Should this item replace the default user registration page.'), - ), - ), - 'primary key' => array('rid'), - ); - return $schema; -} -function autoassignrole_update_6000() { - $items = array(); - $sql = "SELECT name FROM {variable} WHERE name LIKE 'AUTOASSIGNROLE_%%'"; - $vars = db_query($sql); - while ($var = db_fetch_object($vars)) { - variable_del($var->name); - } - $items[] = array("success" => 1, "query" => "Cleaned up old variables and installed new table"); - - autoassignrole_install(); - return $items; -} - -function autoassignrole_update_6001() { - $ret = array(); - - // Rename the old contrib actions table if it exists so the contrib version - // of the module can do something with the old data. - if (!db_table_exists('autoassignrole_page')) { - $schema['autoassignrole_page'] = array( - 'description' => t('Stores autoassignrole page information.'), - 'fields' => array( - 'rid' => array( - 'type' => 'int', - 'not null' => TRUE, - 'description' => t('The rid for this page.'), - ), - 'display' => array( - 'type' => 'text', - 'not null' => TRUE, - 'description' => t('How to display the navigation to this page.'), - ), - 'path' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'description' => t('The path for this page.'), - ), - 'menu' => array( - 'type' => 'varchar', - 'length' => 255, - 'not null' => TRUE, - 'description' => t('The default parent menu for this menu item.'), - ), - 'title' => array( - 'type' => 'text', - 'not null' => TRUE, - 'description' => t('The title for this page.'), - ), - 'description' => array( - 'type' => 'text', - 'size' => 'big', - 'not null' => FALSE, - 'description' => t('The description for this page.'), - ), - 'format' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => FALSE, - 'description' => t('The filter format to use on the description.'), - ), - 'weight' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => FALSE, - 'default' => 0, - 'description' => t('The weight of this item.'), - ), - 'registration' => array( - 'type' => 'int', - 'size' => 'tiny', - 'not null' => FALSE, - 'default' => 0, - 'description' => t('Should this item replace the default user registration page.'), - ), - ), - 'primary key' => array('rid'), - ); - - db_create_table($ret, 'autoassignrole_page', $schema['autoassignrole_page']); - } - - return $ret; -} - -function autoassignrole_update_6002() { - $ret = array(); - if (!db_column_exists('autoassignrole_page', 'menu')) { - $ret[] = update_sql('ALTER TABLE {autoassignrole_page} ADD menu VARCHAR(255)'); - } - return $ret; -} - -function autoassignrole_update_6003() { - $ret = array(); - if (!db_column_exists('autoassignrole_page', 'registration')) { - $ret[] = update_sql('ALTER TABLE {autoassignrole_page} ADD registration TINYINT default 0'); - } - return $ret; -} - -function autoassignrole_update_6004() { - $value = db_fetch_object(db_query("SELECT arid FROM {autoassignrole} WHERE arid = '%s'", 'auto_admin_active')); - if (!$value->arid) { - $sql = ""; - $ret[] = update_sql("INSERT into {autoassignrole} (arid,value) VALUES ('auto_admin_active', '0')"); - } - return $ret; -} -function autoassignrole_update_6005() { - $value = db_fetch_object(db_query("SELECT arid FROM {autoassignrole} WHERE arid = '%s'", 'node_user_register')); - if (!$value->arid) { - $sql = ""; - $ret[] = update_sql("INSERT into {autoassignrole} (arid,value) VALUES ('node_user_register', '')"); - } - return $ret; -} Index: autoassignrole.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/autoassignrole/autoassignrole.module,v retrieving revision 1.18 diff -u -p -r1.18 autoassignrole.module --- autoassignrole.module 10 Oct 2010 12:32:34 -0000 1.18 +++ autoassignrole.module 10 Oct 2010 20:22:42 -0000 @@ -11,673 +11,39 @@ */ /** - * Implementation of hook_menu(). - * - * @return array - */ -function autoassignrole_menu() { - $items = array(); - $items['admin/user/autoassignrole/autocomplete/node'] = array( - 'title' => 'Node autocomplete', - 'page callback' => 'autoassignrole_autocomplete_node', - 'access callback' => 'user_access', - 'access arguments' => array('administer autoassignrole'), - 'type' => MENU_CALLBACK, - 'file' => 'autoassignrole-admin.inc', - ); - $items['admin/user/autoassignrole'] = array( - 'title' => t('Auto assign role'), - 'description' => t('Designate a role to assign all new users to.'), - 'page callback' => 'drupal_get_form', - 'page arguments' => array('autoassignrole_admin_form'), - 'access arguments' => array('administer autoassignrole'), - 'type' => MENU_NORMAL_ITEM, - 'file' => 'autoassignrole-admin.inc', - ); - - // path based role assignments that are listed as a menu item - $result = db_query("SELECT rid, path, display, title, weight, menu FROM {autoassignrole_page}"); - while ($r = db_fetch_object($result)) { - switch ($r->display) { - case 0: - $items[$r->path] = array( - 'title' => check_plain($r->title), - 'page arguments' => array($r->rid), - 'page callback' => 'autoassignrole_path', - 'access callback' => '_autoassignrole_path_access', - 'file' => 'autoassignrole-path.inc', - 'weight' => $r->weight, - 'type' => MENU_NORMAL_ITEM, - 'menu_name' => $r->menu, - ); - $items[$r->path .'/register'] = array( - 'title' => 'Create new account', - 'page arguments' => array($r->rid), - 'page callback' => 'autoassignrole_path', - 'access callback' => '_autoassignrole_path_access', - 'file' => 'autoassignrole-path.inc', - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); - $items[$r->path .'/login'] = array( - 'title' => 'Log in', - 'access callback' => 'user_is_anonymous', - 'type' => MENU_LOCAL_TASK, - 'page callback' => '_autoassignrole_user_login', - 'file' => 'autoassignrole-path.inc', - ); - - $items[$r->path .'/password'] = array( - 'title' => 'Request new password', - 'access callback' => 'user_is_anonymous', - 'type' => MENU_LOCAL_TASK, - 'page callback' => '_autoassignrole_user_password', - 'file' => 'autoassignrole-path.inc', - ); - break; - // tabs on user registration pages - case 1: - $items['user/'. $r->path] = array( - 'title' => check_plain($r->title), - 'page arguments' => array($r->rid), - 'page callback' => 'autoassignrole_path', - 'access callback' => '_autoassignrole_path_access', - 'file' => 'autoassignrole-path.inc', - 'type' => MENU_LOCAL_TASK, - 'weight' => $r->weight - ); - break; - // no menu items - case 2: - $items[$r->path] = array( - 'title' => check_plain($r->title), - 'page arguments' => array($r->rid), - 'page callback' => 'autoassignrole_path', - 'access callback' => '_autoassignrole_path_access', - 'file' => 'autoassignrole-path.inc', - 'weight' => $r->weight, - 'type' => MENU_CALLBACK, - ); - $items[$r->path .'/register'] = array( - 'title' => 'Create new account', - 'page arguments' => array($r->rid), - 'page callback' => 'autoassignrole_path', - 'access callback' => '_autoassignrole_path_access', - 'file' => 'autoassignrole-path.inc', - 'type' => MENU_DEFAULT_LOCAL_TASK, - ); - $items[$r->path .'/login'] = array( - 'title' => 'Log in', - 'access callback' => 'user_is_anonymous', - 'type' => MENU_LOCAL_TASK, - 'page callback' => '_autoassignrole_user_login', - 'file' => 'autoassignrole-path.inc', - ); - - $items[$r->path .'/password'] = array( - 'title' => 'Request new password', - 'access callback' => 'user_is_anonymous', - 'type' => MENU_LOCAL_TASK, - 'page callback' => '_autoassignrole_user_password', - 'file' => 'autoassignrole-path.inc', - ); - break; - } - - } - return $items; -} - -function _autoassignrole_path_access() { - global $user; - if (variable_get('user_register', 1) && $user->uid == 0) { - return TRUE; - } - if (arg(0) == 'admin' && arg(2) == 'menu-customize' && user_access('administer menu', $user)) { - return TRUE; - } - - return FALSE; -} - -/** - * Implementation of hook_perm(). - * @return array - */ -function autoassignrole_perm() { - return array('administer autoassignrole'); -} - -/** - * Implementation of hook_user(). - */ -function autoassignrole_user($op, &$edit, &$account, $category = NULL) { - switch ($op) { - case 'insert': - // If this is an administrator creating the account only use auto_assign if - // allowed by auto_admin_active - if (arg(0) == 'admin' && _autoassignrole_get_settings('auto_admin_active') == 0) { - return; - } - $rolenames = user_roles(); - if (_autoassignrole_get_settings('user_active') == 1) { - $user_roles = _autoassignrole_get_settings('user_roles'); - if (is_array($edit['user_roles'])) { - foreach ($edit['user_roles'] as $k => $v) { - if ($v != 0 && in_array($k, $user_roles, TRUE)) { - $edit['roles'][$k] = $v; - } - } - } - elseif ($edit['user_roles'] != '') { - $edit['roles'][$edit['user_roles']] = $edit['user_roles']; - } - } - if ($rid = autoassignrole_get_active_path_rid()) { - $edit['roles'][$rid] = $rolenames[$rid]; - } - if (_autoassignrole_get_settings('auto_active') == 1) { - $auto_roles = _autoassignrole_get_settings('auto_roles'); - foreach ($auto_roles as $k => $v) { - $edit['roles'][$k] = $rolenames[$k]; - } - } - break; - case 'register': - if (_autoassignrole_get_settings('user_active') == 1) { - $roles = user_roles(TRUE); - $user_roles = _autoassignrole_get_settings('user_roles'); - $path_roles = db_query("SELECT rid from {autoassignrole_page}"); - while ($path_role = db_fetch_object($path_roles)) { - unset($roles[$path_role->rid]); - } - foreach ($roles as $k => $r) { - if (!in_array($k, $user_roles, TRUE)) { - unset($roles[$k]); - } - } - if (count($roles)) { - $form['autoassignrole_user'] = array( - '#type' => 'fieldset', - '#title' => _autoassignrole_get_settings('user_fieldset_title'), - '#collapsible' => FALSE, - '#collapsed' => FALSE, - ); - if (_autoassignrole_get_settings('user_sort') == "SORT_ASC") { - uasort($roles, '_autoassignrole_array_asc'); - } - else { - uasort($roles, '_autoassignrole_array_desc'); - } - - if (!$edit || !array_key_exists('user_roles', $edit)) { - $edit['user_roles'] = array(); - } - if (_autoassignrole_get_settings('user_selection') == 0) { - $form['autoassignrole_user']['user_roles'] = array( - '#type' => 'radios', - '#title' => t(_autoassignrole_get_settings('user_title')), - '#options' => $roles, - '#description' => t(_autoassignrole_get_settings('user_description')), - ); - } - if (_autoassignrole_get_settings('user_selection') == 1) { - $form['autoassignrole_user']['user_roles'] = array( - '#type' => 'select', - '#title' => t(_autoassignrole_get_settings('user_title')), - '#default_value' => '', - '#description' => t(_autoassignrole_get_settings('user_description')), - ); - - if (_autoassignrole_get_settings('user_multiple')) { - $form['autoassignrole_user']['user_roles']['#multiple'] = TRUE; - unset($form['autoassignrole_user']['user_roles']['#default_value']); - } - else { - $roles[''] = ''; - } - $form['autoassignrole_user']['user_roles']['#options'] = $roles; - } - if (_autoassignrole_get_settings('user_selection') == 2) { - $form['autoassignrole_user']['user_roles'] = array( - '#type' => 'checkboxes', - '#title' => t(_autoassignrole_get_settings('user_title')), - '#default_value' => $edit['user_roles'], - '#options' => $roles, - '#description' => _autoassignrole_get_settings('user_description'), - ); - } - if (_autoassignrole_get_settings('user_required')) { - $form['autoassignrole_user']['user_roles']['#required'] = TRUE; - } - return $form; - } - } - break; - } -} - -/** - * API function that returns an array of AAR's settings. - * - */ -function _autoassignrole_get_settings($value) { - // if we are dealing with a variable in the format of path_field_rid get - // variables from {autoassignrole_page} - if (preg_match('/^path_(\D[^_]*)_(\d*)/i', $value, $matches)) { - $result = db_fetch_array(db_query("SELECT rid, display, path, title, description, format, weight FROM {autoassignrole_page} WHERE rid = %d", $matches[2])); - if (count($result) > 1 && $matches[1] == 'active') { - return 1; - } - elseif (count($result) == 1 && $matches[1] == 'active') { - return 0; - } - else { - if (isset($result[$matches[1]])) { - return $result[$matches[1]]; - } - else { - return FALSE; - } - } - } - - if (preg_match('/^path_(\d*)/i', $value, $matches)) { - $result = db_fetch_array(db_query("SELECT path FROM {autoassignrole_page} WHERE rid = %d", $matches[1])); - if (isset($result['path'])) { - return $result['path']; - } - else { - return FALSE; - } - } - - // if we are not dealing with {autoassignrole_page} then get variables from - // {autoassignrole} - $settings = array(); - $result = db_query("SELECT arid, value from {autoassignrole}"); - while ($s = db_fetch_object($result)) { - $settings[$s->arid] = $s->value; - } - - // break apart auto_roles[x] and user_roles[x] - if (preg_match('/^(\D[^_]*_\D[^_]*)\[(\d*)]/i', $value, $matches)) { - $values = unserialize($settings[$matches[1]]); - return $values[$matches[2]]; - } - - switch ($value) { - case 'auto_roles': - case 'user_roles': - // return all instances as an array - $roles = $settings[$value]; - $roles = unserialize($roles); - if (!is_array($roles)) { - $roles = array(); - } - foreach ($roles as $k => $r) { - if ($r == 0) { - unset($roles[$k]); - } - } - return $roles; - break; - default: - if (isset($settings[$value])) { - return $settings[$value]; - } - else { - return FALSE; - } - break; - } -} - -function _autoassignrole_path($rid, $value) { - static $pages = array(); - if (count($pages) == 0) { - $result = db_query("SELECT rid, display, path, title, description from {autoassignrole_page}"); - while ($s = db_fetch_object($result)) { - $pages[$s->rid] = array('path' => $s->path, - 'display' => $s->display, - 'title' => $s->title, - 'description' => $s->description - ); - } - return _autoassignrole_get_settings($rid, $value); - } - else { - if (isset($pages[$rid][$value])) { - $return = $pages[$rid][$value]; - } - else { - $return = ''; - } - return $return; - } -} - -function _autoassignrole_user_input($args) { - switch ($args) { - case 'type': - if (_autoassignrole_get_settings('user_multiple') == 0) { - $type = 'radios'; - } - else { - $type = 'checkboxes'; - } - return $type; - break; - case 'required': - if (_autoassignrole_get_settings('user_required') == 0) { - $required = FALSE; - } - else { - $required = TRUE; - } - return $required; - break; - } -} - -function _autoassignrole_array_intersect_key($isec, $keys) { - $argc = func_num_args(); - if ($argc > 2) { - for ($i = 1; !empty($isec) && $i < $argc; $i++) { - $arr = func_get_arg($i); - foreach (array_keys($isec) as $key) { - if (!isset($arr[$key])) { - unset($isec[$key]); - } - } - } - return $isec; - } - else { - $res = array(); - foreach (array_keys($isec) as $key) { - if (isset($keys[$key])) { - $res[$key] = t($isec[$key]); - } - } - return $res; - } -} - -/** - * method to sort array in a descending fashion while preserving keys - * @param string $a a string to compare - * @param string $b a string to compare - * @return int - */ -function _autoassignrole_array_desc($a, $b) { - if ($a == $b) { - return 0; - } - return ($a > $b) ? -1 : 1; -} - -/** - * method to sort array in an ascending fashion while preserving keys - * @param string $a a string to compare - * @param string $b a string to compare - * @return int + * Implements hook_permission(). */ -function _autoassignrole_array_asc($a, $b) { - if ($a == $b) { - return 0; - } - return ($a < $b) ? -1 : 1; +function autoassignrole_permission() { + return array( + 'administer autoassignrole' => array( + 'title' => t('Administer auto assign role'), + 'description' => t('Perform administration tasks for auto assign role.'), + ), + ); } /** - * Implementation of hook_form_alter(). + * Implements hook_menu(). * - * Integrate with content profile's registration integration - */ -function autoassignrole_form_alter(&$form, $form_state, $form_id) { - if ($form_id == 'content_profile_admin_settings') { - $type = $form_state['type']; - $result = db_query("SELECT ar.path, ar.rid, r.name FROM {autoassignrole_page} ar INNER JOIN {role} r ON ar.rid = r.rid"); - $options = array(); - while ($r = db_fetch_object($result)) { - $options[$r->rid] = filter_xss_admin("($r->name) $r->path"); - } - $form['registration']['autoassignrole_use'] = array( - '#type' => 'checkboxes', - '#title' => t('Use on Auto Assign Role paths'), - '#default_value' => content_profile_get_settings($type, 'autoassignrole_use'), - '#options' => $options, - '#description' => t('The Auto Assign Role module gives you the ability to assign paths a user can register from for a role. After associating a path with a role your selection can associate this content type with a path.', array('@aar' => url('admin/user/autoassignrole'))), - '#disabled' => count($options) == 0, - '#weight' => 3, - ); - array_unshift($form['#submit'], 'autoassignrole_content_profile_admin_form_submit'); - } - elseif ($form_id == 'user_register' && module_exists('content_profile_registration')) { - if (implode('/', arg()) != 'user/register' && $rid = autoassignrole_get_active_path_rid()) { - // Set the content types which should be shown by the content_profile_registration module - $form['#content_profile_registration_use_types'] = array(); - foreach (content_profile_get_types('names') as $type => $name) { - $cp_settings = content_profile_get_settings($type, 'autoassignrole_use'); - if (is_array($cp_settings) && in_array($rid, $cp_settings)) { - $form['#content_profile_registration_use_types'][$type] = $name; - } - } - } - } -} - -function autoassignrole_content_profile_admin_form_submit($form, &$form_state) { - $form_state['values']['autoassignrole_use'] = array_keys(array_filter($form_state['values']['autoassignrole_use'])); -} - -/** - * Implementation of hook_content_profile_settings(). - */ -function autoassignrole_content_profile_settings() { - static $autoassignrole; - if (empty($autoassignrole)) { - $autoassignrole['autoassignrole_use'] = array_keys(user_roles((TRUE))); - } - - return $autoassignrole; -} - -/** - * Returns the role id of the currently active AAR-path. - * - * @return - * array An array of roles - */ -function autoassignrole_get_active_path_rid() { - $item = menu_get_item(); - if ($item['page_callback'] == 'autoassignrole_path') { - return $item['page_arguments'][0]; - } - return FALSE; -} - -/** - * Get all assignable roles that AAR can possibly apply. - * - * @return - * array An array of roles - */ -function autoassignrole_get_all_roles() { - $aar_roles = array(); - // Select all auto and user selectable roles - $sql = "SELECT value FROM {autoassignrole} WHERE arid = 'auto_roles' OR arid = 'user_roles'"; - $result = db_fetch_object(db_query($sql)); - $aar_roles = unserialize($result->value); - - // unset anything that isn't active - foreach ($aar_roles as $key => $value) { - if ($value == 0) { - unset($aar_roles[$key]); - } - } - - // Select all path assignable roles - $sql = "SELECT rid FROM {autoassignrole_page}"; - $result = db_query($sql); - while ($row = db_fetch_object($result)) { - $aar_roles[$row->rid] = $row->rid; - } - return _autoassignrole_clean_roles($aar_roles); -} - -/** - * Get all assignable roles that AAR will apply automatically. - * - * @return - * array An array of roles - */ -function autoassignrole_get_auto_roles() { - $aar_roles = array(); - // Select all auto roles - $sql = "SELECT value FROM {autoassignrole} WHERE arid = 'auto_roles'"; - $result = db_fetch_object(db_query($sql)); - $aar_roles = unserialize($result->value); - - // unset anything that isn't active - foreach ($aar_roles as $key => $value) { - if ($value == 0) { - unset($aar_roles[$key]); - } - } - return _autoassignrole_clean_roles($aar_roles); -} - -/** - * Get all user selectable roles - * - * @return - * array An array of roles - */ -function autoassignrole_get_user_selectable_roles() { - $aar_roles = array(); - // Select all user selectable roles - $sql = "SELECT value FROM {autoassignrole} WHERE arid = 'user_roles'"; - $result = db_fetch_object(db_query($sql)); - $aar_roles = unserialize($result->value); - return _autoassignrole_clean_roles($aar_roles); -} - -/** - * Get all path assignable roles - * - * @param $path - * array Optional array of paths to restrict selection to when using an $op of path - * @return - * array An array of roles - */ -function autoassignrole_get_path_roles($path = array()) { - $aar_roles = array(); - // Select path based roles - if (count($path) == 0) { - $sql = "SELECT rid FROM {autoassignrole_page}"; - $result = db_query($sql); - } - else { - foreach ($path as $key => $value) { - $path[$key] = "'$value'"; - } - $sql = "SELECT rid FROM {autoassignrole_page} WHERE path IN(". implode(',', $path) .")"; - $result = db_query($sql); - } - while ($row = db_fetch_object($result)) { - $aar_roles[$row->rid] = $row->rid; - } - return _autoassignrole_clean_roles($aar_roles); -} - -function _autoassignrole_clean_roles($aar_roles) { - $roles = user_roles(); - // Use the results of user_roles() and unset anything not available from AAR - foreach ($roles as $key => $role) { - if (!array_key_exists($key, $aar_roles)) { - unset($roles[$key]); - } - } - - return $roles; -} - -/** - * Get all roles that will be assigned based on the active path - * - * @param $path - * string An optional path to use as the active path - * @return - * array An array of roles - */ -function autoassignrole_get_active_path_roles($path = NULL) { - if (!is_array($path)) { - $path = array($path); - } - if (count($path) == 0) { - $menu_item = menu_get_item(); - $path[] = $menu_item['path']; - } - $path_roles = autoassignrole_get_path_roles($path); - $auto_roles = autoassignrole_get_auto_roles(); - foreach ($auto_roles as $key => $value) { - $path_roles[$key] = $value; - } - return _autoassignrole_clean_roles($path_roles); -} - -/** - * An API like call to return the roles a user has available or will be assigned - * - * @param $op - * string active, all, auto, path, user - * @param $path - * array Optional array of paths to restrict selection to when using an $op of path - * @return - * array An array of roles + * @return array */ -function autoassignrole_get_roles($op = 'all', $path = array()) { - - switch ($op) { - case 'active' : - return autoassignrole_get_active_path_roles($path); - break; - case 'all': - return autoassignrole_get_all_roles(); - break; - case 'auto': - return autoassignrole_get_auto_roles(); - break; - case 'path': - return autoassignrole_get_path_roles($path); - break; - case 'user': - return autoassignrole_get_user_selectable_roles(); - break; - } -} +function autoassignrole_menu() { + $items = array(); + $items['admin/config/autoassignrole'] = array( + 'title' => 'Auto assign role', + 'description' => 'Configure auto role assignment.', + 'access arguments' => array('administer autoassignrole'), + 'page callback' => 'autoassignrole_admin_block_page', + 'file' => 'autoassignrole.admin.inc', + ); + $items['admin/config/autoassignrole/auto'] = array( + 'title' => 'Auto role assignment', + 'description' => 'Configure which role(s) are automatically assigned on user creation.', + 'access arguments' => array('administer autoassignrole'), + 'page callback' => 'drupal_get_form', + 'page arguments' => array('autoassignrole_auto_settings'), + 'file' => 'autoassignrole.admin.inc', + ); -/** - * Implementation of hook_menu_alter() - * - * Redirect the default user register page to a path that has been designated as - * the replacement registration path. - */ -function autoassignrole_menu_alter(&$items) { - $row = db_fetch_object(db_query("SELECT rid, path, display, title, weight, menu, registration FROM {autoassignrole_page} WHERE registration = 1")); - if (isset($row->registration)) { - $path = $row->path; - } - else { - $row = db_fetch_object(db_query("SELECT arid, value FROM {autoassignrole} WHERE arid = '%s'", 'node_user_register')); - if (!empty($row->value)) { - $path = 'node/'. $row->value; - } - } - if (isset($path)) { - $items['user/register']['page arguments'] = array($path); - $items['user/register']['page callback'] = 'drupal_goto'; - $items['user/register']['type'] = MENU_LOCAL_TASK; - } + return $items; }