use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class AttributesManagerEntryIntegrationTest method removeUserExtSourceAttributeWhenWrongAttrAssignment.
@Test(expected = WrongAttributeAssignmentException.class)
public void removeUserExtSourceAttributeWhenWrongAttrAssignment() throws Exception {
System.out.println(CLASS_NAME + "removeUserExtSourceAttributeWhenWrongAttrAssignment");
UserExtSource ues = setUpUserExtSourceTest();
attributes = setUpFacilityAttribute();
attributesManager.removeAttribute(sess, ues, attributes.get(0));
// shouldn't find facility attribute on user external source
}
use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class AttributesManagerEntryIntegrationTest method setUpUserExtSourceTest.
private UserExtSource setUpUserExtSourceTest() throws Exception {
vo = setUpVo();
member = setUpMember();
User user = perun.getUsersManager().getUserByMember(sess, member);
List<UserExtSource> userExtSources = perun.getUsersManager().getUserExtSources(sess, user);
UserExtSource ues = null;
for (UserExtSource u : userExtSources) {
if (!"PERUN".equals(u.getExtSource().getName())) {
ues = u;
break;
}
}
assertTrue("User has more more UserExtSources than expected. Expected 2 (PERUN, testExtSource), contains " + userExtSources.size(), userExtSources.size() == 2);
return ues;
}
use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class ModulesUtilsEntryIntegrationTest method setUpMember.
private Member setUpMember() throws Exception {
String userFirstName = Long.toHexString(Double.doubleToLongBits(Math.random()));
String userLastName = Long.toHexString(Double.doubleToLongBits(Math.random()));
// his login in external source
String extLogin = Long.toHexString(Double.doubleToLongBits(Math.random()));
//Mockito.mock(Candidate.class);
Candidate candidate = new Candidate();
candidate.setFirstName(userFirstName);
candidate.setId(0);
candidate.setMiddleName("");
candidate.setLastName(userLastName);
candidate.setTitleBefore("");
candidate.setTitleAfter("");
UserExtSource userExtSource = new UserExtSource(new ExtSource(0, "testExtSource", "cz.metacentrum.perun.core.impl.ExtSourceInternal"), extLogin);
candidate.setUserExtSource(userExtSource);
candidate.setAttributes(new HashMap<String, String>());
Member member = perun.getMembersManagerBl().createMemberSync(sess, vo, candidate);
assertNotNull("No member created", member);
usersForDeletion.add(perun.getUsersManager().getUserByMember(sess, member));
// save user for deletion after test
return member;
}
use of cz.metacentrum.perun.core.api.UserExtSource in project perun by CESNET.
the class PerunBlImpl method getPerunSession.
public PerunSession getPerunSession(PerunPrincipal principal, PerunClient client) throws InternalErrorException {
if (principal.getUser() == null && this.getUsersManagerBl() != null && !dontLookupUsersForLogins.contains(principal.getActor())) {
// Get the user if we are completely initialized
try {
principal.setUser(this.getUsersManagerBl().getUserByExtSourceNameAndExtLogin(getPerunSession(), principal.getExtSourceName(), principal.getActor()));
// Try to update LoA for userExtSource
ExtSource es = this.getExtSourcesManagerBl().getExtSourceByName(getPerunSession(), principal.getExtSourceName());
UserExtSource ues = this.getUsersManagerBl().getUserExtSourceByExtLogin(getPerunSession(), es, principal.getActor());
if (ues.getLoa() != principal.getExtSourceLoa()) {
ues.setLoa(principal.getExtSourceLoa());
this.getUsersManagerBl().updateUserExtSource(getPerunSession(), ues);
}
// Update last access for userExtSource
this.getUsersManagerBl().updateUserExtSourceLastAccess(getPerunSession(), ues);
} catch (ExtSourceNotExistsException | UserExtSourceNotExistsException | UserNotExistsException e) {
// OK - We don't know user yet
}
}
return new PerunSessionImpl(this, principal, client);
}
use of cz.metacentrum.perun.core.api.UserExtSource 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;
}
Aggregations