/** * {@inheritdoc} */ protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) { switch ($operation) { case 'view': if ($account->hasPermission('access grade items') && $account->isAuthenticated() && $account->id() == $entity->getOwnerId()) { return AccessResult::allowed()->cachePerPermissions()->cachePerUser()->cacheUntilEntityChanges($entity); } case 'update': if ($account->hasPermission('administer grade items')) { return AccessResult::allowed()->cachePerPermissions(); } if (!$account->hasPermission('access grade items')) { return AccessResult::neutral()->cachePerPermissions(); } return AccessResult::allowedIf($account->hasPermission('customize grade items') && $entity == grade_scale_current_displayed_set($account))->cachePerPermissions()->cacheUntilEntityChanges($entity); case 'delete': return AccessResult::allowedIf($account->hasPermission('administer grade items') && $entity->id() != 'default')->cachePerPermissions(); default: // No opinion. return AccessResult::neutral(); } }
/** * Tests unassigning a shortcut set. */ function testGradeScaleUnassign() { $new_set = $this->generateGradeScale($this->randomMachineName()); $grade_scale_storage = \Drupal::entityManager()->getStorage('grade_scale'); $grade_scale_storage->assignUser($new_set, $this->shortcutUser); $grade_scale_storage->unassignUser($this->shortcutUser); $current_set = grade_scale_current_displayed_set($this->shortcutUser); $default_set = shortcut_default_set($this->shortcutUser); $this->assertTrue($current_set->id() == $default_set->id(), "Successfully unassigned another user's grade scale."); }