package com.hongguaninfo.ams.service.privilege;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import org.hibernate.Hibernate;
import org.hibernate.SessionFactory;
import org.hibernate.criterion.Restrictions;
import org.hibernate.transform.Transformers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.Authentication;
import org.springframework.security.GrantedAuthority;
import org.springframework.security.context.SecurityContextHolder;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springside.modules.orm.hibernate.Page;
import org.springside.modules.orm.hibernate.SimpleHibernateTemplate;
import com.hongguaninfo.ams.entity.privilege.Authority;
import com.hongguaninfo.ams.entity.privilege.Role;
import com.hongguaninfo.ams.entity.privilege.User;
/**
* 整个User模块内的业务逻辑Facade类. 组合user,role,auth三者的DAO.
* DAO均由SimpleHibernateTemplate指定泛型生成.
*
* @author sunjian
* @author liuguohui
* @author zhuchao
*/
@Service
@Transactional
public class UserManager {
private static String AUTH_HQL = "select count(u) from User u where u.loginName=? and u.password=?";
private static String TREE_HQL = "select id,name,is_menu,href,display_name,parent_id,level from authorities start with parent_id=0 connect by prior id=parent_id ";
private static String PAGEID_HQL = "select id from authorities a where (select count(*) from authorities where parent_id = a.id) = 0 ";
private SimpleHibernateTemplate<User, Long> userDao;
private SimpleHibernateTemplate<Role, Long> roleDao;
private SimpleHibernateTemplate<Authority, Long> authDao;
private SessionFactory sessionFactory;
@Autowired
public void setSessionFactory(SessionFactory sessionFactory) {
userDao = new SimpleHibernateTemplate<User, Long>(sessionFactory,
User.class);
roleDao = new SimpleHibernateTemplate<Role, Long>(sessionFactory,
Role.class);
authDao = new SimpleHibernateTemplate<Authority, Long>(sessionFactory,
Authority.class);
this.sessionFactory = sessionFactory;
}
@Transactional(readOnly = true)
public User getUser(Long id) {
return userDao.get(id);
}
@Transactional(readOnly = true)
public Page<User> getAllUsers(Page<User> page) {
return userDao.findAll(page);
}
@Transactional(readOnly = true)
public User getUserByLoginName(String loginName) {
return userDao.findUniqueByProperty("loginName", loginName);
}
public void saveUser(User user) {
userDao.save(user);
}
public void deleteUser(Long id) {
User user = userDao.get(id);
userDao.delete(user);
}
@Transactional(readOnly = true)
public boolean isNameUnique(String Name, String orgName) {
return roleDao.isUnique("name", Name, orgName);
}
@Transactional(readOnly = true)
public boolean isLoginNameUnique(String loginName, String orgLoginName) {
return userDao.isUnique("loginName", loginName, orgLoginName);
}
/**
* 验证用户名密码. 因为使用acegi做安全管理,此函数已作废,仅用作demo.
*
* @return 验证通过时返回true.用户名或密码错误均返回false.
*/
@Transactional(readOnly = true)
public boolean auth(String loginName, String password) {
if (!StringUtils.hasText(loginName) || !StringUtils.hasText(password))
return false;
return (userDao.findLong(AUTH_HQL, loginName, password) == 1);
}
@Transactional(readOnly = true)
public Page<Role> getAllRoles(Page<Role> page) {
return roleDao.findAll(page);
}
public List<Role> getAllRoles() {
return roleDao.findAll();
}
@Transactional(readOnly = true)
public Role getRole(Long id) {
return roleDao.get(id);
}
public void saveRole(Role role) {
roleDao.save(role);
}
public void deleteRole(Long id) {
Role role = roleDao.get(id);
roleDao.delete(role);
}
@Transactional(readOnly = true)
public List<Authority> getAllAuths() {
return authDao.findAll();
}
@Transactional(readOnly = true)
public Authority getAuthority(Long id) {
return authDao.get(id);
}
/**
* 根据关键字查询
*
* @guohui
* @return page<user>
*/
@Transactional(readOnly = true)
public Page<User> getUserByKeyword(Page<User> page, String keyword) {
return userDao.findByCriteria(page, Restrictions.or(Restrictions.like(
"name", "%" + keyword + "%"), Restrictions.like("loginName",
"%" + keyword + "%")));
}
/**
* 获取当前登录的用户的权限
*
* @guohui
* @return GrantedAuthority
*/
public static GrantedAuthority[] getGrantedAuthority() {
Authentication auth = SecurityContextHolder.getContext()
.getAuthentication();
org.springframework.security.userdetails.User user = (org.springframework.security.userdetails.User) auth
.getPrincipal();
return user.getAuthorities();
}
/**
* 根据当前登录的用户的权限返回对应权限列表
*
* @guohui
* @return listAutority
*/
public List<Map> createUserTreeList(List<Map> list,
GrantedAuthority[] grantedAuthority) {
List<Map> listAutority = new ArrayList<Map>();
if (grantedAuthority.length != 0) {
for (int j = 0; j < list.size(); j++) {
Map map = (Map) list.get(j);
String AuthorityName = map.get("NAME").toString();
for (int i = 0; i < grantedAuthority.length; i++) {
if ((AuthorityName.equals(grantedAuthority[i]
.getAuthority()))
&& (map.get("IS_MENU").toString().equals("1")))
listAutority.add(map);
}
}
}
return listAutority;
}
/**
* 根据用户的已有的角色返回剩与的角色
*
* @author guohui
* @param list
* 角色总集
* @param set
* 已选的角色
* @return 未选角色集合
*/
public List<Role> leaveRoles(List<Role> list, Set<Role> set) {
List<Role> leaveRoles = new ArrayList<Role>();
if (!set.isEmpty()) {
for (int i = 0; i < list.size(); i++) {
Role role = list.get(i);
boolean isUnselected = true;
for (Iterator<Role> it = set.iterator(); it.hasNext();) {
Role selectedRole = it.next();
if (role.getId() == selectedRole.getId()) {
isUnselected = false;
break;
}
}
if (isUnselected == true) {
leaveRoles.add(role);
}
}
} else
leaveRoles = list;
return leaveRoles;
}
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List<Map> getTreelist() {
List authList = sessionFactory.getCurrentSession().createSQLQuery(
TREE_HQL)
.setResultTransformer(Transformers.ALIAS_TO_ENTITY_MAP).list();
return authList;
}
@SuppressWarnings("unchecked")
@Transactional(readOnly = true)
public List getPagid() {
List pageid = sessionFactory.getCurrentSession().createSQLQuery(
PAGEID_HQL).addScalar("ID", Hibernate.LONG).list();
return pageid;
}
}
没有合适的资源?快使用搜索试试~ 我知道了~
dhtmlxtree
共56个文件
svn-base:16个
format:12个
entries:12个
1星 需积分: 0 33 下载量 20 浏览量
2008-12-19
21:35:23
上传
评论
收藏 42KB RAR 举报
温馨提示
dhtmlxtree实现动态树(对一棵动态树的管理如:新增子节点,编辑树,删除子节点)
资源详情
资源评论
资源推荐
收起资源包目录
src.rar (56个子文件)
src
ehcache-hibernate.xml 689B
com
.svn
props
entries 233B
text-base
format 2B
tmp
props
text-base
prop-base
prop-base
hongguaninfo
.svn
props
entries 237B
text-base
format 2B
tmp
props
text-base
prop-base
prop-base
ams
.svn
props
entries 279B
text-base
format 2B
tmp
props
text-base
prop-base
prop-base
web
.svn
props
entries 251B
text-base
format 2B
tmp
props
text-base
prop-base
prop-base
privilege
UserAction.java 5KB
.svn
props
entries 559B
text-base
RoleAction.java.svn-base 4KB
UserAction.java.svn-base 5KB
format 2B
tmp
props
text-base
prop-base
prop-base
RoleAction.java 4KB
service
.svn
props
entries 430B
text-base
ServiceException.java.svn-base 322B
format 2B
tmp
props
text-base
prop-base
prop-base
security
.svn
props
entries 414B
text-base
UserDetailServiceImpl.java.svn-base 2KB
format 2B
tmp
props
text-base
prop-base
prop-base
UserDetailServiceImpl.java 2KB
privilege
UserManager.java 7KB
.svn
props
entries 409B
text-base
UserManager.java.svn-base 7KB
format 2B
tmp
props
text-base
prop-base
prop-base
ServiceException.java 322B
entity
.svn
props
entries 254B
text-base
format 2B
tmp
props
text-base
prop-base
prop-base
privilege
Role.java 2KB
.svn
props
entries 700B
text-base
Role.java.svn-base 2KB
User.java.svn-base 3KB
Authority.java.svn-base 2KB
format 2B
tmp
props
text-base
prop-base
prop-base
User.java 3KB
Authority.java 2KB
util
.svn
props
entries 544B
text-base
XMLUtilTest.java.svn-base 2KB
XMLUtil.java.svn-base 2KB
format 2B
tmp
props
text-base
prop-base
prop-base
XMLUtilTest.java 2KB
XMLUtil.java 2KB
.svn
props
entries 1KB
text-base
application.properties.svn-base 184B
struts.xml.svn-base 414B
applicationContext.xml.svn-base 3KB
ehcache-hibernate.xml.svn-base 689B
logback.xml.svn-base 2KB
applicationContext-security.xml.svn-base 2KB
format 2B
tmp
props
text-base
prop-base
prop-base
logback.xml 2KB
applicationContext.xml 3KB
struts.xml 414B
applicationContext-security.xml 2KB
application.properties 184B
共 56 条
- 1
zhuchao456
- 粉丝: 0
- 资源: 1
上传资源 快速赚钱
- 我的内容管理 展开
- 我的资源 快来上传第一个资源
- 我的收益 登录查看自己的收益
- 我的积分 登录查看自己的积分
- 我的C币 登录后查看C币余额
- 我的收藏
- 我的下载
- 下载帮助
安全验证
文档复制为VIP权益,开通VIP直接复制
信息提交成功
评论1