How to change the key values in the Casbin Policy stored in MongoDB using Adapter?

47 Views Asked by At

I am in the initial stages of developing a RBAC model using Casbin in Springboot. This is my model.conf file

[request_definition]
r = sub, dom, obj, act

[policy_definition]
p = sub, dom, obj, act

[role_definition]
g = _, _, _

[policy_effect]
e = some(where (p.eft == allow))

[matchers]
m = g(r.sub, p.sub, r.dom) &&(p.dom == "*" || r.dom == p.dom) &&(p.obj == "*" || r.obj == p.obj) && (p.act == "*" || r.act == p.act)

I have also defined a Casbin Policy Class as follows:

package com.abc.backend.model;

import jakarta.persistence.Id;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.Getter;
import lombok.NoArgsConstructor;
import org.springframework.data.mongodb.core.mapping.Document;

@Getter
@AllArgsConstructor
@NoArgsConstructor
@Data
@Document(collection = "casbin")
public class CasbinPolicy {
    @Id
    private Long id;
    private String subject;
    private String domain;
    private String object;
    private String action;



    public CasbinPolicy(String subject, String domain, String object, String action) {
        this.subject = subject;
        this.domain = domain;
        this.object = object;
        this.action = action;
    }

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getSubject() {
        return subject;
    }

    public void setSubject(String subject) {
        this.subject = subject;
    }

    public String getDomain() {
        return domain;
    }

    public void setDomain(String domain) {
        this.domain = domain;
    }

    public String getObject() {
        return object;
    }

    public void setObject(String object) {
        this.object = object;
    }

    public String getAction() {
        return action;
    }

    public void setAction(String action) {
        this.action = action;
    }
}

When I am trying to add a new policy by executing the following function, I get the name of keys as v0, v1, v2 etc which is no

 public void addPolicy(@RequestBody PolicyRequest request) {
        String subject = request.getSubject();
        String domain = request.getDomain();
        String resource = request.getResource();
        String action = request.getAction();
        if(domain != null) {
            enforcer.addPolicy(subject, domain, resource, action);
        } else {
            enforcer.addPolicy(subject, resource, action);
        }
    }

Sample Document { "_id": { "$oid": "650e6bc8e882ea1d24bdb5a7" }, "ptype": "p", "v0": "admin", "v1": "*", "v2": "*", "v3": "*", "v4": "", "v5": "" }

Is there any way to change the key values and add Objects of classes other than String in Casbin?

0

There are 0 best solutions below