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));
}
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);
}
Aggregations