XAF-如何实现自定义权限系统用户对象
本示例使用XPO.
新建一个XAF项目.填加两个类进来:
[DefaultClassOptions]
public class Employee : Person {
public Employee(Session session)
: base(session) { }
[Association("Employee-Task")]
public XPCollection<EmployeeTask> OwnTasks {
get { return GetCollection<EmployeeTask>("OwnTasks"); }
}
}
[DefaultClassOptions, ImageName("BO_Task")]
public class EmployeeTask : Task {
public EmployeeTask(Session session)
: base(session) { }
private Employee owner;
[Association("Employee-Task")]
public Employee Owner {
get { return owner; }
set { SetPropertyValue("Owner", ref owner, value); }
}
}
实现接口: ISecurityUser
填加引用: DevExpress.ExpressApp.Security.v16.2.dll 程序集
using DevExpress.ExpressApp.Security;
using DevExpress.Persistent.Validation;
// ...
public class Employee : Person, ISecurityUser {
// ...
#region ISecurityUser Members
private bool isActive = true;
public bool IsActive {
get { return isActive; }
set { SetPropertyValue("IsActive", ref isActive, value); }
}
private string userName = String.Empty;
[RuleRequiredField("EmployeeUserNameRequired", DefaultContexts.Save)]
[RuleUniqueValue("EmployeeUserNameIsUnique", DefaultContexts.Save,
"The login with the entered user name was already registered within the system.")]
public string UserName {
get { return userName; }
set { SetPropertyValue("UserName", ref userName, value); }
}
#endregion
}
实现 IAuthenticationStandardUser 接口
using System.ComponentModel;
using DevExpress.Persistent.Base.Security;
// ...
public class Employee : Person, ISecurityUser, IAuthenticationStandardUser {
// ...
#region IAuthenticationStandardUser Members
private bool changePasswordOnFirstLogon;
public bool ChangePasswordOnFirstLogon {
get { return changePasswordOnFirstLogon; }
set {
SetPropertyValue("ChangePasswordOnFirstLogon", ref changePasswordOnFirstLogon, value);
}
}
private string storedPassword;
[Browsable(false), Size(SizeAttribute.Unlimited), Persistent, SecurityBrowsable]
protected string StoredPassword {
get { return storedPassword; }
set { storedPassword = value; }
}
public bool ComparePassword(string password) {
return PasswordCryptographer.VerifyHashedPasswordDelegate(this.storedPassword, password);
}
public void SetPassword(string password) {
this.storedPassword = PasswordCryptographer.HashPasswordDelegate(password);
OnChanged("StoredPassword");
}
#endregion
}
如果不想支持AuthenticationStandard验证方式,则不需要实现这个接口.
支持IAuthenticationActiveDirectoryUser 接口,为活动目录验证方式提供支持
using DevExpress.Persistent.Base.Security;
// ...
public class Employee : Person, ISecurityUser,
IAuthenticationStandardUser, IAuthenticationActiveDirectoryUser {
// ...
}
如果不想支持 AuthenticationActiveDirectory 验证方式则不需要实现此步骤.
支持 ISecurityUserWithRoles 接口.即,让用户支持角色
[DefaultClassOptions]
public class Employee : Person, ISecurityUser,
IAuthenticationStandardUser, IAuthenticationActiveDirectoryUser,
ISecurityUserWithRoles {
// ...
#region ISecurityUserWithRoles Members
IList<ISecurityRole> ISecurityUserWithRoles.Roles {
get {
IList<ISecurityRole> result = new List<ISecurityRole>();
foreach (EmployeeRole role in EmployeeRoles) {
result.Add(role);
}
return result;
}
}
#endregion
[Association("Employees-EmployeeRoles")]
[RuleRequiredField("EmployeeRoleIsRequired", DefaultContexts.Save,
TargetCriteria = "IsActive",
CustomMessageTemplate = "An active employee must have at least one role assigned")]
public XPCollection<EmployeeRole> EmployeeRoles {
get {
return GetCollection<EmployeeRole>("EmployeeRoles");
}
}
}
定义了一个角色,是继承自系统内置的:
using DevExpress.Persistent.BaseImpl.PermissionPolicy;
// ...
[ImageName("BO_Role")]
public class EmployeeRole : PermissionPolicyRoleBase, IPermissionPolicyRoleWithUsers {
public EmployeeRole(Session session)
: base(session) {
}
[Association("Employees-EmployeeRoles")]
public XPCollection<Employee> Employees {
get {
return GetCollection<Employee>("Employees");
}
}
IEnumerable<IPermissionPolicyUser> IPermissionPolicyRoleWithUsers.Users {
get { return Employees.OfType<IPermissionPolicyUser>(); }
}
}
支持 IPermissionPolicyUser 接口,为实现每个用户提供权限数据
using DevExpress.ExpressApp.Utils;
// ...
[DefaultClassOptions]
public class Employee : Person, ISecurityUser,
IAuthenticationStandardUser, IAuthenticationActiveDirectoryUser,
IPermissionPolicyUser {
// ...
#region IPermissionPolicyUser Members
IEnumerable<IPermissionPolicyRole> IPermissionPolicyUser.Roles {
get { return EmployeeRoles.OfType<IPermissionPolicyRole>(); }
}
#endregion
}
其实这个接口还是挺有用的,上面的代码是从角色中读取权限的数据.
支持 ICanInitialize 接口
ICanInitialize.Initialize 在 AuthenticationActiveDirectory 验证方式并且设置了 AuthenticationActiveDirectory.CreateUserAutomatically为true时.如果你不需要支持这个自动创建,可以跳过这里.
using DevExpress.ExpressApp;
using DevExpress.Data.Filtering;
// ...
[DefaultClassOptions]
public class Employee : Person, ISecurityUser,
IAuthenticationStandardUser, IAuthenticationActiveDirectoryUser,
IPermissionPolicyUser, ICanInitialize {
// ...
#region ICanInitialize Members
void ICanInitialize.Initialize(IObjectSpace objectSpace, SecurityStrategyComplex security) {
EmployeeRole newUserRole = (EmployeeRole)objectSpace.FindObject<EmployeeRole>(
new BinaryOperator("Name", security.NewUserRoleName));
if (newUserRole == null) {
newUserRole = objectSpace.CreateObject<EmployeeRole>();
newUserRole.Name = security.NewUserRoleName;
newUserRole.IsAdministrative = true;
newUserRole.Employees.Add(this);
}
}
#endregion
}
应用创建的自定义类
如图所示,在属性栏中设置自定义的类.

创建管理员账号
using DevExpress.ExpressApp.Security.Strategy;
// ...
public override void UpdateDatabaseAfterUpdateSchema() {
base.UpdateDatabaseAfterUpdateSchema();
EmployeeRole adminEmployeeRole = ObjectSpace.FindObject<EmployeeRole>(
new BinaryOperator("Name", SecurityStrategy.AdministratorRoleName));
if (adminEmployeeRole == null) {
adminEmployeeRole = ObjectSpace.CreateObject<EmployeeRole>();
adminEmployeeRole.Name = SecurityStrategy.AdministratorRoleName;
adminEmployeeRole.IsAdministrative = true;
adminEmployeeRole.Save();
}
Employee adminEmployee = ObjectSpace.FindObject<Employee>(
new BinaryOperator("UserName", "Administrator"));
if (adminEmployee == null) {
adminEmployee = ObjectSpace.CreateObject<Employee>();
adminEmployee.UserName = "Administrator";
adminEmployee.SetPassword("");
adminEmployee.EmployeeRoles.Add(adminEmployeeRole);
}
ObjectSpace.CommitChanges();
}
就是说,在数据库结构创建(或更新)完成后,执行上面的代码.
运行
只显示当前用户的的"任务"
CurrentUserId会取得当前用户的id
[ListViewFilter("All Tasks", "")]
[ListViewFilter("My Tasks", "[Owner.Oid] = CurrentUserId()")]
public class EmployeeTask : Task {
// ...
}
结果如下.


XAF-如何实现自定义权限系统用户对象的更多相关文章
- How to use the windows active directory to authenticate user via logon form 如何自定义权限系统,使用 active directory验证用户登录
https://www.devexpress.com/Support/Center/Question/Details/Q345615/how-to-use-the-windows-active-dir ...
- linux用户权限 -> 系统用户管理
用户基本概述: Linux用户属于多用户操作系统,在windows中,可以创建多个用户,但不允许同一时间多个用户进行系统登陆,但是Linux可以同时支持多个用户同时登陆操作系统,登陆后互相之间并不影响 ...
- oracle 对象权限 系统权限 角色权限
系统权限: 允许用户执行特定的数据库动作,如创建表.创建索引.连接实例等 对象权限: 允许用户操纵一些特定的对象,如读取视图,可更新某些列.执行存储过程等 select * from user_sys ...
- django(权限、认证)系统——用户Login,Logout
上面两篇文章,讲述的Django的Authentication系统的核心模型对象User API和相关的使用,本文继续深入,讨论如何在Web中使用Authentication系统. 前面说了,Djan ...
- Entrust - Laravel 用户权限系统解决方案
Zizaco/Entrust 是 Laravel 下 用户权限系统 的解决方案, 配合 用户身份认证 扩展包 Zizaco/confide 使用, 可以快速搭建出一套具备高扩展性的用户系统. Conf ...
- Entrust - Laravel 用户权限系统解决方案 | Laravel China 社区 - 高品质的 Laravel 和 PHP 开发者社区 - Powered by PHPHub
说明# Zizaco/Entrust 是 Laravel 下 用户权限系统 的解决方案, 配合 用户身份认证 扩展包 Zizaco/confide 使用, 可以快速搭建出一套具备高扩展性的用户系统. ...
- ubuntu12.04+proftpd1.3.4a的系统用户+虚拟用户权限应用实践
目录: 一.什么是Proftpd? 二.Proftpd的官方网站在哪里? 三.在哪里下载? 四.如何安装? 1)系统用户的配置+权限控制 2)虚拟用户的配置+权限控制 一.什么是Proftpd? ...
- grant 权限 on 数据库对象 to 用户
grant 权限 on 数据库对象 to 用户 一.grant 普通数据用户,查询.插入.更新.删除 数据库中所有表数据的权利. grant select on testdb.* to common_ ...
- linux用户权限 -> 系统特殊权限
set_uid 运行一个命令的时候,相当于这个命令的所有者,而不是执行者的身份. suid的授权方法 suid 权限字符s(S),用户位置上的x位上设置. 授权方法: passwd chmod u+s ...
随机推荐
- oracle 28001错误 密码过期失效
背景 服务器演示地址,无法使用.排查原因,是数据库密码有问题,报28001错误 问题 确定是oracle密码机制,180失效了 解决 1.进入sqlplus模式 sqlplus / as sysdba ...
- (转)透明光照模型与环境贴图之基础理论篇(折射率、色散、fresnel定律) .
摘抄“GPU Programming And Cg Language Primer 1rd Edition” 中文名“GPU编程与CG语言之阳春白雪下里巴人” 材质和光的交互除了反射现象,对于透明物 ...
- PHP 实现单点登录
1.准备两个虚拟域名 127.0.0.1 www.openpoor.com 127.0.0.1 www.myspace.com 2.在openpoor的根目录下创建以下文件 index.PHP [ ...
- symfony学习笔记2—纯的PHP代码和symfony的区别
Symfony vs 纯PHP为啥symfony比普通的php文件访问要好?这一章我们写一个简单的php文件项目,然后组织它,你会发现为什么web应用会发展到现在这个样子.最后我们将学习symfony ...
- 关于layui(layer)子页面获取不到父页面jQuery对象的问题。
如果在使用layui-layer模块过程中,在子页面执行代码: window.parent.$("#id").val() 报错:window.parent.$ is not a f ...
- 原生JS和jQuery分别使用jsonp来获取“当前天气信息”
需掌握的技能点: jsonp.跨域相关等. 以下两种代码,均可直接运行. 1.使用原生JS: <!DOCTYPE html> <html lang="en"> ...
- JavaScript的DOM操作获取元素周边大小
一.clientLeft 和 clientTop 这组属性可以获取元素设置了左边框和上边框的大小,目前只提供了 Left 和 Top 这组,并没有提供 Right 和 Bottom. <scri ...
- SGU---102 欧拉函数
题目链接: https://cn.vjudge.net/problem/SGU-102#author=0 题目大意: 求解小于等于N的且与N互质的数字有多少个 解题思路: 直接求欧拉函数即可 关于欧拉 ...
- markdownpad 2 pro版本 注册码
注册email: www.zixue.it 注册码: 4vuvQFtGkF0oH7by922v75FtaUGq7niFveCKDxqC2KSqYTfaSGzxzxKQXNhc2BG51N9URrF7 ...
- jQuery内容横向拖拽滚动
如果有业务需求:使用横向滚动,而又不想用滚动条,可以使用横向拖拽滚动,主要是利用元素的scrollLeft特性: 废话不多说直接上代码: css: .box{ width:100%; height:3 ...