在xaf 14 中实现 Tonyyang原文中的action权限

时间:2021-04-19 15:51:59

具体实现过程,主要修改了以下几个地方:

第一:角色和用户类中需要修改成SecurityStrategy的方式:

具体代码

MySecurityRole:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security;
using System.Text;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Validation;
using System.Collections.ObjectModel;
using DevExpress.Persistent.Base.Security;
namespace SecurityDemoTest.Module.BusinessObjects
{
[DefaultClassOptions]
public class MySecurityRole : SecuritySystemRole
{
public const string DefaultAdministratorsGroupName = "Administrators";
//private List<IPermission> _Permissions = new List<IPermission>();
public MySecurityRole(Session session)
: base(session)
{ } [Association("ActionDatas-MySecurityRoles")]
public XPCollection<ActionData> ActionPermissions
{
get
{
return GetCollection<ActionData>("ActionPermissions");
}
}
}
}

  MySecurityUser:

using System;
using System.Collections.Generic;
using System.Linq;
//using DevExpress.ExpressApp.Security;
using System.Drawing;
using System.Security;
using DevExpress.ExpressApp.Security.Strategy;
using DevExpress.Persistent.Validation;
using DevExpress.Xpo;
using DevExpress.Persistent.Base;
using DevExpress.Persistent.Base.General;
using DevExpress.Persistent.Base.Security;
using DevExpress.ExpressApp.DC;
using System.ComponentModel;
//using WinWebSolution.Module; namespace SecurityDemoTest.Module.BusinessObjects
{
[XafDisplayName("User"), Persistent, ImageName("BO_User")]
public class MySecurityUser : SecuritySystemUser // SecurityUserWithRolesBase
{
public MySecurityUser(DevExpress.Xpo.Session session)
: base(session)
{
//_Permissions = new List<IPermission>();
} [Persistent("Color")]
private int _Color;
[NonPersistent]
public Color Color
{
get { return Color.FromArgb(_Color); }
set { SetPropertyValue("Color", ref _Color, value.ToArgb()); }
}
public override void AfterConstruction()
{
base.AfterConstruction();
_Color = Color.White.ToArgb();
}
private string _Description;
public string Description
{
get
{
return _Description;
}
set
{
SetPropertyValue("Description", ref _Description, value);
}
} }
}

  第二:需要在ViewController中添加如下代码:

        private static bool IsAllowAccessAction(string actionId)
{
MySecurityUser currentUser = SecuritySystem.CurrentUser as MySecurityUser;
Guard.ArgumentNotNull(currentUser, "CurrentUser");
Guard.ArgumentNotNullOrEmpty(actionId, "ActionId");
foreach (MySecurityRole role in currentUser.Roles)
{
foreach (ActionData ap in role.ActionPermissions)
{
if (ap.ActionId == actionId && ap.Kind == "Custom")
return true;
}
}
return false;
}