Search in sources :

Example 1 with ActionEvents

use of com.cloud.event.ActionEvents in project cloudstack by apache.

the class AccountManagerImpl method createUserAccount.

// ///////////////////////////////////////////////////
// ////////////// API commands /////////////////////
// ///////////////////////////////////////////////////
@Override
@DB
@ActionEvents({ @ActionEvent(eventType = EventTypes.EVENT_ACCOUNT_CREATE, eventDescription = "creating Account"), @ActionEvent(eventType = EventTypes.EVENT_USER_CREATE, eventDescription = "creating User") })
public UserAccount createUserAccount(final String userName, final String password, final String firstName, final String lastName, final String email, final String timezone, String accountName, final short accountType, final Long roleId, Long domainId, final String networkDomain, final Map<String, String> details, String accountUUID, final String userUUID, final User.Source source) {
    if (accountName == null) {
        accountName = userName;
    }
    if (domainId == null) {
        domainId = Domain.ROOT_DOMAIN;
    }
    if (StringUtils.isEmpty(userName)) {
        throw new InvalidParameterValueException("Username is empty");
    }
    if (StringUtils.isEmpty(firstName)) {
        throw new InvalidParameterValueException("Firstname is empty");
    }
    if (StringUtils.isEmpty(lastName)) {
        throw new InvalidParameterValueException("Lastname is empty");
    }
    // Validate domain
    Domain domain = _domainMgr.getDomain(domainId);
    if (domain == null) {
        throw new InvalidParameterValueException("The domain " + domainId + " does not exist; unable to create account");
    }
    // Check permissions
    checkAccess(CallContext.current().getCallingAccount(), domain);
    if (!_userAccountDao.validateUsernameInDomain(userName, domainId)) {
        throw new InvalidParameterValueException("The user " + userName + " already exists in domain " + domainId);
    }
    if (networkDomain != null && networkDomain.length() > 0) {
        if (!NetUtils.verifyDomainName(networkDomain)) {
            throw new InvalidParameterValueException("Invalid network domain. Total length shouldn't exceed 190 chars. Each domain label must be between 1 and 63 characters long, can contain ASCII letters 'a' through 'z', the digits '0' through '9', " + "and the hyphen ('-'); can't start or end with \"-\"");
        }
    }
    final String accountNameFinal = accountName;
    final Long domainIdFinal = domainId;
    final String accountUUIDFinal = accountUUID;
    Pair<Long, Account> pair = Transaction.execute(new TransactionCallback<Pair<Long, Account>>() {

        @Override
        public Pair<Long, Account> doInTransaction(TransactionStatus status) {
            // create account
            String accountUUID = accountUUIDFinal;
            if (accountUUID == null) {
                accountUUID = UUID.randomUUID().toString();
            }
            AccountVO account = createAccount(accountNameFinal, accountType, roleId, domainIdFinal, networkDomain, details, accountUUID);
            long accountId = account.getId();
            // create the first user for the account
            UserVO user = createUser(accountId, userName, password, firstName, lastName, email, timezone, userUUID, source);
            if (accountType == Account.ACCOUNT_TYPE_RESOURCE_DOMAIN_ADMIN) {
                // set registration token
                byte[] bytes = (domainIdFinal + accountNameFinal + userName + System.currentTimeMillis()).getBytes();
                String registrationToken = UUID.nameUUIDFromBytes(bytes).toString();
                user.setRegistrationToken(registrationToken);
            }
            return new Pair<Long, Account>(user.getId(), account);
        }
    });
    long userId = pair.first();
    Account account = pair.second();
    // create correct account and group association based on accountType
    if (accountType != Account.ACCOUNT_TYPE_PROJECT) {
        Map<Long, Long> accountGroupMap = new HashMap<Long, Long>();
        accountGroupMap.put(account.getId(), new Long(accountType + 1));
        _messageBus.publish(_name, MESSAGE_ADD_ACCOUNT_EVENT, PublishScope.LOCAL, accountGroupMap);
    }
    CallContext.current().putContextParameter(Account.class, account.getUuid());
    // check success
    return _userAccountDao.findById(userId);
}
Also used : HashMap(java.util.HashMap) TransactionStatus(com.cloud.utils.db.TransactionStatus) VpnUserVO(com.cloud.network.VpnUserVO) InvalidParameterValueException(com.cloud.exception.InvalidParameterValueException) Domain(com.cloud.domain.Domain) Pair(com.cloud.utils.Pair) DB(com.cloud.utils.db.DB) ActionEvents(com.cloud.event.ActionEvents)

Aggregations

Domain (com.cloud.domain.Domain)1 ActionEvents (com.cloud.event.ActionEvents)1 InvalidParameterValueException (com.cloud.exception.InvalidParameterValueException)1 VpnUserVO (com.cloud.network.VpnUserVO)1 Pair (com.cloud.utils.Pair)1 DB (com.cloud.utils.db.DB)1 TransactionStatus (com.cloud.utils.db.TransactionStatus)1 HashMap (java.util.HashMap)1