| 1 |
<?php |
<?php |
| 2 |
// $Id: views_plugin_argument_validate_user.inc,v 1.1 2009/02/06 21:13:04 merlinofchaos Exp $ |
// $Id: views_plugin_argument_validate_user.inc,v 1.2 2009/02/17 23:32:33 merlinofchaos Exp $ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* Validate whether an argument is a valid user. |
* Validate whether an argument is a valid user. |
| 9 |
* argument's title to the username. |
* argument's title to the username. |
| 10 |
*/ |
*/ |
| 11 |
class views_plugin_argument_validate_user extends views_plugin_argument_validate { |
class views_plugin_argument_validate_user extends views_plugin_argument_validate { |
| 12 |
function validate_form(&$form, &$form_state) { |
function option_definition() { |
| 13 |
// We are unable to rely on options having already been set, so let's make |
$options = parent::option_definition(); |
| 14 |
// sure defaults are here: |
$options['type'] = array('default' => 'uid'); |
| 15 |
if (!isset($this->argument->options['validate_user_argument_type'])) { |
$options['restrict_roles'] = array('default' => FALSE); |
| 16 |
$this->argument->options['validate_user_argument_type'] = 'uid'; |
$options['roles'] = array('default' => array()); |
| 17 |
$this->argument->options['validate_user_roles'] = array(); |
|
| 18 |
} |
return $options; |
| 19 |
|
} |
| 20 |
|
|
| 21 |
|
function options_form(&$form, &$form_state) { |
| 22 |
$form['validate_user_argument_type'] = array( |
$form['validate_user_argument_type'] = array( |
| 23 |
'#type' => 'radios', |
'#type' => 'radios', |
| 24 |
'#title' => t('Type of user argument to allow'), |
'#title' => t('Type of user argument to allow'), |
| 27 |
'name' => t('Only allow string usernames'), |
'name' => t('Only allow string usernames'), |
| 28 |
'either' => t('Allow both numeric UIDs and string usernames'), |
'either' => t('Allow both numeric UIDs and string usernames'), |
| 29 |
), |
), |
| 30 |
'#default_value' => $this->argument->options['validate_user_argument_type'], |
'#default_value' => $this->options['type'], |
|
'#process' => array('expand_radios', 'views_process_dependency'), |
|
|
'#dependency' => array('edit-options-validate-type' => array($this->id)), |
|
|
'#prefix' => '<div id="edit-options-validate-user-argument-type-wrapper">', |
|
|
'#suffix' => '</div>', |
|
| 31 |
); |
); |
| 32 |
|
|
| 33 |
$form['validate_user_restrict_roles'] = array( |
$form['validate_user_restrict_roles'] = array( |
| 34 |
'#type' => 'checkbox', |
'#type' => 'checkbox', |
| 35 |
'#title' => t('Restrict user based on role'), |
'#title' => t('Restrict user based on role'), |
| 36 |
'#default_value' => !empty($this->argument->options['validate_user_restrict_roles']), |
'#default_value' => $this->options['restrict_roles'], |
|
'#process' => array('views_process_dependency'), |
|
|
'#dependency' => array('edit-options-validate-type' => array($this->id)), |
|
| 37 |
); |
); |
| 38 |
|
|
| 39 |
$form['validate_user_roles'] = array( |
$form['roles'] = array( |
| 40 |
'#type' => 'checkboxes', |
'#type' => 'checkboxes', |
| 41 |
'#prefix' => '<div id="edit-options-validate-user-roles-wrapper">', |
'#prefix' => '<div id="edit-options-validate-user-roles-wrapper">', |
| 42 |
'#suffix' => '</div>', |
'#suffix' => '</div>', |
| 43 |
'#title' => t('Restrict to the selected roles'), |
'#title' => t('Restrict to the selected roles'), |
| 44 |
'#options' => user_roles(TRUE), |
'#options' => user_roles(TRUE), |
| 45 |
'#default_value' => $this->argument->options['validate_user_roles'], |
'#default_value' => $this->options['roles'], |
| 46 |
'#description' => t('If no roles are selected, users from any role will be allowed.'), |
'#description' => t('If no roles are selected, users from any role will be allowed.'), |
| 47 |
'#process' => array('expand_checkboxes', 'views_process_dependency'), |
'#process' => array('expand_checkboxes', 'views_process_dependency'), |
| 48 |
'#dependency' => array( |
'#dependency' => array( |
|
'edit-options-validate-type' => array($this->id), |
|
| 49 |
'edit-options-validate-user-restrict-roles' => array(1), |
'edit-options-validate-user-restrict-roles' => array(1), |
| 50 |
), |
), |
|
'#dependency_count' => 2, |
|
| 51 |
); |
); |
| 52 |
} |
} |
| 53 |
|
|
| 54 |
|
function options_submit(&$form, &$form_state, &$options) { |
| 55 |
|
// filter trash out of the options so we don't store giant unnecessary arrays |
| 56 |
|
$options['roles'] = array_filter($options['roles']); |
| 57 |
|
} |
| 58 |
|
|
| 59 |
|
function convert_options(&$options) { |
| 60 |
|
if (!isset($options['type']) && isset($this->argument->options['validate_user_argument_type'])) { |
| 61 |
|
$options['type'] = $this->argument->options['validate_user_argument_type']; |
| 62 |
|
$options['restrict_roles'] = $this->argument->options['validate_user_restrict_roles']; |
| 63 |
|
$options['roles'] = $this->argument->options['validate_user_roles']; |
| 64 |
|
} |
| 65 |
|
} |
| 66 |
|
|
| 67 |
function validate_argument($argument) { |
function validate_argument($argument) { |
| 68 |
$type = $this->argument->options['validate_user_argument_type']; |
$type = $this->options['type']; |
| 69 |
// is_numeric() can return false positives, so we ensure it's an integer. |
// is_numeric() can return false positives, so we ensure it's an integer. |
| 70 |
// However, is_integer() will always fail, since $argument is a string. |
// However, is_integer() will always fail, since $argument is a string. |
| 71 |
if (is_numeric($argument) && $argument == (int)$argument) { |
if (is_numeric($argument) && $argument == (int)$argument) { |
| 92 |
} |
} |
| 93 |
|
|
| 94 |
// See if we're filtering users based on roles. |
// See if we're filtering users based on roles. |
| 95 |
if (!empty($this->argument->options['validate_user_restrict_roles']) && !empty($this->argument->options['validate_user_roles'])) { |
if (!empty($this->options['restrict_roles']) && !empty($this->options['roles'])) { |
| 96 |
$roles = $this->argument->options['validate_user_roles']; |
$roles = $this->options['roles']; |
| 97 |
$acccont->roles = array(); |
$acccont->roles = array(); |
| 98 |
$account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID; |
$account->roles[] = $account->uid ? DRUPAL_AUTHENTICATED_RID : DRUPAL_ANONYMOUS_RID; |
| 99 |
$result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid); |
$result = db_query('SELECT rid FROM {users_roles} WHERE uid = %d', $account->uid); |
| 100 |
while ($role = db_fetch_object($result)) { |
while ($role = db_fetch_object($result)) { |
| 101 |
$account->roles[] = $role->rid; |
$account->roles[] = $role->rid; |
| 102 |
} |
} |
| 103 |
if (!(bool)array_intersect($account->roles, $roles)) { |
if (!(bool) array_intersect($account->roles, $roles)) { |
| 104 |
return FALSE; |
return FALSE; |
| 105 |
} |
} |
| 106 |
} |
} |