5

I have created an entity using Entity Construction Kit (ECK) and wish to add extra permissions for that entity such as view own, edit own and delete own. I see there has been some movement in this thread Better Permissions and a few others to create better permissions in general for entities but I want to know how can I do it in my own custom module.

I do not want every person on the site to be able to view,edit,delete so I have disabled those perms for my created entity.

I'm stuck and cannot find any good documentation on it.

  • Content Access does that for nodes, it's source could give an example. – jhr Dec 15 '12 at 2:02
6

You need to alter the entity info using:

hook_entity_info_alter

and add the access callback

e.g:

function EXAMPLE_entity_info_alter(&$entity_info) {
  $entity_info['my_entity']['access callback'] = 'my_entity_access';
}

and then in my_entity_access

function my_entity_access($op, $entity = NULL, $account = NULL) {
  switch($op){
    case 'view':
    // view check
    // e.g return user_access('view my_entity', $account);
    break;
    case 'create':
    // create check
    break;
    case 'update':
    // update check
    break;
    case 'delete':
    // delete check
    break;
  }
  return FALSE;
}

See node_access for more info

You may want to use hook_permission to define your own permissions also.

2

Here is a working example:

function my_permission() {
  return array(
    'view own forum post' => array(
      'title' => t('View own forum post'),
      'description' => t('View own forum post.'),
    ),
    'edit own forum post' => array(
      'title' => t('Edit own forum post'),
      'description' => t('Edit own forum post.'),
    ),
    'delete own forum post' => array(
      'title' => t('Delete own forum post'),
      'description' => t('Delete own forum post.'),
    ),
  );
}

function my_entity_info_alter(&$entity_info) {
  $entity_info['forum']['access callback'] = 'my_forum_access';
}

function my_forum_access($op, $entity) {

  global $user;

  $entity_type = $op == 'create' ? $entity : $entity->type;

  if (eck__entity_access($op, $entity_type, $user, 'forum')) {
    return TRUE;
  }

  if ($entity && $entity_type == 'forum_post') {
    switch ($op) {
      case 'view':
        if (user_access('view own forum post', $user) && $user->uid == $entity->uid) {
          return TRUE;
        }
        break;
      case 'update':
        if (user_access('edit own forum post', $user) && $user->uid == $entity->uid) {
          return TRUE;
        }
        break;
      case 'delete':
        if (user_access('delete own forum post', $user) && $user->uid == $entity->uid) {
          return TRUE;
        }
        break;
    }
  }

  return FALSE;
}
0

There is a reference to the own functionality through eck's own behavior system, in the "better permissions" issue you mentioned above. Check it out and give us some feedback.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.