Esempio n. 1
0
        public async Task <UserEntity> AddUserAsync(FacebookProfile model)
        {
            UserEntity userEntity = new UserEntity
            {
                Document    = "...",
                Email       = model.Email,
                FirstName   = model.FirstName,
                LastName    = model.LastName,
                PicturePath = model.Picture?.Data?.Url,
                PhoneNumber = "...",
                UserName    = model.Email,
                UserType    = UserType.CallAdviser,
                LoginType   = LoginType.Facebook,
                UserCode    = GetUserCode(model.FirstName, model.LastName),
                Campaign    = await _campaignHelper.GetCampaign(1)
            };

            IdentityResult result = await _userManager.CreateAsync(userEntity, model.Id);

            if (result != IdentityResult.Success)
            {
                return(null);
            }

            UserEntity newUser = await GetUserAsync(model.Email);

            await AddUserToRoleAsync(newUser, userEntity.UserType.ToString());

            return(newUser);
        }
        public async Task <IActionResult> AddUser([FromBody] UserRequest request)
        {
            if (!ModelState.IsValid)
            {
                return(BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "Bad request",
                    Result = ModelState
                }));
            }

            UserEntity user = await _userHelper.GetUserAsync(request.Email);

            if (user != null)
            {
                return(BadRequest(new Response
                {
                    IsSuccess = false,
                    Message = "User already exists"
                }));
            }

            string picturePath = string.Empty;

            if (request.PictureArray != null && request.PictureArray.Length > 0)
            {
                picturePath = _imageHelper.UploadImage(request.PictureArray, "Users");
            }

            user = new UserEntity
            {
                Email       = request.Email,
                UserCode    = _userHelper.GetUserCode(request.FirstName, request.LastName),
                Document    = request.Document,
                FirstName   = request.FirstName,
                LastName    = request.LastName,
                PhoneNumber = request.Phone,
                UserName    = request.Email,
                PicturePath = picturePath,
                UserType    = request.UserTypeId == 1 ? UserType.Supervisor : UserType.CallAdviser,
                Campaign    = await _campaignHelper.GetCampaign(request.CampaignId)
            };

            IdentityResult result = await _userHelper.AddUserAsync(user, request.Password);

            if (result != IdentityResult.Success)
            {
                return(BadRequest(result.Errors.FirstOrDefault().Description));
            }

            UserEntity userNew = await _userHelper.GetUserAsync(request.Email);

            await _userHelper.AddUserToRoleAsync(userNew, user.UserType.ToString());

            string myToken = await _userHelper.GenerateEmailConfirmationTokenAsync(user);

            string tokenLink = Url.Action("ConfirmEmail", "Account", new
            {
                userid = user.Id,
                token  = myToken
            }, protocol: HttpContext.Request.Scheme);

            _mailHelper.SendMail(request.Email, "Email Confirmation", $"<h1>Email Confirmation</h1>" +
                                 $"</br>To allow the user, please click on this link:</br><a href = \"{tokenLink}\">Confirm Email</a>");

            return(Ok(new Response
            {
                IsSuccess = true,
                Message = "A confirmation email was sent. Please confirm your account and log into the App."
            }));
        }