class commerce_file_plugin_argument_validate_user in Commerce File 7
Validate whether an argument is a valid user.
This supports either numeric arguments (UID) or strings (username) and converts either one into the user's UID. This validator also sets the argument's title to the username.
Hierarchy
- class \views_object
Expanded class hierarchy of commerce_file_plugin_argument_validate_user
1 string reference to 'commerce_file_plugin_argument_validate_user'
- commerce_file_views_plugins in views/
commerce_file.views.inc - Implements hook_views_plugins
File
- views/
handlers/ commerce_file_plugin_argument_validate_user.inc, line 15 - Argument plugin for a valid user.
View source
class commerce_file_plugin_argument_validate_user extends views_plugin_argument_validate_user {
function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$form['restrict_roles']['#title'] = t('If the current user does not match the argument, validate the argument for users with specific roles.');
$form['roles'] = array(
'#type' => 'checkboxes',
'#title' => t('Grant access for the selected roles'),
'#description' => t('If no roles are selected, additional access will not be granted.'),
'#options' => array_map('check_plain', user_roles(TRUE)),
'#default_value' => $this->options['roles'],
'#process' => array(
'form_process_checkboxes',
'ctools_dependent_process',
),
'#dependency' => array(
'edit-options-validate-options-commerce-file-user-restrict-roles' => array(
1,
),
),
'#prefix' => '<div id="edit-options-validate-options-commerce-file-user-restrict-roles-wrapper">',
'#suffix' => '</div>',
);
}
function validate_argument($argument) {
global $user;
$type = $this->options['type'];
// is_numeric() can return false positives, so we ensure it's an integer.
// However, is_integer() will always fail, since $argument is a string.
if (is_numeric($argument) && $argument == intval($argument)) {
if ($type == 'uid' || $type == 'either') {
if ($argument == $GLOBALS['user']->uid) {
// If you assign an object to a variable in PHP, the variable
// automatically acts as a reference, not a copy, so we use
// clone to ensure that we don't actually mess with the
// real global $user object.
$account = clone $GLOBALS['user'];
}
$where = 'uid = :argument';
}
}
else {
if ($type == 'name' || $type == 'either') {
if ($argument == $GLOBALS['user']->name) {
$account = clone $GLOBALS['user'];
}
$where = "name = :argument";
}
}
// If we don't have a WHERE clause, the argument is invalid.
if (empty($where)) {
return FALSE;
}
if (!isset($account)) {
$query = 'SELECT uid, name FROM {users} WHERE ' . $where;
$account = db_query($query, array(
':argument' => $argument,
))
->fetchObject();
}
if (empty($account)) {
// User not found.
return FALSE;
}
// access checks
$owner_access = $user->uid == $account->uid;
$admin_access = user_access('access any ' . COMMERCE_FILE_LICENSE_ENTITY_NAME, $user) || user_access(COMMERCE_FILE_ADMIN_PERM, $user);
// Validate user access
if ($owner_access || $admin_access) {
// ALLOW access if owner or admin access
$this
->_set_allowed_argument($account);
return TRUE;
}
// See if we're filtering users based on roles.
if (!empty($this->options['restrict_roles']) && !empty($this->options['roles'])) {
// Look for matching roles on the current user.
$matching_roles = array_intersect(array_keys($user->roles), $this->options['roles']);
if (!empty($matching_roles)) {
// ALLOW if the user does not match any of the roles.
$this
->_set_allowed_argument($account);
return TRUE;
}
}
// DENY by default
return FALSE;
}
/**
* Sets argument properties for an allowed argument
*/
private function _set_allowed_argument($account) {
$this->argument->argument = $account->uid;
$this->argument->validated_title = check_plain($account->name);
}
}
Members
Name![]() |
Modifiers | Type | Description | Overrides |
---|---|---|---|---|
commerce_file_plugin_argument_validate_user:: |
function |
Provide the default form for setting options. Overrides views_plugin_argument_validate_user:: |
||
commerce_file_plugin_argument_validate_user:: |
function |
Overrides views_plugin_argument_validate_user:: |
||
commerce_file_plugin_argument_validate_user:: |
private | function | Sets argument properties for an allowed argument | |
views_object:: |
public | property | Handler's definition. | |
views_object:: |
public | property | Except for displays, options for the object will be held here. | 1 |
views_object:: |
function | Collect this handler's option definition and alter them, ready for use. | ||
views_object:: |
public | function | Views handlers use a special construct function. | 4 |
views_object:: |
public | function | Destructor. | 2 |
views_object:: |
public | function | 1 | |
views_object:: |
public | function | ||
views_object:: |
public | function | Always exports the option, regardless of the default value. | |
views_object:: |
public | function | Set default options on this object. | 1 |
views_object:: |
public | function | Set default options. | |
views_object:: |
public | function | Let the handler know what its full definition is. | |
views_object:: |
public | function | Unpack options over our existing defaults, drilling down into arrays so that defaults don't get totally blown away. | |
views_object:: |
public | function | Unpack a single option definition. | |
views_object:: |
public | function | Unpacks each handler to store translatable texts. | |
views_object:: |
public | function | ||
views_plugin:: |
public | property | The current used views display. | |
views_plugin:: |
public | property | The plugin name of this plugin, for example table or full. | |
views_plugin:: |
public | property | The plugin type of this plugin, for example style or query. | |
views_plugin:: |
public | property |
The top object of a view. Overrides views_object:: |
1 |
views_plugin:: |
public | function | Provide a list of additional theme functions for the theme info page. | |
views_plugin:: |
public | function | Return the human readable name of the display. | |
views_plugin:: |
public | function | Add anything to the query that we might need to. | 7 |
views_plugin:: |
public | function | Returns the summary of the settings in the display. | 8 |
views_plugin:: |
public | function | Provide a full list of possible theme templates used by this style. | |
views_plugin:: |
public | function | Validate that the plugin is correct and can be saved. | 3 |
views_plugin_argument_validate:: |
public | function | Determine if the administrator has the privileges to use this plugin. | 1 |
views_plugin_argument_validate:: |
public | function | If we don't have access to the form but are showing it anyway, ensure that the form is safe and cannot be changed from user input. | |
views_plugin_argument_validate:: |
public | function | Initialize this plugin with the view and the argument it is linked to. | 1 |
views_plugin_argument_validate:: |
public | function |
Provide the default form form for validating options. Overrides views_plugin:: |
|
views_plugin_argument_validate_user:: |
public | function |
Convert options from the older style. Overrides views_plugin_argument_validate:: |
|
views_plugin_argument_validate_user:: |
public | function |
Provide the default form form for submitting options Overrides views_plugin_argument_validate:: |
|
views_plugin_argument_validate_user:: |
public | function |
Retrieve the options when this is a new access control plugin. Overrides views_plugin_argument_validate:: |
|
views_plugin_argument_validate_user:: |
public | function |
Process the summary arguments for displaying. Overrides views_plugin_argument_validate:: |