Skip to content

Commit

Permalink
Some code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Matteo Alessandroni committed Mar 21, 2018
1 parent c82057c commit 6b12461
Show file tree
Hide file tree
Showing 5 changed files with 68 additions and 63 deletions.
32 changes: 7 additions & 25 deletions src/main/java/net/tirasa/connid/bundles/azure/AzureConnector.java
Original file line number Diff line number Diff line change
Expand Up @@ -411,34 +411,16 @@ public Uid update(ObjectClass objectClass, Uid uid, Set<Attribute> replaceAttrib
if (ObjectClass.ACCOUNT.equals(objectClass)) {
Uid returnUid = uid;

// username
String userID = accessor.findString(AzureAttributes.USER_ID);
if (userID == null) {
userID = accessor.findString(Name.NAME);
}
String mailNickname = accessor.findString(AzureAttributes.USER_MAIL_NICKNAME);
String displayName = accessor.findString(AzureAttributes.USER_DISPLAY_NAME);
Attribute status = accessor.find(OperationalAttributes.ENABLE_NAME);

if (displayName == null) {
AzureUtils.handleGeneralError("The "
+ AzureAttributes.USER_DISPLAY_NAME
AzureUtils.handleGeneralError("The " + AzureAttributes.USER_DISPLAY_NAME
+ " property cannot be cleared during updates");
}

User user = new User();
if (uid.getUidValue().contains("@")) {
user.setUserPrincipalName(uid.getUidValue());
} else {
user.setObjectId(uid.getUidValue());
}

if (!uid.getUidValue().equals(userID)) {
LOG.info("Update - uid value different from user ID");

user.setDisplayName(displayName);
user.setMailNickname(mailNickname);
}
user.setObjectId(uid.getUidValue());

if (status == null
|| status.getValue() == null
Expand Down Expand Up @@ -482,29 +464,29 @@ public Uid update(ObjectClass objectClass, Uid uid, Set<Attribute> replaceAttrib
ownGroups.add(group.getObjectId());
}
} catch (Exception ex) {
LOG.error(ex, "Could not list groups for user {0}", uid.getUidValue());
LOG.error(ex, "Could not list groups for User {0}", uid.getUidValue());
}

List<Object> groups = CollectionUtil.nullAsEmpty(accessor.findList(PredefinedAttributes.GROUPS_NAME));
for (Object group : groups) {
if (!ownGroups.contains(group.toString())) {
try {
client.getAuthenticated().addUserToGroup(returnUid.getUidValue(), group.toString());
LOG.info("User added to group: {0} after update", group);
LOG.ok("User added to group: {0} after update", group);
} catch (Exception e) {
AzureUtils.wrapGeneralError(
"Could not add group : " + group + " to user : " + returnUid.getUidValue(), e);
"Could not add group : " + group + " to User : " + returnUid.getUidValue(), e);
}
}
}
for (String group : ownGroups) {
if (!groups.contains(group)) {
try {
client.getAuthenticated().deleteUserFromGroup(returnUid.getUidValue(), group);
LOG.info("User removed from group: {0} after update", group);
LOG.ok("User removed from group: {0} after update", group);
} catch (Exception e) {
AzureUtils.wrapGeneralError(
"Could not remove group : " + group + " to user : " + returnUid.getUidValue(), e);
"Could not remove group : " + group + " from User : " + returnUid.getUidValue(), e);
}
}
}
Expand Down
24 changes: 24 additions & 0 deletions src/main/java/net/tirasa/connid/bundles/azure/dto/User.java
Original file line number Diff line number Diff line change
Expand Up @@ -981,4 +981,28 @@ private void doSetAttribute(final String name, final List<Object> values) {
}
}

@Override
public String toString() {
return "User{" + "objectId=" + objectId + ", displayName=" + displayName + ", objectType=" + objectType
+ ", accountEnabled=" + accountEnabled + ", city=" + city + ", country=" + country + ", department="
+ department + ", dirSyncEnabled=" + dirSyncEnabled + ", facsimileTelephoneNumber="
+ facsimileTelephoneNumber + ", givenName=" + givenName + ", immutableId=" + immutableId + ", jobTitle="
+ jobTitle + ", lastDirSyncTime=" + lastDirSyncTime + ", mail=" + mail + ", mailNickname="
+ mailNickname + ", mobile=" + mobile + ", passwordPolicies=" + passwordPolicies
+ ", physicalDeliveryOfficeName=" + physicalDeliveryOfficeName + ", postalCode=" + postalCode
+ ", preferredLanguage=" + preferredLanguage + ", state=" + state + ", streetAddress=" + streetAddress
+ ", surname=" + surname + ", telephoneNumber=" + telephoneNumber + ", usageLocation=" + usageLocation
+ ", userPrincipalName=" + userPrincipalName + ", companyName=" + companyName + ", creationType="
+ creationType + ", employeeId=" + employeeId + ", isCompromised=" + isCompromised
+ ", onPremisesDistinguishedName=" + onPremisesDistinguishedName + ", onPremisesSecurityIdentifier="
+ onPremisesSecurityIdentifier + ", refreshTokensValidFromDateTime=" + refreshTokensValidFromDateTime
+ ", showInAddressList=" + showInAddressList + ", sipProxyAddress=" + sipProxyAddress + ", userType="
+ userType + ", passwordProfile=" + passwordProfile + ", userIdentities=" + userIdentities
+ ", signInNames=" + signInNames + ", otherMails=" + otherMails + ", provisionedPlans="
+ provisionedPlans + ", provisioningErrors=" + provisioningErrors + ", proxyAddresses=" + proxyAddresses
+ ", assignedLicenses=" + assignedLicenses + ", assignedPlans=" + assignedPlans + ", password="
+ password + ", thumbnailPhoto=" + thumbnailPhoto + ", license=" + license + ", unlicense=" + unlicense
+ '}';
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -595,7 +595,7 @@ private AzureObject doCreate(final AzureObject obj) {
AzureUtils.handleGeneralError("While creating User", ex);
}

return obj;
return body;
}

private AzureObject doUpdate(final AzureObject obj) {
Expand All @@ -605,9 +605,9 @@ private AzureObject doUpdate(final AzureObject obj) {
if (updated instanceof User) {
User updatedUser = User.class.cast(updated);
webClient = azureService.getWebclient("users/"
+ (StringUtils.isBlank(updatedUser.getObjectId())
? updatedUser.getUserPrincipalName()
: updatedUser.getObjectId()), null);
+ (StringUtils.isBlank(updatedUser.getUserPrincipalName())
? updatedUser.getObjectId()
: updatedUser.getUserPrincipalName()), null);

// handle PasswordProfile object - password update
if ((updatedUser.getPassword() != null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,7 @@ public static void handleGeneralError(final String message, final Exception ex)
}

public static void wrapGeneralError(final String message, final Exception ex) {
String newMessage = message
+ (ex.getMessage() != null ? " - " + ex.getMessage() : "");
LOG.error(ex, newMessage);
LOG.error(ex, message);
throw ConnectorException.wrap(ex);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -253,12 +253,12 @@ public void crud() {
Uid created = connector.create(ObjectClass.GROUP, groupAttrs, new OperationOptionsBuilder().build());
assertNotNull(created);

testGroupUid = created.getUidValue();

// GET GROUP
Group group = client.getAuthenticated().getGroup(created.getUidValue());
Group group = client.getAuthenticated().getGroup(testGroupUid);
assertNotNull(group);
assertEquals(group.getObjectId(), created.getUidValue());

testGroupUid = created.getUidValue();
assertEquals(group.getObjectId(), testGroupUid);

LOG.info("Created Group with name {0} on service!",
group.getDisplayName());
Expand All @@ -277,8 +277,10 @@ public void crud() {
created = connector.create(ObjectClass.ACCOUNT, userAttrs, new OperationOptionsBuilder().build());
assertNotNull(created);

testUserUid = created.getUidValue();

// GET USER
User user = client.getAuthenticated().getUser(created.getUidValue());
User user = client.getAuthenticated().getUser(testUserUid);
assertNotNull(user);
assertEquals(user.getObjectId(), created.getUidValue());

Expand All @@ -299,14 +301,12 @@ public void crud() {
// can't test password update here, Azure API always return '"passwordProfile": null'

// GET USER GROUPS
List<Group> groupsForUser = client.getAuthenticated().getAllGroupsForUser(created.getUidValue());
List<Group> groupsForUser = client.getAuthenticated().getAllGroupsForUser(testUserUid);

assertNotNull(groupsForUser);
assertEquals(1, groupsForUser.size());
assertEquals(groupName, groupsForUser.get(0).getDisplayName());

testUserUid = created.getUidValue();

LOG.info("Added User with name {0} to Group with name : {1}",
user.getDisplayName(), group.getDisplayName());

Expand All @@ -323,7 +323,7 @@ public void crud() {
ObjectClass.GROUP, new Uid(testGroupUid), groupAttrs, new OperationOptionsBuilder().build());
assertNotNull(updated);
assertEquals(testGroupUid, updated.getUidValue());
assertNotEquals(created.getUidValue(), updated.getUidValue());
assertNotEquals(testUserUid, updated.getUidValue());

testGroupUid = updated.getUidValue();

Expand Down Expand Up @@ -360,7 +360,7 @@ public void crud() {
updated = connector.update(
ObjectClass.ACCOUNT, new Uid(testUserUid), userAttrs, new OperationOptionsBuilder().build());
assertNotNull(updated);
assertEquals(created.getUidValue(), updated.getUidValue());
assertEquals(testUserUid, updated.getUidValue());
assertNotEquals(user.getDisplayName(), username);

// GET NEW USER (to check 'accountEnabled' update)
Expand All @@ -373,15 +373,15 @@ public void crud() {

// UPDATE USER (using 'userPrincipalName' attribute)
String anotherUsername = UUID.randomUUID().toString();

userAttrs.clear();
userAttrs.add(AttributeBuilder.build(AzureAttributes.USER_DISPLAY_NAME, anotherUsername));
userAttrs.add(AttributeBuilder.build(AzureAttributes.USER_PRINCIPAL_NAME, user.getUserPrincipalName()));

updated = connector.update(
ObjectClass.ACCOUNT, new Uid(testUserUid), userAttrs, new OperationOptionsBuilder().build());
assertNotNull(updated);
assertEquals(created.getUidValue(), updated.getUidValue());
assertEquals(testUserUid, updated.getUidValue());
assertNotEquals(updatedUser.getDisplayName(), anotherUsername);

// GET NEW USER (to check update using 'userPrincipalName' attribute)
Expand Down Expand Up @@ -475,9 +475,10 @@ public void serviceTest() {
// CREATE USER
User user = new User();
user.setAccountEnabled(true);
user.setDisplayName("Test-" + uid.toString());
user.setMailNickname("test-" + uid.toString());
user.setPassword(AzureUtils.createPassword("Test1234"));
user.setDisplayName("TestUser-" + uid.toString());
user.setMailNickname("testuser-" + uid.toString());
// user.setUserPrincipalName("testuser@" + CONF.getDomain()); // will be [mailNickname@AZURE_DOMAIN]
user.setPassword(AzureUtils.createPassword("Password01"));
user.setUsageLocation("NL");

User userCreated = client.getAuthenticated().createUser(user);
Expand All @@ -487,8 +488,8 @@ public void serviceTest() {

// CREATE GROUP
Group group = new Group();
group.setDisplayName("Test-" + uid.toString());
group.setMailNickname("test-" + uid.toString());
group.setDisplayName("TestGroup-" + uid.toString());
group.setMailNickname("testgroup-" + uid.toString());
group.setSecurityEnabled(true);

Group groupCreated = client.getAuthenticated().createGroup(group);
Expand All @@ -500,7 +501,7 @@ public void serviceTest() {
testGroup = groupCreated.getObjectId();

// GET USER
User userFound = client.getAuthenticated().getUser(userCreated.getObjectId());
User userFound = client.getAuthenticated().getUser(testUser);
assertNotNull(userFound);
assertNotNull(userFound.getObjectId());
LOG.info("User found : {0}", userFound);
Expand All @@ -509,7 +510,7 @@ public void serviceTest() {
LOG.info("Attributes user : {0}", userFound.toAttributes());

// GET GROUP
Group groupFound = client.getAuthenticated().getGroup(groupCreated.getObjectId());
Group groupFound = client.getAuthenticated().getGroup(testGroup);
assertNotNull(groupFound);
assertNotNull(groupFound.getObjectId());
LOG.info("Group found : {0}", groupFound);
Expand All @@ -527,9 +528,9 @@ public void serviceTest() {
License assignedLicense =
License.create(Arrays.asList(subscriptions.get(1)), true);
LOG.info("New assignedLicense : {0}", assignedLicense);
client.getAuthenticated().assignLicense(userCreated.getObjectId(), assignedLicense);
client.getAuthenticated().assignLicense(testUser, assignedLicense);

User userWithNewLicense = client.getAuthenticated().getUser(userCreated.getObjectId());
User userWithNewLicense = client.getAuthenticated().getUser(testUser);
assertNotNull(userWithNewLicense);
assertNotNull(userWithNewLicense.getObjectId());
assertFalse(userWithNewLicense.getAssignedLicenses().isEmpty());
Expand Down Expand Up @@ -562,7 +563,7 @@ public void serviceTest() {
assertFalse(usersPaged2.getUsers().get(0).getObjectId().equals(usersPaged.getUsers().get(0).getObjectId()));
LOG.info("Users paged 2 : {0}", usersPaged2);

List<User> usersFound = client.getAuthenticated().getUsersByName("test-" + uid.toString());
List<User> usersFound = client.getAuthenticated().getUsersByName("testuser-" + uid.toString());
assertNotNull(usersFound);
assertFalse(usersFound.isEmpty());
LOG.info("User found : {0}", usersFound);
Expand Down Expand Up @@ -591,43 +592,43 @@ public void serviceTest() {
LOG.info("groupUpdated : {0}", groupUpdated);

// ADD USER TO GROUP
client.getAuthenticated().addUserToGroup(userCreated.getObjectId(), groupCreated.getObjectId());
client.getAuthenticated().addUserToGroup(testUser, testGroup);
Boolean memberOf =
client.getAuthenticated().isMemberOf(userCreated.getObjectId(), groupCreated.getObjectId());
client.getAuthenticated().isMemberOf(testUser, testGroup);
assertTrue(memberOf);

// GET GROUP MEMBERS
List<User> groupMembers = client.getAuthenticated().getAllMembersOfGroup(groupCreated.getObjectId());
List<User> groupMembers = client.getAuthenticated().getAllMembersOfGroup(testGroup);
assertNotNull(groupMembers);
assertFalse(groupMembers.isEmpty());
LOG.info("Members of Group : {0}", groupMembers);

// GET USER GROUPS
List<Group> userGroups = client.getAuthenticated().getAllGroupsForUser(userCreated.getObjectId());
List<Group> userGroups = client.getAuthenticated().getAllGroupsForUser(testUser);
assertNotNull(userGroups);
assertFalse(userGroups.isEmpty());
LOG.info("User groups list : {0}", userGroups);

// GET MEMBERS OF
List<String> memberGroups =
client.getAuthenticated().getMemberGroups("users", userCreated.getObjectId(), false);
client.getAuthenticated().getMemberGroups("users", testUser, false);
assertNotNull(memberGroups);
LOG.info("getMemberGroups : {0}", memberGroups);

List<String> memberObjects =
client.getAuthenticated().getMemberObjects("users", userCreated.getObjectId(), false);
client.getAuthenticated().getMemberObjects("users", testUser, false);
assertNotNull(memberObjects);
LOG.info("getMemberObjects : {0}", memberObjects);

// DELETE USER FROM GROUP
client.getAuthenticated().deleteUserFromGroup(userCreated.getObjectId(), groupCreated.getObjectId());
client.getAuthenticated().deleteUserFromGroup(testUser, testGroup);
memberOf =
client.getAuthenticated().isMemberOf(userCreated.getObjectId(), groupCreated.getObjectId());
client.getAuthenticated().isMemberOf(testUser, testGroup);
assertFalse(memberOf);

// AVAILABLE EXTENSION PROPERTIES
AvailableExtensionProperties availableProperties =
client.getAuthenticated().getAvailableExtensionProperties(userCreated.getObjectId(), false);
client.getAuthenticated().getAvailableExtensionProperties(testUser, false);
assertNotNull(availableProperties);
LOG.info("availableProperties : {0}", availableProperties);
} catch (Exception e) {
Expand Down

0 comments on commit 6b12461

Please sign in to comment.