Search in sources :

Example 11 with LdapUser

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

the class LinkDomainToLdapCmd method execute.

@Override
public void execute() throws ServerApiException {
    try {
        LinkDomainToLdapResponse response = _ldapManager.linkDomainToLdap(this);
        if (admin != null) {
            LdapUser ldapUser = null;
            try {
                ldapUser = _ldapManager.getUser(admin, type, getLdapDomain(), domainId);
            } catch (NoLdapUserMatchingQueryException e) {
                s_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()));
                        s_logger.info("created an account with name " + admin + " in the given domain " + domainId);
                    } catch (Exception e) {
                        s_logger.info("an exception occurred while creating account with name " + admin + " in domain " + domainId, e);
                    }
                } else {
                    s_logger.debug("an account with name " + admin + " already exists in the domain " + domainId);
                }
            } else {
                s_logger.debug("ldap user with username " + admin + " is disabled in the given group/ou");
            }
        }
        response.setObjectName("LinkDomainToLdap");
        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) LinkDomainToLdapResponse(org.apache.cloudstack.api.response.LinkDomainToLdapResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UserAccount(com.cloud.user.UserAccount) ServerApiException(org.apache.cloudstack.api.ServerApiException) NoLdapUserMatchingQueryException(org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException)

Example 12 with LdapUser

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

the class LdapCreateAccountCmd method execute.

@Override
public void execute() throws ServerApiException {
    if (getAccountType() == null && getRoleId() == null) {
        throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Both account type and role ID are not provided");
    }
    final CallContext callContext = getCurrentContext();
    String finalAccountName = getAccountName();
    Long finalDomainId = getDomainId();
    callContext.setEventDetails("Account Name: " + finalAccountName + ", Domain Id:" + finalDomainId);
    try {
        final LdapUser user = _ldapManager.getUser(username, domainId);
        validateUser(user);
        final UserAccount userAccount = createCloudstackUserAccount(user, finalAccountName, finalDomainId);
        if (userAccount != null) {
            final AccountResponse response = _responseGenerator.createUserAccountResponse(ResponseView.Full, userAccount);
            response.setResponseName(getCommandName());
            setResponseObject(response);
        } else {
            throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to create a user account");
        }
    } catch (NoLdapUserMatchingQueryException e) {
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, "No LDAP user exists with the username of " + username);
    }
}
Also used : NoLdapUserMatchingQueryException(org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException) LdapUser(org.apache.cloudstack.ldap.LdapUser) ServerApiException(org.apache.cloudstack.api.ServerApiException) AccountResponse(org.apache.cloudstack.api.response.AccountResponse) CallContext(org.apache.cloudstack.context.CallContext) UserAccount(com.cloud.user.UserAccount)

Example 13 with LdapUser

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

the class LdapImportUsersCmd method createLdapUserResponse.

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

Example 14 with LdapUser

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

the class LdapListUsersCmd method createLdapUserResponse.

/**
 * (as a check for isACloudstackUser is done) only non cloudstack users should be shown
 * @param users a list of {@code LdapUser}s
 * @return a (filtered?) list of user response objects
 */
private List<LdapUserResponse> createLdapUserResponse(final List<LdapUser> users) {
    final List<LdapUserResponse> ldapResponses = new ArrayList<LdapUserResponse>();
    for (final LdapUser user : users) {
        final LdapUserResponse ldapResponse = _ldapManager.createLdapUserResponse(user);
        ldapResponse.setObjectName("LdapUser");
        ldapResponses.add(ldapResponse);
    }
    return ldapResponses;
}
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