use of cz.metacentrum.perun.core.api.exceptions.GroupExistsException in project perun by CESNET.
the class GroupsManagerImpl method createGroup.
@Override
public Group createGroup(PerunSession sess, Vo vo, Group group) throws GroupExistsException {
Utils.notNull(group, "group");
Utils.notNull(group.getName(), "group.getName()");
// Check if the group already exists
if (group.getParentGroupId() == null) {
// check if the TOP level group exists
if (1 == jdbc.queryForInt("select count('x') from groups where lower(name)=lower(?) and vo_id=? and parent_group_id IS NULL", group.getName(), vo.getId())) {
throw new GroupExistsException("Group [" + group.getName() + "] already exists under VO [" + vo.getShortName() + "] and has parent Group with id is [NULL]");
}
} else {
// check if subgroup exists under parent group
if (1 == jdbc.queryForInt("select count('x') from groups where lower(name)=lower(?) and vo_id=? and parent_group_id=?", group.getName(), vo.getId(), group.getParentGroupId())) {
throw new GroupExistsException("Group [" + group.getName() + "] already exists under VO [" + vo.getShortName() + "] and has parent Group with id [" + group.getParentGroupId() + "]");
}
}
Utils.validateGroupName(group.getShortName());
int newId;
try {
// Store the group into the DB
newId = Utils.getNewId(jdbc, "groups_id_seq");
jdbc.update("insert into groups (id, parent_group_id, name, dsc, vo_id, created_by,created_at,modified_by,modified_at,created_by_uid,modified_by_uid) " + "values (?,?,?,?,?,?," + Compatibility.getSysdate() + ",?," + Compatibility.getSysdate() + ",?,?)", newId, group.getParentGroupId(), group.getName(), group.getDescription(), vo.getId(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), sess.getPerunPrincipal().getUserId());
} catch (RuntimeException err) {
throw new InternalErrorException(err);
}
Group newGroup;
try {
newGroup = getGroupById(sess, newId);
} catch (GroupNotExistsException e) {
throw new InternalErrorException("Failed to read newly created group with id: " + newId, e);
}
group.setId(newId);
group.setUuid(newGroup.getUuid());
group.setVoId(newGroup.getVoId());
return newGroup;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupExistsException in project perun by CESNET.
the class VosManagerBlImpl method createVo.
@Override
public Vo createVo(PerunSession sess, Vo vo) throws VoExistsException {
// Create entries in the DB and Grouper
vo = getVosManagerImpl().createVo(sess, vo);
getPerunBl().getAuditer().log(sess, new VoCreated(vo));
User user = sess.getPerunPrincipal().getUser();
// set creator as VO manager
if (user != null) {
try {
AuthzResolverBlImpl.setRole(sess, user, vo, Role.VOADMIN);
log.debug("User {} added like administrator to VO {}", user, vo);
} catch (AlreadyAdminException ex) {
throw new ConsistencyErrorException("Add manager to newly created VO failed because there is a particular manager already assigned", ex);
} catch (RoleCannotBeManagedException e) {
throw new InternalErrorException(e);
}
} else {
log.error("Can't set VO manager during creating of the VO. User from perunSession is null. {} {}", vo, sess);
}
try {
// Create group containing VO members
Group members = new Group(VosManager.MEMBERS_GROUP, VosManager.MEMBERS_GROUP_DESCRIPTION + " for VO " + vo.getName());
getPerunBl().getGroupsManagerBl().createGroup(sess, vo, members);
log.debug("Members group created, vo '{}'", vo);
} catch (GroupExistsException e) {
throw new ConsistencyErrorException("Group already exists", e);
}
// create empty application form
getVosManagerImpl().createApplicationForm(sess, vo);
log.info("Vo {} created", vo);
return vo;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupExistsException in project perun by CESNET.
the class GroupsManagerBlImpl method updateGroupDescription.
/**
* Update group description.
*
* Method is used by group structure synchronization.
*
* @param sess
* @param groupWithOldDescription group with description which will be chaged
* @param groupWithNewDescription candidate group with description which will replace the old one
* @return true if description changed, false otherwise
* @throws InternalErrorException
*/
private boolean updateGroupDescription(PerunSession sess, Group groupWithOldDescription, CandidateGroup groupWithNewDescription) {
// If the old description is not null, compare it with the newDescription and update it if they differ
if (groupWithOldDescription.getDescription() != null) {
if (!groupWithOldDescription.getDescription().equals(groupWithNewDescription.asGroup().getDescription())) {
groupWithOldDescription.setDescription(groupWithNewDescription.asGroup().getDescription());
try {
groupsManagerImpl.updateGroup(sess, groupWithOldDescription);
} catch (GroupExistsException ex) {
throw new InternalErrorException("Unexpected exception when trying to modify group description!");
}
getPerunBl().getAuditer().log(sess, new GroupUpdated(groupWithOldDescription));
return true;
}
// If the new description is not null set the old description to new one
} else if (groupWithNewDescription.asGroup().getDescription() != null) {
groupWithOldDescription.setDescription(groupWithNewDescription.asGroup().getDescription());
try {
groupsManagerImpl.updateGroup(sess, groupWithOldDescription);
} catch (GroupExistsException ex) {
throw new InternalErrorException("Unexpected exception when trying to modify group description!");
}
getPerunBl().getAuditer().log(sess, new GroupUpdated(groupWithOldDescription));
return true;
}
return false;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupExistsException in project perun by CESNET.
the class GroupsManagerBlImpl method addMissingGroupsWhileSynchronization.
/**
* Add missing groups under base group in Perun
*
* If some problem occurs, add candidateGroup to skippedGroups and skip it.
*
* Method is used by group structure synchronization.
*
* @param sess
* @param baseGroup under which we will be synchronizing groups
* @param candidateGroupsToAdd list of new groups (candidateGroups)
* @param loginAttributeDefinition attribute definition for login of group
* @param skippedGroups groups to be skipped because of any expected problem
*
* @throws InternalErrorException if some internal error occurs
*/
private void addMissingGroupsWhileSynchronization(PerunSession sess, Group baseGroup, List<CandidateGroup> candidateGroupsToAdd, AttributeDefinition loginAttributeDefinition, List<String> skippedGroups, List<String> mergeAttributes) {
Map<CandidateGroup, Group> groupsToUpdate = new HashMap<>();
// create all groups under base group first
for (CandidateGroup candidateGroup : candidateGroupsToAdd) {
try {
// create group
Group createdGroup = createGroup(sess, baseGroup, candidateGroup.asGroup());
groupsToUpdate.put(candidateGroup, createdGroup);
log.info("Group structure synchronization under base group {}: New Group id {} created during synchronization.", baseGroup, createdGroup.getId());
// set login for group
String login = candidateGroup.getLogin();
if (login == null)
throw new InternalErrorException("Login of candidate group " + candidateGroup + " can't be null!");
Attribute loginAttribute = new Attribute(loginAttributeDefinition);
loginAttribute.setValue(login);
getPerunBl().getAttributesManagerBl().setAttribute(sess, createdGroup, loginAttribute);
} catch (GroupExistsException e) {
log.warn("Group {} was added to group structure {} before adding process. Skip this group.", candidateGroup, baseGroup);
skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because it was added to group structure before adding process: Exception: " + e.getName() + " => " + e.getMessage() + "]");
} catch (GroupRelationNotAllowed e) {
log.warn("Can't create group from candidate group {} due to group relation not allowed exception {}.", candidateGroup, e);
skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because group relation was not allowed: Exception: " + e.getName() + " => " + e.getMessage() + "]");
} catch (GroupRelationAlreadyExists e) {
log.warn("Can't create group from candidate group {} due to group relation already exists exception {}.", candidateGroup, e);
skippedGroups.add("GroupEntry:[" + candidateGroup + "] was skipped because group relation already exists: Exception: " + e.getName() + " => " + e.getMessage() + "]");
} catch (WrongAttributeAssignmentException ex) {
// this means wrong setting of login attribute
throw new InternalErrorException(ex);
} catch (WrongAttributeValueException | WrongReferenceAttributeValueException ex) {
throw new InternalErrorException("Group login can't be set because of wrong value!", ex);
}
}
// update newly added groups cause the hierarchy could be incorrect
// no need to send list of removed parent groups here, because it is no need to resolve it for new groups at all
updateExistingGroupsWhileSynchronization(sess, baseGroup, groupsToUpdate, Collections.emptyList(), loginAttributeDefinition, skippedGroups, mergeAttributes);
}
use of cz.metacentrum.perun.core.api.exceptions.GroupExistsException in project perun by CESNET.
the class GroupsManagerImpl method updateGroup.
@Override
public Group updateGroup(PerunSession sess, Group group) throws GroupExistsException {
Utils.notNull(group.getName(), "group.getName()");
// Get the group stored in the DB
Group dbGroup;
try {
dbGroup = this.getGroupById(sess, group.getId());
} catch (GroupNotExistsException e) {
throw new InternalErrorException("Group existence was checked at the higher level", e);
}
// we allow only update on shortName part of name
if (!dbGroup.getShortName().equals(group.getShortName())) {
Utils.validateGroupName(group.getShortName());
dbGroup.setShortName(group.getShortName());
try {
jdbc.update("update groups set name=?,modified_by=?, modified_by_uid=?, modified_at=" + Compatibility.getSysdate() + " where id=?", dbGroup.getName(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), dbGroup.getId());
} catch (DataIntegrityViolationException e) {
throw new GroupExistsException("The name must be unique and it's already occupied - " + dbGroup, e);
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
}
if (group.getDescription() != null && !group.getDescription().equals(dbGroup.getDescription())) {
try {
jdbc.update("update groups set dsc=?, modified_by=?, modified_by_uid=?, modified_at=" + Compatibility.getSysdate() + " where id=?", group.getDescription(), sess.getPerunPrincipal().getActor(), sess.getPerunPrincipal().getUserId(), group.getId());
} catch (RuntimeException e) {
throw new InternalErrorException(e);
}
dbGroup.setDescription(group.getDescription());
}
return dbGroup;
}
Aggregations