/** * {@inheritdoc} */ public function getFieldAccess($operation, $items, AccountInterface $account, $field_definition) { $default_type = FieldPermissionsService::fieldGetPermissionType($field_definition); if (in_array("administrator", $account->getRoles()) || $default_type == FIELD_PERMISSIONS_PUBLIC) { return TRUE; } if ($default_type == FIELD_PERMISSIONS_PRIVATE) { if ($operation === "view") { if ($items->getEntity()->getOwnerId() == $account->id()) { return $account->hasPermission($operation . "_own_" . $field_name); } else { return FALSE; } } elseif ($operation === "edit") { if ($items->getEntity()->isNew()) { return $account->hasPermission("create_" . $field_name); } elseif ($items->getEntity()->getOwnerId() == $account->id()) { return $account->hasPermission($operation . "_own_" . $field_name); } else { return FALSE; } } } if ($default_type == FIELD_PERMISSIONS_CUSTOM) { if ($operation === "view") { if ($account->hasPermission($operation . "_" . $field_name)) { return $account->hasPermission($operation . "_" . $field_name); } elseif ($items->getEntity()->getOwnerId() == $account->id()) { return $account->hasPermission($operation . "_own_" . $field_name); } } elseif ($operation === "edit") { if ($items->getEntity()->isNew()) { return $account->hasPermission("create_" . $field_name); } if ($account->hasPermission($operation . "_" . $field_name)) { return $account->hasPermission($operation . "_" . $field_name); } elseif ($items->getEntity()->getOwnerId() == $account->id()) { return $account->hasPermission($operation . "_own_" . $field_name); } } } }
/** * Checks access for a list of the user's purchased file downloads. * * @param \Drupal\Core\Session\AccountInterface $account * Run access checks for this account. */ public function accessUserDownloads(AccountInterface $account) { $user = \Drupal::currentUser(); return $user->id() && ($user->hasPermission('view all downloads') || $user->id() == $account->id()); }