You are here

function profile_profile_access in Profile 2 8

Implements hook_profile_access().

File

./profile.module, line 51
Support for configurable user profiles.

Code

function profile_profile_access(ProfileInterface $profile, $op, $account) {
  $type = $profile
    ->bundle();
  switch ($op) {
    case 'view':
      if ($account
        ->hasPermission('view any ' . $type . ' profile', $account)) {
        return AccessResult::allowed()
          ->cachePerRole();
      }
      else {
        return AccessResult::allowedIfHasPermission($account, 'view own ' . $type . ' profile');
      }
    case 'create':
      if ($account
        ->hasPermission('add any ' . $type . ' profile', $account)) {
        return AccessResult::allowed()
          ->cachePerRole();
      }
      else {
        return AccessResult::allowedIfHasPermission($account, 'add own ' . $type . ' profile');
      }
    case 'update':
      if ($account
        ->hasPermission('edit any ' . $type . ' profile', $account)) {
        return AccessResult::allowed()
          ->cachePerRole();
      }
      else {
        return AccessResult::allowedIf($account
          ->hasPermission('edit own ' . $type . ' profile', $account) && $account
          ->id() == $profile
          ->getOwnerId())
          ->cachePerRole()
          ->cachePerUser()
          ->cacheUntilEntityChanges($profile);
      }
    case 'delete':
      if ($account
        ->hasPermission('delete any ' . $type . ' profile', $account)) {
        return AccessResult::allowed()
          ->cachePerRole();
      }
      else {
        return AccessResult::allowedIf($account
          ->hasPermission('delete own ' . $type . ' profile', $account) && $account
          ->id() == $profile
          ->getOwnerId())
          ->cachePerRole()
          ->cachePerUser()
          ->cacheUntilEntityChanges($profile);
      }
    default:

      // No opinion.
      return AccessResult::neutral();
  }
}