コード例 #1
0
        public UserViewModel Map(User source, UserViewModel destination)
        {
            if (source == null)
            {
                return null;
            }

            if (destination == null)
            {
                destination = new UserViewModel();
            }
            destination.Id = source.Id;
            destination.UserName = source.UserName;
            destination.FirstName = source.FirstName;
            destination.LastName = source.LastName;
            destination.Password = source.Password;
            destination.Email = source.Email;
            destination.Country = source.Country;
            destination.City = source.City;
            destination.StreetRow = source.StreetRow;
            destination.Phone = source.Phone;

            foreach (var role in source.Roles)
            {
                destination.IsAdmin = role.Id == (int)UserRoleEnum.Admin;
                destination.IsWorker = role.Id == (int)UserRoleEnum.Worker;
            }

            return destination;

        }
コード例 #2
0
        public User Map(UserViewModel source, User destination)
        {
            if (source == null)
            {
                return null;
            }

            if (destination == null)
            {
                destination = new User();
            }

            destination.FirstName = source.FirstName;
            destination.LastName = source.LastName;
            destination.Email = source.Email;
            destination.Country = source.Country;
            destination.City = source.City;
            destination.StreetRow = source.StreetRow;
            destination.Phone = source.Phone;

            if (destination.Roles == null)
            {
                destination.Roles = new List<Role>();
            }

            return destination;
        }
コード例 #3
0
        public virtual ActionResult Edit(UserViewModel model)
        {
            if (model == null || !ModelState.IsValid)
            {
                return View(model);
            }

            try
            {
                var currentUser = userService.GetUserById(model.Id);

                if (currentUser == null)
                {
                    return RedirectToAction(MVC.User.List());
                }

                currentUser = ViewModelToEntityMapper.Mapper.Map(model, currentUser);

                var roles = userService.GetAllRoles();

                currentUser.Roles = new List<Role>();
                
                if (roles != null && roles.Count > 0)
                {
                    if (model.IsAdmin)
                    {
                        currentUser.Roles.Add(roles.Find(x => x.Id == (int)UserRoleEnum.Admin));
                    }
                    if (model.IsWorker)
                    {
                        currentUser.Roles.Add(roles.Find(x => x.Id == (int)UserRoleEnum.Worker));
                    }
                }


                if (!string.IsNullOrEmpty(model.Password))
                {
                    if (model.Password != model.PasswordConfirmation)
                    {
                        ModelState.AddModelError("Password", " Nesutampa įvesti slaptažodžiai.");
                        return View(model);
                    }
                    else
                    {
                        currentUser.PasswordSalt = cryptoService.GetSalt();
                        currentUser.Password = cryptoService.GetHashedValue(model.Password, currentUser.PasswordSalt);
                    }
                }

                var user = userService.SaveUser(currentUser);


                if (user != null)
                {
                    ModelState.Clear();
                    var viewModel = EntityToViewModelMapper.Mapper.Map(user, new UserViewModel());
                    viewModel.Message = new MessageViewModel
                        {
                            IsError = false,
                            Message = "Vartotojo informacija atnaujinta"
                        };
                    return View(viewModel);
                }

            }
            catch (Exception ex)
            {
                model.Message = new MessageViewModel
                    {
                        IsError = true,
                        Message = "Išsaugant įvyko klaida, bandykite dar kartą"
                    };
                return View(model);
            }

            return RedirectToAction(MVC.User.List());

        }