package com.oa.services.impl;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.persistence.Query;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
import com.oa.model.ACL;
import com.oa.model.Module;
import com.oa.model.Permission;
import com.oa.service.base.DaoSupport;
import com.oa.services.AclService;
@Service @Transactional
public class AclServiceBean extends DaoSupport implements AclService {
//授权过程
public void addOrUpdatePermission(String principalType, int principalSn,
int resourceSn, int permission, boolean yes) {
//根据主体标识和资源标识查找ACL实例
ACL acl = findACL(principalType, principalSn, resourceSn);
//如果存在ACL实例,则更新其授权
if(acl!=null) {
acl.setPermission(permission, yes);
update(acl);
return ;
}
//不存在ACL实例,则创建ACL实例
acl = new ACL();
acl.setPrincipalType(principalType);
acl.setPrincipalSn(principalSn);
acl.setResourceSn(resourceSn);
acl.setPermission(permission, yes);
save(acl);
}
//设置用户某个资源授权的继承特性
public void addOrUpdateUserExtends(int userId, int resourceSn, boolean yes) {
//根据主体标识和资源标识查找ACL实例
ACL acl = findACL(ACL.TYPE_USER, userId, resourceSn);
if(acl != null) {
acl.setExtends(yes);
update(acl);
return;
}
acl = new ACL();
acl.setPrincipalType(ACL.TYPE_USER);
acl.setPrincipalSn(userId);
acl.setResourceSn(resourceSn);
acl.setExtends(yes);
save(acl);
}
//删除授权
public void delPermission(String principalType, int principalSn,
int resourceSn) {
em.remove(findACL(principalType, principalSn, resourceSn));
}
//即时认证
@SuppressWarnings("unchecked")
@Transactional(readOnly=false,propagation=Propagation.NOT_SUPPORTED)
public boolean hasPermission(int userId, int resourceSn, int permission) {
//查找直接授予用户的授权
ACL acl = findACL(ACL.TYPE_USER, userId, resourceSn);
if(acl != null){
int yesOrNo = acl.getPermission(permission);
//如果是确定的授权
if(yesOrNo != ACL.ACL_NEUTRAL){
return yesOrNo == ACL.ACL_YES ? true : false;
}
}
//继续查找用户的角色授权
String hql = "select r.id from UserRole ur join ur.role r join ur.user u " +
"where u.id = ?1 order by ur.orderNo";
List<Integer> rids = em.createQuery(hql).setParameter(1, userId).getResultList();
if(rids.size()>0) {
//依照角色优先级依次查找其授权
for(Integer rid : rids) {
acl = findACL(ACL.TYPE_ROLE, rid, resourceSn);
}
//一旦发现授权,即可返回结果
if(acl != null){
return acl.getPermission(permission) == ACL.ACL_YES ? true : false;
}
}
return false;
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=false,propagation=Propagation.NOT_SUPPORTED)
public boolean hasPermissionByResourceSn(int userId, String resourceSn,
int permission) {
String hql = "select m.id from Module m where m.sn = ?1 ";
Query query = em.createQuery(hql).setParameter(1, resourceSn);
List<Integer> mid = query.getResultList();
boolean flag = false;
if(mid.size()>0) {
flag = hasPermission(
userId,
mid.get(0),
permission);
}
return flag;
}
@SuppressWarnings("unchecked")
@Transactional(readOnly=false,propagation=Propagation.NOT_SUPPORTED)
public List<ACL> searchAclRecord(String principalType, int principalSn) {
String sql = "select resourceSn,aclState&1,aclState&2," +
"aclState&4,aclState&8,aclTriState " +
"from T_ACL where principalType = '"+principalType +
"' and principalSn = "+principalSn;
List<ACL> acls = em.createNativeQuery(sql).getResultList();
return acls;
}
//搜索某个用户拥有读取权限的模块列表(用于登录,形成导航菜单的时候)
@SuppressWarnings("unchecked")
@Transactional(readOnly=false,propagation=Propagation.NOT_SUPPORTED)
public List<Module> searchModules(int userId) {
//定义临时变量
Map temp = new HashMap();
//按优先级从低到高查找用户拥有的角色 这样高优先级的角色权限就把低的覆盖了
String hql = "select r.id from UserRole ur join ur.role r join ur.user u " +
"where u.id = ?1 order by ur.orderNo desc";
List<Integer> aclIds = em.createQuery(hql).setParameter(1, userId).getResultList();
//依次循环角色
for(Integer rid : aclIds) {
//根据角色获得角色拥有的授权列表
List<ACL> acls = findRoleACLs(rid);
//把角色授权放入临时变量
for (Iterator iterator = acls.iterator(); iterator.hasNext();) {
ACL acl = (ACL) iterator.next();
temp.put(acl.getResourceSn(), acl);
}
}
//查找直接授予用户的授权列表
List<ACL> acls = findUserACLs(userId);
//把用户授权放入临时变量
for (Iterator iter = acls.iterator(); iter.hasNext();) {
ACL acl = (ACL) iter.next();
temp.put(acl.getResourceSn(), acl);
}
//现在已获得用户拥有的所有授权(包括直接授予用户自身以及其包含的角色的授权)
List delResources = new ArrayList();
Set entries = temp.entrySet();
for (Iterator iter = entries.iterator(); iter.hasNext();) {
Map.Entry entry = (Map.Entry) iter.next();
ACL acl = (ACL)entry.getValue();
//如果没有读取权限,则需要在临时变量中删除这个授权
if(acl.getPermission(Permission.READ) == ACL.ACL_NO){
delResources.add(entry.getKey());
}
}
//在临时变量中删除这些需要删除的授权
for (Iterator iter = delResources.iterator(); iter.hasNext();) {
Object key = (Object) iter.next();
temp.remove(key);
}
//如果授权列表是空的,则返回0长度的集合
if(temp.isEmpty()){
return new ArrayList();
}
//现在已获得用户拥有读取权限的授权
String searchModules = "select m from Module m where m.id in (:ids)";
return em.createQuery(searchModules).setParameter("ids", temp.keySet()).getResultList();
}
//根据主体类型、主体标识和资源标识查找ACL实例
@SuppressWarnings("unchecked")
@Transactional(readOnly=false,propagation=Propagation.NOT_SUPPORTED)
private ACL findACL(String principalType, int principalSn,
int resourceSn){
Query query = em.createQuery("select a from ACL a where a.principalType=?1 and a.principalSn=?2 and " +
"a.resourceSn=?3 ");
ACL acl = null;
query.setParameter(1, principalType).setParameter(2, principalSn).setParameter(3, resourceSn);
List<ACL> acls = query.getResultList();
if(acls != null && acls.size()>0) {
acl = acls.get(0);
}
return acl;
}
//根据角色查找角色的授权列表,返回列表的元素是:ACL实例
@SuppressWarnings("unchecked")
@Transactional(readOnly=false,propagation=Propagation.NOT_SUPPORTED)
private List<ACL> findRoleACLs(int roleId){
String hql = "select acl from ACL acl where acl.principalType = ?1 " +
"and acl.principalSn = ?2 ";
return em.createQuery(hql).setParameter(1, ACL.TYPE_ROLE).setParameter(2, roleId).getResultList();
}
//根据用户查找直接授予用户的授权列表(注意:如果直接授予用户的授权是继承的话,则不应该包含在这个列表中),返回的列表元素是:ACL实例
@SuppressWarnings("unchecked")
@Transactional(readOnly=false,propagation=Propagation.NOT_SUPPORTED)
private List<ACL> findUserACLs(int userId){
String hql = "select acl from ACL acl where acl.principalType = ?1 " +
"and acl.principalSn = ?2 and acl.aclTriState = 0";
return em.createQuery(hql).setParameter(1, ACL.TYPE_USER).setParameter(2, userId).getResultList();
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
oa系统_ssh2_Struts2+spring2.5+JPA)
共217个文件
class:55个
java:55个
gif:42个
4星 · 超过85%的资源 需积分: 10 83 下载量 96 浏览量
2010-01-02
12:27:03
上传
评论 2
收藏 233KB RAR 举报
温馨提示
oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA) oa系统_ssh2_Struts2+spring2.5+JPA)
资源推荐
资源详情
资源评论
收起资源包目录
oa系统_ssh2_Struts2+spring2.5+JPA) (217个子文件)
AclServiceBean.class 7KB
DaoSupport.class 7KB
InitSystemDatasBean.class 6KB
UserAction.class 6KB
UserServiceBean.class 5KB
DicServiceBean.class 5KB
OrgAction.class 3KB
DicServiceBeanTest.class 3KB
ModuleAction.class 3KB
AclAction.class 3KB
OrgTreeAction.class 3KB
Person.class 3KB
ACL.class 3KB
PersonServiceTest.class 3KB
Module.class 3KB
Organization.class 3KB
PersonAction.class 3KB
OrgServiceBean.class 2KB
ModuleServiceBean.class 2KB
User.class 2KB
RoleAction.class 2KB
PersonServiceBean.class 2KB
IndexAction.class 2KB
OrgServiceBeanTest.class 2KB
SystemException.class 2KB
Dictionary.class 2KB
UserRole.class 2KB
LoginAction.class 2KB
DAO.class 2KB
UserServiceBeanTest.class 2KB
ModuleServiceBeanTest.class 1KB
DateConverter.class 1KB
LoginIntercepter.class 1KB
Maptest.class 1KB
InitSystemDatasTest.class 1KB
DateContent.class 1KB
QueryResult.class 1KB
Role.class 1KB
DateUtil.class 1010B
SecurityFunctions.class 935B
UserService.class 715B
DicService.class 692B
TestInt.class 691B
Pager.class 675B
AclService.class 640B
CreateTable.class 636B
BaseAction.class 590B
RoleServiceBean.class 509B
ModuleService.class 480B
Permission.class 419B
OrgService.class 402B
PersonService.class 379B
DwrTest.class 377B
InitSystemDatas.class 213B
RoleService.class 150B
.classpath 5KB
oa.css 6KB
dtree.css 795B
logo_bg.gif 32KB
logo.gif 7KB
logo_copyright_bg.gif 5KB
logo.gif 2KB
userLogin_button.gif 2KB
userLogin.gif 2KB
addpic.gif 1KB
globe.gif 1KB
title_middle.gif 1KB
trash.gif 1KB
question.gif 1KB
base.gif 1KB
cz.gif 940B
nolines_plus.gif 870B
nolines_minus.gif 861B
musicfolder.gif 633B
imgfolder.gif 622B
page.gif 582B
title_left.gif 544B
peditor.gif 450B
arrowdown.gif 424B
netm.gif 414B
arrowup.gif 407B
folderopen.gif 376B
folder.gif 372B
mail.gif 369B
news.gif 368B
word.gif 366B
title_right.gif 303B
cd.gif 239B
userLogin_down.gif 177B
dela.gif 121B
file1.gif 89B
plus.gif 89B
plusbottom.gif 88B
minus.gif 86B
minusbottom.gif 85B
join.gif 69B
line.gif 66B
joinbottom.gif 66B
logo_under_line.gif 62B
empty.gif 62B
共 217 条
- 1
- 2
- 3
资源评论
- sangenmutou2013-09-17可以做参考,还算不错的资源
- Nineth9992014-01-13可以做参考,还算不错的资源
- 展S开K双Y翼2014-04-22功能过于简单。只有一些参照价值
- huangxiangangas2014-03-06有很多地方值得学习了 谢了
- Vincent.cc2013-03-26代码挺有参考价值, 如果spring是3.0就更好啦.
dengmingrun
- 粉丝: 9
- 资源: 24
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
最新资源
资源上传下载、课程学习等过程中有任何疑问或建议,欢迎提出宝贵意见哦~我们会及时处理!
点击此处反馈
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功