public ActionResult Add()
        {
            if (!_permissionService.Authorize(PermissionProvider.ManageUserRoles))
                return AccessDeniedView();

            var model = new UserRoleModel
            {
                AvailablePermissionRecords = _permissionService.GetAllPermissionRecords().Select(p => p.ToModel()).ToList()
            };

            PrepareBreadcrumbs();
            AddBreadcrumb("Add New Role", null);

            return View(model);
        }
        public ActionResult Add(UserRoleModel model, FormCollection form)
        {
            if(!_permissionService.Authorize(PermissionProvider.ManageUserRoles))
                return AccessDeniedView();

            if(ModelState.IsValid)
            {
                try
                {
                    var role = model.ToEntity();
                    role.CreatedBy = _workContext.CurrentUser.Id;
                    role.LastModifiedBy = _workContext.CurrentUser.Id;
                    role.LastModifiedDate = DateTime.Now;

                    var allowedPermissionRecords = form["allow"] != null ? form["allow"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList() : new List<string>();
                    var permissions = _permissionService.GetAllPermissionRecords();

                    foreach (var permissionSystemName in allowedPermissionRecords)
                        role.PermissionRecords.Add(permissions.First(x => x.SystemName == permissionSystemName));

                    _userService.InsertUserRole(role);

                    SuccessNotification("The user role has been inserted successfully.");
                    return RedirectToAction("Index");
                }
                catch (Exception)
                {
                    ErrorNotification("An error occurred saving the user role, please try again.");
                }
            }

            PrepareBreadcrumbs();
            AddBreadcrumb("Add New Role", null);

            return View(model);
        }
        public ActionResult Block(UserRoleModel model)
        {
            if (!_permissionService.Authorize(PermissionProvider.ManageUserRoles))
                return AccessDeniedView();

            // get the role
            var role = _userService.GetUserRoleById(model.Id);

            // check we have a role and it's not deleted
            if (role == null || role.Deleted)
                return RedirectToAction("Index");

            try
            {
                role.Active = !role.Active;
                _userService.UpdateUserRole(role);

                SuccessNotification("The role has been " + (role.Active ? "shown" : "hidden") + " successfully.");
                return RedirectToAction("Edit", role.Id);
            }
            catch (Exception)
            {
                ErrorNotification("An error occurred " + (role.Active ? "showing" : "hiding") + " the role, please try again.");
            }

            PrepareBreadcrumbs();
            AddBreadcrumb("Edit Role", null);

            model = PrepareUserRoleModel(role);
            return View(model);
        }
        public ActionResult Edit(UserRoleModel model, FormCollection form)
        {
            if (!_permissionService.Authorize(PermissionProvider.ManageUserRoles))
                return AccessDeniedView();

            // get the role
            var role = _userService.GetUserRoleById(model.Id);

            // check we have a role and it's not deleted
            if(role == null || role.Deleted)
                return RedirectToAction("Index");

            if(ModelState.IsValid)
            {
                try
                {
                    role.Name = model.Name;
                    role.SystemName = model.SystemName;

                    var allowedPermissionRecords = form["allow"] != null ? form["allow"].Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList() : new List<string>();
                    var availablePermissions = _permissionService.GetAllPermissionRecords();

                    role.PermissionRecords.Clear();
                    foreach(var permissionSystemName in allowedPermissionRecords)
                        role.PermissionRecords.Add(availablePermissions.First(x => x.SystemName == permissionSystemName));

                    _userService.UpdateUserRole(role);

                    SuccessNotification("The role has been updated successfully.");
                    return RedirectToAction("Edit", role.Id);
                }
                catch (Exception)
                {
                    ErrorNotification("An error occurred saving the role, please try again.");
                }
            }
            else
            {
                ErrorNotification("We were unable to make the change, please review the form and correct the errors.");
            }

            PrepareBreadcrumbs();
            AddBreadcrumb("Edit Role", null);

            model = PrepareUserRoleModel(role);
            return View(model);
        }
        public static UserRoleModel ToModel(this UserRole entity)
        {
            if (entity == null)
                return null;

            var model = new UserRoleModel
            {
                Active = entity.Active,
                CreatedDate = entity.CreatedDate,
                Id = entity.Id,
                IsSiteOwnerRole = (entity.SystemName == SystemUserRoleNames.SiteOwner),
                IsSystemRole = entity.IsSystemRole,
                LastModifiedDate = entity.LastModifiedDate,
                Name = entity.Name,
                SystemName = entity.SystemName,
                PermissionRecords = entity.PermissionRecords.Select(ToModel).ToList()
            };

            return model;
        }