You are here

function user_field_privacy_field_access in User Field Privacy 7

Implements hook_field_access().

File

./user_field_privacy.module, line 201
Provides options for users to override visibility of their own fields.

Code

function user_field_privacy_field_access($op, $field, $entity_type, $entity, $account) {
  if ($entity_type == 'user') {
    switch ($op) {
      case 'view':
        $instance = field_info_instance('user', $field['field_name'], 'user');
        if ($instance['settings']['user_field_privacy']) {

          // Grant access if this permission is granted to the viewer.
          if (user_access('access private fields')) {
            return TRUE;
          }

          // If the to-be-displayed field's owner have not submitted the user
          // add/edit form with the user_field_privacy checkbox on it, then s/he
          // will not have any data related to this field, so her/his $entity
          // user object is not populated here. In other words, her/his field
          // should be available to the public, so grant access.
          if (!is_object($entity)) {
            return TRUE;
          }

          // If the field is to be kept private, only grant access if the viewer
          // has the same uid as the $user being viewed.
          if (_user_field_privacy_value($field['id'], $entity->uid)) {
            return (bool) ($entity->uid == $account->uid);
          }

          // If the field is not to be kept private, grant access.
          return TRUE;
        }
        break;
    }
  }
}