function user_patterns_validate in Patterns 7.2
Same name and namespace in other branches
- 7 patterns_components/components/user.inc \user_patterns_validate()
Parameters
string $action Type of action being executed:
string $tag Type of tag to be validated:
array $data Associative array containing the data action processed from the pattern:
Return value
mixed through patterns_results($status, $msg, $result) function. Status of the operation, error messages and semantic warnings through $result
File
- patterns_components/
components/ user.inc, line 260
Code
function user_patterns_validate($action, $tag, &$data) {
$result = array();
$status = PATTERNS_SUCCESS;
$msg = '';
/*
* Syntactic validation:
* - Syntactic warnings: They refer to wrong grammar statements that will not
* provoke any execution error. Ex.: non-interpretable attributes.
* - Syntactic errors: They refer to wrong grammar statements that will provoke
* an execution error. Ex.: misspelling in required attribute.
*
*/
switch ($tag) {
case 'role':
switch ($action) {
case PATTERNS_CREATE:
//Check mandatory fields, in this case is only one.
$mandatory_attributes = array(
'name',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case there are not optional attributes, so we can also use this set as interpretable attributes
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_MODIFY:
//Check mandatory fields
$mandatory_attributes = array(
'name',
'rid',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case there are not optional attributes, so we can also use this set as interpretable attributes
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_DELETE:
//Check mandatory fields, in this case is only one.
$mandatory_attributes = array(
'rid',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case there are not optional attributes, so we can also use this set as interpretable attributes
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
}
break;
case 'permission':
//Create and delete actions for the permission case are considered as syntactical errors
if ($action == PATTERNS_CREATE || $action == PATTERNS_DELETE) {
$msg = t('Action %action is uncompatible for tag %tag.', array(
'%action' => $action,
'%tag' => $tag,
));
return patterns_results(PATTERNS_ERR, $msg);
}
else {
if ($action == PATTERNS_MODIFY) {
$mandatory_attributes = array(
'role',
'value',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case there are not optional attributes, so we can also use this set as interpretable attributes
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
}
}
break;
case 'user':
switch ($action) {
case PATTERNS_CREATE:
//Check mandatory fields
$mandatory_attributes = array(
'name',
'mail',
'password',
'timezone',
'status',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case we will need to define as well the attributes generated by the hook_prepare(): uid and pass
$interpretable_attributes = array(
'name',
'mail',
'password',
'timezone',
'status',
'uid',
'pass',
);
if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_MODIFY:
//Check mandatory fields
$mandatory_attributes = array(
'uid',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//All the fields but uid are in this case optional. Pass is generated by hook_prepare()
$interpretable_attributes = array(
'uid',
'name',
'mail',
'timezone',
'status',
'pass',
);
if (_patterns_has_uninterpretable_attributes($data, $interpretable_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
case PATTERNS_DELETE:
//Check mandatory fields, in this case is only one.
$mandatory_attributes = array(
'uid',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case there are not optional attributes, so we can also use this set as interpretable attributes
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
}
break;
case 'role_users':
switch ($action) {
case PATTERNS_CREATE:
case PATTERNS_DELETE:
//Check mandatory fields, in this case the syntax is the same for DELETE and CREATE
$mandatory_attributes = array(
'role',
'users',
);
if (!_patterns_has_all_mandatory_attributes($data, $mandatory_attributes, $msg)) {
return patterns_results(PATTERNS_ERR, $msg);
}
//In this case there are not optional attributes, so we can also use this set as interpretable attributes
if (_patterns_has_uninterpretable_attributes($data, $mandatory_attributes, $msg)) {
$status = PATTERNS_WARN;
}
break;
default:
$msg = t('Action %action is uncompatible for tag %tag.', array(
'%action' => $action,
'%tag' => $tag,
));
return patterns_results(PATTERNS_ERR, $msg);
}
break;
}
/*
* Semantic validation:
* - Semantic warnings: They refer to the meaning of the pattern itself, and they
* might provoke execution errors if they are not solved.
*
*/
switch ($tag) {
case 'role':
//Prepare common role values
$current_roles = user_roles();
switch ($action) {
case PATTERNS_CREATE:
$role_name = $data['name'];
//Create semantic warning if the role name has already been defined
if (in_array($role_name, $current_roles)) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The role name %role_name already exists in the system.', array(
'%role_name' => $role_name,
)),
);
}
break;
case PATTERNS_MODIFY:
$role_name = $data['name'];
$role_rid = $data['rid'];
//Create semantic warning if the rid does not exist
if (!array_key_exists($role_rid, $current_roles)) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The role id %rid does not exist in the system.', array(
'%rid' => $role_rid,
)),
);
}
//Create semantic warning if the role name has already been defined
if (in_array($role_name, $current_roles)) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The role name %role_name already exists in the system.', array(
'%role_name' => $role_name,
)),
);
}
break;
case PATTERNS_DELETE:
$role_rid = $data['rid'];
if (!array_key_exists($role_rid, $current_roles) && !in_array($role_rid, $current_roles)) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The value %rid does not exist in the system as role name neither as a numeric rid.', array(
'%rid' => $role_rid,
)),
);
}
break;
}
break;
case 'permission':
//This is the only case available, the lack of the rest are checking during the syntactic validation
if ($action == PATTERNS_MODIFY) {
$permissions = $data['value'];
$role = $data['role'];
//Check if all permissions exist, if any of them don't raise a syntactic warning
$available_permissions = user_permission_get_modules();
$msg = "";
foreach ($permissions as $permission) {
if (!array_key_exists($permission, $available_permissions)) {
$msg .= t('Permission %permission is not available in the system.<br>', array(
'%permission' => $permission,
));
}
}
if ($msg != "") {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => $msg,
);
}
//Raise semantic warning if the role name does not exist
if (!user_role_load_by_name($role)) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not exist in the system.', array(
'%role' => $role,
)),
);
}
}
break;
case 'user':
$users = _user_patterns_get_all_users();
switch ($action) {
case PATTERNS_CREATE:
//Create semantic warning if username or mail has already been defined
if (user_load_by_name($data['name'])) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The username %username already exists in the system.', array(
'%username' => $data['name'],
)),
);
}
if (user_load_by_mail($data['mail'])) {
$result[] = array(
PATTERNS_WARNING_ALREADY_DEFINED_ELEMENT => t('The username %mail already exists in the system.', array(
'%mail' => $data['mail'],
)),
);
}
break;
case PATTERNS_MODIFY:
//Create semantic warning if the uid does not exist
if (!user_load($data['uid'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The User ID %uid does not exist in the system.', array(
'%uid' => $data['uid'],
)),
);
}
break;
case PATTERNS_DELETE:
//Create semantic warning if the uid does not exist
if (!user_load($data['uid'])) {
$result[] = array(
PATTERNS_WARNING_ELEMENT_UNDEFINED => t('The User ID %uid does not exist in the system.', array(
'%uid' => $data['uid'],
)),
);
}
break;
}
break;
case 'role_users':
switch ($action) {
case PATTERNS_CREATE:
case PATTERNS_DELETE:
//Create semantic warning if the role does not exist
if (!user_role_load_by_name($data['role'])) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('The role %role does not exist in the system.', array(
'%role' => $data['role'],
)),
);
}
//We raised a semantic error for each username that does not exist.
foreach ($data['users'] as $username) {
if (!user_load_by_name($username)) {
$result[] = array(
PATTERNS_WARNING_UNMET_DEPENDENCY => t('The username %username does not exist in the system.', array(
'%username' => $username,
)),
);
}
}
break;
}
break;
}
return patterns_results($status, $msg, $result);
}