Skip to content

Instantly share code, notes, and snippets.

@seveniu
Created October 7, 2013 14:02
Show Gist options
  • Save seveniu/6868531 to your computer and use it in GitHub Desktop.
Save seveniu/6868531 to your computer and use it in GitHub Desktop.
aop pivileges manager
package com.kaishengit.aopPivilegesManager;
import com.kaishengit.Util.ListUtil;
import com.kaishengit.dao.GroupDao;
import com.kaishengit.exception.ServiceException;
import com.kaishengit.pojo.Group;
import com.kaishengit.pojo.User;
import org.aspectj.lang.annotation.*;
import javax.inject.Inject;
import javax.inject.Named;
/**
* Created with IntelliJ IDEA.
* User: niu
* Date: 13-9-27
* Time: 上午10:35
* To change this template use File | Settings | File Templates.
*
*/
@Named
@Aspect
public class GroupPivileges {
@Inject
private GroupDao groupDao;
//过滤 创建者权限 方法
@Pointcut("execution(* com.kaishengit.service.GroupService.owner*(..))")
public void ownerGroup() {}
/******* advice方法 传参 *******/
/***
1. args的名字与before 的参数名一致
2. 映射的顺序是在 args的,类型是按before的参数的类型,两者缺一不可
3. 通知方法中,参数的改变,直接影响 切入点的参数
4. Session 与切入点的 Session 是不同的
***/
//创建者权限
@Before(value = "ownerGroup() && args(curruser,group,..)")
public void managerGroup(User curruser,Group group) {
System.out.println("进入 owner 权限管理。。。");
if ( curruser.getId() == null || group.getId() == null)
throw new ServiceException("没有GroupId,或没有curruserId");
//当前的 Group 与数据库中的不同,因此需要创建副本,
// 并且因为session中id唯一,因此需要临时改变group的id
int id = group.getId();
group.setId(0);
Group temp = groupDao.findById(id);
if (temp == null)
throw new ServiceException("该 Group 不存在");
if (!temp.getCreateUser().getId().equals(curruser.getId()))
throw new ServiceException("你没有权限修改该 Group ");
temp = null;
group.setId(id);
}
//过滤 普通组员的权限 的方法
@Pointcut("execution(* com.kaishengit.service.GroupService.in*(..))")
public void inGroup() {}
//普通组员权限
@Before(value = "inGroup() && args(curruser,group,..)")
public void userInGroup(User curruser,Group group) {
System.out.println("进入 in group 权限管理。。。");
if ( curruser.getId() == null || group.getId() == null)
throw new ServiceException("没有GroupId,或没有curruserId");
group= groupDao.findById(group.getId());
if (group == null)
throw new ServiceException("该 Group 不存在");
if (new ListUtil<User>().hasContain(group.getUsers(),"id",curruser.getId()).size() == 0)
throw new ServiceException("你不是该 Group 的成员");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment