Ejemplo n.º 1
0
        public async Task <IActionResult> AddUser([FromBody] UserViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(model));
                }

                var existingUser = _userRepository.GetUser(model.Email);
                if (existingUser != null)
                {
                    return(BadRequest(Errors.AddErrorToModelState("email", "Email address is already used.", ModelState)));
                }
                model.HashedPassword = _signInService.GetHashedPassword(model.HashedPassword);

                await _userRepository.AddUserAsync(model);

                var newUser = _userRepository.GetUser(model.Email);
                if (newUser != null)
                {
                    await _santasRepo.AddUserAsync(newUser.Id);
                }

                return(new JsonResult(new { id = newUser.Id }));
            }
            catch (System.Exception ex)
            {
                var result = Newtonsoft.Json.JsonConvert.SerializeObject(new { error = ex.Message });
                Response.ContentType = "application/json";
                Response.StatusCode  = (int)System.Net.HttpStatusCode.InternalServerError;
                return(new JsonResult(result));
            }
        }
        public IActionResult Post([FromBody] LoginViewModel model)
        {
            try
            {
                if (!ModelState.IsValid)
                {
                    return(BadRequest(ModelState));
                }
                var hashedPass = _signInService.GetHashedPassword(model.Password);

                var user = _userRepository.GetUser(model.Email);

                if (user == null)
                {
                    return(BadRequest(Errors.AddErrorToModelState("login_failure", "Invalid email or password.", ModelState)));
                }

                var passwordCheck = _signInService.CheckLoginInPassword(model.Password, user.HashedPassword);
                if (!passwordCheck)
                {
                    return(BadRequest(Errors.AddErrorToModelState("login_failure", "Invalid email or password.", ModelState)));
                }

                var claims = _identityResolver.GetIdentityCaims(user);
                var now    = DateTime.UtcNow;

                var jwt = new JwtSecurityToken(
                    issuer: _jwtOptions.Issuer,
                    audience: _jwtOptions.Audience,
                    claims: claims,
                    notBefore: now,
                    expires: now.Add(_jwtOptions.ValidFor),
                    signingCredentials: _jwtOptions.SigningCredentials);

                var encodedJwt = new JwtSecurityTokenHandler().WriteToken(jwt);

                var response = new
                {
                    access_token = encodedJwt,
                    expires_in   = (int)_jwtOptions.ValidFor.TotalSeconds,
                    admin        = user.IsAdmin,
                    username     = user.UserName,
                    id           = user.Id
                };

                var json = JsonConvert.SerializeObject(response);
                return(new OkObjectResult(json));
            }
            catch (Exception ex)
            {
                return(BadRequest(Errors.AddErrorToModelState("application_error", ex.Message, ModelState)));
            }
        }
Ejemplo n.º 3
0
        public async Task <int> UploadUsersFromCsvAsync(TextReader reader, string password)
        {
            var csvReader          = new CsvReader(reader);
            var records            = csvReader.GetRecords <CsvUser>();
            var hashedPassword     = _signInService.GetHashedPassword(password);
            var importedUsersCount = 0;
            var random             = new Random();

            foreach (var record in records)
            {
                var existingRecord = _userRepository.GetUser(record.Email);
                if (existingRecord != null)
                {
                    continue;
                }

                var user = new UserViewModel
                {
                    Email            = record.Email,
                    UserName         = record.UserName,
                    Age              = random.Next(100, 200),
                    IsAdmin          = false,
                    SecretSantaForId = null,
                    HashedPassword   = hashedPassword
                };
                await _userRepository.AddUserAsync(user);

                var newUser = _userRepository.GetUser(user.Email);
                if (newUser != null)
                {
                    await _secretSantasRepository.AddUserAsync(newUser.Id);
                }

                importedUsersCount++;
            }

            return(importedUsersCount);
        }