public static Permission getPermission(ModelObject model, boolean allowNull, String...names) {
ModelList permissionsModel = get(model, ModelList.class, names);
if (!allowNull && !permissionsModel.isDefined()) {
throw invalidValue(null, names);
}
Permission permission = null;
for (int i=0; i<permissionsModel.size(); i++) {
ModelValue mv = permissionsModel.get(i);
String field = resolveField(names) + "[" + i + "]"; // Used for error reporting
if (mv.getValueType() != ModelValue.ModelValueType.STRING) {
throw invalidType(mv, ModelValue.ModelValueType.STRING, field);
}
String perm = mv.asValue(ModelString.class).getValue();
if (perm == null) {
throw requiredFieldWhen("permissions are defined", field);
}
if (perm.equals("Everyone")) {
if (permission != null) {
throw invalidData("Only one value is allowed when 'Everyone' is defined for %s", field);
}
permission = Permission.everyone();
} else {
if (permission != null && permission.isAccessibleToEveryone()) {
throw invalidData("Only one value is allowed when 'Everyone' is defined for %s", field);
} else if (permission != null) {
permission.addMembership(Membership.fromString(perm));
} else {
permission = new Permission(Membership.fromString(perm));
}
}
}
return permission;