Search in sources :

Example 6 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser 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 7 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LinkAccountToLdapCmdTest method execute.

@Test
public void execute() throws Exception {
    // test with valid params and with admin who doesnt exist in cloudstack
    long domainId = 1;
    String type = "GROUP";
    String ldapDomain = "CN=test,DC=ccp,DC=Citrix,DC=com";
    short accountType = Account.ACCOUNT_TYPE_DOMAIN_ADMIN;
    String username = "admin";
    long accountId = 24;
    String accountName = "test";
    setHiddenField(linkAccountToLdapCmd, "ldapDomain", ldapDomain);
    setHiddenField(linkAccountToLdapCmd, "admin", username);
    setHiddenField(linkAccountToLdapCmd, "type", type);
    setHiddenField(linkAccountToLdapCmd, "domainId", domainId);
    setHiddenField(linkAccountToLdapCmd, "accountType", accountType);
    setHiddenField(linkAccountToLdapCmd, "accountName", accountName);
    LinkAccountToLdapResponse response = new LinkAccountToLdapResponse(String.valueOf(domainId), type, ldapDomain, (short) accountType, username, accountName);
    when(ldapManager.linkAccountToLdap(linkAccountToLdapCmd)).thenReturn(response);
    when(ldapManager.getUser(username, type, ldapDomain, 1L)).thenReturn(new LdapUser(username, "admin@ccp.citrix.com", "Admin", "Admin", ldapDomain, "ccp", false, null));
    when(accountService.getActiveAccountByName(username, domainId)).thenReturn(null);
    UserAccountVO userAccount = new UserAccountVO();
    userAccount.setAccountId(24);
    when(accountService.createUserAccount(eq(username), eq(""), eq("Admin"), eq("Admin"), eq("admin@ccp.citrix.com"), isNull(String.class), eq(username), eq(Account.ACCOUNT_TYPE_DOMAIN_ADMIN), eq(RoleType.DomainAdmin.getId()), eq(domainId), isNull(String.class), (java.util.Map<String, String>) isNull(), anyString(), anyString(), eq(User.Source.LDAP))).thenReturn(userAccount);
    linkAccountToLdapCmd.execute();
    LinkAccountToLdapResponse result = (LinkAccountToLdapResponse) linkAccountToLdapCmd.getResponseObject();
    assertEquals("objectName", linkAccountToLdapCmd.APINAME, result.getObjectName());
    assertEquals("commandName", linkAccountToLdapCmd.getCommandName(), result.getResponseName());
    assertEquals("domainId", String.valueOf(domainId), result.getDomainId());
    assertEquals("type", type, result.getType());
    assertEquals("name", ldapDomain, result.getLdapDomain());
    assertEquals("accountId", String.valueOf(accountId), result.getAdminId());
}
Also used : UserAccountVO(com.cloud.user.UserAccountVO) LdapUser(org.apache.cloudstack.ldap.LdapUser) Matchers.anyString(org.mockito.Matchers.anyString) LinkAccountToLdapResponse(org.apache.cloudstack.api.response.LinkAccountToLdapResponse) Test(org.junit.Test)

Example 8 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LdapListUsersCmdTest method isACloudstackUser.

/**
 * given: "We have an LdapUser and a CloudStack user whose username match"
 *  when: "isACloudstackUser is executed"
 *  then: "The result is true"
 *
 * TODO: is this really the valid behaviour? shouldn't the user also be linked to ldap and not accidentally match?
 */
@Test
public void isACloudstackUser() {
    mockACSUserSearch();
    LdapUser ldapUser = new LdapUser("rmurphy", "rmurphy@cloudstack.org", "Ryan", "Murphy", "cn=rmurphy,dc=cloudstack,dc=org", null, false, null);
    boolean result = ldapListUsersCmd.isACloudstackUser(ldapUser);
    assertTrue(result);
}
Also used : LdapUser(org.apache.cloudstack.ldap.LdapUser) PrepareForTest(org.powermock.core.classloader.annotations.PrepareForTest) Test(org.junit.Test)

Example 9 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LdapImportUsersCmdTest method successfulResponseFromExecute.

@Test
public void successfulResponseFromExecute() throws Exception {
    List<LdapUser> users = new ArrayList();
    users.add(new LdapUser("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering", false, null));
    users.add(new LdapUser("bob", "bob@test.com", "Robert", "Young", "cn=bob,ou=engineering,dc=cloudstack,dc=org", "engineering", false, null));
    when(ldapManager.getUsers(null)).thenReturn(users);
    LdapUserResponse response1 = new LdapUserResponse("rmurphy", "rmurphy@test.com", "Ryan", "Murphy", "cn=rmurphy,ou=engineering,dc=cloudstack,dc=org", "engineering");
    LdapUserResponse response2 = new LdapUserResponse("bob", "bob@test.com", "Robert", "Young", "cn=bob,ou=engineering,dc=cloudstack,dc=org", "engineering");
    when(ldapManager.createLdapUserResponse(any(LdapUser.class))).thenReturn(response1).thenReturn(response2);
    Domain domain = new DomainVO("engineering", 1L, 1L, "engineering", UUID.randomUUID().toString());
    when(domainService.getDomainByName("engineering", 1L)).thenReturn(null, domain);
    when(domainService.createDomain(eq("engineering"), eq(1L), eq("engineering"), anyString())).thenReturn(domain);
    ldapImportUsersCmd.execute();
    ListResponse<LdapUserResponse> resp = (ListResponse<LdapUserResponse>) ldapImportUsersCmd.getResponseObject();
    assertEquals(" when LdapListUsersCmd is executed, a list of size 2 should be returned", 2, resp.getResponses().size());
}
Also used : DomainVO(com.cloud.domain.DomainVO) LdapUser(org.apache.cloudstack.ldap.LdapUser) ListResponse(org.apache.cloudstack.api.response.ListResponse) ArrayList(java.util.ArrayList) Domain(com.cloud.domain.Domain) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse) Test(org.junit.Test)

Example 10 with LdapUser

use of org.apache.cloudstack.ldap.LdapUser in project cloudstack by apache.

the class LdapUserSearchCmd method createLdapUserResponse.

private List<LdapUserResponse> createLdapUserResponse(final List<LdapUser> users) {
    final List<LdapUserResponse> ldapUserResponses = new ArrayList<LdapUserResponse>();
    if (users != null) {
        for (final LdapUser user : users) {
            final LdapUserResponse ldapUserResponse = _ldapManager.createLdapUserResponse(user);
            ldapUserResponse.setObjectName("LdapUser");
            ldapUserResponses.add(ldapUserResponse);
        }
    }
    return ldapUserResponses;
}
Also used : LdapUser(org.apache.cloudstack.ldap.LdapUser) ArrayList(java.util.ArrayList) LdapUserResponse(org.apache.cloudstack.api.response.LdapUserResponse)

Aggregations

LdapUser (org.apache.cloudstack.ldap.LdapUser)14 LdapUserResponse (org.apache.cloudstack.api.response.LdapUserResponse)7 ArrayList (java.util.ArrayList)6 Test (org.junit.Test)6 ServerApiException (org.apache.cloudstack.api.ServerApiException)5 NoLdapUserMatchingQueryException (org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException)4 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)3 UserAccount (com.cloud.user.UserAccount)3 Domain (com.cloud.domain.Domain)2 Account (com.cloud.user.Account)2 UserAccountVO (com.cloud.user.UserAccountVO)2 LinkAccountToLdapResponse (org.apache.cloudstack.api.response.LinkAccountToLdapResponse)2 LinkDomainToLdapResponse (org.apache.cloudstack.api.response.LinkDomainToLdapResponse)2 ListResponse (org.apache.cloudstack.api.response.ListResponse)2 Matchers.anyString (org.mockito.Matchers.anyString)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 DomainVO (com.cloud.domain.DomainVO)1 AccountResponse (org.apache.cloudstack.api.response.AccountResponse)1 UserResponse (org.apache.cloudstack.api.response.UserResponse)1 CallContext (org.apache.cloudstack.context.CallContext)1