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 ...
随机推荐
- Opengl---gluLookAt函数详解(转)
转自 http://www.cnblogs.com/jiangu66/archive/2013/04/06/3003122.html 下面的一段摘自百度百科: 视点转换 函数原型 void gluLo ...
- 使用yii AR 完成单个表的CURD操作
什么是AR(ActiveRecord) Active Record (活动记录,以下简称AR)提供了一个面向对象的接口, 用以访问数据库中的数据.一个 AR 类关联一张数据表, 每个 AR 对象对应表 ...
- css3实现border渐变色
案例1 .box{ width: 100px; height: 100px; border:10px solid #ddd; border-image: -webkit-linear-gradient ...
- 安装zabbix3.4的过程(一)
目录 zabbix服务端安装(centos7.4) zabbix客户端安装 (centos6.9) 注释:本次安装为官方推荐的yum安装方式,如果需要编译安装,请查看下边的博文: 博文地址:https ...
- MFC调试时可以,使用生产的exe时,显示未响应解决方案
首先,使用release模式,生成解决方案和exe. 对生成的exe,选择属性,兼容性,win7,应用,即可解决. 此方法对debug无用
- Nodejs Redis 全部操作方法
安装 npm install redis --save demo var redis = require('redis'); var client = redis.createClient('637 ...
- 【原创】Spring 注入方式
Spring 强烈推荐注解在构造器上,且对于不能为null的字段或者属性都用断言. 1. 设值注入 原理:通过setter方法注入 XML配置方式:bean下的property标签,用value指定基 ...
- bootstrap datetimepicker 在 angular 项目中的运用
datetimepocker 是一个日期时间选择器,bootstrap datetimepicker 是 bootstrap 日期时间表单组件.访问 bootstrap-datetimepicker ...
- 【cs231n】图像分类 k-Nearest Neighbor Classifier(K最近邻分类器)【python3实现】
[学习自CS231n课程] 转载请注明出处:http://www.cnblogs.com/GraceSkyer/p/8763616.html k-Nearest Neighbor(KNN)分类器 与其 ...
- python中pandas里面的dataframe数据的筛选小结
pandas大家用的都很多,像我这种用的不够熟练,也不够多的就只能做做笔记,尽量留下点东西吧. 筛选行: a. 按照列的条件筛选 df = pandas.DataFrame(...) # suppos ...