All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.icthh.xm.commons.permission.service.PermissionMappingService Maven / Gradle / Ivy

The newest version!
package com.icthh.xm.commons.permission.service;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.dataformat.yaml.YAMLFactory;
import com.icthh.xm.commons.logging.LoggingAspectConfig;
import com.icthh.xm.commons.permission.domain.Permission;
import com.icthh.xm.commons.permission.service.filter.PermissionMsNameFilter;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.TreeSet;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

@Slf4j
@Service
@RequiredArgsConstructor
public class PermissionMappingService {

    private final ObjectMapper mapper = new ObjectMapper(new YAMLFactory());

    private final PermissionMsNameFilter permissionMsNameFilter;

    @LoggingAspectConfig(inputDetails = false, resultDetails = false)
    public Map ymlToPermissions(String yml) {
        Map result = new TreeMap<>();
        try {
            var permissionMap = deserializeYml(yml);
            permissionMap.entrySet().stream()
                .filter(entry -> permissionMsNameFilter.filterPermission(entry.getKey()))
                .filter(entry -> entry.getValue() != null)
                .forEach(entry -> entry.getValue()
                    .forEach((roleKey, permissions) ->
                        permissions.forEach(permission -> {
                            permission.setMsName(entry.getKey());
                            permission.setRoleKey(roleKey);
                            result.put(roleKey + ":" + permission.getPrivilegeKey(), permission);
                        })
                    ));
        } catch (Exception e) {
            log.error("Failed to create permissions collection from YML file, error: {}", e.getMessage(), e);
        }
        return Collections.unmodifiableMap(result);
    }

    @LoggingAspectConfig(inputDetails = false, resultDetails = false)
    public List ymlToPermissionsList(String yml) {
        List result = new ArrayList<>();
        try {
            var map = deserializeYml(yml);
            map.entrySet().stream()
                .filter(entry -> permissionMsNameFilter.filterPermission(entry.getKey()))
                .filter(entry -> entry.getValue() != null)
                .forEach(entry -> entry.getValue()
                    .forEach((roleKey, permissions) ->
                        permissions.forEach(permission -> {
                            permission.setMsName(entry.getKey());
                            permission.setRoleKey(roleKey);
                            result.add(permission);
                        })
                    ));

        } catch (Exception e) {
            log.error("Failed to create permissions collection from YML file, error: {}", e.getMessage(), e);
        }
        return Collections.unmodifiableList(result);
    }

    private Map>> deserializeYml(String yml) throws java.io.IOException {
        return mapper.readValue(yml, new TypeReference>>>() {
        });
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy