Beispiel #1
0
        public IActionResult EditRole(RoleViewModel model)
        {
            if (!ModelState.IsValid)
            {
                return(View(_userRoleView, model));
            }

            try {
                var role = _userRolesService.GetUserRoleById(model.RoleId);

                if (role.SystemKeyword != model.SystemKeyword)
                {
                    // User attempted to change system keyword for the role. Run necessary verifications
                    var existingRole = _userRolesService.GetUserRoleByKeyword(model.SystemKeyword);
                    if (existingRole != null)
                    {
                        model.Title = "Edit Role";
                        ModelState.AddModelError("", "System Keyword is not available, please try something else.");
                        return(View(_userRoleView, model));
                    }

                    role.DisplayName   = model.DisplayName;
                    role.SystemKeyword = model.SystemKeyword;

                    model.Title     = "Edit Role";
                    ViewBag.Status  = "OK";
                    ViewBag.Message = "Your changes have been saved successfully.";

                    _userRolesService.UpdateUserRole(role);
                    _activityService.InsertActivity(_workContext.CurrentUser, ActivityLogDefaults.EditRoles, $"Edited user role: {role.SystemKeyword}");
                }
            }
            catch (Exception ex) {
                ModelState.AddModelError("", $"Unable to save changes to role: {model.SystemKeyword}");
                _logFactory.InsertLog(LogLevel.Error, $"Unable to save changes to role: {model.SystemKeyword} because {ex.Message}", ex.ToString());
            }

            return(View(_userRoleView, model));
        }