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());
}
}
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());
}
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;
}
Aggregations