Search in sources :

Example 6 with UserAccount

use of com.cloud.user.UserAccount in project cloudstack by apache.

the class DisableUserCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("UserId: " + getId());
    UserAccount user = _regionService.disableUser(this);
    if (user != null) {
        UserResponse response = _responseGenerator.createUserResponse(user);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to disable user");
    }
}
Also used : UserResponse(org.apache.cloudstack.api.response.UserResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) UserAccount(com.cloud.user.UserAccount)

Example 7 with UserAccount

use of com.cloud.user.UserAccount in project cloudstack by apache.

the class GetUserCmd method execute.

@Override
public void execute() {
    UserAccount result = _accountService.getUserByApiKey(getApiKey());
    if (result != null) {
        UserResponse response = _responseGenerator.createUserResponse(result);
        response.setResponseName(getCommandName());
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new InvalidParameterValueException("User with specified API key does not exist");
    }
}
Also used : UserResponse(org.apache.cloudstack.api.response.UserResponse) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) UserAccount(com.cloud.user.UserAccount)

Example 8 with UserAccount

use of com.cloud.user.UserAccount in project cloudstack by apache.

the class UpdateUserCmd method execute.

@Override
public void execute() {
    CallContext.current().setEventDetails("UserId: " + getId());
    UserAccount user = _regionService.updateUser(this);
    if (user != null) {
        UserResponse response = _responseGenerator.createUserResponse(user);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to update user");
    }
}
Also used : UserResponse(org.apache.cloudstack.api.response.UserResponse) ServerApiException(org.apache.cloudstack.api.ServerApiException) UserAccount(com.cloud.user.UserAccount)

Example 9 with UserAccount

use of com.cloud.user.UserAccount in project cloudstack by apache.

the class DefaultLoginAPIAuthenticatorCmd method authenticate.

@Override
public String authenticate(String command, Map<String, Object[]> params, HttpSession session, InetAddress remoteAddress, String responseType, StringBuilder auditTrailSb, final HttpServletRequest req, final HttpServletResponse resp) throws ServerApiException {
    // Disallow non POST requests
    if (HTTPMethod.valueOf(req.getMethod()) != HTTPMethod.POST) {
        throw new ServerApiException(ApiErrorCode.METHOD_NOT_ALLOWED, "Please use HTTP POST to authenticate using this API");
    }
    // FIXME: ported from ApiServlet, refactor and cleanup
    final String[] username = (String[]) params.get(ApiConstants.USERNAME);
    final String[] password = (String[]) params.get(ApiConstants.PASSWORD);
    String[] domainIdArr = (String[]) params.get(ApiConstants.DOMAIN_ID);
    if (domainIdArr == null) {
        domainIdArr = (String[]) params.get(ApiConstants.DOMAIN__ID);
    }
    final String[] domainName = (String[]) params.get(ApiConstants.DOMAIN);
    Long domainId = null;
    if ((domainIdArr != null) && (domainIdArr.length > 0)) {
        try {
            //check if UUID is passed in for domain
            domainId = _apiServer.fetchDomainId(domainIdArr[0]);
            if (domainId == null) {
                domainId = Long.parseLong(domainIdArr[0]);
            }
            // building the params for POST call
            auditTrailSb.append(" domainid=" + domainId);
        } catch (final NumberFormatException e) {
            s_logger.warn("Invalid domain id entered by user");
            auditTrailSb.append(" " + HttpServletResponse.SC_UNAUTHORIZED + " " + "Invalid domain id entered, please enter a valid one");
            throw new ServerApiException(ApiErrorCode.UNAUTHORIZED, _apiServer.getSerializedApiError(HttpServletResponse.SC_UNAUTHORIZED, "Invalid domain id entered, please enter a valid one", params, responseType));
        }
    }
    String domain = null;
    if (domainName != null) {
        domain = domainName[0];
        auditTrailSb.append(" domain=" + domain);
        if (domain != null) {
            // ensure domain starts with '/' and ends with '/'
            if (!domain.endsWith("/")) {
                domain += '/';
            }
            if (!domain.startsWith("/")) {
                domain = "/" + domain;
            }
        }
    }
    String serializedResponse = null;
    if (username != null) {
        final String pwd = ((password == null) ? null : password[0]);
        try {
            final Domain userDomain = _domainService.findDomainByIdOrPath(domainId, domain);
            if (userDomain != null) {
                domainId = userDomain.getId();
            } else {
                throw new CloudAuthenticationException("Unable to find the domain from the path " + domain);
            }
            final UserAccount userAccount = _accountService.getActiveUserAccount(username[0], domainId);
            if (userAccount != null && User.Source.SAML2 == userAccount.getSource()) {
                throw new CloudAuthenticationException("User is not allowed CloudStack login");
            }
            return ApiResponseSerializer.toSerializedString(_apiServer.loginUser(session, username[0], pwd, domainId, domain, remoteAddress, params), responseType);
        } catch (final CloudAuthenticationException ex) {
            // TODO: fall through to API key, or just fail here w/ auth error? (HTTP 401)
            try {
                session.invalidate();
            } catch (final IllegalStateException ise) {
            }
            auditTrailSb.append(" " + ApiErrorCode.ACCOUNT_ERROR + " " + ex.getMessage() != null ? ex.getMessage() : "failed to authenticate user, check if username/password are correct");
            serializedResponse = _apiServer.getSerializedApiError(ApiErrorCode.ACCOUNT_ERROR.getHttpCode(), ex.getMessage() != null ? ex.getMessage() : "failed to authenticate user, check if username/password are correct", params, responseType);
        }
    }
    // We should not reach here and if we do we throw an exception
    throw new ServerApiException(ApiErrorCode.ACCOUNT_ERROR, serializedResponse);
}
Also used : ServerApiException(org.apache.cloudstack.api.ServerApiException) CloudAuthenticationException(com.cloud.exception.CloudAuthenticationException) Domain(com.cloud.domain.Domain) UserAccount(com.cloud.user.UserAccount)

Example 10 with UserAccount

use of com.cloud.user.UserAccount in project CloudStack-archive by CloudStack-extras.

the class CreateAccountCmd method execute.

@Override
public void execute() {
    UserContext.current().setEventDetails("Account Name: " + getAccountName() + ", Domain Id:" + getDomainId());
    UserAccount userAccount = _accountService.createUserAccount(getUsername(), getPassword(), getFirstName(), getLastName(), getEmail(), getTimeZone(), getAccountName(), getAccountType(), getDomainId(), getNetworkDomain(), getDetails());
    if (userAccount != null) {
        AccountResponse response = _responseGenerator.createUserAccountResponse(userAccount);
        response.setResponseName(getCommandName());
        this.setResponseObject(response);
    } else {
        throw new ServerApiException(BaseCmd.INTERNAL_ERROR, "Failed to create a user account");
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) AccountResponse(com.cloud.api.response.AccountResponse) UserAccount(com.cloud.user.UserAccount)

Aggregations

UserAccount (com.cloud.user.UserAccount)23 ServerApiException (org.apache.cloudstack.api.ServerApiException)11 ServerApiException (com.cloud.api.ServerApiException)5 UserResponse (com.cloud.api.response.UserResponse)5 UserResponse (org.apache.cloudstack.api.response.UserResponse)5 Domain (com.cloud.domain.Domain)4 CloudAuthenticationException (com.cloud.exception.CloudAuthenticationException)4 Account (com.cloud.user.Account)4 Pair (com.cloud.utils.Pair)3 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)2 UserAccountVO (com.cloud.user.UserAccountVO)2 CloudRuntimeException (com.cloud.utils.exception.CloudRuntimeException)2 IOException (java.io.IOException)2 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)2 ArrayList (java.util.ArrayList)2 AccountResponse (org.apache.cloudstack.api.response.AccountResponse)2 LoginCmdResponse (org.apache.cloudstack.api.response.LoginCmdResponse)2 LdapUser (org.apache.cloudstack.ldap.LdapUser)2 NoLdapUserMatchingQueryException (org.apache.cloudstack.ldap.NoLdapUserMatchingQueryException)2