Skip to content

Commit

Permalink
Revert "[#4608] feat(core): Supports to load fields lazily" (#4950)
Browse files Browse the repository at this point in the history
Reverts #4690

Co-authored-by: roryqi <[email protected]>
  • Loading branch information
github-actions[bot] and jerqi authored Sep 18, 2024
1 parent 8b55591 commit bcd6f80
Show file tree
Hide file tree
Showing 31 changed files with 870 additions and 555 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,8 @@ public Boolean onOwnerSet(MetadataObject metadataObject, Owner preOwner, Owner n
UserEntity.builder()
.withId(1L)
.withName(newOwner.name())
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
onUserAdded(userEntity);
Expand All @@ -203,7 +204,8 @@ public Boolean onOwnerSet(MetadataObject metadataObject, Owner preOwner, Owner n
GroupEntity.builder()
.withId(1L)
.withName(newOwner.name())
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
onGroupAdded(groupEntity);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -483,7 +483,8 @@ public void testOnGrantedRolesToUser() {
UserEntity.builder()
.withId(1L)
.withName(userName1)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand All @@ -501,7 +502,8 @@ public void testOnGrantedRolesToUser() {
UserEntity.builder()
.withId(1L)
.withName(userName2)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand All @@ -523,7 +525,8 @@ public void testOnRevokedRolesFromUser() {
UserEntity.builder()
.withId(1L)
.withName(userName1)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand Down Expand Up @@ -552,7 +555,8 @@ public void testOnGrantedRolesToGroup() {
GroupEntity.builder()
.withId(1L)
.withName(groupName1)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand All @@ -570,7 +574,8 @@ public void testOnGrantedRolesToGroup() {
GroupEntity.builder()
.withId(1L)
.withName(groupName2)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand All @@ -593,7 +598,8 @@ public void testOnRevokedRolesFromGroup() {
GroupEntity.builder()
.withId(1L)
.withName(groupName1)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand Down Expand Up @@ -674,7 +680,8 @@ public void testCreateUser() {
.withId(0L)
.withName(currentFunName())
.withAuditInfo(auditInfo)
.withRoles(Collections.emptyList())
.withRoleIds(null)
.withRoleNames(null)
.build();
Assertions.assertTrue(rangerAuthPlugin.onUserAdded(user));
Assertions.assertTrue(rangerAuthPlugin.onUserAcquired(user));
Expand All @@ -689,7 +696,8 @@ public void testCreateGroup() {
.withId(0L)
.withName(currentFunName())
.withAuditInfo(auditInfo)
.withRoles(Collections.emptyList())
.withRoleIds(null)
.withRoleNames(null)
.build();

Assertions.assertTrue(rangerAuthPlugin.onGroupAdded(group));
Expand Down Expand Up @@ -761,7 +769,8 @@ public void testCombinationOperation() {
UserEntity.builder()
.withId(1L)
.withName(userName1)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand All @@ -777,7 +786,8 @@ public void testCombinationOperation() {
UserEntity.builder()
.withId(1L)
.withName(userName2)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand All @@ -790,7 +800,8 @@ public void testCombinationOperation() {
UserEntity.builder()
.withId(1L)
.withName(userName3)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand Down Expand Up @@ -822,7 +833,8 @@ public void testCombinationOperation() {
GroupEntity.builder()
.withId(1L)
.withName(groupName1)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand All @@ -840,7 +852,8 @@ public void testCombinationOperation() {
GroupEntity.builder()
.withId(1L)
.withName(groupName2)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand All @@ -858,7 +871,8 @@ public void testCombinationOperation() {
GroupEntity.builder()
.withId(1L)
.withName(groupName3)
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withRoleIds(Collections.emptyList())
.withAuditInfo(auditInfo)
.build();
Assertions.assertTrue(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,9 @@
import static org.apache.gravitino.authorization.AuthorizationUtils.USER_DOES_NOT_EXIST_MSG;

import com.google.common.collect.Lists;
import com.google.common.collect.Maps;
import java.io.IOException;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.apache.commons.lang3.StringUtils;
Expand Down Expand Up @@ -72,10 +70,14 @@ User grantRolesToUser(String metalake, List<String> roles, String user) {
UserEntity.class,
Entity.EntityType.USER,
userEntity -> {
List<RoleEntity> roleEntities = Lists.newArrayList(userEntity.roleEntities());

List<Long> roleIds =
roleEntities.stream().map(RoleEntity::id).collect(Collectors.toList());
List<RoleEntity> roleEntities = Lists.newArrayList();
if (userEntity.roleNames() != null) {
for (String role : userEntity.roleNames()) {
roleEntities.add(roleManager.getRole(metalake, role));
}
}
List<String> roleNames = Lists.newArrayList(toRoleNames(roleEntities));
List<Long> roleIds = Lists.newArrayList(toRoleIds(roleEntities));

for (RoleEntity roleEntityToGrant : roleEntitiesToGrant) {
if (roleIds.contains(roleEntityToGrant.id())) {
Expand All @@ -85,8 +87,8 @@ User grantRolesToUser(String metalake, List<String> roles, String user) {
user,
metalake);
} else {
roleNames.add(roleEntityToGrant.name());
roleIds.add(roleEntityToGrant.id());
roleEntities.add(roleEntityToGrant);
}
}

Expand All @@ -102,7 +104,8 @@ User grantRolesToUser(String metalake, List<String> roles, String user) {
.withNamespace(userEntity.namespace())
.withId(userEntity.id())
.withName(userEntity.name())
.withRoles(roleEntities)
.withRoleNames(roleNames)
.withRoleIds(roleIds)
.withAuditInfo(auditInfo)
.build();
});
Expand Down Expand Up @@ -146,8 +149,13 @@ Group grantRolesToGroup(String metalake, List<String> roles, String group) {
GroupEntity.class,
Entity.EntityType.GROUP,
groupEntity -> {
List<RoleEntity> roleEntities = Lists.newArrayList(groupEntity.roleEntities());

List<RoleEntity> roleEntities = Lists.newArrayList();
if (groupEntity.roleNames() != null) {
for (String role : groupEntity.roleNames()) {
roleEntities.add(roleManager.getRole(metalake, role));
}
}
List<String> roleNames = Lists.newArrayList(toRoleNames(roleEntities));
List<Long> roleIds = Lists.newArrayList(toRoleIds(roleEntities));

for (RoleEntity roleEntityToGrant : roleEntitiesToGrant) {
Expand All @@ -158,8 +166,8 @@ Group grantRolesToGroup(String metalake, List<String> roles, String group) {
group,
metalake);
} else {
roleNames.add(roleEntityToGrant.name());
roleIds.add(roleEntityToGrant.id());
roleEntities.add(roleEntityToGrant);
}
}

Expand All @@ -175,7 +183,8 @@ Group grantRolesToGroup(String metalake, List<String> roles, String group) {
.withId(groupEntity.id())
.withNamespace(groupEntity.namespace())
.withName(groupEntity.name())
.withRoles(roleEntities)
.withRoleNames(roleNames)
.withRoleIds(roleIds)
.withAuditInfo(auditInfo)
.build();
});
Expand Down Expand Up @@ -219,18 +228,19 @@ Group revokeRolesFromGroup(String metalake, List<String> roles, String group) {
GroupEntity.class,
Entity.EntityType.GROUP,
groupEntity -> {
List<RoleEntity> roleEntities = groupEntity.roleEntities();

// Avoid loading securable objects
Map<Long, RoleEntity> roleEntitiesMap = Maps.newHashMap();
for (RoleEntity entity : roleEntities) {
roleEntitiesMap.put(entity.id(), entity);
List<RoleEntity> roleEntities = Lists.newArrayList();
if (groupEntity.roleNames() != null) {
for (String role : groupEntity.roleNames()) {
roleEntities.add(roleManager.getRole(metalake, role));
}
}
List<String> roleNames = Lists.newArrayList(toRoleNames(roleEntities));
List<Long> roleIds = Lists.newArrayList(toRoleIds(roleEntities));

for (RoleEntity roleEntityToRevoke : roleEntitiesToRevoke) {
if (roleEntitiesMap.containsKey(roleEntityToRevoke.id())) {
roleEntitiesMap.remove(roleEntityToRevoke.id());
} else {
roleNames.remove(roleEntityToRevoke.name());
boolean removed = roleIds.remove(roleEntityToRevoke.id());
if (!removed) {
LOG.warn(
"Failed to revoke, role {} does not exist in the group {} of metalake {}",
roleEntityToRevoke.name(),
Expand All @@ -251,7 +261,8 @@ Group revokeRolesFromGroup(String metalake, List<String> roles, String group) {
.withNamespace(groupEntity.namespace())
.withId(groupEntity.id())
.withName(groupEntity.name())
.withRoles(Lists.newArrayList(roleEntitiesMap.values()))
.withRoleNames(roleNames)
.withRoleIds(roleIds)
.withAuditInfo(auditInfo)
.build();
});
Expand Down Expand Up @@ -297,18 +308,20 @@ User revokeRolesFromUser(String metalake, List<String> roles, String user) {
UserEntity.class,
Entity.EntityType.USER,
userEntity -> {
List<RoleEntity> roleEntities = userEntity.roleEntities();

// Avoid loading securable objects
Map<Long, RoleEntity> roleEntitiesMap = Maps.newHashMap();
for (RoleEntity entity : roleEntities) {
roleEntitiesMap.put(entity.id(), entity);
List<RoleEntity> roleEntities = Lists.newArrayList();
if (userEntity.roleNames() != null) {
for (String role : userEntity.roleNames()) {
roleEntities.add(roleManager.getRole(metalake, role));
}
}

List<String> roleNames = Lists.newArrayList(toRoleNames(roleEntities));
List<Long> roleIds = Lists.newArrayList(toRoleIds(roleEntities));

for (RoleEntity roleEntityToRevoke : roleEntitiesToRevoke) {
if (roleEntitiesMap.containsKey(roleEntityToRevoke.id())) {
roleEntitiesMap.remove(roleEntityToRevoke.id());
} else {
roleNames.remove(roleEntityToRevoke.name());
boolean removed = roleIds.remove(roleEntityToRevoke.id());
if (!removed) {
LOG.warn(
"Failed to revoke, role {} doesn't exist in the user {} of metalake {}",
roleEntityToRevoke.name(),
Expand All @@ -328,7 +341,8 @@ User revokeRolesFromUser(String metalake, List<String> roles, String user) {
.withId(userEntity.id())
.withNamespace(userEntity.namespace())
.withName(userEntity.name())
.withRoles(Lists.newArrayList(roleEntitiesMap.values()))
.withRoleNames(roleNames)
.withRoleIds(roleIds)
.withAuditInfo(auditInfo)
.build();
});
Expand Down Expand Up @@ -359,6 +373,10 @@ User revokeRolesFromUser(String metalake, List<String> roles, String user) {
}
}

private List<String> toRoleNames(List<RoleEntity> roleEntities) {
return roleEntities.stream().map(RoleEntity::name).collect(Collectors.toList());
}

private List<Long> toRoleIds(List<RoleEntity> roleEntities) {
return roleEntities.stream().map(RoleEntity::id).collect(Collectors.toList());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ User addUser(String metalake, String name) throws UserAlreadyExistsException {
.withId(idGenerator.nextId())
.withName(name)
.withNamespace(AuthorizationUtils.ofUserNamespace(metalake))
.withRoles(Lists.newArrayList())
.withRoleNames(Lists.newArrayList())
.withAuditInfo(
AuditInfo.builder()
.withCreator(PrincipalUtils.getCurrentPrincipal().getName())
Expand Down Expand Up @@ -117,7 +117,7 @@ Group addGroup(String metalake, String group) throws GroupAlreadyExistsException
.withId(idGenerator.nextId())
.withName(group)
.withNamespace(AuthorizationUtils.ofGroupNamespace(metalake))
.withRoles(Collections.emptyList())
.withRoleNames(Collections.emptyList())
.withAuditInfo(
AuditInfo.builder()
.withCreator(PrincipalUtils.getCurrentPrincipal().getName())
Expand Down
Loading

0 comments on commit bcd6f80

Please sign in to comment.