Esempio n. 1
0
        public async Task <object> AddEdit([FromBody] CreateUserViewModel model)
        {
            var apiUserId = User.ApiGetUserId();

            model.ProcessedById = apiUserId;

            if (ModelState.IsValid)
            {
                var getUser = await _userManager.FindByEmailAsync(model.Email);

                if (getUser != null && getUser.Id != model.Id)
                {
                    ModelState.AddModelError("", _localizer["UserExistWithSameEmail"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }


                var isAdmin = await _userService.UserInRole(apiUserId, "Admin");

                if (!isAdmin && apiUserId != model.Id)
                {
                    ModelState.AddModelError("", _localizer["AdminUserError"]);
                    return(BadRequest(Errors.GetErrorList(ModelState)));
                }


                if (model.Id != null)
                {
                    var user = await _userViewModelService.EditUser(model, isAdmin);

                    return(Ok(await _userViewModelService.ToViewModel(user)));
                }

                if (isAdmin)
                {
                    model.CreatedAt = DateTime.UtcNow;
                    var user = await _userViewModelService.CreateUser(model);

                    await SendGeneratePasswordLink(user);

                    return(Ok(await _userViewModelService.ToViewModel(user)));
                }
            }

            return(BadRequest(Errors.GetErrorList(ModelState)));
        }
        public async Task Should_Update_User()
        {
            var model = new CreateUserViewModel
            {
                Id        = 1.ToString(),
                Email     = "*****@*****.**",
                FirstName = "test",
                LastName  = "Test"
            };

            _context.Users.Add(_userManager.Object.Users.ToList()[0]);
            _context.SaveChanges();
            await _userApiViewModelService.EditUser(model, true);

            var user = _userManager.Object.Users.FirstOrDefault(u => u.Id == "1");

            Assert.Equal(model.Email, user?.Email);
            Assert.Equal(model.FirstName, user?.FirstName);
            Assert.Equal(model.LastName, user?.LastName);
        }