Skip to content

Commit

Permalink
#59 Add to user acess - support for sync requests
Browse files Browse the repository at this point in the history
  • Loading branch information
BBGuy committed Jan 28, 2015
1 parent d565efd commit 197f480
Show file tree
Hide file tree
Showing 3 changed files with 62 additions and 26 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@ class kendra_user_views_plugin_access extends views_plugin_access {
* Determine if the current user has access or not.
*/
function access($account) {
return kendra_user_views_access($this->options['options']);

//drupal_set_message('access');
switch ($this->options['options']) {
case 'Asset owner':
//return FALSE;
Expand All @@ -30,18 +33,22 @@ class kendra_user_views_plugin_access extends views_plugin_access {
}
}

function get_access_callback() {
return array('kendra_user_views_access', array($this->options['options']));
}

function options_form(&$form, &$form_state) {
parent::options_form($form, $form_state);
$options = array(
'Asset owner' => 'Only show on asset pages owned by curent user ro for Kendra admin'
);
$form['options'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('Display view for'),
'#default_value' => $this->options['options'],
'#description' => t(''),
);
parent::options_form($form, $form_state);
$options = array(
'Asset owner' => 'Only show on asset pages owned by curent user ro for Kendra admin',
);
$form['options'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('Display view for'),
'#default_value' => $this->options['options'],
'#description' => t(''),
);
}

}
57 changes: 43 additions & 14 deletions sites/all/modules/custom/kendra_user/kendra_user.module
Original file line number Diff line number Diff line change
Expand Up @@ -27,41 +27,53 @@ function kendra_user_can_user_edit_asset() {
return kendra_user_can_specified_user_edit_asset($user);
}

/**
* Called from the kendra_user_views_plugin_access acess.
*
* Checks if user has access to the view.
* The access check are location specific.
*/
function kendra_user_views_access($option) {
switch ($option) {
case 'Asset owner':
return kendra_user_can_user_edit_asset();
break;

default:
return FALSE;
break;
}
}

/**
* Checks if the specified user allowed to edit the asset.
*
* TRUE if user is is the owner of the asset page or user has kendra admin role.
*/
function kendra_user_can_specified_user_edit_asset($user) {

// If an admin or a kendra admin.
if (in_array('kendra', $user->roles) | in_array('administrator', $user->roles)) {
// Allays allow access.
return TRUE;
}

// If an Asset Author
if (in_array('Asset Author', $user->roles)) {
// check if a node page.
If (arg(0) == 'node') {
$node = node_load(arg(1));
// Check if asset page.
if ($node->type == 'asset') {
// Return TRUE if same user.
return ($node->uid == $user->uid);
}
return kendra_user_can_specified_user_edit_specified_asset($user, $node);
}
else {
return FALSE;
}
}

// All other cases should return FALSE.
return FALSE;

}

/**
* Unlike the above function this will work for both assets and contributions.
* Unlike the above function this will work for both assets, contributions and
* sync-request.
*/
function kendra_user_can_specified_user_edit_specified_asset($user, $node) {
//drupal_set_message('kendra_user_can_specified_user_edit_specified_asset');

switch ($node->type) {
case 'contribution':
// If a kendra admin.
Expand All @@ -80,6 +92,23 @@ function kendra_user_can_specified_user_edit_specified_asset($user, $node) {
}
break;

case 'sync-request':
// If a kendra admin.
if (in_array('kendra', $user->roles)) {
// Then he can edit the asset.
return TRUE;
}
// If user has an asset author role.
if (in_array('Asset Author', $user->roles)) {
// We will get the asset node from the contribution.
$contribution_wrapper = entity_metadata_wrapper('node', $node);
$asset_nid = $contribution_wrapper->field_sr_asset->getIdentifier();
$asset_node = node_load($asset_nid);
// We will recursively call this function but with the asset node.
return kendra_user_can_specified_user_edit_specified_asset($user, $asset_node);
}
break;

case 'asset':
// If a kendra admin.
if (in_array('kendra', $user->roles)) {
Expand Down
2 changes: 1 addition & 1 deletion sites/all/modules/custom/kendra_user/kendra_user.rules.inc
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,4 @@ function kendra_user_can_edit_asset($user, $node) {
return kendra_user_can_specified_user_edit_specified_asset($user, $node);
return TRUE;
return kendra_user_can_specified_user_edit_asset($user);
}
}

0 comments on commit 197f480

Please sign in to comment.