Skip to content

Commit

Permalink
Added UUID in TeamMemberRole.php for JSONAPI (3x) (#902)
Browse files Browse the repository at this point in the history
  • Loading branch information
shishir-intelli authored Aug 1, 2023
1 parent 7fd71be commit eda56f4
Show file tree
Hide file tree
Showing 2 changed files with 80 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

/**
* Copyright 2023 Google Inc.
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of the GNU General Public License
* version 2 as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301, USA.
*/

namespace Drupal\apigee_edge_teams\Entity\Storage;

use Drupal\Core\Entity\ContentEntityTypeInterface;
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;

/**
* Entity storage class for team member role entities.
*/
class TeamMemberRoleStorageSchema extends SqlContentEntityStorageSchema {

/**
* {@inheritdoc}
*/
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
$schema = parent::getEntitySchema($entity_type, $reset);
// For JSONAPI uuid added in entity_keys, but because it, duplicate
// uuid field is generated, which cause error while installing team_member table.
if (!empty($schema['team_member_role']['fields']['uuid'])) {
foreach ($schema['team_member_role']['fields']['uuid'] as $key => $value) {
$schema['team_member_role']['fields']['uuid'][$key] = is_array($value) ? $value[0] : $value;
}
// Fix to remove duplicate UUID field in primary key.
if (!empty($schema['team_member_role']['unique keys']['team_member_role_field__uuid__value'][1]) && $schema['team_member_role']['unique keys']['team_member_role_field__uuid__value'][1] == 'uuid') {
unset($schema['team_member_role']['unique keys']['team_member_role_field__uuid__value'][1]);
}
}
return $schema;
}

}
30 changes: 30 additions & 0 deletions modules/apigee_edge_teams/src/Entity/TeamMemberRole.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@

namespace Drupal\apigee_edge_teams\Entity;

use Drupal\Core\Access\AccessResult;
use Drupal\Core\Access\AccessResultAllowed;
use Drupal\Core\Entity\ContentEntityBase;
use Drupal\Core\Entity\EntityChangedTrait;
use Drupal\Core\Entity\EntityTypeInterface;
use Drupal\Core\Field\BaseFieldDefinition;
use Drupal\Core\Field\FieldStorageDefinitionInterface;
use Drupal\Core\Session\AccountInterface;
use Drupal\user\UserInterface;

/**
Expand All @@ -41,9 +44,11 @@
* data_table = "team_member_role_data",
* handlers = {
* "storage" = "Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorage",
* "storage_schema" = "Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorageSchema",
* },
* entity_keys = {
* "id" = "uuid",
* "uuid" = "uuid",
* },
* )
*
Expand All @@ -56,6 +61,31 @@ final class TeamMemberRole extends ContentEntityBase implements TeamMemberRoleIn

use EntityChangedTrait;

/**
* {@inheritdoc}
*/
public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
if (!$account) {
// If we have hit this without an account return forbidden.
return AccessResult::forbidden();
}

$result = AccessResult::allowedIfHasPermissions($account, [
'administer team',
'manage team members',
], 'OR');

if ($result->isNeutral()) {
$team = $this->getTeam();
$team_permission_handler = \Drupal::service('apigee_edge_teams.team_permissions');
$result = AccessResultAllowed::allowedIf(in_array('team_manage_members', $team_permission_handler->getDeveloperPermissionsByTeam($team, $account)))
->addCacheableDependency($team)
->cachePerUser();
}

return $result;
}

/**
* {@inheritdoc}
*/
Expand Down

0 comments on commit eda56f4

Please sign in to comment.