Skip to content

Commit eda56f4

Browse files
Added UUID in TeamMemberRole.php for JSONAPI (3x) (#902)
1 parent 7fd71be commit eda56f4

File tree

2 files changed

+80
-0
lines changed

2 files changed

+80
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<?php
2+
3+
/**
4+
* Copyright 2023 Google Inc.
5+
*
6+
* This program is free software; you can redistribute it and/or
7+
* modify it under the terms of the GNU General Public License
8+
* version 2 as published by the Free Software Foundation.
9+
*
10+
* This program is distributed in the hope that it will be useful,
11+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13+
* GNU General Public License for more details.
14+
*
15+
* You should have received a copy of the GNU General Public License
16+
* along with this program; if not, write to the Free Software
17+
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18+
* MA 02110-1301, USA.
19+
*/
20+
21+
namespace Drupal\apigee_edge_teams\Entity\Storage;
22+
23+
use Drupal\Core\Entity\ContentEntityTypeInterface;
24+
use Drupal\Core\Entity\Sql\SqlContentEntityStorageSchema;
25+
26+
/**
27+
* Entity storage class for team member role entities.
28+
*/
29+
class TeamMemberRoleStorageSchema extends SqlContentEntityStorageSchema {
30+
31+
/**
32+
* {@inheritdoc}
33+
*/
34+
protected function getEntitySchema(ContentEntityTypeInterface $entity_type, $reset = FALSE) {
35+
$schema = parent::getEntitySchema($entity_type, $reset);
36+
// For JSONAPI uuid added in entity_keys, but because it, duplicate
37+
// uuid field is generated, which cause error while installing team_member table.
38+
if (!empty($schema['team_member_role']['fields']['uuid'])) {
39+
foreach ($schema['team_member_role']['fields']['uuid'] as $key => $value) {
40+
$schema['team_member_role']['fields']['uuid'][$key] = is_array($value) ? $value[0] : $value;
41+
}
42+
// Fix to remove duplicate UUID field in primary key.
43+
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') {
44+
unset($schema['team_member_role']['unique keys']['team_member_role_field__uuid__value'][1]);
45+
}
46+
}
47+
return $schema;
48+
}
49+
50+
}

modules/apigee_edge_teams/src/Entity/TeamMemberRole.php

+30
Original file line numberDiff line numberDiff line change
@@ -20,11 +20,14 @@
2020

2121
namespace Drupal\apigee_edge_teams\Entity;
2222

23+
use Drupal\Core\Access\AccessResult;
24+
use Drupal\Core\Access\AccessResultAllowed;
2325
use Drupal\Core\Entity\ContentEntityBase;
2426
use Drupal\Core\Entity\EntityChangedTrait;
2527
use Drupal\Core\Entity\EntityTypeInterface;
2628
use Drupal\Core\Field\BaseFieldDefinition;
2729
use Drupal\Core\Field\FieldStorageDefinitionInterface;
30+
use Drupal\Core\Session\AccountInterface;
2831
use Drupal\user\UserInterface;
2932

3033
/**
@@ -41,9 +44,11 @@
4144
* data_table = "team_member_role_data",
4245
* handlers = {
4346
* "storage" = "Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorage",
47+
* "storage_schema" = "Drupal\apigee_edge_teams\Entity\Storage\TeamMemberRoleStorageSchema",
4448
* },
4549
* entity_keys = {
4650
* "id" = "uuid",
51+
* "uuid" = "uuid",
4752
* },
4853
* )
4954
*
@@ -56,6 +61,31 @@ final class TeamMemberRole extends ContentEntityBase implements TeamMemberRoleIn
5661

5762
use EntityChangedTrait;
5863

64+
/**
65+
* {@inheritdoc}
66+
*/
67+
public function access($operation, AccountInterface $account = NULL, $return_as_object = FALSE) {
68+
if (!$account) {
69+
// If we have hit this without an account return forbidden.
70+
return AccessResult::forbidden();
71+
}
72+
73+
$result = AccessResult::allowedIfHasPermissions($account, [
74+
'administer team',
75+
'manage team members',
76+
], 'OR');
77+
78+
if ($result->isNeutral()) {
79+
$team = $this->getTeam();
80+
$team_permission_handler = \Drupal::service('apigee_edge_teams.team_permissions');
81+
$result = AccessResultAllowed::allowedIf(in_array('team_manage_members', $team_permission_handler->getDeveloperPermissionsByTeam($team, $account)))
82+
->addCacheableDependency($team)
83+
->cachePerUser();
84+
}
85+
86+
return $result;
87+
}
88+
5989
/**
6090
* {@inheritdoc}
6191
*/

0 commit comments

Comments
 (0)