/// <summary>
        /// Install permissions
        /// </summary>
        /// <param name="permissionProvider">Permission provider</param>
        public virtual void InstallPermissions(IPermissionProvider permissionProvider)
        {
            //install new permissions
            var permissions = permissionProvider.GetPermissions();
            //default customer role mappings
            var defaultPermissions = permissionProvider.GetDefaultPermissions().ToList();

            foreach (var permission in permissions)
            {
                var permission1 = GetPermissionRecordBySystemName(permission.SystemName);
                if (permission1 != null)
                {
                    continue;
                }

                //new permission (install it)
                permission1 = new PermissionRecord
                {
                    Name       = permission.Name,
                    SystemName = permission.SystemName,
                    Category   = permission.Category,
                };

                foreach (var defaultPermission in defaultPermissions)
                {
                    var customerRole = _customerService.GetCustomerRoleBySystemName(defaultPermission.CustomerRoleSystemName);
                    if (customerRole == null)
                    {
                        //new role (save it)
                        customerRole = new CustomerRole
                        {
                            Name       = defaultPermission.CustomerRoleSystemName,
                            Active     = true,
                            SystemName = defaultPermission.CustomerRoleSystemName
                        };
                        _customerService.InsertCustomerRole(customerRole);
                    }

                    var defaultMappingProvided = (from p in defaultPermission.PermissionRecords
                                                  where p.SystemName == permission1.SystemName
                                                  select p).Any();
                    var mappingExists = (from p in customerRole.PermissionRecord_Role_Mapping
                                         where p.CustomerRole.SystemName == permission1.SystemName
                                         select p).Any();
                    if (defaultMappingProvided && !mappingExists)
                    {
                        PermissionRecord_Role_Mapping permissionRecord_Role_Mapping = new PermissionRecord_Role_Mapping()
                        {
                            PermissionRecordId = permission1.Id,
                            CustomerRoleId     = customerRole.Id
                        };
                        InsertPermissionRecordRoleMapping(permissionRecord_Role_Mapping);
                    }
                }

                //save new permission
                InsertPermissionRecord(permission1);
            }
        }
        public virtual void InsertPermissionRecordRoleMapping(PermissionRecord_Role_Mapping permissionRoleMappingProvider)
        {
            if (permissionRoleMappingProvider == null)
            {
                throw new ArgumentNullException(nameof(permissionRoleMappingProvider));
            }

            _permissionRecord_Role_Mapping.Insert(permissionRoleMappingProvider);
        }
        public virtual IActionResult PermissionsSave(IFormCollection form)
        {//PageSize
            //var report = SharedData.PermissionReport;
            //if (report != null)
            //{
            //    SharedData.RowCount = report.RowCount;
            //    SharedData.ReportName = "Permission";
            //}
            //else
            //{
            //    SharedData.RowCount = 10;
            //    SharedData.ReportName = "Permission";
            //}
            //Form Name
            ViewBag.FormName = "Permissions";
            //if (SharedData.isManageAclMenuAccessible == false)
            //    return AccessDeniedView();
            try
            {
                var permissionRecords = _permissionService.GetAllPermissionRecords();
                var customerRoles     = _customerService.GetAllCustomerRoles(true);

                foreach (var cr in customerRoles)
                {
                    var formKey = "allow_" + cr.Id;
                    var permissionRecordSystemNamesToRestrict = !StringValues.IsNullOrEmpty(form[formKey])
                        ? form[formKey].ToString().Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries).ToList()
                        : new List <string>();

                    foreach (var pr in permissionRecords)
                    {
                        var allow = permissionRecordSystemNamesToRestrict.Contains(pr.SystemName);
                        if (allow)
                        {
                            PermissionRecord_Role_Mapping permissionRecord_Role_Mapping = new PermissionRecord_Role_Mapping()
                            {
                                CustomerRoleId     = cr.Id,
                                PermissionRecordId = pr.Id
                            };
                            if (pr.PermissionRecord_Role_Mapping.FirstOrDefault(x => x.CustomerRole.Id == cr.Id) == null)
                            {
                                pr.PermissionRecord_Role_Mapping.Add(permissionRecord_Role_Mapping);
                                _permissionService.UpdatePermissionRecord(pr);
                            }
                        }
                        else
                        {
                            if (pr.PermissionRecord_Role_Mapping.FirstOrDefault(x => x.CustomerRole.Id == cr.Id) != null)
                            {
                                pr.PermissionRecord_Role_Mapping.Remove(pr.PermissionRecord_Role_Mapping.FirstOrDefault(x => x.CustomerRole.Id == cr.Id));
                                _permissionService.UpdatePermissionRecord(pr);
                            }
                        }
                    }
                }

                AddNotification(NotificationMessage.TitleSuccess, NotificationMessage.msgSavePermission, NotificationMessage.TypeSuccess);

                return(RedirectToAction("Permissions"));
            }
            catch (Exception e)
            {
                AddNotification(NotificationMessage.TitleError, NotificationMessage.ErrormsgSavePermission, NotificationMessage.TypeError);

                return(View(e));
            }
        }