* @return
*/
public Optional<OperationAllowed> getOperationAllowedToAdd(final ServiceContext context, final int mdId, final int grpId, final int opId) {
OperationAllowedRepository opAllowedRepo = _applicationContext.getBean(OperationAllowedRepository.class);
UserGroupRepository userGroupRepo = _applicationContext.getBean(UserGroupRepository.class);
final OperationAllowed operationAllowed = opAllowedRepo
.findOneById_GroupIdAndId_MetadataIdAndId_OperationId(grpId, mdId, opId);
if (operationAllowed == null) {
// Check user privileges
// Session may not be defined when a harvester is running
if (context.getUserSession() != null) {
Profile userProfile = context.getUserSession().getProfile();
if (!(userProfile == Profile.Administrator || userProfile == Profile.UserAdmin)) {
int userId = Integer.parseInt(context.getUserSession().getUserId());
// Reserved groups
if (ReservedGroup.isReserved(grpId)) {
Specification<UserGroup> hasUserIdAndProfile = where(UserGroupSpecs.hasProfile(Profile.Reviewer))
.and(UserGroupSpecs.hasUserId(userId));
List<Integer> groupIds = userGroupRepo.findGroupIds(hasUserIdAndProfile);
if (groupIds.isEmpty()) {
throw new ServiceNotAllowedEx("User can't set operation for group " + grpId + " because the user in not a "
+ "Reviewer of any group.");
}
} else {
String userGroupsOnly = settingMan.getValue("system/metadataprivs/usergrouponly");
if (userGroupsOnly.equals("true")) {
// If user is member of the group, user can set operation
if (userGroupRepo.exists(new UserGroupId().setGroupId(grpId).setUserId(userId))) {
throw new ServiceNotAllowedEx("User can't set operation for group " + grpId + " because the user in not"
+ " member of this group.");
}
}
}
}
}
}
if (operationAllowed == null) {
return Optional.of(new OperationAllowed(new OperationAllowedId().setGroupId(grpId).setMetadataId(mdId).setOperationId(opId)));
} else {
return Optional.absent();
}
}