Skip to content

Commit

Permalink
[#2852] feat(core): Add relational backend for Role Entity (#3065)
Browse files Browse the repository at this point in the history
### What changes were proposed in this pull request?

Add relational backend for Role Entity

[note]: wait for #3031  merged first

### Why are the changes needed?

Fix: #2852 

### Does this PR introduce _any_ user-facing change?

N/A

### How was this patch tested?

ut

---------

Co-authored-by: yangliwei <[email protected]>
Co-authored-by: Qi Yu <[email protected]>
  • Loading branch information
3 people authored and web-flow committed Apr 23, 2024
1 parent 046bf5d commit 7e887bd
Show file tree
Hide file tree
Showing 17 changed files with 813 additions and 83 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,8 @@ public <E extends Entity & HasIdentifier> E get(
return (E) UserMetaService.getInstance().getUserByIdentifier(ident);
case GROUP:
return (E) GroupMetaService.getInstance().getGroupByIdentifier(ident);
case ROLE:
return (E) RoleMetaService.getInstance().getRoleByIdentifier(ident);
default:
throw new UnsupportedEntityTypeException(
"Unsupported entity type: %s for get operation", entityType);
Expand All @@ -191,6 +193,8 @@ public boolean delete(NameIdentifier ident, Entity.EntityType entityType, boolea
return UserMetaService.getInstance().deleteUser(ident);
case GROUP:
return GroupMetaService.getInstance().deleteGroup(ident);
case ROLE:
return RoleMetaService.getInstance().deleteRole(ident);
default:
throw new UnsupportedEntityTypeException(
"Unsupported entity type: %s for delete operation", entityType);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
package com.datastrato.gravitino.storage.relational.mapper;

import com.datastrato.gravitino.storage.relational.po.GroupPO;
import java.util.List;
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
Expand All @@ -20,11 +21,12 @@
* href="https://mybatis.org/mybatis-3/getting-started.html"></a>
*/
public interface GroupMetaMapper {
String TABLE_NAME = "group_meta";
String GROUP_TABLE_NAME = "group_meta";
String GROUP_ROLE_RELATION_TABLE_NAME = "group_role_rel";

@Select(
"SELECT group_id as groupId FROM "
+ TABLE_NAME
+ GROUP_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}"
+ " AND deleted_at = 0")
Long selectGroupIdBySchemaIdAndName(
Expand All @@ -37,15 +39,15 @@ Long selectGroupIdBySchemaIdAndName(
+ " current_version as currentVersion, last_version as lastVersion,"
+ " deleted_at as deletedAt"
+ " FROM "
+ TABLE_NAME
+ GROUP_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND group_name = #{groupName}"
+ " AND deleted_at = 0")
GroupPO selectGroupMetaByMetalakeIdAndName(
@Param("metalakeId") Long metalakeId, @Param("groupName") String name);

@Insert(
"INSERT INTO "
+ TABLE_NAME
+ GROUP_TABLE_NAME
+ "(group_id, group_name,"
+ " metalake_id, audit_info,"
+ " current_version, last_version, deleted_at)"
Expand All @@ -62,7 +64,7 @@ GroupPO selectGroupMetaByMetalakeIdAndName(

@Insert(
"INSERT INTO "
+ TABLE_NAME
+ GROUP_TABLE_NAME
+ "(group_id, group_name,"
+ "metalake_id, audit_info,"
+ " current_version, last_version, deleted_at)"
Expand All @@ -86,21 +88,21 @@ GroupPO selectGroupMetaByMetalakeIdAndName(

@Update(
"UPDATE "
+ TABLE_NAME
+ GROUP_TABLE_NAME
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"
+ " WHERE group_id = #{groupId} AND deleted_at = 0")
void softDeleteGroupMetaByGroupId(@Param("groupId") Long groupId);

@Update(
"UPDATE "
+ TABLE_NAME
+ GROUP_TABLE_NAME
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0")
void softDeleteGroupMetasByMetalakeId(@Param("metalakeId") Long metalakeId);

@Update(
"UPDATE "
+ TABLE_NAME
+ GROUP_TABLE_NAME
+ " SET group_name = #{newGroupMeta.groupName},"
+ " metalake_id = #{newGroupMeta.metalakeId},"
+ " audit_info = #{newGroupMeta.auditInfo},"
Expand All @@ -116,4 +118,18 @@ GroupPO selectGroupMetaByMetalakeIdAndName(
+ " AND deleted_at = 0")
Integer updateGroupMeta(
@Param("newGroupMeta") GroupPO newGroupPO, @Param("oldGroupMeta") GroupPO oldGroupPO);

@Select(
"SELECT gr.group_id as groupId, gr.group_name as groupName,"
+ " gr.metalake_id as metalakeId,"
+ " gr.audit_info as auditInfo, gr.current_version as currentVersion,"
+ " gr.last_version as lastVersion, gr.deleted_at as deletedAt"
+ " FROM "
+ GROUP_TABLE_NAME
+ " gr JOIN "
+ GROUP_ROLE_RELATION_TABLE_NAME
+ " re ON gr.group_id = re.group_id"
+ " WHERE re.role_id = #{roleId}"
+ " AND gr.deleted_at = 0 AND re.deleted_at = 0")
List<GroupPO> listGroupsByRoleId(@Param("roleId") Long roleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,13 @@
* href="https://mybatis.org/mybatis-3/getting-started.html"></a>
*/
public interface GroupRoleRelMapper {
String RELATION_TABLE_NAME = "group_role_rel";
String GROUP_TABLE_NAME = "group_meta";
String GROUP_ROLE_RELATION_TABLE_NAME = "group_role_rel";

@Insert({
"<script>",
"INSERT INTO "
+ RELATION_TABLE_NAME
+ GROUP_ROLE_RELATION_TABLE_NAME
+ "(group_id, role_id,"
+ " audit_info,"
+ " current_version, last_version, deleted_at)"
Expand All @@ -46,7 +46,7 @@ public interface GroupRoleRelMapper {
@Insert({
"<script>",
"INSERT INTO "
+ RELATION_TABLE_NAME
+ GROUP_ROLE_RELATION_TABLE_NAME
+ "(group_id, role_id,"
+ " audit_info,"
+ " current_version, last_version, deleted_at)"
Expand All @@ -73,15 +73,15 @@ void batchInsertGroupRoleRelOnDuplicateKeyUpdate(

@Update(
"UPDATE "
+ RELATION_TABLE_NAME
+ GROUP_ROLE_RELATION_TABLE_NAME
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"
+ " WHERE group_id = #{groupId} AND deleted_at = 0")
void softDeleteGroupRoleRelByGroupId(@Param("groupId") Long groupId);

@Update({
"<script>",
"UPDATE "
+ RELATION_TABLE_NAME
+ GROUP_ROLE_RELATION_TABLE_NAME
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"
+ " WHERE group_id = #{groupId} AND role_id in (",
"<foreach collection='roleIds' item='roleId' separator=','>",
Expand All @@ -95,11 +95,18 @@ void softDeleteGroupRoleRelByGroupAndRoles(

@Update(
"UPDATE "
+ RELATION_TABLE_NAME
+ GROUP_ROLE_RELATION_TABLE_NAME
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"
+ " WHERE group_id IN (SELECT group_id FROM "
+ GROUP_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0)"
+ " AND deleted_at = 0")
void softDeleteGroupRoleRelByMetalakeId(Long metalakeId);

@Update(
"UPDATE "
+ GROUP_ROLE_RELATION_TABLE_NAME
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"
+ " WHERE role_id = #{roleId} AND deleted_at = 0")
void softDeleteGroupRoleRelByRoleId(Long roleId);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import org.apache.ibatis.annotations.Insert;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

/**
* A MyBatis Mapper for table meta operation SQLs.
Expand All @@ -21,8 +22,23 @@
*/
public interface RoleMetaMapper {
String ROLE_TABLE_NAME = "role_meta";
String USER_RELATION_TABLE_NAME = "user_role_rel";
String GROUP_RELATION_TABLE_NAME = "group_role_rel";
String USER_ROLE_RELATION_TABLE_NAME = "user_role_rel";
String GROUP_ROLE_RELATION_TABLE_NAME = "group_role_rel";

@Select(
"SELECT role_id as roleId, role_name as roleName,"
+ " metalake_id as metalakeId, properties as properties,"
+ " securable_object_full_name as securableObjectFullName,"
+ " securable_object_type as securableObjectType,"
+ " privileges as privileges,"
+ " audit_info as auditInfo, current_version as currentVersion,"
+ " last_version as lastVersion, deleted_at as deletedAt"
+ " FROM "
+ ROLE_TABLE_NAME
+ " WHERE metalake_id = #{metalakeId} AND role_name = #{roleName}"
+ " AND deleted_at = 0")
RolePO selectRoleMetaByMetalakeIdAndName(
@Param("metalakeId") Long metalakeId, @Param("roleName") String roleName);

@Select(
"SELECT role_id as roleId FROM "
Expand All @@ -35,13 +51,15 @@ Long selectRoleIdByMetalakeIdAndName(
@Select(
"SELECT ro.role_id as roleId, ro.role_name as roleName,"
+ " ro.metalake_id as metalakeId, ro.properties as properties,"
+ " ro.securable_object as securableObject, ro.privileges as privileges,"
+ " securable_object_full_name as securableObjectFullName,"
+ " securable_object_type as securableObjectType,"
+ " ro.privileges as privileges,"
+ " ro.audit_info as auditInfo, ro.current_version as currentVersion,"
+ " ro.last_version as lastVersion, ro.deleted_at as deletedAt"
+ " FROM "
+ ROLE_TABLE_NAME
+ " ro JOIN "
+ USER_RELATION_TABLE_NAME
+ USER_ROLE_RELATION_TABLE_NAME
+ " re ON ro.role_id = re.role_id"
+ " WHERE re.user_id = #{userId}"
+ " AND ro.deleted_at = 0 AND re.deleted_at = 0")
Expand All @@ -50,13 +68,15 @@ Long selectRoleIdByMetalakeIdAndName(
@Select(
"SELECT ro.role_id as roleId, ro.role_name as roleName,"
+ " ro.metalake_id as metalakeId, ro.properties as properties,"
+ " ro.securable_object as securableObject, ro.privileges as privileges,"
+ " ro.securable_object_full_name as securableObjectFullName,"
+ " ro.securable_object_type as securableObjectType,"
+ " ro.privileges as privileges,"
+ " ro.audit_info as auditInfo, ro.current_version as currentVersion,"
+ " ro.last_version as lastVersion, ro.deleted_at as deletedAt"
+ " FROM "
+ ROLE_TABLE_NAME
+ " ro JOIN "
+ GROUP_RELATION_TABLE_NAME
+ GROUP_ROLE_RELATION_TABLE_NAME
+ " ge ON ro.role_id = ge.role_id"
+ " WHERE ge.group_id = #{groupId}"
+ " AND ro.deleted_at = 0 AND ge.deleted_at = 0")
Expand All @@ -67,14 +87,17 @@ Long selectRoleIdByMetalakeIdAndName(
+ ROLE_TABLE_NAME
+ "(role_id, role_name,"
+ " metalake_id, properties,"
+ " securable_object, privileges,"
+ " securable_object_full_name,"
+ " securable_object_type,"
+ " privileges,"
+ " audit_info, current_version, last_version, deleted_at)"
+ " VALUES("
+ " #{roleMeta.roleId},"
+ " #{roleMeta.roleName},"
+ " #{roleMeta.metalakeId},"
+ " #{roleMeta.properties},"
+ " #{roleMeta.securableObject},"
+ " #{roleMeta.securableObjectFullName},"
+ " #{roleMeta.securableObjectType},"
+ " #{roleMeta.privileges},"
+ " #{roleMeta.auditInfo},"
+ " #{roleMeta.currentVersion},"
Expand All @@ -88,14 +111,17 @@ Long selectRoleIdByMetalakeIdAndName(
+ ROLE_TABLE_NAME
+ "(role_id, role_name,"
+ " metalake_id, properties,"
+ " securable_object, privileges,"
+ " securable_object_full_name,"
+ " securable_object_type,"
+ " privileges,"
+ " audit_info, current_version, last_version, deleted_at)"
+ " VALUES("
+ " #{roleMeta.roleId},"
+ " #{roleMeta.roleName},"
+ " #{roleMeta.metalakeId},"
+ " #{roleMeta.properties},"
+ " #{roleMeta.securableObject},"
+ " #{roleMeta.securableObjectFullName},"
+ " #{roleMeta.securableObjectType},"
+ " #{roleMeta.privileges},"
+ " #{roleMeta.auditInfo},"
+ " #{roleMeta.currentVersion},"
Expand All @@ -105,11 +131,26 @@ Long selectRoleIdByMetalakeIdAndName(
+ " role_name = #{roleMeta.roleName},"
+ " metalake_id = #{roleMeta.metalakeId},"
+ " properties = #{roleMeta.properties},"
+ " securable_object = #{roleMeta.securableObject},"
+ " securable_object_full_name = #{roleMeta.securableObjectFullName},"
+ " securable_object_type = #{roleMeta.securableObjectType},"
+ " privileges = #{roleMeta.privileges},"
+ " audit_info = #{roleMeta.auditInfo},"
+ " current_version = #{roleMeta.currentVersion},"
+ " last_version = #{roleMeta.lastVersion},"
+ " deleted_at = #{roleMeta.deletedAt}")
void insertRoleMetaOnDuplicateKeyUpdate(@Param("roleMeta") RolePO rolePO);

@Update(
"UPDATE "
+ ROLE_TABLE_NAME
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"
+ " WHERE role_id = #{roleId} AND deleted_at = 0")
void softDeleteRoleMetaByRoleId(Long roleId);

@Update(
"UPDATE "
+ ROLE_TABLE_NAME
+ " SET deleted_at = UNIX_TIMESTAMP(CURRENT_TIMESTAMP(3)) * 1000.0"
+ " WHERE metalake_id = #{metalakeId} AND deleted_at = 0")
void softDeleteRoleMetasByMetalakeId(@Param("metalakeId") Long metalakeId);
}
Loading

0 comments on commit 7e887bd

Please sign in to comment.