Skip to content
Snippets Groups Projects
Commit 39ad8e04 authored by baldwinlouie's avatar baldwinlouie Committed by Yas Naoi
Browse files

Issue #3031735 by baldwinlouie, yas, Xiaohua Guan: Add more cloud server...

Issue #3031735 by baldwinlouie, yas, Xiaohua Guan: Add more cloud server template entity permissions
parent 521d17ec
No related branches found
Tags 8.x-1.0-rc2
No related merge requests found
Showing
with 232 additions and 130 deletions
......@@ -7,9 +7,8 @@
* Works with Cloud module.
*/
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Entity\EntityInterface;
use Drupal\cloud\Entity\CloudConfig;
use Drupal\Core\Routing\RouteMatchInterface;
/**
* Implements hook_help().
......@@ -46,23 +45,6 @@ function cloud_server_template_theme() {
return $theme;
}
/**
* Implements hook_entity_operation().
*
* @params \Drupal\Core\Entity\EntityInterface $entity
*/
function cloud_server_template_entity_operation(EntityInterface $entity) {
$operations = [];
if ($entity->getEntityTypeId() == 'cloud_server_template' && $entity->hasLinkTemplate('launch')) {
$operations['launch'] = [
'title' => t('Launch'),
'url' => $entity->toUrl('launch'),
'weight' => 100,
];
}
return $operations;
}
/**
* Implements hook_theme_suggestions_HOOK().
*/
......
......@@ -13,17 +13,29 @@ administer cloud server template entities:
description: 'Allow to access the administration form to configure Cloud Server Template entities.'
restrict access: true
delete cloud server template entities:
title: 'Delete Cloud Server Template entities'
delete any cloud server template entities:
title: 'Delete any Cloud Server Template entities'
edit cloud server template entities:
title: 'Edit Cloud Server Template entities'
delete own cloud server template entities:
title: 'Delete own Cloud Server Template entities'
view published cloud server template entities:
title: 'View published Cloud Server Template entities'
edit any cloud server template entities:
title: 'Edit any Cloud Server Template entities'
view unpublished cloud server template entities:
title: 'View unpublished Cloud Server Template entities'
edit own cloud server template entities:
title: 'Edit own Cloud Server Template entities'
view any published cloud server template entities:
title: 'View any published Cloud Server Template entities'
view own published cloud server template entities:
title: 'View own published Cloud Server Template entities'
view any unpublished cloud server template entities:
title: 'View any unpublished Cloud Server Template entities'
view own unpublished cloud server template entities:
title: 'View own unpublished Cloud Server Template entities'
access cloud server template revisions:
title: 'Access Cloud Server Template revisions'
......
......@@ -18,19 +18,42 @@ class CloudServerTemplateAccessControlHandler extends EntityAccessControlHandler
* {@inheritdoc}
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
// First check for cloud_context access.
if (!AccessResult::allowedIfHasPermission($account, 'view ' . $entity->getCloudContext())
->isAllowed()
) {
return AccessResult::neutral();
}
// Determine if the user is the entity owner id.
$is_entity_owner = $account->id() == $entity->getOwner()->id();
/** @var \Drupal\cloud_server_template\Entity\CloudServerTemplateInterface $entity */
switch ($operation) {
case 'view':
if (!$entity->isPublished()) {
return AccessResult::allowedIfHasPermissions($account, ['view unpublished cloud server template entities', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('view any unpublished cloud server template entities')) {
return AccessResult::allowed();
}
return AccessResult::allowedIf($account->hasPermission('view own unpublished cloud server template entities') && $is_entity_owner);
}
return AccessResult::allowedIfHasPermissions($account, ['view published cloud server template entities', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('view any published cloud server template entities')) {
return AccessResult::allowed();
}
return AccessResult::allowedIf($account->hasPermission('view own published cloud server template entities') && $is_entity_owner);
case 'update':
return AccessResult::allowedIfHasPermissions($account, ['edit cloud server template entities', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('edit any cloud server template entities')) {
return AccessResult::allowed();
}
return AccessResult::allowedIf($account->hasPermission('edit own cloud server template entities') && $is_entity_owner);
case 'delete':
return AccessResult::allowedIfHasPermissions($account, ['delete cloud server template entities', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('delete any cloud server template entities')) {
return AccessResult::allowed();
}
return AccessResult::allowedIf($account->hasPermission('delete own cloud server template entities') && $is_entity_owner);
}
// Unknown operation, no opinion.
......
<?php
// Updated by yas 2015/06/03
// Updated by yas 2015/06/01
// Updated by yas 2015/05/31
// Created by yas 2015/05/30.
namespace Drupal\cloud_server_template\Controller;
use Drupal\cloud\Controller\CloudContentListBuilder;
......@@ -32,24 +28,72 @@ class CloudServerTemplateListBuilder extends CloudContentListBuilder {
return $header + parent::buildHeader();
}
/**
* {@inheritdoc}
*/
public function load() {
$header = $this->buildHeader();
$query = $this->getStorage()->getQuery();
// Get cloud_context from a path.
$cloud_context = $this->routeMatch->getParameter('cloud_context');
if (isset($cloud_context)) {
$query->tableSort($header)
->condition('cloud_context', $cloud_context);
}
else {
$query->tableSort($header);
}
// Only return templates the current user owns.
if (!$this->currentUser->hasPermission('view any published cloud server template entities')) {
if ($this->currentUser->hasPermission('view own published cloud server template entities')) {
$query->condition('user_id', $this->currentUser->id());
}
else {
// Don't return any results if the user does not have any of the above conditions.
return [];
}
}
$keys = $query->execute();
return $this->storage->loadMultiple($keys);
}
/**
* {@inheritdoc}
*/
public function buildRow(EntityInterface $entity) {
$row = [];
$account = \Drupal::currentUser();
if ($account->hasPermission('view ' . $entity->getCloudContext())) {
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.cloud_server_template.canonical',
[
'cloud_server_template' => $entity->id(),
'cloud_context' => $entity->getCloudContext(),
]
);
return $row + parent::buildRow($entity);
$row['name'] = Link::createFromRoute(
$entity->label(),
'entity.cloud_server_template.canonical',
[
'cloud_server_template' => $entity->id(),
'cloud_context' => $entity->getCloudContext(),
]
);
return $row + parent::buildRow($entity);
}
/**
* {@inheritdoc}
*/
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity->hasLinkTemplate('launch')) {
if ($this->currentUser->hasPermission('launch server template')) {
$operations['launch'] = [
'title' => t('Launch'),
'url' => $entity->toUrl('launch'),
'weight' => 100,
];
}
}
return $operations;
}
}
......@@ -119,8 +119,8 @@ class CloudServerTemplate extends RevisionableContentEntityBase implements Cloud
}
}
// If no revision author has been set explicitly, make the cloud_server_template owner the
// revision author.
// If no revision author has been set explicitly, make the
// cloud_server_template owner the revision author.
if (!$this->getRevisionUser()) {
$this->setRevisionUserId($this->getOwnerId());
}
......
......@@ -15,7 +15,6 @@ use Drupal\user\EntityOwnerInterface;
*/
interface CloudServerTemplateInterface extends ContentEntityInterface, RevisionLogInterface, EntityChangedInterface, EntityOwnerInterface, CloudContextInterface {
// Add get/set methods for your configuration properties here.
/**
* Gets the Cloud Server Template name.
*
......@@ -68,7 +67,8 @@ interface CloudServerTemplateInterface extends ContentEntityInterface, RevisionL
* Sets the published status of a Cloud Server Template.
*
* @param bool $published
* TRUE to set this Cloud Server Template to published, FALSE to set it to unpublished.
* TRUE to set this Cloud Server Template to published, FALSE to set it to
* unpublished.
*
* @return \Drupal\cloud_server_template\Entity\CloudServerTemplateInterface
* The called Cloud Server Template entity.
......
......@@ -93,7 +93,8 @@ class CloudServerTemplateLaunchConfirm extends ContentEntityConfirmFormBase {
parent::submitForm($form, $form_state);
// Launch the instance here.
$redirect_route = $this->serverTemplatePluginManager->launch($this->entity, $form_state);
// Let other modules alter the redirect after a server template has been launched.
// Let other modules alter the redirect after a server template has been
// launched.
\Drupal::moduleHandler()->invokeAll('cloud_server_template_post_launch_redirect_alter', [&$redirect_route, $this->entity]);
$form_state->setRedirectUrl(new Url($redirect_route['route_name'], $redirect_route['params']));
}
......
......@@ -10,7 +10,7 @@ use Drupal\cloud_server_template\Entity\CloudServerTemplateInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
* Provides a form for reverting a Cloud Server Template revision for a single translation.
* Provides a form for reverting a Cloud Server Template revision.
*
* @ingroup cloud_server_template
*/
......
......@@ -21,9 +21,10 @@ interface CloudServerTemplatePluginInterface {
public function getEntityBundleName();
/**
* Method is responsible for interacting with the implementing cloud's launch functionality.
* Method interacts with the implementing cloud's launch functionality.
*
* The server template contains all the information needed for that particular cloud.
* The server template contains all the information needed for that particular
* cloud.
*
* @param \Drupal\cloud_server_template\Entity\CloudServerTemplateInterface $cloud_server_template
* A Cloud Server Template object.
......@@ -31,7 +32,8 @@ interface CloudServerTemplatePluginInterface {
* Form state if launch is called from a form.
*
* @return array
* An associative array with a redirect route and any parameters to build the route.
* An associative array with a redirect route and any parameters to build
* the route.
*/
public function launch(CloudServerTemplateInterface $cloud_server_template, FormStateInterface $form_state = NULL);
......
......@@ -31,7 +31,8 @@ interface CloudServerTemplatePluginManagerInterface extends PluginManagerInterfa
* Form state if launch is called from a form.
*
* @return mixed
* An associative array with a redirect route and any parameters to build the route.
* An associative array with a redirect route and any parameters to build
* the route.
*/
public function launch(CloudServerTemplateInterface $cloud_server_template, FormStateInterface $form_state = NULL);
......
......@@ -15,6 +15,7 @@ class CloudServerTemplateCloudContextBundleDeriver extends DeriverBase implement
/**
* The entity type bundle info.
*
* @var \Drupal\Core\Entity\EntityTypeBundleInfoInterface
*/
protected $entityTypeBundleInfo;
......
......@@ -37,10 +37,10 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
return [
'add cloud server template entities',
'list cloud server template',
'view published cloud server template entities',
'view unpublished cloud server template entities',
'edit cloud server template entities',
'delete cloud server template entities',
'view any published cloud server template entities',
'view any unpublished cloud server template entities',
'edit any cloud server template entities',
'delete any cloud server template entities',
'access cloud server template revisions',
'revert all cloud server template revisions',
'delete all cloud server template revisions',
......@@ -220,11 +220,9 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
$vpc_index = array_rand($vpcs);
$add[0]['field_vpc'] = $vpcs[$vpc_index]['VpcId'];
$vpc_name = $this->getNameFromArray($vpcs, $vpc_index, 'VpcId');
$subnet_index = array_rand($subnets);
$add[0]['field_subnet'] = $subnets[$subnet_index]['SubnetId'];
$subnet_name = $this->getNameFromArray($subnets, $subnet_index, 'SubnetId');
$this->drupalPostForm("/clouds/design/server_template/add/$cloud_context/aws_cloud",
$add[0],
......@@ -232,13 +230,14 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
$this->assertResponse(200, t('HTTP 200: Add | A New CloudServerTemplate Form #@num', ['@num' => 1]));
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(t('Created the @name Cloud Server Template', [
'@name' => $add[0]['name[0][value]'],
]),
t('Confirm Message') . ': '
. t('Created the @name Cloud Server Template', [
'@name' => $add[0]['name[0][value]'],
]));
$this->assertText(
t('Created the @name Cloud Server Template', [
'@name' => $add[0]['name[0][value]'],
]),
t('Confirm Message') . ': '
. t('Created the @name Cloud Server Template', [
'@name' => $add[0]['name[0][value]'],
]));
// Make sure listing revisions.
$this->drupalGet("/clouds/design/server_template/$cloud_context/1/revisions");
......@@ -258,12 +257,12 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(t('Saved the @name Cloud Server Template.', [
'@name' => $edit['name[0][value]'],
]),
t('Confirm Message') . ': '
. t('Saved the @name Cloud Server Template.', [
'@name' => $edit['name[0][value]'],
]));
'@name' => $edit['name[0][value]'],
]),
t('Confirm Message') . ': '
. t('Saved the @name Cloud Server Template.', [
'@name' => $edit['name[0][value]'],
]));
// Make sure listing revisions.
$this->drupalGet("/clouds/design/server_template/$cloud_context/1/revisions");
......@@ -289,8 +288,8 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(t('Cloud Server Template @name has been reverted', [
'@name' => $edit['name[0][value]'],
]));
'@name' => $edit['name[0][value]'],
]));
// A new revision is created.
$this->assertSession()->linkByHrefExists("server_template/$cloud_context/1/revisions/2/view");
......@@ -302,8 +301,8 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
$this->assertNoText(t('Notice'), t('Make sure w/o Notice'));
$this->assertNoText(t('Warning'), t('Make sure w/o Warnings'));
$this->assertText(t('Cloud Server Template @name has been deleted.', [
'@name' => $edit['name[0][value]'],
]));
'@name' => $edit['name[0][value]'],
]));
// The revision is deleted.
$this->assertSession()->linkByHrefNotExists("server_template/$cloud_context/1/revisions/1/view");
}
......@@ -321,23 +320,23 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
// Input Fields.
$data[] = [
'cloud_context[0][value]' => $this->cloudContext,
'name[0][value]' => "Template #$num - " . date('Y/m/d') . $random->name(16, TRUE),
'field_description[0][value]' => "#$num: " . date('Y/m/d H:i:s - D M j G:i:s T Y')
. ' - SimpleTest Server Template Description - '
. $random->name(32, TRUE),
'field_test_only[value]' => '1',
'field_instance_type' => "m$num.xlarge",
'field_availability_zone' => 'us-west-1',
'field_monitoring[value]' => '1',
'field_image_id' => 1,
'field_min_count[0][value]' => 1,
'field_max_count[0][value]' => 1,
'status[value]' => '1',
'field_kernel_id[0][value]' => 'aki-' . $this->getRandomAwsId(),
'field_ram[0][value]' => 'ari-' . $this->getRandomAwsId(),
'field_security_group' => 1,
'field_ssh_key' => 1,
'cloud_context[0][value]' => $this->cloudContext,
'name[0][value]' => "Template #$num - " . date('Y/m/d') . $random->name(16, TRUE),
'field_description[0][value]' => "#$num: " . date('Y/m/d H:i:s - D M j G:i:s T Y')
. ' - SimpleTest Server Template Description - '
. $random->name(32, TRUE),
'field_test_only[value]' => '1',
'field_instance_type' => "m$num.xlarge",
'field_availability_zone' => 'us-west-1',
'field_monitoring[value]' => '1',
'field_image_id' => 1,
'field_min_count[0][value]' => 1,
'field_max_count[0][value]' => 1,
'status[value]' => '1',
'field_kernel_id[0][value]' => 'aki-' . $this->getRandomAwsId(),
'field_ram[0][value]' => 'ari-' . $this->getRandomAwsId(),
'field_security_group' => 1,
'field_ssh_key' => 1,
];
}
return $data;
......@@ -347,7 +346,6 @@ class CloudServerTemplateTest extends AwsCloudTestCase {
* Create image.
*/
private function createImage() {
$random = $this->random;
// Create image.
$image_id = 'ami-' . $this->getRandomAwsId();
......
<?php
// Updated by yas 2015/09/28
// Updated by yas 2015/06/08
// Created by yas 2015/06/01.
namespace Drupal\cloud\Controller;
use Drupal\Core\Entity\EntityListBuilder;
use Drupal\Core\Entity\EntityInterface;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Routing\RouteMatchInterface;
use Drupal\Core\Session\AccountProxyInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Drupal\Core\Entity\EntityStorageInterface;
/**
* Provides a listing of CloudEntity.
......@@ -14,29 +16,81 @@ use Drupal\Core\Entity\EntityInterface;
class CloudContentListBuilder extends EntityListBuilder {
/**
* Method takes cloud_context into the querying.
* The route match class.
*
* @var \Drupal\Core\Routing\RouteMatchInterface
*/
public function render() {
protected $routeMatch;
/**
* The current active user.
*
* @var \Drupal\Core\Session\AccountProxyInterface
*/
protected $currentUser;
/**
* {@inheritdoc}
*/
public static function createInstance(ContainerInterface $container, EntityTypeInterface $entity_type) {
return new static(
$entity_type,
$container->get('entity.manager')->getStorage($entity_type->id()),
$container->get('current_route_match'),
$container->get('current_user')
);
}
/**
* Constructs a new EntityListBuilder object.
*
* @param \Drupal\Core\Entity\EntityTypeInterface $entity_type
* The entity type definition.
* @param \Drupal\Core\Entity\EntityStorageInterface $storage
* The entity storage class.
* @param \Drupal\Core\Routing\RouteMatchInterface $route_match
* The currently active route match object.
* @param \Drupal\Core\Session\AccountProxyInterface $current_user
* The current user.
*/
public function __construct(EntityTypeInterface $entity_type, EntityStorageInterface $storage, RouteMatchInterface $route_match, AccountProxyInterface $current_user) {
$this->entityTypeId = $entity_type->id();
$this->storage = $storage;
$this->entityType = $entity_type;
$this->routeMatch = $route_match;
$this->currentUser = $current_user;
}
/**
* {@inheritdoc}
*/
public function load() {
$header = $this->buildHeader();
$storage = $this->getStorage();
$query = $storage->getQuery();
$query = $this->getStorage()->getQuery();
// Get cloud_context from a path.
$cloud_context = \Drupal::routeMatch()->getParameter('cloud_context');
$cloud_context = $this->routeMatch->getParameter('cloud_context');
if (isset($cloud_context)) {
$keys = $query->tableSort($header)
->condition('cloud_context', $cloud_context)
->execute();
$query->tableSort($header)
->condition('cloud_context', $cloud_context);
}
else {
$keys = $query->tableSort($header)
->execute();
$query->tableSort($header);
}
$entities = $storage->loadMultiple($keys);
// You need to implement buildRow method.
$keys = $query->execute();
return $this->storage->loadMultiple($keys);
}
/**
* Method takes cloud_context into the querying.
*/
public function render() {
$header = $this->buildHeader();
$entities = $this->load();
$rows = [];
foreach ($entities as $entity) {
$rows[] = $this->buildRow($entity);
......@@ -75,21 +129,5 @@ class CloudContentListBuilder extends EntityListBuilder {
return $operations;
}
/**
* {@inheritdoc}
* For reference.
*/
/*
public function getDefaultOperations(EntityInterface $entity) {
$operations = parent::getDefaultOperations($entity);
if ($entity->hasLinkTemplate('edit-form')) {
$operations['edit'] = array(
'title' => t('Edit Cloud Pricing'),
'weight' => 20,
'url' => $entity->urlInfo('edit-form'),
);
}
return $operations;
}
*/
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment