Search in sources :

Example 1 with LinkAccountToLdapResponse

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

the class LinkAccountToLdapCmd method execute.

@Override
public void execute() throws ServerApiException {
    try {
        LinkAccountToLdapResponse response = _ldapManager.linkAccountToLdap(this);
        if (admin != null) {
            LdapUser ldapUser = null;
            try {
                ldapUser = _ldapManager.getUser(admin, type, ldapDomain, domainId);
            } catch (NoLdapUserMatchingQueryException e) {
                LOGGER.debug("no ldap user matching username " + admin + " in the given group/ou", e);
            }
            if (ldapUser != null && !ldapUser.isDisabled()) {
                Account account = _accountService.getActiveAccountByName(admin, domainId);
                if (account == null) {
                    try {
                        UserAccount userAccount = _accountService.createUserAccount(admin, "", ldapUser.getFirstname(), ldapUser.getLastname(), ldapUser.getEmail(), null, admin, Account.ACCOUNT_TYPE_DOMAIN_ADMIN, RoleType.DomainAdmin.getId(), domainId, null, null, UUID.randomUUID().toString(), UUID.randomUUID().toString(), User.Source.LDAP);
                        response.setAdminId(String.valueOf(userAccount.getAccountId()));
                        LOGGER.info("created an account with name " + admin + " in the given domain " + domainId);
                    } catch (Exception e) {
                        LOGGER.info("an exception occurred while creating account with name " + admin + " in domain " + domainId, e);
                    }
                } else {
                    LOGGER.debug("an account with name " + admin + " already exists in the domain " + domainId);
                }
            } else {
                LOGGER.debug("ldap user with username " + admin + " is disabled in the given group/ou");
            }
        }
        response.setObjectName(APINAME);
        response.setResponseName(getCommandName());
        setResponseObject(response);
    } catch (final InvalidParameterValueException e) {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, e.toString());
    }
}
Also used : Account(com.cloud.user.Account) UserAccount(com.cloud.user.UserAccount) NoLdapUserMatchingQueryException(org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException) LdapUser(org.apache.cloudstack.ldap.LdapUser) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) LinkAccountToLdapResponse(org.apache.cloudstack.api.response.LinkAccountToLdapResponse) UserAccount(com.cloud.user.UserAccount) ServerApiException(org.apache.cloudstack.api.ServerApiException) NoLdapUserMatchingQueryException(org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException)

Example 2 with LinkAccountToLdapResponse

use of org.apache.cloudstack.api.response.LinkAccountToLdapResponse 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 3 with LinkAccountToLdapResponse

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

the class LdapManagerImpl method linkAccountToLdap.

@Override
public LinkAccountToLdapResponse linkAccountToLdap(LinkAccountToLdapCmd cmd) {
    Validate.notNull(_ldapConfiguration.getBaseDn(cmd.getDomainId()), "can not link an account to ldap in a domain for which no basdn is configured");
    Validate.notNull(cmd.getDomainId(), "domainId cannot be null.");
    Validate.notEmpty(cmd.getAccountName(), "accountName cannot be empty.");
    Validate.notEmpty(cmd.getLdapDomain(), "ldapDomain cannot be empty, please supply a GROUP or OU name");
    Validate.notNull(cmd.getType(), "type cannot be null. It should either be GROUP or OU");
    Validate.notEmpty(cmd.getLdapDomain(), "GROUP or OU name cannot be empty");
    LinkType linkType = LdapManager.LinkType.valueOf(cmd.getType().toUpperCase());
    Account account = accountDao.findActiveAccount(cmd.getAccountName(), cmd.getDomainId());
    if (account == null) {
        account = new AccountVO(cmd.getAccountName(), cmd.getDomainId(), null, cmd.getAccountType(), UUID.randomUUID().toString());
        accountDao.persist((AccountVO) account);
    }
    Long accountId = account.getAccountId();
    clearOldAccountMapping(cmd);
    LdapTrustMapVO vo = _ldapTrustMapDao.persist(new LdapTrustMapVO(cmd.getDomainId(), linkType, cmd.getLdapDomain(), cmd.getAccountType(), accountId));
    DomainVO domain = domainDao.findById(vo.getDomainId());
    String domainUuid = "<unknown>";
    if (domain == null) {
        LOGGER.error("no domain in database for id " + vo.getDomainId());
    } else {
        domainUuid = domain.getUuid();
    }
    LinkAccountToLdapResponse response = new LinkAccountToLdapResponse(domainUuid, vo.getType().toString(), vo.getName(), vo.getAccountType(), account.getUuid(), cmd.getAccountName());
    return response;
}
Also used : Account(com.cloud.user.Account) DomainVO(com.cloud.domain.DomainVO) AccountVO(com.cloud.user.AccountVO) LinkAccountToLdapResponse(org.apache.cloudstack.api.response.LinkAccountToLdapResponse)

Aggregations

LinkAccountToLdapResponse (org.apache.cloudstack.api.response.LinkAccountToLdapResponse)3 Account (com.cloud.user.Account)2 LdapUser (org.apache.cloudstack.ldap.LdapUser)2 DomainVO (com.cloud.domain.DomainVO)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 AccountVO (com.cloud.user.AccountVO)1 UserAccount (com.cloud.user.UserAccount)1 UserAccountVO (com.cloud.user.UserAccountVO)1 ServerApiException (org.apache.cloudstack.api.ServerApiException)1 NoLdapUserMatchingQueryException (org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException)1 Test (org.junit.Test)1 Matchers.anyString (org.mockito.Matchers.anyString)1