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

Issue #3041538 by baldwinlouie, yas, Xiaohua Guan, Masami: Add granular...

Issue #3041538 by baldwinlouie, yas, Xiaohua Guan, Masami: Add granular permissions for Image, Snapshot, Volume
parent fc15a385
No related branches found
Tags 8.7.0-beta2
No related merge requests found
Showing
with 427 additions and 162 deletions
......@@ -445,6 +445,13 @@ function aws_cloud_update_8129() {
cloud_update_yml_definitions($files, 'aws_cloud');
}
/**
* Update image entity visibility field to boolean.
*/
function aws_cloud_update_8130() {
aws_cloud_update_entity_definitions();
}
/**
* Helper function to add fields to the entity type.
*
......
......@@ -24,6 +24,7 @@ use Drupal\Core\Ajax\ReplaceCommand;
use Drupal\Core\Link;
use Drupal\Core\Cache\Cache;
use Drupal\Core\StreamWrapper\PrivateStream;
use Drupal\Core\Database\Query\Condition;
// 1 day in seconds.
const AWS_CLOUD_REIMPORT_INTERVAL = 86400;
......@@ -627,10 +628,11 @@ function aws_cloud_entity_operation(EntityInterface $entity) {
* Implements hook_entity_operation_alter().
*
* @params arrays $operations
* Operations array.
* @params \Drupal\Core\Entity\EntityInterface $entity
* Entity interface.
*/
function aws_cloud_entity_operation_alter(array &$operations, EntityInterface $entity) {
$account = \Drupal::currentUser();
if ($entity->getEntityTypeId() == 'aws_cloud_volume') {
if ($entity->getState() == 'in-use') {
......@@ -679,6 +681,66 @@ function aws_cloud_query_aws_cloud_instance_views_access_alter(AlterableInterfac
}
/**
* Implements hook_query_TAG_Alter().
*
* Alter the query for users that can only view their own images.
*/
function aws_cloud_query_aws_cloud_image_views_access_alter(AlterableInterface $query) {
if (!$account = $query->getMetaData('account')) {
$account = \Drupal::currentUser();
}
if ($account->hasPermission('view any aws cloud image')) {
return;
}
else {
// Add a user_id condition and let users view public images.
$or = new Condition('OR');
$or->condition('aws_cloud_image.user_id', $account->id())
->condition('aws_cloud_image.visibility', TRUE);
$query->condition($or);
}
}
/**
* Implements hook_query_TAG_Alter().
*
* Alter the query for users that can only view their own instances.
*/
function aws_cloud_query_aws_cloud_snapshot_views_access_alter(AlterableInterface $query) {
if (!$account = $query->getMetaData('account')) {
$account = \Drupal::currentUser();
}
if ($account->hasPermission('view any aws cloud snapshot')) {
return;
}
else {
// Add a user_id condition.
$query->condition('aws_cloud_snapshot.user_id', $account->id());
}
}
/**
* Implements hook_query_TAG_Alter().
*
* Alter the query for users that can only view their own instances.
*/
function aws_cloud_query_aws_cloud_volume_views_access_alter(AlterableInterface $query) {
if (!$account = $query->getMetaData('account')) {
$account = \Drupal::currentUser();
}
if ($account->hasPermission('view any aws cloud volume')) {
return;
}
else {
// Add a user_id condition.
$query->condition('aws_cloud_volume.user_id', $account->id());
}
}
/**
* Implements hook_form_FORM_ID_alter().
*
......
......@@ -69,16 +69,24 @@ add aws cloud image:
list aws cloud image:
title: 'List AWS Cloud image'
description: 'Allow to list image'
view aws cloud image:
title: 'View AWS Cloud image'
description: 'Allow to view image'
edit aws cloud image:
title: 'Edit AWS Cloud image'
description: 'Allow to edit image'
delete aws cloud image:
title: 'Delete AWS Cloud image'
description: 'Allow to delete image'
view any aws cloud image:
title: 'View any AWS Cloud image'
description: 'Allow to view any image'
view own aws cloud image:
title: 'View own AWS Cloud image'
description: 'Allow to view any image'
edit any aws cloud image:
title: 'Edit any AWS Cloud image'
description: 'Allow to edit any image'
edit own aws cloud image:
title: 'Edit own AWS Cloud image'
description: 'Allow to edit own image'
delete any aws cloud image:
title: 'Delete any AWS Cloud image'
description: 'Allow to delete any image'
delete own aws cloud image:
title: 'Delete own AWS Cloud image'
description: 'Allow to delete edit image'
#############################
# AWS Cloud Network Interface
......@@ -174,15 +182,24 @@ add aws cloud volume:
list aws cloud volume:
title: 'List AWS Cloud EBS volume'
description: 'Allow to list volume'
view aws cloud volume:
title: 'View AWS Cloud EBS volume'
description: 'Allow to view volume'
edit aws cloud volume:
title: 'Edit AWS Cloud EBS volume'
description: 'Allow to edit volume'
delete aws cloud volume:
title: 'Delete AWS Cloud EBS volume'
description: 'Allow to delete volume'
view any aws cloud volume:
title: 'View any AWS Cloud EBS volume'
description: 'Allow to view any volume'
edit any aws cloud volume:
title: 'Edit any AWS Cloud EBS volume'
description: 'Allow to edit any volume'
delete any aws cloud volume:
title: 'Delete any AWS Cloud EBS volume'
description: 'Allow to delete any volume'
view own aws cloud volume:
title: 'View own AWS Cloud EBS volume'
description: 'Allow to view own volume'
edit own aws cloud volume:
title: 'Edit own AWS Cloud EBS volume'
description: 'Allow to edit own volume'
delete own aws cloud volume:
title: 'Delete own AWS Cloud EBS volume'
description: 'Allow to delete own volume'
##############
......@@ -195,15 +212,24 @@ add aws cloud snapshot:
list aws cloud snapshot:
title: 'List AWS Cloud EBS snapshot'
description: 'Allow to list snapshot'
view aws cloud snapshot:
title: 'View AWS Cloud EBS snapshot'
description: 'Allow to view snapshot'
edit aws cloud snapshot:
title: 'Edit AWS Cloud EBS snapshot'
description: 'Allow to edit snapshot'
delete aws cloud snapshot:
title: 'Delete AWS Cloud EBS snapshot'
description: 'Allow to delete snapshot'
view any aws cloud snapshot:
title: 'View any AWS Cloud EBS snapshot'
description: 'Allow to view any snapshot'
edit any aws cloud snapshot:
title: 'Edit any AWS Cloud EBS snapshot'
description: 'Allow to edit any snapshot'
delete any aws cloud snapshot:
title: 'Delete any AWS Cloud EBS snapshot'
description: 'Allow to delete any snapshot'
view own aws cloud snapshot:
title: 'View own AWS Cloud EBS snapshot'
description: 'Allow to view own snapshot'
edit own aws cloud snapshot:
title: 'Edit own AWS Cloud EBS snapshot'
description: 'Allow to edit own snapshot'
delete own aws cloud snapshot:
title: 'Delete own AWS Cloud EBS snapshot'
description: 'Allow to delete own snapshot'
######################
......
......@@ -10,7 +10,7 @@ entity.aws_cloud_instance.list_update:
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'edit own aws cloud instance'
perm: 'edit any aws cloud instance+edit own aws cloud instance'
# Commented out. This operation takes too long to complete and returns everything
entity.aws_cloud_image.list_update:
......@@ -18,7 +18,7 @@ entity.aws_cloud_image.list_update:
defaults:
_controller: '\Drupal\aws_cloud\Controller\Ec2\ApiController::updateImageList'
requirements:
_permission: 'edit aws cloud image'
_permission: 'edit any aws cloud image+edit own aws cloud image'
entity.aws_cloud_security_group.list_update:
path: '/clouds/aws_cloud/{cloud_context}/security_group/update'
......@@ -73,7 +73,7 @@ entity.aws_cloud_volume.list_update:
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'edit aws cloud volume'
perm: 'edit any aws cloud volume+edit own aws cloud volume'
entity.aws_cloud_snapshot.list_update:
path: '/clouds/aws_cloud/{cloud_context}/snapshot/update'
......@@ -84,8 +84,7 @@ entity.aws_cloud_snapshot.list_update:
# Desired permission is passed as an option in the "perm" variable
_custom_access: '\Drupal\cloud\Controller\CloudConfigController::access'
options:
perm: 'edit aws cloud snapshot'
perm: 'edit any aws cloud snapshot+edit own aws cloud snapshot'
# AWS Cloud Admin Settings
aws_cloud.settings:
......
.field--label-inline {
clear: both;
}
.field--name-network-interfaces.field--label-inline .field__items {
float: none;
}
.field--name-network-interfaces .field__items {
display: inline-block;
}
\ No newline at end of file
<?php
// Updated by yas 2016/06/12
// Updated by yas 2016/05/30
// Updated by yas 2016/05/29
// Updated by yas 2016/05/28
// Updated by yas 2016/05/25
// Updated by yas 2016/05/18
// Created by yas 2016/04/19.
namespace Drupal\aws_cloud\Aws\Ec2;
use Drupal\Core\Entity\ContentEntityInterface;
......@@ -18,7 +11,7 @@ use Drupal\user\EntityOwnerInterface;
* @ingroup aws_cloud
*/
interface ImageInterface extends ContentEntityInterface, EntityOwnerInterface {
// Add get/set methods for your configuration properties here.
/**
* {@inheritdoc}
*/
......@@ -154,4 +147,9 @@ interface ImageInterface extends ContentEntityInterface, EntityOwnerInterface {
*/
public function setStatus($status);
/**
* {@inheritdoc}
*/
public function setVisibility($visibility);
}
<?php
// Updated by yas 2016/05/25
// udpated by yas 2016/05/20
// created by yas 2016/05/19.
namespace Drupal\aws_cloud\Controller\Ec2;
use Drupal\Core\Entity\EntityAccessControlHandler;
......@@ -22,16 +19,47 @@ class ImageAccessControlHandler extends EntityAccessControlHandler {
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if (!AccessResult::allowedIfHasPermission($account, 'view ' . $entity->getCloudContext())->isAllowed()) {
return AccessResult::neutral();
}
switch ($operation) {
case 'view':
return AccessResult::allowedIfHasPermissions($account, ['view aws cloud image', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('view any aws cloud image')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('view own aws cloud image') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'update':
case 'edit':
return AccessResult::allowedIfHasPermissions($account, ['edit aws cloud image', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('edit any aws cloud image')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('edit own aws cloud image') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'delete':
return AccessResult::allowedIfHasPermissions($account, ['delete aws cloud image', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('delete any aws cloud image')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('delete own aws cloud image') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
}
// Unknown operation, no opinion.
return AccessResult::neutral();
......@@ -40,7 +68,8 @@ class ImageAccessControlHandler extends EntityAccessControlHandler {
/**
* Not being used anymore.
*
* Access check is performed in \Drupal\cloud\Controller\CloudConfigController::access.
* Access check is performed in
* \Drupal\cloud\Controller\CloudConfigController::access.
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add aws cloud image');
......
<?php
// Updated by yas 2016/05/25
// updated by yas 2016/05/20
// created by yas 2016/05/19.
namespace Drupal\aws_cloud\Controller\Ec2;
use Drupal\Core\Entity\EntityAccessControlHandler;
......@@ -22,16 +19,47 @@ class SnapshotAccessControlHandler extends EntityAccessControlHandler {
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if (!AccessResult::allowedIfHasPermission($account, 'view ' . $entity->getCloudContext())->isAllowed()) {
return AccessResult::neutral();
}
switch ($operation) {
case 'view':
return AccessResult::allowedIfHasPermissions($account, ['view aws cloud snapshot', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('view any aws cloud snapshot')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('view own aws cloud snapshot') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'update':
case 'edit':
return AccessResult::allowedIfHasPermissions($account, ['edit aws cloud snapshot', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('edit any aws cloud snapshot')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('edit own aws cloud snapshot') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'delete':
return AccessResult::allowedIfHasPermissions($account, ['delete aws cloud snapshot', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('delete any aws cloud snapshot')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('delete own aws cloud snapshot') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
}
// Unknown operation, no opinion.
return AccessResult::neutral();
......@@ -40,7 +68,8 @@ class SnapshotAccessControlHandler extends EntityAccessControlHandler {
/**
* Not being used anymore.
*
* Access check is performed in \Drupal\cloud\Controller\CloudConfigController::access
* Access check is performed in
* \Drupal\cloud\Controller\CloudConfigController::access.
*/
protected function checkCreateAccess(AccountInterface $account, array $context, $entity_bundle = NULL) {
return AccessResult::allowedIfHasPermission($account, 'add aws cloud snapshot');
......
<?php
// Updated by yas 2016/05/25
// updated by yas 2016/05/20
// created by yas 2016/05/19.
namespace Drupal\aws_cloud\Controller\Ec2;
use Drupal\Core\Entity\EntityAccessControlHandler;
......@@ -22,29 +19,77 @@ class VolumeAccessControlHandler extends EntityAccessControlHandler {
*/
protected function checkAccess(EntityInterface $entity, $operation, AccountInterface $account) {
if (!AccessResult::allowedIfHasPermission($account, 'view ' . $entity->getCloudContext())->isAllowed()) {
return AccessResult::neutral();
}
switch ($operation) {
case 'view':
return AccessResult::allowedIfHasPermissions($account, ['view aws cloud volume', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('view any aws cloud volume')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('view own aws cloud volume') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'update':
case 'edit':
return AccessResult::allowedIfHasPermissions($account, ['edit aws cloud volume', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('edit any aws cloud volume')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('edit own aws cloud volume') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'delete':
return AccessResult::allowedIfHasPermissions($account, ['delete aws cloud volume', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('delete any aws cloud volume')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('delete own aws cloud volume') &&
($account->id() == $entity->getOwner()->id())
);
}
break;
case 'attach':
if ($entity->getState() == 'available') {
return AccessResult::allowedIfHasPermissions($account, ['edit aws cloud volume', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('edit any aws cloud volume')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('edit own aws cloud volume') &&
($account->id() == $entity->getOwner()->id())
);
}
}
break;
case 'detach':
if ($entity->getState() == 'in-use') {
return AccessResult::allowedIfHasPermissions($account, ['edit aws cloud volume', 'view ' . $entity->getCloudContext()]);
if ($account->hasPermission('edit any aws cloud volume')) {
return AccessResult::allowed();
}
else {
return AccessResult::allowedIf(
$account->hasPermission('edit own aws cloud volume') &&
($account->id() == $entity->getOwner()->id())
);
}
}
break;
}
// Unknown operation, no opinion.
return AccessResult::neutral();
}
......
<?php
// Updated by yas 2016/10/10
// Updated by yas 2016/06/12
// Updated by yas 2016/05/30
// Updated by yas 2016/05/29
// updated by yas 2016/05/28
// updated by yas 2016/05/26
// updated by yas 2016/05/25
// updated by yas 2016/05/23
// updated by yas 2016/05/19
// updated by yas 2016/05/18
// updated by yas 2016/05/17
// updated by yas 2016/05/11
// updated by yas 2016/05/10
// created by yas 2016/04/21.
namespace Drupal\aws_cloud\Entity\Ec2;
use Drupal\aws_cloud\Aws\Ec2\ImageInterface;
......@@ -242,6 +228,13 @@ class Image extends CloudContentEntityBase implements ImageInterface {
return $this->set('status', $status);
}
/**
* {@inheritdoc}
*/
public function setVisibility($visibility) {
return $this->set('visibility', $visibility);
}
/**
* {@inheritdoc}
*/
......@@ -463,7 +456,7 @@ class Image extends CloudContentEntityBase implements ImageInterface {
->setLabel(t('Description'))
->setDescription(t("The description of the AMI that was provided when the image was created. You can click Edit to change your own AMI's description."));
$fields['visibility'] = BaseFieldDefinition::create('string')
$fields['visibility'] = BaseFieldDefinition::create('boolean')
->setLabel(t('Visibility'))
->setDescription(t('The AMI visibility'))
->setDisplayOptions('view', [
......
<?php
// Updated by yas 2016/05/25
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
/**
......@@ -37,6 +35,7 @@ class ImageViewsData extends Ec2BaseViewsData {
'help' => t('The AWS Cloud Image entity ID.'),
];
$data['aws_cloud_image']['table']['base']['access query tag'] = 'aws_cloud_image_views_access';
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
......
<?php
// Updated by yas 2016/05/25
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
/**
......@@ -34,6 +32,7 @@ class SnapshotViewsData extends Ec2BaseViewsData {
'capacity',
];
$data['aws_cloud_snapshot']['table']['base']['access query tag'] = 'aws_cloud_snapshot_views_access';
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
......
<?php
// Updated by yas 2016/05/25
// created by yas 2016/05/20.
namespace Drupal\aws_cloud\Entity\Ec2;
/**
......@@ -29,6 +27,7 @@ class VolumeViewsData extends Ec2BaseViewsData {
'encrypted',
];
$data['aws_cloud_volume']['table']['base']['access query tag'] = 'aws_cloud_volume_views_access';
$this->addDropdownSelector($data, $table_name, $fields, $selectable);
return $data;
......
......@@ -2,19 +2,20 @@
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\aws_cloud\Service\AwsEc2ServiceInterface;
use Drupal\cloud\Form\CloudContentForm;
use Drupal\cloud\Plugin\CloudConfigPluginManagerInterface;
use Drupal\cloud\Service\EntityLinkRendererInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Entity\EntityManagerInterface;
use Drupal\Core\Entity\EntityRepositoryInterface;
use Drupal\Core\Messenger\Messenger;
use Drupal\cloud\Service\EntityLinkRendererInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Entity\EntityTypeManagerInterface;
use Drupal\Core\Cache\CacheBackendInterface;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
use Drupal\Core\Messenger\Messenger;
use Drupal\Core\Plugin\CachedDiscoveryClearerInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\Core\StringTranslation\TranslatableMarkup;
use Drupal\aws_cloud\Service\AwsEc2ServiceInterface;
use Drupal\cloud\Form\CloudContentForm;
use Drupal\cloud\Plugin\CloudConfigPluginManagerInterface;
use Symfony\Component\DependencyInjection\ContainerInterface;
/**
......@@ -64,6 +65,13 @@ class AwsCloudContentForm extends CloudContentForm {
*/
protected $cloudConfigPluginManager;
/**
* The current user.
*
* @var \Drupal\Core\Session\AccountInterface
*/
protected $currentUser;
/**
* AwsCloudContentForm constructor.
*
......@@ -85,6 +93,8 @@ class AwsCloudContentForm extends CloudContentForm {
* A plugin cache clear instance.
* @param \Drupal\cloud\Plugin\CloudConfigPluginManagerInterface $cloud_config_plugin_manager
* The cloud config plugin manager service.
* @param \Drupal\Core\Session\AccountInterface $current_user
* The current user.
*/
public function __construct(EntityManagerInterface $manager,
AwsEc2ServiceInterface $aws_ec2_service,
......@@ -94,7 +104,8 @@ class AwsCloudContentForm extends CloudContentForm {
EntityTypeManagerInterface $entity_type_manager,
CacheBackendInterface $cacheRender,
CachedDiscoveryClearerInterface $plugin_cache_clearer,
CloudConfigPluginManagerInterface $cloud_config_plugin_manager) {
CloudConfigPluginManagerInterface $cloud_config_plugin_manager,
AccountInterface $current_user) {
parent::__construct($manager, $entity_repository, $messenger);
$this->awsEc2Service = $aws_ec2_service;
$this->entityLinkRenderer = $entity_link_renderer;
......@@ -102,6 +113,7 @@ class AwsCloudContentForm extends CloudContentForm {
$this->cacheRender = $cacheRender;
$this->pluginCacheClearer = $plugin_cache_clearer;
$this->cloudConfigPluginManager = $cloud_config_plugin_manager;
$this->currentUser = $current_user;
}
/**
......@@ -117,7 +129,8 @@ class AwsCloudContentForm extends CloudContentForm {
$container->get('entity_type.manager'),
$container->get('cache.render'),
$container->get('plugin.cache_clearer'),
$container->get('plugin.manager.cloud_config_plugin')
$container->get('plugin.manager.cloud_config_plugin'),
$container->get('current_user')
);
}
......@@ -237,4 +250,28 @@ class AwsCloudContentForm extends CloudContentForm {
return $t->render() . ': ';
}
/**
* Set the uid tag for different entities.
*
* @param string $resource_id
* The resource id. For example, instance_id, volume_id.
* @param string $key
* The key to store this value.
* @param string $uid
* The drupal user id.
*/
protected function setUidInAws($resource_id, $key, $uid) {
$this->awsEc2Service->setCloudContext($this->entity->getCloudContext());
// Update the volume_created_by_uid.
$this->awsEc2Service->createTags([
'Resources' => [$resource_id],
'Tags' => [
[
'Key' => $key,
'Value' => $uid,
],
],
]);
}
}
......@@ -92,6 +92,13 @@ class ImageCreateForm extends AwsCloudContentForm {
&& ($entity->setImageId($result['ImageId']))
&& ($entity->set('account_id', $account_id))
&& ($entity->save())) {
$this->setUidInAws(
$this->entity->getImageId(),
'image_created_by_uid',
$this->entity->getOwner()->id()
);
$message = $this->t('The @label "%label (@image_id)" has been created.', [
'@label' => $entity->getEntityType()->getLabel(),
'%label' => $entity->label(),
......
<?php
// Created by yas 2016/05/30.
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
/**
* Form controller for the CloudScripting entity edit forms.
......@@ -181,4 +179,16 @@ class ImageEditForm extends AwsCloudContentForm {
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$this->setUidInAws(
$this->entity->getImageId(),
'image_created_by_uid',
$this->entity->getOwner()->id()
);
}
}
<?php
// Updated by yas 2016/06/04
// Updated by yas 2016/05/31
// Updated by yas 2016/05/30
// Updated by yas 2016/05/25
// Updated by yas 2016/05/21
// Updated by yas 2016/05/20
// Created by yas 2016/05/19.
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
......@@ -56,7 +49,7 @@ class SnapshotCreateForm extends AwsCloudContentForm {
$form['volume_id'] = [
'#type' => 'select',
'#title' => $this->t('Volume ID'),
'#options' => $this->getVolumeOptions(),
'#options' => $this->getVolumeOptions($cloud_context),
'#default_value' => $volume_id,
'#weight' => -5,
'#required' => TRUE,
......@@ -104,6 +97,12 @@ class SnapshotCreateForm extends AwsCloudContentForm {
&& ($entity->setEncrypted($result['Encrypted']))
&& ($entity->save())) {
$this->setUidInAws(
$this->entity->getSnapshotId(),
'snapshot_created_by_uid',
$entity->getOwner()->id()
);
$message = $this->t('The @label "%label (@snapshot_id)" has been created.', [
'@label' => $entity->getEntityType()->getLabel(),
'%label' => $entity->label(),
......@@ -126,34 +125,32 @@ class SnapshotCreateForm extends AwsCloudContentForm {
/**
* Helper function to get volume options.
*
* @param string $cloud_context
* Cloud context to use in the query.
*
* @return array
* Volume options.
*/
private function getVolumeOptions() {
private function getVolumeOptions($cloud_context) {
$options = [];
$result = $this->awsEc2Service->describeVolumes();
if ($result != NULL) {
$volumes = $result['Volumes'];
foreach ($volumes as $volume) {
$volume_id = $volume['VolumeId'];
$options[$volume_id] = $volume_id;
$volume_name = '';
if (isset($volume['Tags'])) {
foreach ($volume['Tags'] as $tag) {
if ($tag['Key'] == 'Name') {
$volume_name = $tag['Value'];
break;
}
}
}
if ($volume_name != '') {
$options[$volume_id] = "$volume_name ($volume_id)";
}
$params = [
'cloud_context' => $cloud_context,
];
if (!$this->currentUser->hasPermission('view any aws cloud volume')) {
$params['user_id'] = $this->currentUser->id();
}
$volumes = $this->entityTypeManager
->getStorage('aws_cloud_volume')
->loadByProperties($params);
foreach ($volumes as $volume) {
if ($volume->getName() != $volume->getVolumeId()) {
$options[$volume->getVolumeId()] = "{$volume->getName()} ({$volume->getVolumeId()})";
}
else {
$options[$volume->getVolumeId()] = $volume->getVolumeId();
}
}
return $options;
}
......
<?php
// Updated by yas 2016/06/04
// Updated by yas 2016/05/31
// Created by yas 2016/05/30.
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
/**
* Form controller for the CloudScripting entity edit forms.
......@@ -92,4 +88,18 @@ class SnapshotEditForm extends AwsCloudContentForm {
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
$this->setUidInAws(
$this->entity->getSnapshotId(),
'snapshot_created_by_uid',
$this->entity->getOwner()->id()
);
}
}
<?php
// Updated by yas 2016/09/11
// Updated by yas 2016/06/04
// Updated by yas 2016/05/31
// Updated by yas 2016/05/30
// Updated by yas 2016/05/25
// Updated by yas 2016/05/20
// Created by yas 2016/05/19.
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
......@@ -71,7 +64,7 @@ class VolumeCreateForm extends AwsCloudContentForm {
$form['snapshot_id'] = [
'#type' => 'select',
'#title' => $this->t('Snapshot ID'),
'#options' => $this->getSnapshotOptions(),
'#options' => $this->getSnapshotOptions($cloud_context),
'#default_value' => $snapshot_id,
'#weight' => -5,
'#required' => FALSE,
......@@ -219,6 +212,14 @@ class VolumeCreateForm extends AwsCloudContentForm {
'%label' => $entity->label(),
'@volume_id' => $entity->getVolumeId(),
]);
// Store the drupal uid in Aws.
$this->setUidInAws(
$this->entity->getVolumeId(),
'volume_created_by_uid',
$this->entity->getOwner()->id()
);
$this->messenger->addMessage($message);
$form_state->setRedirect('view.aws_volume.page_1', ['cloud_context' => $entity->getCloudContext()]);
}
......@@ -261,34 +262,32 @@ class VolumeCreateForm extends AwsCloudContentForm {
/**
* Helper function to get snapshot options.
*
* @param string $cloud_context
* Cloud context to use in the query.
*
* @return array
* Snapshot options.
*/
private function getSnapshotOptions() {
private function getSnapshotOptions($cloud_context) {
$options = [];
$result = $this->awsEc2Service->describeSnapshots();
if ($result != NULL) {
$snapshots = $result['Snapshots'];
foreach ($snapshots as $snapshot) {
$snapshot_id = $snapshot['SnapshotId'];
$options[$snapshot_id] = $snapshot_id;
$snapshot_name = '';
if (isset($snapshot['Tags'])) {
foreach ($snapshot['Tags'] as $tag) {
if ($tag['Key'] == 'Name') {
$snapshot_name = $tag['Value'];
break;
}
}
}
if ($snapshot_name != '') {
$options[$snapshot_id] = "$snapshot_name ($snapshot_id)";
}
$params = [
'cloud_context' => $cloud_context,
];
if (!$this->currentUser->hasPermission('view any aws cloud snapshot')) {
$params['user_id'] = $this->currentUser->id();
}
$snapshots = $this->entityTypeManager
->getStorage('aws_cloud_snapshot')
->loadByProperties($params);
foreach ($snapshots as $snapshot) {
if ($snapshot->getName() != $snapshot->getSnapshotId()) {
$options[$snapshot->getSnapshotId()] = "{$snapshot->getName()} ({$snapshot->getSnapshotId()})";
}
else {
$options[$snapshot->getSnapshotId()] = $snapshot->getSnapshotId();
}
}
return $options;
}
......
<?php
// Updated by yas 2016/05/31
// Created by yas 2016/05/30.
namespace Drupal\aws_cloud\Form\Ec2;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Language\Language;
/**
* Form controller for the CloudScripting entity edit forms.
......@@ -67,4 +64,19 @@ class VolumeEditForm extends AwsCloudContentForm {
return $form;
}
/**
* {@inheritdoc}
*/
public function save(array $form, FormStateInterface $form_state) {
parent::save($form, $form_state);
// Store the drupal uid in Aws.
$this->setUidInAws(
$this->entity->getVolumeId(),
'volume_created_by_uid',
$this->entity->getOwner()->id()
);
}
}
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