Search in sources :

Example 11 with ActionHelper

use of app.hongs.action.ActionHelper in project HongsCORE by ihongs.

the class AuthAction method service.

/**
 * 服务方法
 * 判断配置和消息有没有生成, 如果没有则生成; 消息按客户语言存放
 * @param req
 * @param rsp
 * @throws java.io.IOException
 * @throws javax.servlet.ServletException
 */
@Override
public void service(HttpServletRequest req, HttpServletResponse rsp) throws ServletException, IOException {
    // 受是否登录、不同用户等影响, 权限经常变化,必须禁止缓存
    rsp.setHeader("Expires", "0");
    rsp.addHeader("Pragma", "no-cache");
    rsp.setHeader("Cache-Control", "no-cache");
    Core core = ActionDriver.getActualCore(req);
    ActionHelper helper = core.get(ActionHelper.class);
    String name = req.getPathInfo();
    if (name == null || name.length() == 0) {
        helper.error400("Path info required");
        return;
    }
    int p = name.lastIndexOf('.');
    if (p < 0) {
        helper.error400("File type required");
        return;
    }
    String type = name.substring(1 + p);
    name = name.substring(1, p);
    if (!"js".equals(type) && !"json".equals(type)) {
        helper.error400("Wrong file type: " + type);
        return;
    }
    String s;
    try {
        NaviMap sitemap = NaviMap.getInstance(name);
        Set<String> authset = sitemap.getAuthSet();
        // 没有设置 rsname 的不公开
        if (null == sitemap.session) {
            helper.error404("Auth data for '" + name + "' is not open to the public");
            return;
        }
        Map<String, Boolean> datamap = new HashMap();
        if (null == authset)
            authset = new HashSet();
        for (String act : sitemap.actions) {
            datamap.put(act, authset.contains(act));
        }
        s = Data.toString(datamap);
    } catch (HongsException | HongsExpedient | HongsError ex) {
        if (ex.getErrno() == 0x10e0) {
            helper.error404(ex.getMessage());
        } else {
            helper.error500(ex.getMessage());
        }
        return;
    }
    // 输出权限信息
    if ("json".equals(type)) {
        helper.print(s, "application/json");
    } else {
        String c = req.getParameter("callback");
        if (c != null && c.length() != 0) {
            if (!c.matches("^[a-zA-Z_\\$][a-zA-Z0-9_]*$")) {
                helper.error400("Illegal callback function name!");
                return;
            }
            helper.print("function " + c + "() { return " + s + "; }", "text/javascript");
        } else {
            helper.print("if(!self.HsAUTH)self.HsAUTH={};Object.assign(self.HsAUTH," + s + ");", "text/javascript");
        }
    }
}
Also used : HongsError(app.hongs.HongsError) HashMap(java.util.HashMap) HongsExpedient(app.hongs.HongsExpedient) NaviMap(app.hongs.action.NaviMap) HongsException(app.hongs.HongsException) ActionHelper(app.hongs.action.ActionHelper) Core(app.hongs.Core) HashSet(java.util.HashSet)

Example 12 with ActionHelper

use of app.hongs.action.ActionHelper in project HongsCORE by ihongs.

the class CmdletRunner method init.

public static String[] init(String[] args) throws IOException, HongsException {
    Map<String, Object> opts;
    opts = CmdletHelper.getOpts(args, "debug:i", "corepath:s", "confpath:s", "datapath:s", "basepath:s", "basehref:s", "language:s", "timezone:s");
    args = (String[]) opts.get("");
    Core.THREAD_CORE.set(Core.GLOBAL_CORE);
    Core.ACTION_TIME.set(Core.STARTS_TIME);
    /**
     * 核心属性配置 *
     */
    Core.ENVIR = 0;
    Core.DEBUG = Synt.declare(opts.get("debug"), (byte) 0);
    Core.CORE_PATH = Synt.declare(opts.get("corepath"), System.getProperty("user.dir"));
    Core.CORE_PATH = new File(Core.CORE_PATH).getAbsolutePath();
    Core.CONF_PATH = Synt.declare(opts.get("confpath"), Core.CORE_PATH + File.separator + "etc");
    Core.DATA_PATH = Synt.declare(opts.get("datapath"), Core.CORE_PATH + File.separator + "var");
    Core.BASE_PATH = Synt.declare(opts.get("basepath"), Core.CORE_PATH + File.separator + "web");
    Core.BASE_HREF = Synt.declare(opts.get("basehref"), "");
    // 如果 web 目录不存在, 则视为在 WEB-INF 下
    File bp = new File(Core.BASE_PATH);
    if (!bp.exists()) {
        Core.BASE_PATH = bp.getParentFile().getParent();
    }
    // 项目 url 须以 / 开头, 如有缺失则自动补全
    if (Core.BASE_HREF.length() != 0) {
        if (Core.BASE_HREF.startsWith("/") == false) {
            Core.BASE_HREF = "/" + Core.BASE_HREF;
        }
        if (Core.BASE_HREF.endsWith("/") == true) {
            Core.BASE_HREF = Core.BASE_HREF.substring(0, Core.BASE_HREF.length() - 1);
        }
    }
    /**
     * 系统属性配置 *
     */
    CoreConfig cnf;
    cnf = CoreConfig.getInstance();
    Core.SERVER_ID = cnf.getProperty("core.server.id", "0");
    cnf = CoreConfig.getInstance("defines");
    Map m = new HashMap();
    m.put("SERVER_ID", Core.SERVER_ID);
    m.put("BASE_PATH", Core.BASE_PATH);
    m.put("CORE_PATH", Core.CORE_PATH);
    m.put("CONF_PATH", Core.CONF_PATH);
    m.put("DATA_PATH", Core.DATA_PATH);
    // 启动系统属性
    for (Map.Entry et : cnf.entrySet()) {
        String k = (String) et.getKey();
        String v = (String) et.getValue();
        if (k.startsWith("envir.")) {
            k = k.substring(6);
            v = Tool.inject(v, m);
            System.setProperty(k, v);
        }
    }
    if (0 < Core.DEBUG && 8 != (8 & Core.DEBUG)) {
        // 调试系统属性
        for (Map.Entry et : cnf.entrySet()) {
            String k = (String) et.getKey();
            String v = (String) et.getValue();
            if (k.startsWith("debug.")) {
                k = k.substring(6);
                v = Tool.inject(v, m);
                System.setProperty(k, v);
            }
        }
    }
    /**
     * 实例属性配置 *
     */
    cnf = CoreConfig.getInstance();
    String act = null;
    if (args.length > 0) {
        List<String> argz = new ArrayList();
        argz.addAll(Arrays.asList(args));
        act = argz.remove(0);
        args = argz.toArray(new String[0]);
    }
    Core.ACTION_NAME.set(act);
    String zone = null;
    if (opts.containsKey("timezone")) {
        zone = (String) opts.get("timezone");
    }
    if (zone == null || zone.length() == 0) {
        if (cnf.getProperty("core.timezone.probing", false)) {
            zone = TimeZone.getDefault().getID();
        } else {
            zone = cnf.getProperty("core.timezone.default", "GMT+8");
        }
    }
    Core.ACTION_ZONE.set(zone);
    String lang = null;
    if (opts.containsKey("language")) {
        lang = (String) opts.get("language");
    }
    if (lang == null || lang.length() == 0) {
        if (cnf.getProperty("core.language.probing", false)) {
            /**
             * 获取系统默认的区域
             * 仅保留 语言[_地区]
             */
            lang = Locale.getDefault().toString();
            int pos = lang.indexOf('_');
            if (pos > 0) {
                pos = lang.indexOf('_', pos + 1);
                if (pos > 0) {
                    lang = lang.substring(0, pos);
                }
            }
            lang = CoreLocale.getAcceptLanguage(lang);
            if (lang == null) {
                lang = cnf.getProperty("core.language.default", "zh_CN");
            }
        } else {
            lang = cnf.getProperty("core.language.default", "zh_CN");
        }
    } else {
        /**
         * 检查语言参数设置
         */
        String leng;
        leng = lang;
        lang = CoreLocale.getAcceptLanguage(lang);
        if (lang == null) {
            CoreLogger.error("ERROR: Unsupported language: " + leng + ".");
            System.exit(1);
        }
    }
    Core.ACTION_LANG.set(lang);
    /**
     * 初始化动作助手, 可复用动作组件 *
     */
    ActionHelper hlpr = new ActionHelper(null, null, null, null);
    Core.getInstance().put(ActionHelper.class.getName(), hlpr);
    hlpr.updateOutput(System.out, new PrintWriter(System.out));
    // Clean instatnces fis core at exit
    Runtime.getRuntime().addShutdownHook(new Thread() {

        @Override
        public void run() {
            Core.THREAD_CORE.get().close();
            Core.GLOBAL_CORE.close();
        }
    });
    return args;
}
Also used : CoreConfig(app.hongs.CoreConfig) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) ActionHelper(app.hongs.action.ActionHelper) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 13 with ActionHelper

use of app.hongs.action.ActionHelper in project HongsCORE by ihongs.

the class SearchCmdlet method search.

@Cmdlet("search")
public void search(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "name=s", "id*s", "wd*s", "rb*s", "ob*s", "pn:i", "gn:i", "rn:i" });
    String conf = Synt.asString(opts.remove("conf"));
    String name = Synt.asString(opts.remove("name"));
    ActionHelper ah = Core.getInstance(ActionHelper.class);
    LuceneRecord so = LuceneRecord.getInstance(conf, name);
    Map req = ah.getRequestData();
    req.putAll(opts);
    Map rsp = so.search(req);
    CmdletHelper.preview(rsp);
}
Also used : ActionHelper(app.hongs.action.ActionHelper) LuceneRecord(app.hongs.dh.lucene.LuceneRecord) Map(java.util.Map) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 14 with ActionHelper

use of app.hongs.action.ActionHelper in project HongsCORE by ihongs.

the class SearchCmdlet method update.

@Cmdlet("update")
public void update(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, new String[] { "conf=s", "name=s", "id*s" });
    String conf = Synt.asString(opts.remove("conf"));
    String name = Synt.asString(opts.remove("name"));
    List<String> ds = Synt.asList(opts.remove("id"));
    ActionHelper ah = Core.getInstance(ActionHelper.class);
    LuceneRecord so = LuceneRecord.getInstance(conf, name);
    Map rd = ah.getRequestData();
    if (!rd.isEmpty()) {
        // 有数据则校验数据
        VerifyHelper vh = new VerifyHelper();
        vh.addRulesByForm(conf, name);
        rd = vh.verify(rd);
        try {
            so.begin();
            for (String id : ds) {
                so.set(id, rd);
            }
            so.commit();
        } catch (HongsException ex) {
            so.revert();
            throw ex;
        } finally {
            so.close();
        }
    } else {
        // 不给内容即为删除
        try {
            so.begin();
            for (String id : ds) {
                so.del(id);
            }
            so.commit();
        } catch (HongsException ex) {
            so.revert();
            throw ex;
        } finally {
            so.close();
        }
    }
}
Also used : VerifyHelper(app.hongs.action.VerifyHelper) HongsException(app.hongs.HongsException) ActionHelper(app.hongs.action.ActionHelper) LuceneRecord(app.hongs.dh.lucene.LuceneRecord) Map(java.util.Map) Cmdlet(app.hongs.cmdlet.anno.Cmdlet)

Example 15 with ActionHelper

use of app.hongs.action.ActionHelper in project HongsCORE by ihongs.

the class Dept method permit.

protected void permit(String id, Map data) throws HongsException {
    String pid = null;
    if (data != null) {
        // 权限限制, 仅能赋予当前登录用户所有的权限
        if (data.containsKey("roles")) {
            data.put("rtime", System.currentTimeMillis() / 1000);
            List list = Synt.asList(data.get("roles"));
            AuthKit.cleanDeptRoles(list, id);
            if (list.isEmpty()) {
                throw new HongsException.Notice("ex.master.user.dept.error").setLocalizedContext("master");
            }
            data.put("roles", list);
        }
        // 部门限制, 默认顶级, 是否可操作在下方判断
        pid = Synt.declare(data.get("pid"), "");
        if ("".equals(pid))
            pid = Cnst.ADM_GID;
    } else {
        // 删除限制, 如果部门下有用户则中止当前操作
        User user = new User();
        List list = user.table.fetchMore(user.fetchCase().gotJoin("depts").from("a_master_user_dept").by(FetchCase.INNER).on("`depts`.`user_id` = `user`.`id`").filter("`depts`.`dept_id` = ?", id).limit(1));
        if (list.size() != 0) {
            throw new HongsException.Notice("ex.master.dept.have.users").setLocalizedContext("master");
        }
    }
    if (id == null && pid == null) {
        throw new NullPointerException("id and pid cannot be all null");
    }
    if (id != null || pid != null) {
        // 超级管理员可操作任何部门
        ActionHelper helper = Core.getInstance(ActionHelper.class);
        String uid = (String) helper.getSessibute(Cnst.UID_SES);
        if (Cnst.ADM_UID.equals(uid)) {
            return;
        }
        // 超级管理组可操作任何部门
        // 但禁止操作顶级部门
        Set cur = AuthKit.getUserDepts(uid);
        if (cur.contains(Cnst.ADM_GID) && !Cnst.ADM_GID.equals(id)) {
            return;
        }
        // 仅可以操作下级部门
        for (Object gid : cur) {
            Set cld = new HashSet(this.getChildIds((String) gid, true));
            if (null != pid && (gid.equals(pid) || cld.contains(pid))) {
                return;
            }
            if (null != id && cld.contains(id)) {
                return;
            }
        }
        throw new HongsException.Notice("ex.master.dept.unit.error").setLocalizedContext("master");
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HongsException(app.hongs.HongsException) ActionHelper(app.hongs.action.ActionHelper) List(java.util.List) HashSet(java.util.HashSet)

Aggregations

ActionHelper (app.hongs.action.ActionHelper)21 HashSet (java.util.HashSet)10 Map (java.util.Map)7 Set (java.util.Set)7 HongsException (app.hongs.HongsException)5 HashMap (java.util.HashMap)5 Core (app.hongs.Core)4 HongsError (app.hongs.HongsError)4 ActionRunner (app.hongs.action.ActionRunner)4 List (java.util.List)4 NaviMap (app.hongs.action.NaviMap)3 Cmdlet (app.hongs.cmdlet.anno.Cmdlet)3 HongsExpedient (app.hongs.HongsExpedient)2 Table (app.hongs.db.Table)2 LuceneRecord (app.hongs.dh.lucene.LuceneRecord)2 SimpleDateFormat (java.text.SimpleDateFormat)2 ArrayList (java.util.ArrayList)2 Date (java.util.Date)2 CoreConfig (app.hongs.CoreConfig)1 FormSet (app.hongs.action.FormSet)1