= 개인정보 취급자 권한변경이력

== 개요
역할 및 업무그룹의 권한 정보를 변경한 이력을 남긴다.

=== 역할 권한 변경 이력 로깅
* AOP를 이용하여 RoleService의 사용자 역할 권한 추가/수정/삭제 메서드가 호출될때 이력을 남긴다.
* log4j2.xml에 설정한 파일에 이력이 남는다.

.RoleHistoryLoggingAspect.class
[source,java]
----
@Aspect
@Component
@Log4j2
public class RoleHistoryLoggingAspect extends HistoryLoggingSupport{

	private static final Logger ROLE_HISTORY_LOG = LogManager.getLogger("RoleHistoryLog");

	@Value("${node-id}")
    private String nodeId;

    private final IdGenService idGenService;

	public RoleHistoryLoggingAspect(IdGenService idGenService) {
		this.idGenService = idGenService;
	}

	@Pointcut("execution(* com.samsung.role.impl.RoleServiceImpl.insertUserRoleList(..))")
	public void insertUserRolePointcut() {
		// Do nothing because pointcut
	}

	@Pointcut("execution(* com.samsung.role.impl.RoleServiceImpl.updateUserRoleList(..))")
	public void updateUserRolePointcut() {
		// Do nothing because pointcut
	}

	@Pointcut("execution(* com.samsung.role.impl.RoleServiceImpl.deleteUserRoleList(..)) || execution(* com.samsung.role.impl.RoleServiceImpl.deleteUserRole(..))")
	public void deleteUserRolePointcut() {
		// Do nothing because pointcut
	}

	@After(value = "insertUserRolePointcut() || updateUserRolePointcut() || deleteUserRolePointcut()")
	public void writeRoleHistoryLog() {
		writeHistoryLog(ROLE_HISTORY_LOG, idGenService, nodeId);
	}
}
----
.HistoryLoggingSupport.class
[source,java]
----
public class HistoryLoggingSupport {
	private static final ObjectMapper mapper = new ObjectMapper();

	@Autowired
	protected WebUtil webUtil;

	public void writeHistoryLog(Logger logger, IdGenService idGenService, String nodeId) {
		HttpServletRequest request = ((ServletRequestAttributes) RequestContextHolder.currentRequestAttributes()).getRequest();
		String requestUri = request.getRequestURI();
		String requestMethod = request.getMethod();
		User user = Account.currentUser();
		if(ObjectUtils.isNotEmpty(user)) {	// 로그인된 사용자
			try {
				HistoryLog log = new HistoryLog();
				log.setLogId(idGenService.getNextStringId());
				log.setNodeId(nodeId);
				if(ObjectUtils.isNotEmpty(user)) {
					log.setWorkerId(user.getUserId());
					log.setWorkerName(user.getUserName());
				}
				log.setWorkDatetime(DateTime.now().toString());
				log.setRemoteAddr(webUtil.getClientIp(request));
				log.setRequestMethod(requestMethod);
				log.setRequestUri(requestUri);

				String jsonVal = mapper.writeValueAsString(log);
				logger.info(jsonVal);
			} catch (JsonProcessingException e) {
				logger.warn(e.getMessage());
			}
		}
	}
}
----

.log4j2.xml
[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <RollingFile name="RoleHistoryAppender" fileName="/logs/history/role-history-${date:yyyy-MM-dd}-${hostName}.log"
                     filePattern="/logs/history/role-history-%d{yyyy-MM-dd}-${hostName}.log">
            <PatternLayout>
                <Pattern>%d %-5p [%t] %-17c{2} \(%13F:%L\) - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="RoleHistoryLog" level="INFO" additivity="false">
            <AppenderRef ref="RoleHistoryAppender"/>
        </Logger>
    </Loggers>
</Configuration>
----

=== 업무그룹 권한 변경 이력 로깅
* AOP를 이용하여 WorkGroupService서비스의 업무그룹 권한 추가/수정/삭제 메서드가 호출될때 이력을 남긴다.
* log4j2.xml에 설정한 파일에 이력이 남는다.

.WorkgroupHistoryLoggingAspect.class
[source,java]
----
@Aspect
@Component
@Log4j2
public class WorkgroupHistoryLoggingAspect extends HistoryLoggingSupport {

	private static final Logger WORKGROUP_HISTORY_LOG = LogManager.getLogger("WorkgroupHistoryLog");

	@Value("${node-id}")
    private String nodeId;

    private final IdGenService idGenService;

	public WorkgroupHistoryLoggingAspect(IdGenService idGenService) {
		this.idGenService = idGenService;
	}

	@Pointcut("execution(* com.samsung.workgroup.impl.WorkgroupServiceImpl.insertWorkgroupRoleList(..))")
	public void insertWorkgroupRoleList() {
		// Do nothing because pointcut
	}

	@Pointcut("execution(* com.samsung.workgroup.impl.WorkgroupServiceImpl.insertWorkgroupMenuList(..))")
	public void insertWorkgroupMenuList() {
		// Do nothing because pointcut
	}

	@Pointcut("execution(* com.samsung.workgroup.impl.WorkgroupServiceImpl.updateWorkgroupRoleList(..))")
	public void updateWorkgroupRoleList() {
		// Do nothing because pointcut
	}

	@Pointcut("execution(* com.samsung.workgroup.impl.WorkgroupServiceImpl.updateWorkgroupMenuList(..))")
	public void updateWorkgroupMenuList() {
		// Do nothing because pointcut
	}

	@Pointcut("execution(* com.samsung.workgroup.impl.WorkgroupServiceImpl.deleteWorkgroupRoleList(..))")
	public void deleteWorkgroupRoleList() {
		// Do nothing because pointcut
	}

	@Pointcut("execution(* com.samsung.workgroup.impl.WorkgroupServiceImpl.deleteWorkgroupMenuList(..))")
	public void deleteWorkgroupMenuList() {
		// Do nothing because pointcut
	}

	@After(value = "insertWorkgroupRoleList() || insertWorkgroupMenuList() || updateWorkgroupRoleList() || updateWorkgroupMenuList() || deleteWorkgroupRoleList() || deleteWorkgroupMenuList()")
	public void writeWorkgroupHistoryLog() {
		writeHistoryLog(WORKGROUP_HISTORY_LOG, idGenService, nodeId);
	}
}
----

.log4j2.xml
[source,xml]
----
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="INFO">
    <Appenders>
        <RollingFile name="WorkgroupHistoryAppender"
                     fileName="/logs/history/workgroup-history-${date:yyyy-MM-dd}-${hostName}.log"
                     filePattern="/logs/history/workgroup-history-%d{yyyy-MM-dd}-${hostName}.log">
            <PatternLayout>
                <Pattern>%d %-5p [%t] %-17c{2} \(%13F:%L\) - %m%n</Pattern>
            </PatternLayout>
            <Policies>
                <TimeBasedTriggeringPolicy interval="1" modulate="true"/>
            </Policies>
        </RollingFile>
    </Appenders>
    <Loggers>
        <Logger name="WorkgroupHistoryLog" level="INFO" additivity="false">
            <AppenderRef ref="WorkgroupHistoryAppender"/>
        </Logger>
    </Loggers>
</Configuration>
----