Play

来源:互联网 发布:不用网络的游戏大全 编辑:程序博客网 时间:2024/04/28 10:20
play.mvc.Controller:play framework的控制器基类

Scope.Flash flash:一个存放在hash map中存储临时变量的变量范围。。通常情况下,在request的处理过程中把值放进去,然后在紧接着的下一个请求处理中被使用,下一个请求(第2个请求)处理完毕后,值即被销毁

Scope.Params params:在整个请求的处理过程中都能被访问到,即存放当前请求的参数

Scope.RenderArgs renderArgs:一个存放在hash map中并在渲染阶段使用的变量范围。存储在这里的变量在渲染阶段(即模板阶段)被使用。

Scope.RouteArgs routeArgs:一个存放在hash map中并在路由处理阶段使用的变量范围。

Scope.Session session:play 1.x framework中,session并不存储在服务器端,而是存储在客户端。实际上它存储在cookie中,因此大小被限制在4kb以内。

---------------------------------------------------------------------------------------------------

package controllers;

import org.apache.commons.lang.StringUtils;

import com.tom.basic.service.ServiceResult;

import basic.service.AuthService;
import infrastructure.play.control.ControllerUtils;
import infrastructure.support.Constants;
import play.data.validation.Required;
import play.mvc.Before;
import seller.model.Seller;
import seller.model.Store;
import seller.service.SellerService;
import user.model.User;

public class Secure extends ControllerBase {

    /**
     * @throws Throwable
     */
    @Before(unless = { "login", "authenticate", "logout","Application.home","CommonMgr.imageNotify",
            "TradeMgr.exportQrcodeImg","register","SellerMgr.doRegister","SellerMgr.doCheckField",
            "SellerMgr.imageCode"})
    static void checkAccess() throws Throwable {
        String username = session.get(Constants.SESSION_KEY_USER);
        if (username == null) {
            session.clear();
//            String[] url =  request.action.split("\\.");
            session.put(Constants.SESSION_KEY_RELOGIN_CONTROLLER, request.url);
            if(request.headers.get("x-requested-with") != null){
                renderJSON(ServiceResult.asSuccess("ReLogin"));
            }
            login();
        } else {
            User curUser = User.find("byName", username).first();
            if (curUser == null) {
                login();
            } else {
                //菜单权限过滤拦截
                if(curUser.isSeller()){
                    Seller curSeller = Seller.findById(curUser.extendId);
                    String storeId = session.get(Constants.SESSION_KEY_STORE);
                    Store curStore = null;
                    if(StringUtils.isNotBlank(storeId)){
                        curStore = Store.findById(Long.parseLong(storeId));
                    }else{
                        if(curUser.getDefaultStoreIdFromOrderMeta() != null){
                            curStore = Store.findById(curUser.getDefaultStoreIdFromOrderMeta());
                        }
                    }
                    if(AuthService.isNeedAuth(request.actionMethod,curUser) &&
                            !request.action.equals("Application.index") && !AuthService.isSellerMenu(request.path,curUser,curSeller,curStore)){
                        renderTemplate("/Secure/nonAuthority.html");
                    }
                    
                    threadSeller.set(curSeller);
                    ControllerUtils.addAttri4Render("currentSeller", curSeller);//模板中都可以获取到
                    
                    threadStore.set(curStore);
                    ControllerUtils.addAttri4Render("currentStore", curStore);
                    
                }else{
                    if(AuthService.isNeedAuth(request.actionMethod,curUser) && StringUtils.isNotBlank(curUser.authSourcesStr)&&
                            !request.action.equals("Application.index") && !curUser.authSourcesStr.contains(request.actionMethod)){
                        renderTemplate("/Secure/nonAuthority.html");
                    }
                    
                    threadSeller.set(null);
                    threadStore.set(null);
                }
                
                threadUser.set(curUser);
                ControllerUtils.addAttri4Render("currentUser", curUser);
                flash.put(Constants.SESSION_KEY_USER, curUser);
                
                // 操作权限过滤拦截
                if (!AuthService.authenticateByUrl(request.url,currentUser())){
                    renderJSON(ServiceResult.asFail("Current user cannot operate!").toJson());
                }
            }
        }
    }

    // ~~~ Login
    public static void login() throws Throwable {
        User rootUser = User.find("byName", "root").first();
        if (rootUser == null) {
            rootUser = new User();
            rootUser.password = "root";
            rootUser.name = "root";
            rootUser.save();
        }
        if(StringUtils.isNotBlank(session.get(Constants.SESSION_KEY_RELOGIN_CONTROLLER))){
            render("Secure/relogin.html");
        }
        if(request.domain.contains("admin")){
            render("Secure/login_admin.html");
        }else{
            render("Secure/login.html");
        }
    }

    public static void authenticate(@Required String username, String password)
            throws Throwable {

        User curUser = User.find("byIsDeleteAndNameAndPassword", 0 ,username, password)
                .first();
        if (curUser != null) {
            Long storeIdDefault = curUser.getDefaultStoreIdFromOrderMeta();
            if(storeIdDefault == null){
                session.put(Constants.SESSION_KEY_STORE, "");
            }else{
                session.put(Constants.SESSION_KEY_STORE, storeIdDefault);
            }
            session.put(Constants.SESSION_KEY_USER, username);
            ServiceResult<String> result = SellerService
                    .checkPassword(password);
            if (!result.success) {
                Long userId = curUser.id;
                renderTemplate("/Secure/modifyPassword.html", userId);
            }
            if(StringUtils.isNotBlank(session.get(Constants.SESSION_KEY_RELOGIN_CONTROLLER))){
                String[] url =  session.get(Constants.SESSION_KEY_RELOGIN_CONTROLLER).split("\\/");
                if(url[url.length -1].startsWith("do")){
                    redirect("/admin/");
                }
                redirect(session.get(Constants.SESSION_KEY_RELOGIN_CONTROLLER));
            }else{
                redirect("/admin/");
            }
            
            
        } else {
            params.flash();
            flash.error("登录密码错误,请重试");
            login();
        }
    }

    public static void logout() throws Throwable {
        session.clear();
        login();
    }
    
    public static void register() {
        render();
    }

}

public static void addAttri4Render(String key, Object value) {
        Scope.RenderArgs templateBinding = Scope.RenderArgs.current();
        Map<String, Object> argsMap = new HashMap<String, Object>();
        argsMap.put(key, value);
        templateBinding.data.putAll(argsMap);
    }

原创粉丝点击