Search in sources :

Example 11 with Privilege

use of io.crate.user.Privilege in project crate by crate.

the class UsersPrivilegesMetadata method privilegeFromXContent.

private static void privilegeFromXContent(XContentParser parser, Set<Privilege> privileges) throws IOException {
    XContentParser.Token currentToken;
    State state = null;
    Privilege.Type type = null;
    Privilege.Clazz clazz = null;
    String ident = null;
    String grantor = null;
    while ((currentToken = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
        if (currentToken == XContentParser.Token.FIELD_NAME) {
            String currentFieldName = parser.currentName();
            currentToken = parser.nextToken();
            switch(currentFieldName) {
                case "state":
                    if (currentToken != XContentParser.Token.VALUE_NUMBER) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'state' value is not a number [{}]", currentToken);
                    }
                    state = State.values()[parser.intValue()];
                    break;
                case "type":
                    if (currentToken != XContentParser.Token.VALUE_NUMBER) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'type' value is not a number [{}]", currentToken);
                    }
                    type = Privilege.Type.values()[parser.intValue()];
                    break;
                case "class":
                    if (currentToken != XContentParser.Token.VALUE_NUMBER) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'class' value is not a number [{}]", currentToken);
                    }
                    clazz = Privilege.Clazz.values()[parser.intValue()];
                    break;
                case "ident":
                    if (currentToken != XContentParser.Token.VALUE_STRING && currentToken != XContentParser.Token.VALUE_NULL) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'ident' value is not a string or null [{}]", currentToken);
                    }
                    ident = parser.textOrNull();
                    break;
                case "grantor":
                    if (currentToken != XContentParser.Token.VALUE_STRING) {
                        throw new ElasticsearchParseException("failed to parse privilege, 'grantor' value is not a string [{}]", currentToken);
                    }
                    grantor = parser.text();
                    break;
                default:
                    throw new ElasticsearchParseException("failed to parse privilege");
            }
        } else if (currentToken == XContentParser.Token.END_ARRAY) {
            // empty privileges set
            return;
        }
    }
    privileges.add(new Privilege(state, type, clazz, ident, grantor));
}
Also used : State(io.crate.user.Privilege.State) ElasticsearchParseException(org.elasticsearch.ElasticsearchParseException) Privilege(io.crate.user.Privilege) XContentParser(org.elasticsearch.common.xcontent.XContentParser)

Example 12 with Privilege

use of io.crate.user.Privilege in project crate by crate.

the class UsersPrivilegesMetadata method swapPrivileges.

public static UsersPrivilegesMetadata swapPrivileges(UsersPrivilegesMetadata usersPrivileges, RelationName source, RelationName target) {
    HashMap<String, Set<Privilege>> privilegesByUser = new HashMap<>();
    for (Map.Entry<String, Set<Privilege>> userPrivileges : usersPrivileges.usersPrivileges.entrySet()) {
        String user = userPrivileges.getKey();
        Set<Privilege> privileges = userPrivileges.getValue();
        Set<Privilege> updatedPrivileges = new HashSet<>();
        for (Privilege privilege : privileges) {
            PrivilegeIdent ident = privilege.ident();
            if (ident.clazz() == Privilege.Clazz.TABLE) {
                if (source.fqn().equals(ident.ident())) {
                    updatedPrivileges.add(new Privilege(privilege.state(), ident.type(), ident.clazz(), target.fqn(), privilege.grantor()));
                } else if (target.fqn().equals(ident.ident())) {
                    updatedPrivileges.add(new Privilege(privilege.state(), ident.type(), ident.clazz(), source.fqn(), privilege.grantor()));
                } else {
                    updatedPrivileges.add(privilege);
                }
            } else {
                updatedPrivileges.add(privilege);
            }
        }
        privilegesByUser.put(user, updatedPrivileges);
    }
    return new UsersPrivilegesMetadata(privilegesByUser);
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) EnumSet(java.util.EnumSet) HashMap(java.util.HashMap) Privilege(io.crate.user.Privilege) PrivilegeIdent(io.crate.user.PrivilegeIdent) HashMap(java.util.HashMap) Map(java.util.Map) HashSet(java.util.HashSet)

Aggregations

Privilege (io.crate.user.Privilege)12 HashMap (java.util.HashMap)5 HashSet (java.util.HashSet)5 Set (java.util.Set)5 PrivilegeIdent (io.crate.user.PrivilegeIdent)4 EnumSet (java.util.EnumSet)4 Map (java.util.Map)4 Test (org.junit.Test)3 UserPrivileges (io.crate.user.UserPrivileges)2 DenyPrivilege (io.crate.sql.tree.DenyPrivilege)1 GrantPrivilege (io.crate.sql.tree.GrantPrivilege)1 RevokePrivilege (io.crate.sql.tree.RevokePrivilege)1 State (io.crate.user.Privilege.State)1 Nullable (javax.annotation.Nullable)1 ElasticsearchParseException (org.elasticsearch.ElasticsearchParseException)1 XContentParser (org.elasticsearch.common.xcontent.XContentParser)1