Appfuse的权限控制依赖于Struts的Menu机制,common下的menu.jsp是对菜单顺序的定义,详细的菜单项和菜单链接及权限再menu-config.xml中控制,如下:
<Menu name="Logout" title="user.logout" page="/logout" roles="ROLE_ADMIN,ROLE_USER,ROLE_PRODUCT" />
roles中的值即是role表中的name,给那个角色分配菜单的权限,则把角色的name加入到菜单定义的roles中即可。
但使用过程中会发现,这样定义好之后菜单出来是乱的,源自appfuse中的一个Bug,修改文件navbarMenu.vm即可,如下:
#macro( displayNavbarMenu $menu $count)
#if ($displayer.isAllowed($menu))
#set ($count = $count + 1)
## set menu title
#set ($title = $displayer.getMessage($menu.title))
#if (!$menu.url) #set ($url="javascript:void(0)") #else #set ($url=$menu.url) #end ## create a single menu item
#if ($menu.components.size() == 0)
<li class="#if ($menu.name == $currentMenu)active#end">
<a href="$url" title="$title" #if($menu.target)target="$menu.target" #end#if($menu.width)style="width: ${menu.width}px"#end>${title}</a>
#else ## create multiple menu items in a menu
#if ($menu.components.size() > 0)
#set ($hasViewableChildren = false)
#set ($renderedChildren = 0)
#foreach ($menuIt in $menu.components)
#if ($displayer.isAllowed($menuIt))
#set($hasViewableChildren = true)
#set($renderedChildren = $renderedChildren + 1)
#end
#end
#end <li#if ($hasViewableChildren) class="dropdown#if ($menu.name == $currentMenu) active#end"#end>
<a href="#" title="$title"
#if($menu.target)target="$menu.target" #end
#if($menu.width)style="width: ${menu.width}px"#end
class="dropdown-toggle" data-toggle="dropdown">${title}</a>
#end #if ($menu.components.size() > 0)
#if ($hasViewableChildren)
<ul class="dropdown-menu">
#end #set ($count = 0)
#foreach ($menuIt in $menu.components)
#displayNavbarMenu($menuIt, $count)
#end #if ($hasViewableChildren && ($count == $renderedChildren))
</ul></li>
#else
</ul>
#if ($count > $renderedChildren)
</li>
#end
#end
#else
</li>
#if ($menu.parent && $count == $menu.parent.components.size())
##</ul>
#end
#end
#end
#end #displayNavbarMenu($menu, 0)
navbarMenu.vm
使用Menu控制权限只能控制到菜单的可见性,如果想深入到数据控制需要自己再处理。下面是通过切入OnSubmit方法做的操作控制:
1. 首先定义切面,切入OnSubmit方法
/**
* 做权限验证,如果用户无权限,则拒绝请求
*/
@Override
public Object invoke(MethodInvocation invocation) throws Throwable {
try {
Boolean allow = false;
// 当前执行的操作
String action = "";
// 获取当前操作的用户的角色
User user = userManager.get(getCurrentUserID());
Set<Role> roleList = user.getRoles();
// 当前执行的操作,从Request中获取
// 基于OnSubmit的签名获取RequestString onSubmit(Greatplace greatplace,
// BindingResult errors, HttpServletRequest request,
// HttpServletResponse response)
if (invocation.getArguments().length == 4
&& invocation.getArguments()[2].getClass() == HttpServletRequest.class) {
HttpServletRequest request = (HttpServletRequest) invocation
.getArguments()[2];
if (request.getParameter("save") != null) {
action = RolePermissionManager.PERMISSION_SAVE;
} else if (request.getParameter("delete") != null) {
action = RolePermissionManager.PERMISSION_DELETE;
} else if (request.getParameter("approve") != null
|| request.getParameter("unapprove") != null) {
action = RolePermissionManager.PERMISSION_APPROVE;
}
for (Role r : roleList) {
allow = RolePermissionManager.hasPermission(r.getName(),
action);
if (allow)
break;
}
if (!allow) {
request.getSession().setAttribute("successMessages",
"对不起,您无权执行该操作!");
return request.getPathInfo().replace("/", "");
}
}
Object result = invocation.proceed();
return result; } catch (IllegalArgumentException ex) {
log.error(ex);
throw ex;
}
}
SubmitAdvice
2. 定义自己的权限验证方法,下面是个简单的示例
public class RolePermissionManager {
/**
* 权限项:保存
*/
public static final String PERMISSION_SAVE = "save";
/**
* 权限项:审批
*/
public static final String PERMISSION_APPROVE = "approve";
/**
* 权限项:删除
*/
public static final String PERMISSION_DELETE = "delete"; /**
* 角色:管理员
*/
public static final String ROLE_ADMIN = "ROLE_ADMIN";
/**
* 角色:普通用户
*/
public static final String ROLE_USER = "ROLE_USER"; /**
* 基于角色的权限矩阵
*/
private static Map<String,List<String>> permissionList; /**
* 初始化角色的权限项
*/
public RolePermissionManager(){
}
/**
* 判断当前角色是否有指定的权限项
* @param roleName 角色名称
* @param permissionName 权限项名称
* @return
*/
public static Boolean hasPermission(String roleName,String permissionName){
return getPermissionList().get(roleName).contains(permissionName);
}
/**
* 定义角色和权限项的规则
* @return
*/
public static Map<String,List<String>> getPermissionList(){
if(permissionList == null){
permissionList = new HashMap<String,List<String>>();
//定义管理员的权限
List<String> adminPermissionList = new ArrayList<String>();
adminPermissionList.add(PERMISSION_SAVE);
adminPermissionList.add(PERMISSION_APPROVE);
adminPermissionList.add(PERMISSION_DELETE);
permissionList.put(ROLE_ADMIN, adminPermissionList);
//定义普通用户的权限
List<String> userPermissionList = new ArrayList<String>();
permissionList.put(ROLE_USER, userPermissionList);
}
return permissionList;
}
}
RolePermissionManager
这样基本的权限控制目的就能达到。
关于用户看到的数据权限我通过url中的参数,结合search来控制,但这样只要修改url中的参数就可越过数据权限,有待优化。