public ActionResult Edit(int id, UserEntityViewModel entityViewModel)
 {
     var currentUser = _authenticationService.GetCurrentUser();
     if (currentUser.UserLevel == (int)UserLevels.Administrator || currentUser.UserID == id)
     {
         try
         {
             _userViewService.SaveUserViewModel(id, entityViewModel, EditModes.Update);
             return RedirectToAction("Index", "Administration");
         }
         catch (ValidationException)
         {
             ModelState.AddModelErrors(_userViewService.GetUserViewModelErrors(EditModes.Update));
             return View(entityViewModel);
         }
         catch (EntityException ex)
         {
             ViewData["Error"] = ex.Message;
             return RedirectToAction("Index", "Administration");
         }
     }
     else
     {
         Error = "You cannot edit this user.";
         return HomePage();
     }
 }
Ejemplo n.º 2
0
        public void MapToModel(UserEntityViewModel entityViewModel, User model, bool passwordChanged)
        {
            model.FirstName = entityViewModel.Firstname.CapFirstLetter();
            model.LastName = entityViewModel.Lastname.CapFirstLetter();
            model.Email = entityViewModel.Email.ToLower();
            model.Username = entityViewModel.Username.ToLower();
            model.UserLevel = entityViewModel.UserLevel;

            if (passwordChanged)
            {
                model.PasswordSalt = _cryptoService.CreateSalt();
                model.PasswordHash = _cryptoService.GetPasswordHash(entityViewModel.Password, model.PasswordSalt);
            }
        }
Ejemplo n.º 3
0
        public UserEntityViewModel MapToViewModel(User model)
        {
            var viewModel = new UserEntityViewModel();
            if (model != null)
            {
                viewModel.Id = model.UserID;
                viewModel.Email = model.Email;
                viewModel.Firstname = model.FirstName;
                viewModel.Lastname = model.LastName;
                viewModel.UserLevel = model.UserLevel;
                viewModel.Username = model.Username;
            }

            return viewModel;
        }
 public ActionResult New(TeamEntityViewModel teamEntityViewModel, UserEntityViewModel userEntityViewModel)
 {
     try
     {
         teamEntityViewModel.ManagerEntityViewModel = userEntityViewModel;
         _teamViewService.SaveTeamViewModel(teamEntityViewModel, EditModes.Insert);
         return RedirectToAction("Index", "Administration");
     }
     catch(ValidationException)
     {
         _teamViewService.FillModelStateErrors(ModelState, teamEntityViewModel, EditModes.Insert);
         _teamViewService.InitializeViewModel(teamEntityViewModel);
         return View(teamEntityViewModel);
     }
     catch(EntityException x)
     {
         Error = x.Message;
         return RedirectToAction("Index", "Administration");
     }
 }
 public void SaveUserViewModel(int? userId, UserEntityViewModel userEntityViewModel, EditModes editMode)
 {
     _userValidator.EntityViewModel = userEntityViewModel;
     bool passwordChanged = !String.IsNullOrEmpty(userEntityViewModel.Password);
     bool isValid = false;
     switch (editMode)
     {
         case EditModes.Insert:
             isValid = _userValidator.IsInsertValid();
             break;
         case EditModes.Update:
             isValid = _userValidator.IsUpdateValid(passwordChanged);
             break;
     }
     if (isValid)
     {
         User user = null;
         if (userId == null) user = new User();
         else user = _userRepository.GetByID((int) userId);
         if(user == null) throw new EntityException("User does not exist.");
         _userMapper.MapToModel(userEntityViewModel, user, passwordChanged);
         switch (userEntityViewModel.UserInsertScenario)
         {
             case UserInsertScenario.WorkshopMember:
                 _userService.AddToWorkshop((int)userEntityViewModel.OrganizationID, user);
                 break;
             case UserInsertScenario.WorkshopManager:
                 _userService.ChangeWorkshopManager((int)userEntityViewModel.OrganizationID, user);
                 break;
             case UserInsertScenario.TeamManager:
                 _userService.ChangeTeamManager((int)userEntityViewModel.OrganizationID, user);
                 break;
             case UserInsertScenario.Manager:
                 _userService.AddToManagementTeam(user);
                 break;
             case UserInsertScenario.Director:
                 _userService.ChangeDirector(user);
                 break;
             case UserInsertScenario.Unassigned:
             default:
                 _userService.SaveUser(user);
                 break;
         }
     }
     else
     {
         throw new ValidationException();
     }
 }
Ejemplo n.º 6
0
 public void MapToModel(UserEntityViewModel entityViewModel, User model)
 {
     MapToModel(entityViewModel, model, true);
 }
 public ActionResult New(UserEntityViewModel userEntityViewModel)
 {
     try
     {
         _userViewService.SaveUserViewModel(null, userEntityViewModel, EditModes.Insert);
         return RedirectToAction("Index", "Administration");
     }
     catch (ValidationException)
     {
         ModelState.AddModelErrors(_userViewService.GetUserViewModelErrors(EditModes.Insert));
         return View(userEntityViewModel);
     }
 }