Search in sources :

Example 11 with UserAccount

use of com.cloud.legacymodel.user.UserAccount in project cosmic by MissionCriticalCloud.

the class EnableUserCmd method execute.

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

Example 12 with UserAccount

use of com.cloud.legacymodel.user.UserAccount in project cosmic by MissionCriticalCloud.

the class CreateAccountCmd method execute.

// ///////////////////////////////////////////////////
// ///////////////// Accessors ///////////////////////
// ///////////////////////////////////////////////////
@Override
public void execute() {
    validateParams();
    CallContext.current().setEventDetails("Account Name: " + getAccountName() + ", Domain Id:" + getDomainId());
    final UserAccount userAccount = _accountService.createUserAccount(getUsername(), getPassword(), getFirstName(), getLastName(), getEmail(), getTimeZone(), getAccountName(), getAccountType(), getDomainId(), getNetworkDomain(), getDetails(), getAccountUUID(), getUserUUID());
    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");
    }
}
Also used : ServerApiException(com.cloud.api.ServerApiException) AccountResponse(com.cloud.api.response.AccountResponse) UserAccount(com.cloud.legacymodel.user.UserAccount)

Example 13 with UserAccount

use of com.cloud.legacymodel.user.UserAccount in project cosmic by MissionCriticalCloud.

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 default to ROOT
    if (domainId == null) {
        if (domainPath == null || domainPath.trim().length() == 0) {
            domainId = Domain.ROOT_DOMAIN;
        } else {
            final Domain domainObj = _domainMgr.findDomainByPath(domainPath);
            if (domainObj != null) {
                domainId = domainObj.getId();
            } else {
                // if an unknown path is passed in, fail the login call
                throw new CloudAuthenticationException("Unable to find the domain from the path " + domainPath);
            }
        }
    }
    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());
        }
        if (domain.getName() != null) {
            session.setAttribute(ApiConstants.DOMAIN_NAME, domain.getName());
        }
        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.legacymodel.user.UserAccount) Account(com.cloud.legacymodel.user.Account) CloudAuthenticationException(com.cloud.legacymodel.exceptions.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.legacymodel.domain.Domain) UserAccount(com.cloud.legacymodel.user.UserAccount)

Example 14 with UserAccount

use of com.cloud.legacymodel.user.UserAccount in project cosmic by MissionCriticalCloud.

the class LdapCreateAccountCmd method execute.

@Override
public void execute() throws ServerApiException {
    final CallContext callContext = getCurrentContext();
    final String finalAccountName = getAccountName();
    final 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 (final NoLdapUserMatchingQueryException e) {
        throw new ServerApiException(ApiErrorCode.RESOURCE_UNAVAILABLE_ERROR, "No LDAP user exists with the username of " + username);
    }
}
Also used : NoLdapUserMatchingQueryException(com.cloud.ldap.NoLdapUserMatchingQueryException) LdapUser(com.cloud.ldap.LdapUser) ServerApiException(com.cloud.api.ServerApiException) AccountResponse(com.cloud.api.response.AccountResponse) CallContext(com.cloud.context.CallContext) UserAccount(com.cloud.legacymodel.user.UserAccount)

Example 15 with UserAccount

use of com.cloud.legacymodel.user.UserAccount in project cosmic by MissionCriticalCloud.

the class LdapImportUsersCmd method createCloudstackUserAccount.

private void createCloudstackUserAccount(final LdapUser user, final String accountName, final Domain domain) {
    final 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, accountType, domain.getId(), domain.getNetworkDomain(), details, UUID.randomUUID().toString(), UUID.randomUUID().toString(), User.Source.LDAP);
    } else {
        // check if the user exists. if yes, call update
        final 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.legacymodel.user.UserAccount) Account(com.cloud.legacymodel.user.Account) UserAccount(com.cloud.legacymodel.user.UserAccount)

Aggregations

UserAccount (com.cloud.legacymodel.user.UserAccount)21 Account (com.cloud.legacymodel.user.Account)10 InvalidParameterValueException (com.cloud.legacymodel.exceptions.InvalidParameterValueException)8 ServerApiException (com.cloud.api.ServerApiException)7 UserResponse (com.cloud.api.response.UserResponse)5 CloudRuntimeException (com.cloud.legacymodel.exceptions.CloudRuntimeException)5 User (com.cloud.legacymodel.user.User)5 ActionEvent (com.cloud.event.ActionEvent)4 Domain (com.cloud.legacymodel.domain.Domain)4 Pair (com.cloud.legacymodel.utils.Pair)4 CloudAuthenticationException (com.cloud.legacymodel.exceptions.CloudAuthenticationException)3 PermissionDeniedException (com.cloud.legacymodel.exceptions.PermissionDeniedException)3 VpnUserVO (com.cloud.network.VpnUserVO)3 AccountResponse (com.cloud.api.response.AccountResponse)2 DomainVO (com.cloud.domain.DomainVO)2 LdapUser (com.cloud.ldap.LdapUser)2 NoLdapUserMatchingQueryException (com.cloud.ldap.NoLdapUserMatchingQueryException)2 UserAuthenticator (com.cloud.server.auth.UserAuthenticator)2 DB (com.cloud.utils.db.DB)2 TransactionStatus (com.cloud.utils.db.TransactionStatus)2