Search in sources :

Example 21 with UserAccount

use of com.cloud.user.UserAccount 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);
        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 22 with UserAccount

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

the class LdapImportUsersCmd method createCloudstackUserAccount.

private void createCloudstackUserAccount(LdapUser user, String accountName, Domain domain) {
    Account account = _accountService.getActiveAccountByName(accountName, domain.getId());
    if (account == null) {
        s_logger.debug("No account exists with name: " + accountName + " creating the account and an user with name: " + user.getUsername() + " in the account");
        _accountService.createUserAccount(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone, accountName, getAccountType(), getRoleId(), domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString(), User.Source.LDAP);
    } else {
        //            check if the user exists. if yes, call update
        UserAccount csuser = _accountService.getActiveUserAccount(user.getUsername(), domain.getId());
        if (csuser == null) {
            s_logger.debug("No user exists with name: " + user.getUsername() + " creating a user in the account: " + accountName);
            _accountService.createUser(user.getUsername(), generatePassword(), user.getFirstname(), user.getLastname(), user.getEmail(), timezone, accountName, domain.getId(), UUID.randomUUID().toString(), User.Source.LDAP);
        } else {
            s_logger.debug("account with name: " + accountName + " exist and user with name: " + user.getUsername() + " exists in the account. Updating the account.");
            _accountService.updateUser(csuser.getId(), user.getFirstname(), user.getLastname(), user.getEmail(), null, null, null, null, null);
        }
    }
}
Also used : UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) UserAccount(com.cloud.user.UserAccount)

Example 23 with UserAccount

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

the class ApiServer method loginUser.

@Override
public ResponseObject loginUser(final HttpSession session, final String username, final String password, Long domainId, final String domainPath, final InetAddress loginIpAddress, final Map<String, Object[]> requestParameters) throws CloudAuthenticationException {
    // We will always use domainId first. If that does not exist, we will use domain name. If THAT doesn't exist
    // we will default to ROOT
    final Domain userDomain = domainMgr.findDomainByIdOrPath(domainId, domainPath);
    if (userDomain == null || userDomain.getId() < 1L) {
        throw new CloudAuthenticationException("Unable to find the domain from the path " + domainPath);
    } else {
        domainId = userDomain.getId();
    }
    final UserAccount userAcct = accountMgr.authenticateUser(username, password, domainId, loginIpAddress, requestParameters);
    if (userAcct != null) {
        final String timezone = userAcct.getTimezone();
        float offsetInHrs = 0f;
        if (timezone != null) {
            final TimeZone t = TimeZone.getTimeZone(timezone);
            s_logger.info("Current user logged in under " + timezone + " timezone");
            final java.util.Date date = new java.util.Date();
            final long longDate = date.getTime();
            final float offsetInMs = (t.getOffset(longDate));
            offsetInHrs = offsetInMs / (1000 * 60 * 60);
            s_logger.info("Timezone offset from UTC is: " + offsetInHrs);
        }
        final Account account = accountMgr.getAccount(userAcct.getAccountId());
        // set the userId and account object for everyone
        session.setAttribute("userid", userAcct.getId());
        final UserVO user = (UserVO) accountMgr.getActiveUser(userAcct.getId());
        if (user.getUuid() != null) {
            session.setAttribute("user_UUID", user.getUuid());
        }
        session.setAttribute("username", userAcct.getUsername());
        session.setAttribute("firstname", userAcct.getFirstname());
        session.setAttribute("lastname", userAcct.getLastname());
        session.setAttribute("accountobj", account);
        session.setAttribute("account", account.getAccountName());
        session.setAttribute("domainid", account.getDomainId());
        final DomainVO domain = (DomainVO) domainMgr.getDomain(account.getDomainId());
        if (domain.getUuid() != null) {
            session.setAttribute("domain_UUID", domain.getUuid());
        }
        session.setAttribute("type", Short.valueOf(account.getType()).toString());
        session.setAttribute("registrationtoken", userAcct.getRegistrationToken());
        session.setAttribute("registered", Boolean.toString(userAcct.isRegistered()));
        if (timezone != null) {
            session.setAttribute("timezone", timezone);
            session.setAttribute("timezoneoffset", Float.valueOf(offsetInHrs).toString());
        }
        // (bug 5483) generate a session key that the user must submit on every request to prevent CSRF, add that
        // to the login response so that session-based authenticators know to send the key back
        final SecureRandom sesssionKeyRandom = new SecureRandom();
        final byte[] sessionKeyBytes = new byte[20];
        sesssionKeyRandom.nextBytes(sessionKeyBytes);
        final String sessionKey = Base64.encodeBase64URLSafeString(sessionKeyBytes);
        session.setAttribute(ApiConstants.SESSIONKEY, sessionKey);
        return createLoginResponse(session);
    }
    throw new CloudAuthenticationException("Failed to authenticate user " + username + " in domain " + domainId + "; please provide valid credentials");
}
Also used : UserAccount(com.cloud.user.UserAccount) Account(com.cloud.user.Account) CloudAuthenticationException(com.cloud.exception.CloudAuthenticationException) Date(java.util.Date) SecureRandom(java.security.SecureRandom) Date(java.util.Date) ResponseDate(org.apache.http.protocol.ResponseDate) DomainVO(com.cloud.domain.DomainVO) TimeZone(java.util.TimeZone) UserVO(com.cloud.user.UserVO) Domain(com.cloud.domain.Domain) 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