2017-04-04 22:36
@JFinal 成功扩展了一个ifHasAuth指令,多谢波总指点迷津。上代码:
public class IfHasAuth extends Directive {
private Expr valueExpr;
public void setExprList(ExprList exprList) {
this.valueExpr = exprList.getExprArray()[0];
}
@Override
public void exec(Env env, Scope scope, Writer writer) {
HttpSession session = (HttpSession) scope.get("session");
if (null != session) {
User user = (User) session.getAttribute("user");
String actionKey = (String) valueExpr.eval(scope);
if (auth(user, actionKey)) {
stat.exec(env, scope, writer);
}
}
}
public boolean hasEnd() {
return true;
}
/**
* 判断用户是否有权限
*
* @param user
* --用户对象
* @param actionKey
* --访问地址
* @return
*/
private boolean auth(User user, String actionKey) {
if (user != null && StrKit.notBlank(actionKey)) {
String authUrls = user.getStr("ress");
for (String u : authUrls.split(",")) {
if (actionKey.equals(u)) {
return true;
}
}
}
return false;
}
页面上直接用#ifHasAuth("/user/add")
新增
#end 这样可以控制新增按钮是否显示了。以上代码是否有改进空间,请波总指教