roryqi commented on code in PR #11209:
URL: https://github.com/apache/gravitino/pull/11209#discussion_r3300752422
##########
plugins/idp-basic/src/main/java/org/apache/gravitino/idp/storage/service/IdpGroupMetaService.java:
##########
@@ -63,47 +75,104 @@ public List<String> listUsernamesByGroupName(String
groupName) {
@Monitored(
metricsSource = GRAVITINO_RELATIONAL_STORE_METRIC_NAME,
baseMetricName = "insertIdpGroup")
- public void insertIdpGroup(IdpGroupPO groupPO) {
- SessionUtils.doWithCommit(IdpGroupMetaMapper.class, mapper ->
mapper.insertIdpGroup(groupPO));
+ public void insertIdpGroup(IdpGroupPO groupPO) throws IOException {
+ try {
+ SessionUtils.doWithCommit(IdpGroupMetaMapper.class, mapper ->
mapper.insertIdpGroup(groupPO));
+ } catch (RuntimeException re) {
+ IdpExceptionUtils.checkSQLException(re, "group", groupPO.getGroupName());
+ throw re;
+ }
}
+ /**
+ * Deletes a built-in IdP group.
+ *
+ * @param groupName the group name
+ * @param force when false, rejects deletion if the group still has members;
when true, removes
+ * memberships and deletes the group
+ * @return true if the group was deleted
+ */
@Monitored(
metricsSource = GRAVITINO_RELATIONAL_STORE_METRIC_NAME,
baseMetricName = "deleteIdpGroup")
- public boolean deleteIdpGroup(String groupName) {
+ public boolean deleteIdpGroup(String groupName, boolean force) {
+ if (!force && !listUsernamesByGroupName(groupName).isEmpty()) {
+ throw new IllegalStateException(
+ String.format("IdP group %s is not empty, use force=true to delete
it", groupName));
+ }
+
+ int[] deletedCount = new int[] {0};
SessionUtils.doMultipleWithCommit(
- () ->
+ () -> {
+ if (force) {
SessionUtils.doWithoutCommit(
IdpUserGroupRelMapper.class,
- mapper -> mapper.softDeleteRelationsByGroupName(groupName)),
- () ->
- SessionUtils.doWithoutCommit(
- IdpGroupMetaMapper.class, mapper ->
mapper.softDeleteIdpGroup(groupName)));
- return true;
+ mapper -> mapper.softDeleteRelationsByGroupName(groupName));
+ }
+ },
+ () -> {
+ Integer deleted =
+ SessionUtils.getWithoutCommit(
+ IdpGroupMetaMapper.class, mapper ->
mapper.softDeleteIdpGroup(groupName));
+ deletedCount[0] = deleted == null ? 0 : deleted;
+ });
+ return deletedCount[0] > 0;
}
+ /**
+ * Changes built-in IdP group membership in a single transaction.
+ *
+ * @param groupName The group name.
+ * @param additions The usernames to add, or null if none.
+ * @param removals The usernames to remove, or null if none.
+ */
@Monitored(
metricsSource = GRAVITINO_RELATIONAL_STORE_METRIC_NAME,
- baseMetricName = "addUsersToGroup")
- public void addUsersToGroup(String groupName, List<String> usernames) {
- IdpGroupPO group = getIdpGroupPOByName(groupName);
- Map<String, Long> userIds =
- IdpUserMetaService.getInstance().resolveUserIdsByUsernames(usernames);
- List<IdpUserGroupRelPO> relations = new ArrayList<>(usernames.size());
- for (String username : usernames) {
- relations.add(newUserGroupRelation(group.getGroupId(),
userIds.get(username)));
+ baseMetricName = "changeGroupMembership")
+ public void changeGroupMembership(
+ String groupName, @Nullable List<String> additions, @Nullable
List<String> removals) {
+ Set<String> oldUsernames =
Sets.newHashSet(listUsernamesByGroupName(groupName));
Review Comment:
Could u put the logic into one transaction?
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]