Example #1
0
        public async Task <IHttpActionResult> CreateUser(UserCreateRequestModel model)
        {
            if (string.IsNullOrWhiteSpace(model.Id))
            {
                model.Id = Guid.NewGuid().ToString();
            }
            model.UserName = model.Email;

            if (!string.IsNullOrWhiteSpace(model.UserName))
            {
                ModelState.Remove("model.UserName");
            }

            if (!ModelState.IsValid)
            {
                return(BadRequest(ModelState));
            }

            if (_service.IsEmailExist(model.Email))
            {
                return(BadRequest($"{model.Email} email address already exist."));
            }


            if (string.IsNullOrWhiteSpace(model.TenantId))
            {
                model.TenantId   = User.Identity.GetTenantId();
                model.TenantName = User.Identity.GetTenantName();
                model.CompanyId  = User.Identity.GetCompanyId();
            }
            else
            {
                var tenant = await _tenantProvider.GetTenantAsync(model.TenantId);

                var company = await _tenantProvider.GetTenantCompanyAsync(model.TenantId);

                model.TenantName = tenant.TenancyName;
                model.CompanyId  = company?.Id;
            }

            model.CreatedBy      = User.Identity.GetUserId();
            model.IsActive       = !model.SendActivationEmailToUser;
            model.EmailConfirmed = !model.SendActivationEmailToUser;

            var  featureUsers = _featureProvider.GetEditionFeatureValue(model.TenantId, StaticFeature.Users.Name);
            bool isReachedMaximumUsersCount =
                _service.IsReachedMaximumUsersCount(model.TenantId, Convert.ToInt32(featureUsers));

            if (isReachedMaximumUsersCount)
            {
                return(BadRequest("You already have added " + featureUsers + " users. You can not add more user with your current subscription."));
            }

            var identityResult = await _service.CreateUserAsync(model);

            if (model.SendActivationEmailToUser)
            {
                var user = await _service.GetUserAsync(model.Id);

                user.EmailConfirmationCode = await UserManager.GenerateEmailConfirmationTokenAsync(model.Id);

                user.EmailConfirmed = false;
                user.EmailConfirmationCodeExpireTime = DateTime.Now.AddMinutes(30);
                user.PhoneConfirmationCode           = await UserManager.GenerateChangePhoneNumberTokenAsync(model.Id, model.PhoneNumber);

                user.PhoneNumberConfirmed            = false;
                user.PhoneConfirmationCodeExpireTime = DateTime.Now.AddMinutes(30);

                await _service.UpdateUserAsync(user);

                await _service.SendEmailConfirmationLinkAsync(model.Id, model.FullName(), model.Email, user.EmailConfirmationCode);
            }

            return(Ok(identityResult));
        }