Search in sources :

Example 1 with LdapUserResponse

use of org.apache.cloudstack.api.response.LdapUserResponse in project cloudstack by apache.

the class LdapListUsersCmdTest method applyAnyDomain.

/**
 * filter all acs users
 *
 * @throws NoSuchFieldException
 * @throws IllegalAccessException
 */
@Test
public void applyAnyDomain() throws NoSuchFieldException, IllegalAccessException, NoLdapUserMatchingQueryException {
    mockACSUserSearch();
    mockResponseCreation();
    useSubdomain();
    setHiddenField(ldapListUsersCmd, "userFilter", "AnyDomain");
    setHiddenField(ldapListUsersCmd, "domainId", 2l);
    ldapListUsersCmd.execute();
    // 'rmurphy' annotated with native
    // 'bob' still in
    // 'abhi' is filtered out
    List<ResponseObject> responses = ((ListResponse) ldapListUsersCmd.getResponseObject()).getResponses();
    assertEquals(2, responses.size());
    for (ResponseObject response : responses) {
        if (!(response instanceof LdapUserResponse)) {
            fail("unexpected return-type from API backend method");
        } else {
            LdapUserResponse userResponse = (LdapUserResponse) response;
            // further validate this user
            if ("rmurphy".equals(userResponse.getUsername()) && !User.Source.NATIVE.toString().equalsIgnoreCase(userResponse.getUserSource())) {
                fail("expected murphy from ldap");
            }
            if ("bob".equals(userResponse.getUsername()) && !"".equals(userResponse.getUserSource())) {
                fail("expected bob from without usersource");
            }
        }
    }
}
Also used : ListResponse(org.apache.cloudstack.api.response.ListResponse) ResponseObject(org.apache.cloudstack.api.ResponseObject) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 2 with LdapUserResponse

use of org.apache.cloudstack.api.response.LdapUserResponse in project cloudstack by apache.

the class LdapListUsersCmdTest method mockResponseCreation.

private void mockResponseCreation() throws NoLdapUserMatchingQueryException {
    List<LdapUser> users = new ArrayList();
    LdapUser murphy = new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", "mythical", false, null);
    LdapUser bob = new LdapUser("bob", "bob@test.com", "Robert", "Young", "cn=bob,ou=engineering,dc=cloudstack,dc=org", LOCAL_DOMAIN_NAME, false, null);
    LdapUser abhi = new LdapUser("abhi", "abhi@test.com", "Abhi", "YoungOrOld", "cn=abhi,ou=engineering,dc=cloudstack,dc=org", LOCAL_DOMAIN_NAME, false, null);
    users.add(murphy);
    users.add(bob);
    users.add(abhi);
    doReturn(users).when(ldapManager).getUsers(any());
    LdapUserResponse response = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null);
    doReturn(response).when(ldapManager).createLdapUserResponse(murphy);
    LdapUserResponse bobResponse = new LdapUserResponse("bob", "bob@test.com", "Robert", "Young", "cn=bob,ou=engineering,dc=cloudstack,dc=org", LOCAL_DOMAIN_NAME);
    doReturn(bobResponse).when(ldapManager).createLdapUserResponse(bob);
    LdapUserResponse abhiResponse = new LdapUserResponse("abhi", "abhi@test.com", "Abhi", "YoungOrOld", "cn=abhi,ou=engineering,dc=cloudstack,dc=org", LOCAL_DOMAIN_NAME);
    doReturn(abhiResponse).when(ldapManager).createLdapUserResponse(abhi);
}
Also used : LdapUser(org.apache.cloudstack.ldap.LdapUser) ArrayList(java.util.ArrayList) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse)

Example 3 with LdapUserResponse

use of org.apache.cloudstack.api.response.LdapUserResponse in project cloudstack by apache.

the class LdapImportUsersCmd method execute.

@Override
public void execute() throws ResourceUnavailableException, InsufficientCapacityException, ServerApiException, ConcurrentOperationException, ResourceAllocationException, NetworkRuleConflictException {
    if (getAccountType() == null && getRoleId() == null) {
        throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Both account type and role ID are not provided");
    }
    List<LdapUser> users;
    try {
        if (StringUtils.isNotBlank(groupName)) {
            users = _ldapManager.getUsersInGroup(groupName, domainId);
        } else {
            users = _ldapManager.getUsers(domainId);
        }
    } catch (NoLdapUserMatchingQueryException ex) {
        users = new ArrayList<LdapUser>();
        s_logger.info("No Ldap user matching query. " + " ::: " + ex.getMessage());
    }
    List<LdapUser> addedUsers = new ArrayList<LdapUser>();
    for (LdapUser user : users) {
        Domain domain = getDomain(user);
        try {
            createCloudstackUserAccount(user, getAccountName(user), domain);
            addedUsers.add(user);
        } catch (InvalidParameterValueException ex) {
            s_logger.error("Failed to create user with username: " + user.getUsername() + " ::: " + ex.getMessage());
        }
    }
    ListResponse<LdapUserResponse> response = new ListResponse<LdapUserResponse>();
    response.setResponses(createLdapUserResponse(addedUsers));
    response.setResponseName(getCommandName());
    setResponseObject(response);
}
Also used : NoLdapUserMatchingQueryException(org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException) LdapUser(org.apache.cloudstack.ldap.LdapUser) ServerApiException(org.apache.cloudstack.api.ServerApiException) ListResponse(org.apache.cloudstack.api.response.ListResponse) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) ArrayList(java.util.ArrayList) Domain(com.cloud.domain.Domain) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse)

Example 4 with LdapUserResponse

use of org.apache.cloudstack.api.response.LdapUserResponse in project cloudstack by apache.

the class LdapListUsersCmd method getCloudstackUser.

private UserResponse getCloudstackUser(LdapUserResponse user) {
    UserResponse returnObject = null;
    final List<UserResponse> cloudstackUsers = getCloudstackUsers();
    if (cloudstackUsers != null) {
        for (final UserResponse cloudstackUser : cloudstackUsers) {
            if (user.getUsername().equals(cloudstackUser.getUsername())) {
                returnObject = cloudstackUser;
                if (returnObject.getDomainId() == this.getCurrentDomainId()) {
                    break;
                }
            }
        }
    }
    return returnObject;
}
Also used : UserResponse(org.apache.cloudstack.api.response.UserResponse) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse)

Example 5 with LdapUserResponse

use of org.apache.cloudstack.api.response.LdapUserResponse in project cloudstack by apache.

the class LdapListUsersCmd method isNotAlreadyImportedInTheCurrentDomain.

/**
 * @return true unless the the user is imported in the specified cloudstack domain from LDAP
 */
private boolean isNotAlreadyImportedInTheCurrentDomain(LdapUserResponse user) {
    UserResponse cloudstackUser = getCloudstackUser(user);
    String domainId = getCurrentDomainId();
    return cloudstackUser == null || /*doesn't exist in cloudstack*/
    !(cloudstackUser.getUserSource().equalsIgnoreCase(User.Source.LDAP.toString()) && domainId.equals(cloudstackUser.getDomainId()));
/* is from another source */
}
Also used : UserResponse(org.apache.cloudstack.api.response.UserResponse) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse)

Aggregations

LdapUserResponse (org.apache.cloudstack.api.response.LdapUserResponse)12 ArrayList (java.util.ArrayList)8 LdapUser (org.apache.cloudstack.ldap.LdapUser)6 ListResponse (org.apache.cloudstack.api.response.ListResponse)3 UserResponse (org.apache.cloudstack.api.response.UserResponse)3 Domain (com.cloud.domain.Domain)2 Test (org.junit.Test)2 DomainVO (com.cloud.domain.DomainVO)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 ResponseObject (org.apache.cloudstack.api.ResponseObject)1 ServerApiException (org.apache.cloudstack.api.ServerApiException)1 NoLdapUserMatchingQueryException (org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException)1 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)1