/// <summary>
        /// Populate the role selection drop down lists for picking the roles to view permissions for
        /// </summary>
        /// <param name="model"></param>
        protected void PopulateDropDownLists(GetEffectivePermissionsForSecurableViewModel model)
        {
            var request = new SecurityAccessMaintenanceGetRolesRequest()
            {
                SelectedRoleIds = model.SelectedRoleIds
            };
            var getRolesResponse = _service.GetRoles(request);

            model.AvailableRoles = getRolesResponse.AvailableRoles;
            model.SelectedRoles  = getRolesResponse.SelectedRoles;
        }
        public virtual ActionResult GetEffectivePermissionsForSecurable(int?securitySecurableId, List <int> selectedRoleIds = null)
        {
            //
            //Create empty view model
            //
            var model = new GetEffectivePermissionsForSecurableViewModel()
            {
                SecuritySecurableId = securitySecurableId,
                SelectedRoleIds     = selectedRoleIds
            };

            //
            //Populate the role selection lists
            //
            PopulateDropDownLists(model);

            //
            //Call the service to get the effective permissions for the selected roles
            //
            var request = new SecurityAccessMaintenanceGetEffectivePermissionsForSecurableRequest()
            {
                SecuritySecurableId = securitySecurableId,
                SelectedRoleIds     = model.SelectedRoleIds
            };

            var response = _service.GetEffectivePermissionsForSecurable(request);

            if (response.IsSuccessful)
            {
                model.EffectivePermissionsForSecurable = response;
                model.IsSuccessful = true;
            }
            else
            {
                model.Message      = response.Message;
                model.IsSuccessful = false;
            }
            return(PartialView("_EffectivePermissionsPartial", model));
        }
        public virtual ActionResult GetEffectivePermissionsForSecurable(GetEffectivePermissionsForSecurableViewModel model)
        {
            //
            //Populate the role selection lists
            //
            PopulateDropDownLists(model);

            //
            //Save any permission changes that came in
            //
            if (model.SubmitAction == SubmitActions.Save.ToString() &&
                model.SecurityAccessSettings != null &&
                model.SecurityAccessSettings.Count > 0)
            {
                var permissionsToSave = new List <PermissionToSave>();
                //
                //The permissions get submitted in a dictionary.
                //The key is security securable action ID + "~" + security role Id
                //The value is Allowed, NotAllowed, or Inherited
                //
                foreach (var key in model.SecurityAccessSettings.Keys)
                {
                    //
                    //Break the key into its components: securitySecurableActionId and roleId
                    //Create the record of the permission to save for the service layer
                    //
                    var value       = model.SecurityAccessSettings[key];
                    var keyElements = key.Split('~');
                    if (keyElements != null && keyElements.Count() == 2)
                    {
                        var securitySecurableActionId = int.Parse(keyElements[0]);
                        var roleId = int.Parse(keyElements[1]);

                        var permissionToSave = new PermissionToSave()
                        {
                            SecurityRoleId            = roleId,
                            SecuritySecurableActionId = securitySecurableActionId,
                            PermissionValue           = value
                        };
                        permissionsToSave.Add(permissionToSave);
                    }
                }

                //
                //Call the service layer to persist the permissions
                //
                var saveRequest = new SecurityAccessMaintenanceSavePermissionsRequest()
                {
                    SecuritySecurableId = model.SecuritySecurableId,
                    PermissionsToSave   = permissionsToSave
                };

                var saveResponse = _service.SavePermissions(saveRequest);
                if (!saveResponse.IsSuccessful)
                {
                    ModelState.AddModelError("", saveResponse.Message);
                }
                else if (saveResponse.NumPermissionsUpdated > 0)
                {
                    AddToastMessage(new ToastMessage()
                    {
                        Message  = saveResponse.Message,
                        Type     = ToastMessage.ToastType.Success,
                        AutoHide = true,
                        Position = ToastMessage.ToastPosition.TopCenter
                    });
                }
            }

            //
            //Call the service to get the effective permissions for the selected roles
            //
            var request = new SecurityAccessMaintenanceGetEffectivePermissionsForSecurableRequest()
            {
                SecuritySecurableId = model.SecuritySecurableId,
                SelectedRoleIds     = model.SelectedRoleIds
            };

            var response = _service.GetEffectivePermissionsForSecurable(request);

            if (response.IsSuccessful)
            {
                model.EffectivePermissionsForSecurable = response;
                model.IsSuccessful = true;
            }
            else
            {
                model.Message      = response.Message;
                model.IsSuccessful = false;
            }
            return(PartialView("_EffectivePermissionsPartial", model));
        }