class ServicesClientUserRoleCondition in Services Client 7.2
Hierarchy
- class \ServicesClientPlugin implements ServicesClientConfigurableInterface
- class \ServicesClientConditionPlugin implements ServicesClientConditionInterface
Expanded class hierarchy of ServicesClientUserRoleCondition
1 string reference to 'ServicesClientUserRoleCondition'
- services_client_services_client_condition in ./
services_client.plugins.inc - List availalable condition plugins.
File
- include/
condition.inc, line 351
View source
class ServicesClientUserRoleCondition extends ServicesClientConditionPlugin {
/**
* Retrieve default configuration.
*/
protected function getDefaultConfiguration() {
return array(
'roles' => array(),
'intersect' => FALSE,
'reverse' => FALSE,
);
}
/**
* Retrieve condition summary for UI.
*
* @return string
* UI short summary.
*/
public function getSummary() {
if (empty($this->config['roles'])) {
return '[ ' . t('User roles condition - not configured') . ' ]';
}
else {
$intersect = $this->config['intersect'] ? t('all') : t('at least one');
$roles = array();
foreach ($this->config['roles'] as $rid => $role) {
$role = user_role_load($role);
$roles[] = $role->name;
}
return format_string('User has @intersect of these roles: @roles', array(
'@intersect' => $intersect,
'@roles' => implode(', ', $roles),
));
}
}
/**
* Check if entity matches condition.
*
* @param stdClass $entity
* Drupal entity that should be matched.
*
* @return bool
* TRUE if matches condition.
*/
public function match($entity) {
$roles_intersect = array_intersect($this->config['roles'], $entity->roles);
if ($this->config['intersect']) {
$matched = $this->config['roles'] == $roles_intersect;
}
else {
$matched = !empty($roles_intersect);
}
return $this->config['reverse'] ? !$matched : $matched;
}
/**
* Configuration form.
*/
public function configForm(&$form, &$form_state) {
$form['roles'] = array(
'#type' => 'select',
'#multiple' => TRUE,
'#title' => t('User Roles'),
'#required' => TRUE,
'#options' => user_roles(),
'#description' => t('All users with such roles will be picked'),
'#default_value' => isset($this->config['roles']) ? $this->config['roles'] : NULL,
);
$form['intersect'] = array(
'#type' => 'checkbox',
'#title' => t('All roles should match?'),
'#description' => t('Otherwise user has to have at least one role.'),
'#default_value' => isset($this->config['intersect']) ? $this->config['intersect'] : NULL,
);
$form['reverse'] = array(
'#type' => 'checkbox',
'#title' => t('Reverse'),
'#description' => t('All users without such roles will be picked.'),
'#default_value' => isset($this->config['reverse']) ? $this->config['reverse'] : NULL,
);
}
/**
* Submit handler.
*/
public function configFormSubmit(&$form, &$form_state) {
$this->config['roles'] = $form_state['values']['roles'];
$this->config['intersect'] = $form_state['values']['intersect'];
$this->config['reverse'] = $form_state['values']['reverse'];
}
}