use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class PerunNotifEmailGroupSender method send.
@Override
public Set<Integer> send(List<PerunNotifMessageDto> dtosToSend) {
Set<Integer> usedPoolIds = new HashSet<Integer>();
List<PerunNotifEmailMessageToSendDto> messagesToSend = new ArrayList<PerunNotifEmailMessageToSendDto>();
for (PerunNotifMessageDto messageDto : dtosToSend) {
PoolMessage dto = messageDto.getPoolMessage();
PerunNotifTemplate template = messageDto.getTemplate();
PerunNotifReceiver receiver = messageDto.getReceiver();
try {
String groupSender = dto.getKeyAttributes().get(template.getSender());
if (groupSender == null || groupSender.isEmpty()) {
groupSender = template.getSender();
}
logger.debug("Calculated sender : {}", groupSender);
Integer groupId = Integer.valueOf(receiver.getTarget());
Group group = perun.getGroupsManagerBl().getGroupById(session, groupId);
List<Member> groupMembers = perun.getGroupsManagerBl().getGroupMembers(session, group);
if (groupMembers != null) {
for (Member member : groupMembers) {
try {
PerunNotifEmailMessageToSendDto memberEmailDto = new PerunNotifEmailMessageToSendDto();
memberEmailDto.setMessage(messageDto.getMessageToSend());
memberEmailDto.setSubject(messageDto.getSubject());
memberEmailDto.setReceiver((String) perun.getAttributesManagerBl().getAttribute(session, perun.getUsersManager().getUserByMember(session, member), "urn:perun:user:attribute-def:def:preferredMail").getValue());
memberEmailDto.setSender(groupSender);
messagesToSend.add(memberEmailDto);
} catch (Exception ex) {
logger.error("PreferredEmail cannot be retrieved, userId: {}", member.getUserId(), ex);
}
}
}
usedPoolIds.addAll(messageDto.getUsedPoolIds());
} catch (NumberFormatException ex) {
logger.error("GroupId cannot be parsed: {}", receiver.getTarget());
} catch (GroupNotExistsException ex) {
logger.error("Group with id: {} does not exists.", receiver.getTarget());
} catch (InternalErrorException ex) {
logger.error("Error during processing messageDto.", ex);
}
}
perunNotifEmailManager.sendMessages(messagesToSend);
return usedPoolIds;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class VOOT method getGroupByName.
/**
* Return group by name, which is consist of short name of VO, short name of parents group and short name of current group, e.g. 'vo1:group1:group2'.
*
* @param name name of group, e.g. 'vo1:group1:group2'
* @return group
* @throws VOOTException if can not read group
*/
private Group getGroupByName(String name) throws VOOTException {
String voName = name.split(":")[0];
Vo vo = null;
try {
vo = perun.getVosManagerBl().getVoByShortName(session, voName);
} catch (InternalErrorException ex) {
throw new VOOTException("internal_server_error");
} catch (VoNotExistsException ex) {
throw new VOOTException("internal_server_error", "vo not exists");
}
Group group = null;
try {
group = perun.getGroupsManagerBl().getGroupByName(session, vo, name.substring(name.indexOf(":") + 1, name.length()));
} catch (GroupNotExistsException ex) {
throw new VOOTException("internal_server_error", "group not exists");
} catch (InternalErrorException ex) {
throw new VOOTException("internal_server_error");
}
return group;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class MembersManagerBlImpl method createMember.
//MAIN METHOD
public Member createMember(PerunSession sess, Vo vo, SpecificUserType specificUserType, Candidate candidate, List<Group> groups, List<String> overwriteUserAttributes) throws InternalErrorException, WrongAttributeValueException, WrongReferenceAttributeValueException, AlreadyMemberException, ExtendMembershipException, GroupOperationsException {
log.debug("Creating member for VO {} from candidate {}", vo, candidate);
// Get the user
User user = null;
if (candidate.getUserExtSources() != null) {
for (UserExtSource ues : candidate.getUserExtSources()) {
// Check if the extSource exists
ExtSource tmpExtSource = getPerunBl().getExtSourcesManagerBl().checkOrCreateExtSource(sess, ues.getExtSource().getName(), ues.getExtSource().getType());
// Set the extSource ID
ues.getExtSource().setId(tmpExtSource.getId());
try {
// Try to find the user by userExtSource
user = getPerunBl().getUsersManagerBl().getUserByExtSourceNameAndExtLogin(sess, ues.getExtSource().getName(), ues.getLogin());
} catch (UserExtSourceNotExistsException e) {
// This is OK, non-existent userExtSource will be assigned later
} catch (UserNotExistsException e) {
// Ignore, we are only checking if the user exists
} catch (ExtSourceNotExistsException e) {
// Ignore, we are only checking if the user exists
}
}
}
// If user hasn't been found, then create him
if (user == null) {
user = new User();
user.setFirstName(candidate.getFirstName());
user.setLastName(candidate.getLastName());
user.setMiddleName(candidate.getMiddleName());
user.setTitleAfter(candidate.getTitleAfter());
user.setTitleBefore(candidate.getTitleBefore());
if (specificUserType.equals(specificUserType.SERVICE))
user.setServiceUser(true);
if (specificUserType.equals(specificUserType.SPONSORED))
user.setSponsoredUser(true);
// Store the user, this must be done in separate transaction
user = getPerunBl().getUsersManagerBl().createUser(sess, user);
log.debug("createMember: new user: {}", user);
}
// Assign missing userExtSource and update LoA
if (candidate.getUserExtSources() != null) {
for (UserExtSource userExtSource : candidate.getUserExtSources()) {
try {
UserExtSource currentUserExtSource = getPerunBl().getUsersManagerBl().getUserExtSourceByExtLogin(sess, userExtSource.getExtSource(), userExtSource.getLogin());
// Update LoA
currentUserExtSource.setLoa(userExtSource.getLoa());
getPerunBl().getUsersManagerBl().updateUserExtSource(sess, currentUserExtSource);
} catch (UserExtSourceNotExistsException e) {
// Create userExtSource
try {
getPerunBl().getUsersManagerBl().addUserExtSource(sess, user, userExtSource);
} catch (UserExtSourceExistsException e1) {
throw new ConsistencyErrorException("Adding userExtSource which already exists: " + userExtSource);
}
}
}
}
try {
Member member = getMemberByUser(sess, vo, user);
throw new AlreadyMemberException(member);
} catch (MemberNotExistsException IGNORE) {
}
// Create the member
Member member = getMembersManagerImpl().createMember(sess, vo, user);
getPerunBl().getAuditer().log(sess, "{} created.", member);
// Create the member's attributes
List<Attribute> membersAttributes = new ArrayList<Attribute>();
List<Attribute> usersAttributesToMerge = new ArrayList<>();
List<Attribute> usersAttributesToModify = new ArrayList<>();
if (candidate.getAttributes() != null) {
for (String attributeName : candidate.getAttributes().keySet()) {
AttributeDefinition attributeDefinition;
try {
attributeDefinition = getPerunBl().getAttributesManagerBl().getAttributeDefinition(sess, attributeName);
} catch (AttributeNotExistsException ex) {
throw new InternalErrorException(ex);
}
Attribute attribute = new Attribute(attributeDefinition);
attribute.setValue(getPerunBl().getAttributesManagerBl().stringToAttributeValue(candidate.getAttributes().get(attributeName), attribute.getType()));
if (getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_MEMBER_ATTR_DEF) || getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_MEMBER_ATTR_OPT)) {
// This is member's attribute
membersAttributes.add(attribute);
} else if (getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_USER_ATTR_DEF) || getPerunBl().getAttributesManagerBl().isFromNamespace(sess, attribute, AttributesManager.NS_USER_ATTR_OPT)) {
if (overwriteUserAttributes != null && !overwriteUserAttributes.isEmpty() && overwriteUserAttributes.contains(attribute.getName())) {
usersAttributesToModify.add(attribute);
} else {
usersAttributesToMerge.add(attribute);
}
}
}
}
// Store the attributes
try {
//if empty, skip setting or merging empty arrays of attributes at all
if (!membersAttributes.isEmpty())
getPerunBl().getAttributesManagerBl().setAttributes(sess, member, membersAttributes);
if (!usersAttributesToMerge.isEmpty())
getPerunBl().getAttributesManagerBl().mergeAttributesValues(sess, user, usersAttributesToMerge);
if (!usersAttributesToModify.isEmpty())
getPerunBl().getAttributesManagerBl().setAttributes(sess, user, usersAttributesToModify);
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
// Set the initial membershipExpiration
// Get user LOA
String memberLoa = null;
try {
Attribute loa = getPerunBl().getAttributesManagerBl().getAttribute(sess, member, AttributesManager.NS_MEMBER_ATTR_VIRT + ":loa");
memberLoa = (String) loa.getValue();
} catch (AttributeNotExistsException e) {
// user has no loa defined - if required by VO, it will be stopped in checking method later
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
// check if user can be member
this.canBeMemberInternal(sess, vo, user, memberLoa, true);
// set initial membership expiration
this.extendMembership(sess, member);
insertToMemberGroup(sess, member, vo);
// add member also to all groups in list
if (groups != null && !groups.isEmpty()) {
for (Group group : groups) {
try {
perunBl.getGroupsManagerBl().addMember(sess, group, member);
} catch (NotMemberOfParentGroupException ex) {
throw new InternalErrorException("Member " + member + " can't be add to the group " + group + " because he is not member of it's parent group.", ex);
} catch (GroupNotExistsException e) {
throw new ConsistencyErrorException(e);
}
}
}
return member;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class MembersManagerBlImpl method createMember.
public Member createMember(PerunSession sess, Vo vo, User user, List<Group> groups) throws InternalErrorException, AlreadyMemberException, ExtendMembershipException, WrongAttributeValueException, WrongReferenceAttributeValueException, GroupOperationsException {
try {
Member member = getMemberByUser(sess, vo, user);
throw new AlreadyMemberException(member);
} catch (MemberNotExistsException IGNORE) {
}
Member member = getMembersManagerImpl().createMember(sess, vo, user);
getPerunBl().getAuditer().log(sess, "{} created.", member);
// Set the initial membershipExpiration
// Get user LOA
String memberLoa = null;
try {
Attribute loa = getPerunBl().getAttributesManagerBl().getAttribute(sess, member, AttributesManager.NS_MEMBER_ATTR_VIRT + ":loa");
memberLoa = (String) loa.getValue();
} catch (AttributeNotExistsException e) {
// user has no loa defined - if required by VO, it will be stopped in checking method later
} catch (WrongAttributeAssignmentException e) {
throw new InternalErrorException(e);
}
// check if user can be member - service members are not checked for LoA
this.canBeMemberInternal(sess, vo, user, memberLoa, true);
// set initial membership expiration
this.extendMembership(sess, member);
insertToMemberGroup(sess, member, vo);
// add member also to all groups in list
if (groups != null && !groups.isEmpty()) {
for (Group group : groups) {
try {
perunBl.getGroupsManagerBl().addMember(sess, group, member);
} catch (NotMemberOfParentGroupException ex) {
throw new InternalErrorException("Member " + member + " can't be add to the group " + group + " because he is not member of it's parent group.", ex);
} catch (GroupNotExistsException e) {
throw new ConsistencyErrorException(e);
}
}
}
return member;
}
use of cz.metacentrum.perun.core.api.exceptions.GroupNotExistsException in project perun by CESNET.
the class MembersManagerBlImpl method deleteMember.
public void deleteMember(PerunSession sess, Member member) throws InternalErrorException, MemberAlreadyRemovedException, GroupOperationsException {
Vo vo = this.getMemberVo(sess, member);
User user;
try {
user = getPerunBl().getUsersManagerBl().getUserById(sess, member.getUserId());
} catch (UserNotExistsException e1) {
throw new ConsistencyErrorException("Removing member who doesn't have corresponding user.", e1);
}
List<Facility> allowedFacilities = getPerunBl().getFacilitiesManagerBl().getAllowedFacilities(sess, user);
Map<Facility, List<Attribute>> requiredAttributesBeforeMemberRemove = new HashMap<Facility, List<Attribute>>();
for (Facility facility : allowedFacilities) {
// Get actually required attributes, they will be later compared with list of required attributes when the member will be removed from all resources in this VO
requiredAttributesBeforeMemberRemove.put(facility, getPerunBl().getAttributesManagerBl().getRequiredAttributes(sess, facility, user));
}
// Remove member from all groups
List<Group> memberGroups = getPerunBl().getGroupsManagerBl().getMemberDirectGroups(sess, member);
for (Group group : memberGroups) {
// Member must be removed from the members group using separate method
if (group.getName().equals(VosManager.MEMBERS_GROUP))
continue;
try {
getPerunBl().getGroupsManagerBl().removeMember(sess, group, member);
} catch (NotGroupMemberException e) {
throw new ConsistencyErrorException("getMemberGroups return group where the member is not member", e);
} catch (GroupNotExistsException e) {
throw new ConsistencyErrorException(e);
}
}
// Remove member from the VO members group
try {
Group g = getPerunBl().getGroupsManagerBl().getGroupByName(sess, vo, VosManager.MEMBERS_GROUP);
try {
getPerunBl().getGroupsManagerBl().removeMemberFromMembersOrAdministratorsGroup(sess, g, member);
} catch (NotGroupMemberException e) {
throw new ConsistencyErrorException("Member is not in the \"members\" group." + member + " " + g, e);
}
} catch (GroupNotExistsException e) {
throw new InternalErrorException(e);
}
// Remove member's attributes (namespaces: member and resource-member)
try {
getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, member);
List<Resource> resources = getPerunBl().getResourcesManagerBl().getResources(sess, vo);
for (Resource resource : resources) {
getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, resource, member);
}
} catch (AttributeValueException ex) {
throw new ConsistencyErrorException("Member is removed from all groups. There are no required attribute for this member. Member's attributes can be removed without problem.", ex);
} catch (WrongAttributeAssignmentException ex) {
throw new InternalErrorException(ex);
}
// Remove user-facility attributes which are no longer required
for (Facility facility : allowedFacilities) {
List<Attribute> requiredAttributes = requiredAttributesBeforeMemberRemove.get(facility);
//remove currently required attributes from requiredAttributesBeforeMemberRemove
requiredAttributes.removeAll(getPerunBl().getAttributesManagerBl().getRequiredAttributes(sess, facility, user));
//remove attributes which are no longer required
try {
getPerunBl().getAttributesManagerBl().removeAttributes(sess, facility, user, requiredAttributes);
} catch (AttributeValueException | WrongAttributeAssignmentException ex) {
throw new ConsistencyErrorException(ex);
}
}
//Remove all members bans
List<BanOnResource> bansOnResource = getPerunBl().getResourcesManagerBl().getBansForMember(sess, member.getId());
for (BanOnResource banOnResource : bansOnResource) {
try {
getPerunBl().getResourcesManagerBl().removeBan(sess, banOnResource.getId());
} catch (BanNotExistsException ex) {
//it is ok, we just want to remove it anyway
}
}
/* TODO this can be used for future optimization. If the user is not asigned to the facility anymore all user-facility attributes (for this facility) can be safely removed.
for (Facility facility: facilitiesBeforeMemberRemove) {
// Remove user-facility attributes
try {
getPerunBl().getAttributesManagerBl().removeAllAttributes(sess, facility, user);
log.debug("Removing user-facility attributes for facility {}", facility);
} catch (AttributeValueException e) {
throw new ConsistencyErrorException("Member is removed from all resources. There are no required attribute for this member. User-facility attributes can be removed without problem.", e);
}
}
*/
// Remove member from the DB
getMembersManagerImpl().deleteMember(sess, member);
getPerunBl().getAuditer().log(sess, "{} deleted.", member);
}
Aggregations