/*
Kooboo is a content management system based on ASP.NET MVC framework. Copyright 2009 Yardi Technology Limited.
This program is free software: you can redistribute it and/or modify it under the terms of the
GNU General Public License version 3 as published by the Free Software Foundation.
This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along with this program.
If not, see http://www.kooboo.com/gpl3/.
*/
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Security.Principal;
using Everest.Library.Extjs.Tree;
using Everest.CmsServices.Models;
using Everest.CmsServices.Services;
using Everest.Library;
namespace Everest.CmsServices.Providers{
public class CmsTreeNodeChecker : ITreeNodeChecker
{
PermissionService permissionService = UnityManager.Resolve<PermissionService>();
Dictionary<FolderType, List<FolderType>> folderRelations = new Dictionary<FolderType, List<FolderType>>();
public CmsTreeNodeChecker()
{
folderRelations.Add(FolderType.System, new List<FolderType>()
{
FolderType.Schedule,
FolderType.UserGroup
});
folderRelations.Add(FolderType.UserGroup, new List<FolderType>()
{
FolderType.Users,
FolderType.Roles,
FolderType.UserProfile
});
folderRelations.Add(FolderType.Applications, new List<FolderType>()
{
FolderType.Application
});
folderRelations.Add(FolderType.Application, new List<FolderType>()
{
FolderType.Schedule,
FolderType.Schema,
FolderType.Template,
FolderType.Extension,
FolderType.Content
});
folderRelations.Add(FolderType.SiteManager, new List<FolderType>()
{
FolderType.SiteSetting,
FolderType.Schedule,
FolderType.Workflow,
FolderType.Schema,
FolderType.SearchSetting
});
folderRelations.Add(FolderType.Schema, new List<FolderType>()
{
FolderType.TextSchema,
FolderType.BinarySchema,
FolderType.ValidatorGroup,
FolderType.Validator
});
folderRelations.Add(FolderType.Template, new List<FolderType>() {
FolderType.ContentTemplate,
FolderType.LayoutTemplate,
FolderType.TextResource,
FolderType.BinaryResource
});
folderRelations.Add(FolderType.Extension, new List<FolderType>()
{
FolderType.PagePlugin,
FolderType.Module,
FolderType.StaticCode
});
}
#region ITreeNodeChecker Members
/// <summary>
/// Determines whether the specified node is visible.
/// </summary>
/// <param name="node">The node.</param>
/// <returns>
/// <c>true</c> if the specified node is visible; otherwise, <c>false</c>.
/// </returns>
public virtual bool IsVisible(TreeNode node)
{
var user = GetUser();
bool isVisible = false;
FolderType folderType = (FolderType)Enum.Parse(typeof(FolderType), node.Attributes["folderType"].ToString());
string application = node.Attributes["application"].ToString();
switch (folderType)
{
case FolderType.BlankFolder:
return true;
case FolderType.BinaryContent:
case FolderType.TextContent:
//
return permissionService.IsAllowedEditContent(user.Identity.Name, new Guid(node.Attributes["folderUUID"].ToString()), application, ActionType.View)
|| permissionService.IsAllowed(user.Identity.Name, FolderType.ContentFolder.ToString(), application, ActionType.View);
case FolderType.System:
case FolderType.UserGroup:
case FolderType.UserProfile:
return true;
case FolderType.Root:
case FolderType.SiteSetting:
case FolderType.Users:
case FolderType.Roles:
case FolderType.Schema:
case FolderType.TextSchema:
case FolderType.BinarySchema:
case FolderType.Template:
case FolderType.ContentTemplate:
case FolderType.LayoutTemplate:
case FolderType.Extension:
case FolderType.PagePlugin:
case FolderType.Package:
case FolderType.Applications:
case FolderType.Application:
case FolderType.Content:
case FolderType.Page:
case FolderType.BaseFolder:
case FolderType.DerivedFolder:
case FolderType.BaseNode:
case FolderType.DerivedNode:
case FolderType.Validator:
case FolderType.ValidatorGroup:
case FolderType.BinaryResource:
//case FolderType.BinaryContent:
case FolderType.BinaryResourceFolder:
case FolderType.Schedule:
case FolderType.TextResource:
case FolderType.SearchSetting:
case FolderType.KoobooStore:
default:
if (folderRelations.ContainsKey(folderType))
{
isVisible = IsChildrenVisible(user, folderType, application);
}
else
{
isVisible = isVisible || permissionService.IsAllowed(user.Identity.Name, folderType.ToString(), application, ActionType.View);
}
break;
}
return isVisible;
}
/// <summary>
/// Determines whether [is children visible] [the specified user].
/// </summary>
/// <param name="user">The user.</param>
/// <param name="folderType">Type of the folder.</param>
/// <returns>
/// <c>true</c> if [is children visible] [the specified user]; otherwise, <c>false</c>.
/// </returns>
private bool IsChildrenVisible(IPrincipal user, FolderType folderType, string application)
{
bool isVisible = false;
foreach (var item in folderRelations[folderType])
{
if (folderRelations.ContainsKey(item))
{
isVisible = IsChildrenVisible(user, item, application);
}
else
{
isVisible = isVisible || permissionService.IsAllowed(user.Identity.Name, item.ToString(), application, ActionType.View);
}
if (isVisible)
{
return true;
}
}
return isVisible;
}
private IPrincipal GetUser()
{
return System.Threading.Thread.CurrentPrincipal;
}
#endregion
}
}
|