fix #477: add group based role grants
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
package com.cloudhandson.vpdbackoffice.domain.group;
|
||||
|
||||
public record AppGroup(
|
||||
long groupId,
|
||||
String groupCode,
|
||||
String groupName,
|
||||
String description,
|
||||
String activeYn
|
||||
) {
|
||||
|
||||
public boolean active() {
|
||||
return "Y".equalsIgnoreCase(activeYn);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
package com.cloudhandson.vpdbackoffice.domain.group;
|
||||
|
||||
public record GroupCreateCommand(
|
||||
String groupCode,
|
||||
String groupName,
|
||||
String description
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.cloudhandson.vpdbackoffice.domain.group;
|
||||
|
||||
public record GroupRoleView(
|
||||
long groupId,
|
||||
String groupCode,
|
||||
String groupName,
|
||||
long roleId,
|
||||
String roleName
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,10 @@
|
||||
package com.cloudhandson.vpdbackoffice.domain.group;
|
||||
|
||||
public record GroupUserView(
|
||||
long groupId,
|
||||
String groupCode,
|
||||
String groupName,
|
||||
long userId,
|
||||
String username
|
||||
) {
|
||||
}
|
||||
@@ -0,0 +1,33 @@
|
||||
package com.cloudhandson.vpdbackoffice.mapper;
|
||||
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.AppGroup;
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.GroupCreateCommand;
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.GroupRoleView;
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.GroupUserView;
|
||||
import java.util.List;
|
||||
import org.apache.ibatis.annotations.Mapper;
|
||||
import org.apache.ibatis.annotations.Param;
|
||||
|
||||
@Mapper
|
||||
public interface GroupMapper {
|
||||
|
||||
List<AppGroup> findAll();
|
||||
|
||||
List<GroupUserView> findGroupUsers();
|
||||
|
||||
List<GroupRoleView> findGroupRoles();
|
||||
|
||||
long nextGroupId();
|
||||
|
||||
void insertGroup(@Param("groupId") long groupId, @Param("command") GroupCreateCommand command);
|
||||
|
||||
int updateActive(@Param("groupId") long groupId, @Param("activeYn") String activeYn);
|
||||
|
||||
void insertGroupUser(@Param("groupId") long groupId, @Param("userId") long userId);
|
||||
|
||||
int deleteGroupUser(@Param("groupId") long groupId, @Param("userId") long userId);
|
||||
|
||||
void insertGroupRole(@Param("groupId") long groupId, @Param("roleId") long roleId);
|
||||
|
||||
int deleteGroupRole(@Param("groupId") long groupId, @Param("roleId") long roleId);
|
||||
}
|
||||
@@ -47,6 +47,15 @@ public class BackofficeSchemaService {
|
||||
""");
|
||||
addColumn(results, "cb_app_role", "max_sensitivity_level",
|
||||
"ALTER TABLE cb_app_role ADD (max_sensitivity_level VARCHAR2(20) DEFAULT 'PUBLIC' NOT NULL)");
|
||||
createTable(results, "cb_app_group", """
|
||||
CREATE TABLE cb_app_group (
|
||||
group_id NUMBER PRIMARY KEY,
|
||||
group_code VARCHAR2(100) NOT NULL UNIQUE,
|
||||
group_name VARCHAR2(100) NOT NULL,
|
||||
description VARCHAR2(200),
|
||||
active_yn CHAR(1) DEFAULT 'Y' CHECK (active_yn IN ('Y','N')) NOT NULL
|
||||
)
|
||||
""");
|
||||
createTable(results, "cb_user_role", """
|
||||
CREATE TABLE cb_user_role (
|
||||
user_id NUMBER NOT NULL,
|
||||
@@ -54,6 +63,20 @@ public class BackofficeSchemaService {
|
||||
CONSTRAINT cb_user_role_pk PRIMARY KEY (user_id, role_id)
|
||||
)
|
||||
""");
|
||||
createTable(results, "cb_user_group", """
|
||||
CREATE TABLE cb_user_group (
|
||||
group_id NUMBER NOT NULL,
|
||||
user_id NUMBER NOT NULL,
|
||||
CONSTRAINT cb_user_group_pk PRIMARY KEY (group_id, user_id)
|
||||
)
|
||||
""");
|
||||
createTable(results, "cb_group_role", """
|
||||
CREATE TABLE cb_group_role (
|
||||
group_id NUMBER NOT NULL,
|
||||
role_id NUMBER NOT NULL,
|
||||
CONSTRAINT cb_group_role_pk PRIMARY KEY (group_id, role_id)
|
||||
)
|
||||
""");
|
||||
createTable(results, "cb_permission", """
|
||||
CREATE TABLE cb_permission (
|
||||
perm_id NUMBER PRIMARY KEY,
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.cloudhandson.vpdbackoffice.service;
|
||||
|
||||
import com.cloudhandson.vpdbackoffice.domain.audit.AuditEvent;
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.AppGroup;
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.GroupCreateCommand;
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.GroupRoleView;
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.GroupUserView;
|
||||
import com.cloudhandson.vpdbackoffice.mapper.GroupMapper;
|
||||
import java.util.List;
|
||||
import org.springframework.stereotype.Service;
|
||||
import org.springframework.transaction.annotation.Transactional;
|
||||
|
||||
@Service
|
||||
public class GroupService {
|
||||
|
||||
private final GroupMapper groupMapper;
|
||||
private final AuditService auditService;
|
||||
|
||||
public GroupService(GroupMapper groupMapper, AuditService auditService) {
|
||||
this.groupMapper = groupMapper;
|
||||
this.auditService = auditService;
|
||||
}
|
||||
|
||||
public List<AppGroup> findAll() {
|
||||
return groupMapper.findAll();
|
||||
}
|
||||
|
||||
public List<GroupUserView> findGroupUsers() {
|
||||
return groupMapper.findGroupUsers();
|
||||
}
|
||||
|
||||
public List<GroupRoleView> findGroupRoles() {
|
||||
return groupMapper.findGroupRoles();
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void createGroup(GroupCreateCommand command) {
|
||||
long groupId = groupMapper.nextGroupId();
|
||||
groupMapper.insertGroup(groupId, command);
|
||||
auditService.record(new AuditEvent("GROUP_CREATED", null, null, "SUCCESS", null, null, command.groupCode()));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void setActive(long groupId, boolean active) {
|
||||
int updated = groupMapper.updateActive(groupId, active ? "Y" : "N");
|
||||
if (updated == 0) {
|
||||
throw new AppException("그룹을 찾을 수 없습니다.");
|
||||
}
|
||||
auditService.record(new AuditEvent("GROUP_ACTIVE_CHANGED", null, null, "SUCCESS", null, null,
|
||||
"groupId=" + groupId + ",active=" + active));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addUser(long groupId, long userId) {
|
||||
groupMapper.insertGroupUser(groupId, userId);
|
||||
auditService.record(new AuditEvent("GROUP_USER_ADDED", null, null, "SUCCESS", null, null,
|
||||
"groupId=" + groupId + ",userId=" + userId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void removeUser(long groupId, long userId) {
|
||||
int deleted = groupMapper.deleteGroupUser(groupId, userId);
|
||||
if (deleted == 0) {
|
||||
throw new AppException("삭제할 그룹 사용자 매핑을 찾을 수 없습니다.");
|
||||
}
|
||||
auditService.record(new AuditEvent("GROUP_USER_REMOVED", null, null, "SUCCESS", null, null,
|
||||
"groupId=" + groupId + ",userId=" + userId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void addRole(long groupId, long roleId) {
|
||||
groupMapper.insertGroupRole(groupId, roleId);
|
||||
auditService.record(new AuditEvent("GROUP_ROLE_ADDED", null, null, "SUCCESS", null, null,
|
||||
"groupId=" + groupId + ",roleId=" + roleId));
|
||||
}
|
||||
|
||||
@Transactional
|
||||
public void removeRole(long groupId, long roleId) {
|
||||
int deleted = groupMapper.deleteGroupRole(groupId, roleId);
|
||||
if (deleted == 0) {
|
||||
throw new AppException("삭제할 그룹 역할 매핑을 찾을 수 없습니다.");
|
||||
}
|
||||
auditService.record(new AuditEvent("GROUP_ROLE_REMOVED", null, null, "SUCCESS", null, null,
|
||||
"groupId=" + groupId + ",roleId=" + roleId));
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
package com.cloudhandson.vpdbackoffice.web;
|
||||
|
||||
import com.cloudhandson.vpdbackoffice.domain.group.GroupCreateCommand;
|
||||
import com.cloudhandson.vpdbackoffice.service.GroupService;
|
||||
import com.cloudhandson.vpdbackoffice.service.PermissionService;
|
||||
import com.cloudhandson.vpdbackoffice.service.UserService;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.RequestParam;
|
||||
import org.springframework.web.servlet.mvc.support.RedirectAttributes;
|
||||
|
||||
@Controller
|
||||
public class GroupController {
|
||||
|
||||
private final GroupService groupService;
|
||||
private final UserService userService;
|
||||
private final PermissionService permissionService;
|
||||
|
||||
public GroupController(GroupService groupService, UserService userService, PermissionService permissionService) {
|
||||
this.groupService = groupService;
|
||||
this.userService = userService;
|
||||
this.permissionService = permissionService;
|
||||
}
|
||||
|
||||
@GetMapping("/groups")
|
||||
public String groups(Model model) {
|
||||
model.addAttribute("groups", groupService.findAll());
|
||||
model.addAttribute("users", userService.findAll());
|
||||
model.addAttribute("roles", permissionService.findRoles());
|
||||
model.addAttribute("groupUsers", groupService.findGroupUsers());
|
||||
model.addAttribute("groupRoles", groupService.findGroupRoles());
|
||||
return "groups";
|
||||
}
|
||||
|
||||
@PostMapping("/groups")
|
||||
public String create(
|
||||
@RequestParam String groupCode,
|
||||
@RequestParam String groupName,
|
||||
@RequestParam(required = false) String description,
|
||||
RedirectAttributes redirectAttributes
|
||||
) {
|
||||
groupService.createGroup(new GroupCreateCommand(groupCode, groupName, description));
|
||||
redirectAttributes.addFlashAttribute("message", "그룹을 추가했습니다.");
|
||||
return "redirect:/groups";
|
||||
}
|
||||
|
||||
@PostMapping("/groups/active")
|
||||
public String active(
|
||||
@RequestParam long groupId,
|
||||
@RequestParam boolean active,
|
||||
RedirectAttributes redirectAttributes
|
||||
) {
|
||||
groupService.setActive(groupId, active);
|
||||
redirectAttributes.addFlashAttribute("message", "그룹 상태를 변경했습니다.");
|
||||
return "redirect:/groups";
|
||||
}
|
||||
|
||||
@PostMapping("/groups/users")
|
||||
public String addUser(
|
||||
@RequestParam long groupId,
|
||||
@RequestParam long userId,
|
||||
RedirectAttributes redirectAttributes
|
||||
) {
|
||||
groupService.addUser(groupId, userId);
|
||||
redirectAttributes.addFlashAttribute("message", "그룹에 사용자를 추가했습니다.");
|
||||
return "redirect:/groups";
|
||||
}
|
||||
|
||||
@PostMapping("/groups/users/delete")
|
||||
public String removeUser(
|
||||
@RequestParam long groupId,
|
||||
@RequestParam long userId,
|
||||
RedirectAttributes redirectAttributes
|
||||
) {
|
||||
groupService.removeUser(groupId, userId);
|
||||
redirectAttributes.addFlashAttribute("message", "그룹 사용자를 해제했습니다.");
|
||||
return "redirect:/groups";
|
||||
}
|
||||
|
||||
@PostMapping("/groups/roles")
|
||||
public String addRole(
|
||||
@RequestParam long groupId,
|
||||
@RequestParam long roleId,
|
||||
RedirectAttributes redirectAttributes
|
||||
) {
|
||||
groupService.addRole(groupId, roleId);
|
||||
redirectAttributes.addFlashAttribute("message", "그룹에 역할을 부여했습니다.");
|
||||
return "redirect:/groups";
|
||||
}
|
||||
|
||||
@PostMapping("/groups/roles/delete")
|
||||
public String removeRole(
|
||||
@RequestParam long groupId,
|
||||
@RequestParam long roleId,
|
||||
RedirectAttributes redirectAttributes
|
||||
) {
|
||||
groupService.removeRole(groupId, roleId);
|
||||
redirectAttributes.addFlashAttribute("message", "그룹 역할을 해제했습니다.");
|
||||
return "redirect:/groups";
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user