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

ru.foodtechlab.lib.auth.service.domain.roleAccess.entity.RoleAccessEntity Maven / Gradle / Ivy

There is a newer version: 4.1.0
Show newest version
package ru.foodtechlab.lib.auth.service.domain.roleAccess.entity;

import lombok.Getter;
import lombok.NoArgsConstructor;
import lombok.Setter;
import lombok.experimental.SuperBuilder;
import ru.foodtechlab.abe.domain.entities.BaseDeleteEntity;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

@Getter
@Setter
@SuperBuilder
@NoArgsConstructor
public class RoleAccessEntity extends BaseDeleteEntity {
    private String serviceName;
    private Method method;
    private String requestPathPattern;

    public enum Method {
        GET, HEAD, POST, PUT, DELETE, CONNECT, OPTIONS, TRACE, PATCH, ANY
    }

    public boolean resourceAvailable(String serviceName, Method method, String requestPath) {
        return (this.serviceName.equals(serviceName) || this.serviceName.equals("*"))
                && (this.method.equals(Method.ANY) ||this.method.equals(method))
                && checkPath(requestPath);
    }

    public boolean checkPath(String requestPath) {
        var p = "^" + this.requestPathPattern.replaceAll("/\\*\\*", ".\\*") + "$";
        Pattern pattern = Pattern.compile(p);
        Matcher matcher = pattern.matcher(requestPath);
        return matcher.find();
    }

    public static RoleAccessEntity createGodMode() {
        var access = new RoleAccessEntity();
        access.setServiceName("*");
        access.setMethod(Method.ANY);
        access.setRequestPathPattern("/**");
        return access;
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy