/// <summary>
        /// Saves the resource permissions.
        /// </summary>
        /// <param name="model">The model.</param>
        /// <param name="type">The type.</param>
        public bool ApplyPermissions(PermissionsModel model, Type type)
        {
            var result = true;
            var roleService = ServiceLocator.Current.GetInstance<IRoleService>();
            var permissionsService = ServiceLocator.Current.GetInstance<IPermissionService>();
            var entityTypeService = ServiceLocator.Current.GetInstance<IEntityTypeService>();

            var permissibleItem = MvcApplication.PermissibleObjects.FirstOrDefault(
               perm =>
               perm.GetType() ==
               type);
            
            if (permissibleItem != null)
            {
                var operations =
                    permissibleItem.Operations.Where(
                        operation =>
                        operation.OperationLevel != PermissionOperationLevel.Type).ToList();

                var roles = roleService.GetAll();
                var permissions = permissionsService.GetResourcePermissions(type, model.EntityId, false);
                var entityType = entityTypeService.GetByType(type);

                foreach (var role in roles)
                {
                    var rolePermission = permissions.Where(permission => permission.Role == role).FirstOrDefault();
                    if (rolePermission == null)
                    {
                        rolePermission = new Permission
                                             {
                                                 Role = role,
                                                 EntityId = model.EntityId,
                                                 EntityType = entityType,
                                             };
                    }

                    foreach (var operation in operations)
                    {
                        if (model.Actions == null || !model.Actions.Contains(String.Format("{0}_{1}", role.Id, operation.Key)))
                        {
                            rolePermission.Permissions = rolePermission.Permissions & (~operation.Key);
                        }
                        else
                        {
                            rolePermission.Permissions = rolePermission.Permissions | operation.Key;
                        }
                    }
                    result = result && permissionsService.Save(rolePermission);
                }
            }

            return result;
        }
        public virtual ActionResult ApplyWidgetPermissions(PermissionsModel model)
        {
            var widgetService = ServiceLocator.Current.GetInstance<IPageWidgetService>();
            PageWidget pageWidget = widgetService.Find(model.EntityId);
            if (pageWidget != null && pageWidget.Widget != null)
            {
                ICoreWidget coreWidget =
                    MvcApplication.Widgets.FirstOrDefault(wd => wd.Identifier == pageWidget.Widget.Identifier);
                ICorePrincipal currentPrincipal = this.CorePrincipal();
                bool isOwner = currentPrincipal != null && pageWidget.User != null &&
                               currentPrincipal.PrincipalId == pageWidget.User.PrincipalId;

                if (coreWidget != null && coreWidget is BaseWidget && permissionService.IsAllowed(((BaseWidget)coreWidget).PermissionOperationCode, currentPrincipal, coreWidget.GetType(), pageWidget.Id, isOwner, PermissionOperationLevel.Object))
                {
                    permissionHelper.ApplyPermissions(model, coreWidget.GetType());

                    if (permissionService.IsAllowed(((BaseWidget)coreWidget).PermissionOperationCode, currentPrincipal, coreWidget.GetType(), pageWidget.Id, isOwner, PermissionOperationLevel.Object))
                    {
                        return Content(Url.Action(MVC.Pages.Show(pageWidget.Page.Url)));
                    }
                }
            }

            return Content(Url.Action(MVC.Home.Index()));
        }
        public virtual ActionResult ApplyPagePermissions(PermissionsModel model)
        {
            Page page = pageService.Find(model.EntityId);

            if (page != null)
            {
                if (permissionService.IsAllowed((Int32)PageOperations.Permissions, this.CorePrincipal(), typeof(Page), page.Id, IsPageOwner(page), PermissionOperationLevel.Object))
                {
                    permissionHelper.ApplyPermissions(model, typeof(Page));
                }
                if (permissionService.IsAllowed((Int32)PageOperations.Permissions, this.CorePrincipal(), typeof(Page), page.Id, IsPageOwner(page), PermissionOperationLevel.Object))
                    return Content(Url.Action(MVC.Pages.Show(page.Url)));
            }

            return Content(Url.Action(MVC.Home.Index()));
        }
        public virtual ActionResult ApplyPermissions(PermissionsModel model)
        {
            var form = formsService.Find(model.EntityId);

            if (form != null)
            {
                if (permissionService.IsAllowed((Int32)FormOperations.Permissions, this.CorePrincipal(), typeof(Form), form.Id, IsFormOwner(form), PermissionOperationLevel.Object))
                {
                    permissionsHelper.ApplyPermissions(model, typeof(Form));
                }
                if (permissionService.IsAllowed((Int32)FormOperations.Permissions, this.CorePrincipal(), typeof(Form), form.Id, IsFormOwner(form), PermissionOperationLevel.Object))
                {
                    Success(HttpContext.Translate("Messages.PermitionsSuccess", ResourceHelper.GetControllerScope(this))/*"Successfully apply permissions."*/);
                    return Content(Url.Action("ShowPermissions", "Forms", new { formId = form.Id }));
                }
                Error(String.Format(HttpContext.Translate("Messages.PermitionsUnSuccess", ResourceHelper.GetControllerScope(this))/*"Could not apply permissions to Form Entity: {0}"*/, model.EntityId));
            }
            Error(String.Format(HttpContext.Translate("Messages.NotFoundEntity", ResourceHelper.GetControllerScope(this))/*"Could not found Form Entity: {0}"*/, model.EntityId));
            return Content(Url.Action("ShowAll"));
        }
        public virtual ActionResult ApplyPermissions(PermissionsModel model)
        {
            var category = categoryService.Find(model.EntityId);

            if (category != null)
            {
                if (permissionService.IsAllowed((Int32)CategoryOperations.Permissions, this.CorePrincipal(), typeof(WebContentCategory), category.Id, IsCategoryOwner(category), PermissionOperationLevel.Object))
                {
                    permissionsHelper.ApplyPermissions(model, typeof(WebContentCategory));
                }
                if (permissionService.IsAllowed((Int32)CategoryOperations.Permissions, this.CorePrincipal(), typeof(WebContentCategory), category.Id, IsCategoryOwner(category), PermissionOperationLevel.Object))
                {
                    Success(HttpContext.Translate("Messages.PermitionsSuccess", ResourceHelper.GetControllerScope(this)));
                    return Content(Url.Action("ShowPermissions", "WebContentCategory", new { categoryId = category.Id }));
                }
                Error(String.Format(HttpContext.Translate("Messages.PermitionsUnSuccess", ResourceHelper.GetControllerScope(this)), model.EntityId));
            }
            Error(String.Format(HttpContext.Translate("Messages.NotFoundEntity", ResourceHelper.GetControllerScope(this)), model.EntityId));

            return Content(Url.Action("Show"));
        }
        public virtual ActionResult ApplyPermissions(PermissionsModel model)
        {
            var form = _formsService.Find(model.EntityId);

            if (form != null)
            {
                if (_permissionService.IsAllowed((Int32)FormOperations.Permissions, this.CorePrincipal(), typeof(Form), form.Id, IsFormOwner(form), PermissionOperationLevel.Object))
                {
                    _permissionsHelper.ApplyPermissions(model, typeof(Form));
                }
                if (_permissionService.IsAllowed((Int32)FormOperations.Permissions, this.CorePrincipal(), typeof(Form), form.Id, IsFormOwner(form), PermissionOperationLevel.Object))
                    return Content(Url.Action("ShowPermissions", "Forms", new { formId = form.Id }));
            }

            return Content(Url.Action("ShowAll"));
        }
        public virtual ActionResult ApplyPermissions(PermissionsModel model)
        {
            var article = articleService.Find(model.EntityId);

            if (article != null)
            {
                if (permissionService.IsAllowed((Int32)ArticleOperations.Permissions, this.CorePrincipal(), typeof(Article), article.Id, IsArticleOwner(article), PermissionOperationLevel.Object))
                {
                    permissionsHelper.ApplyPermissions(model, typeof(Article));
                }
                if (permissionService.IsAllowed((Int32)ArticleOperations.Permissions, this.CorePrincipal(), typeof(Article), article.Id, IsArticleOwner(article), PermissionOperationLevel.Object))
                {
                    Success(HttpContext.Translate("Messages.PermitionsSuccess", ResourceHelper.GetControllerScope(this)));
                    return Content(Url.Action("ShowPermissions", "Article", new { articleId = article.Id }));
                }
                Error(String.Format(HttpContext.Translate("Messages.PermitionsUnSuccess", ResourceHelper.GetControllerScope(this)), model.EntityId));
            }
            Error(String.Format(HttpContext.Translate("Messages.NotFoundEntity", ResourceHelper.GetControllerScope(this)), model.EntityId));

            return Content(Url.Action("Show"));
        }