Beispiel #1
0
        public async Task <IActionResult> Edit([Bind("UserEntity, Roles")] UsersCreateUpdateViewModel model)
        {
            if (!UserIsAdmin())
            {
                return(Error("You need to be logged in as admin to do this."));
            }

            if (!ModelState.IsValid)
            {
                model.RoleDropDown = RoleDefaults();
                return(View(model));
            }

            if (model.UserEntity.Password != model.UserEntity.ConfirmPassword)
            {
                model.RoleDropDown   = RoleDefaults();
                ViewBag.errorMessage = "Password and Confirm Password fields must match!";
                return(View(model));
            }

            ActionObject _actionObject = await _accountActions.EditPost(model, User.Identity.Name);

            if (!_actionObject.Success)
            {
                return(Error(_actionObject.Message));
            }

            ViewBag.successMessage = "User details have been modified.";
            return(RedirectToAction("Index"));
        }
Beispiel #2
0
 private void BuildUserEntity(UsersCreateUpdateViewModel model, ref User user)
 {
     user.UserId          = model.UserEntity.UserId;
     user.UserName        = model.UserEntity.UserName;
     user.Password        = model.UserEntity.Password;
     user.ConfirmPassword = model.UserEntity.ConfirmPassword;
     user.IsAdmin         = model.UserEntity.IsAdmin;
 }
Beispiel #3
0
        public IActionResult Create()
        {
            //TODO restrict to admin
            UsersCreateUpdateViewModel model = new UsersCreateUpdateViewModel();

            model.RoleDropDown = RoleDefaults();

            model.Roles = Enumerable.Range(0, model.RoleDropDown.Count).Select(x => 0).ToList();

            if (UserIsAdmin())
            {
                return(View(model));
            }
            else
            {
                return(Error("You need to be logged in as admin to do this."));
            }
        }
Beispiel #4
0
        public async Task <ActionObject> EditPost(UsersCreateUpdateViewModel model, string currentUserName)
        {
            ActionObject _actionObject = new ActionObject();

            if (!_unitOfWork.UserRepository.UserExists(model.UserEntity.UserId))
            {
                _actionObject.Success = false;
                _actionObject.Message = "User with user id " + model.UserEntity.UserId.ToString() + " doesn't exist. Please refresh the page.";
                return(_actionObject);
            }

            User user = _unitOfWork.UserRepository.GetUserAndRolesById(model.UserEntity.UserId);

            BuildUserEntity(model, ref user);


            List <int> roleList = model.Roles.Where(x => x != 0).Distinct().ToList();

            if (user.UserRoles.Count > 0)
            {
                _unitOfWork.UserRoleRepository.RemoveRange(user.UserRoles);
                await _unitOfWork.CompleteAsync();
            }

            if (roleList.Count > 0)
            {
                user.UserRoles = CreateUserRoles(user, roleList);
            }


            user.Password = Hash(user.Password);
            _unitOfWork.Update(user);

            _unitOfWork.LogRepository.CreateLog(
                currentUserName,
                "User edited  with user name: [" + user.UserName + "].",
                DateTime.Now,
                null);
            await _unitOfWork.CompleteAsync();

            return(_actionObject);
        }
Beispiel #5
0
        public CreateObject CreatePost(string currentUserName, UsersCreateUpdateViewModel createViewModel)
        {
            using (_unitOfWork)
            {
                CreateObject _actionObject = new CreateObject();
                _actionObject.UserIsAdmin = UserIsAdmin(currentUserName);
                if (!_actionObject.UserIsAdmin)
                {
                    return(_actionObject);
                }

                if (_unitOfWork.UserRepository.GetUserByName(createViewModel.UserEntity.UserName) != null)
                {
                    _actionObject.UserCreated = false;
                    _actionObject.Message     = "A user with the same user name already exists!";
                    return(_actionObject);
                }

                var  roleList = createViewModel.Roles.Where(x => x != null)?.Distinct()?.ToList();
                User user     = new User();
                BuildUserEntity(createViewModel, ref user);

                if (roleList.Count > 0)
                {
                    user.UserRoles = CreateUserRoles(user, roleList);
                }
                user.Password = Hash(user.Password);
                _unitOfWork.UserRepository.Add(user);

                _unitOfWork.LogRepository.CreateLog(
                    currentUserName,
                    "Created a user. With user name: [" + user.UserName + "].",
                    DateTime.Now,
                    null);
                _unitOfWork.Complete();

                _actionObject.UserCreated = true;
                return(_actionObject);
            }
        }
Beispiel #6
0
        public async Task <IActionResult> Create(
            [Bind("UserEntity, Roles")] UsersCreateUpdateViewModel model)
        {
            if (!ModelState.IsValid)
            {
                model.RoleDropDown = RoleDefaults();
                return(View(model));
            }
            if (model.UserEntity.Password != model.UserEntity.ConfirmPassword)
            {
                model.RoleDropDown   = RoleDefaults();
                ViewBag.errorMessage = "Password and Confirm Password fields must match!";
                return(View(model));
            }

            string       _userName     = HttpContext.User.Identity.Name;
            CreateObject _actionObject = _accountActions.CreatePost(_userName, model);

            if (!_actionObject.UserIsAdmin)
            {
                return(Error("You need to be logged in as admin to do this."));
            }
            else if (!_actionObject.UserCreated)
            {
                model.RoleDropDown   = RoleDefaults();
                ViewBag.errorMessage = _actionObject.Message;
                return(View(model));
            }
            if (_actionObject.UserCreated)
            {
                TempData["success"] = "User created.";
                return(RedirectToAction("Index"));
            }
            else
            {
                TempData["error"] = "Oops, something went wrong, try again.";
                return(RedirectToAction("Index"));
            }
        }
Beispiel #7
0
        public IActionResult Edit(int?id)
        {
            if (!UserIsAdmin())
            {
                return(Error("You have to be logged in as admin to do this."));
            }

            if (id == null)
            {
                return(NotFound());
            }
            int  idNotNull = (int)id;
            User user      = _accountActions.EditGet(idNotNull);

            if (user == null)
            {
                return(Error("User with id " + id.ToString() + " doesn't exist."));
            }

            UsersCreateUpdateViewModel model = BuildModelFromUser(user);

            return(View(model));
        }