Search in sources :

Example 6 with ActionHelper

use of io.github.ihongs.action.ActionHelper in project HongsCORE by ihongs.

the class CmdletRunner method init.

/**
 * 命令启动初始化
 * @param args
 * @return
 */
public static String[] init(String[] args) {
    Map<String, Object> opts;
    opts = CmdletHelper.getOpts(args, "DEBUG:i", "COREPATH:s", "CONFPATH:s", "DATAPATH:s", "BASEPATH:s", "BASEHREF:s", "LANGUAGE:s", "TIMEZONE:s", "!U", "!A");
    args = (String[]) opts.get("");
    Core.THREAD_CORE.set(Core.GLOBAL_CORE);
    Core.ACTION_TIME.set(Core.STARTS_TIME);
    Core.ACTION_NAME.set(args.length != 0 ? args[0] : null);
    /**
     * 核心属性配置 *
     */
    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");
    // 如果 web 目录不存在, 则视为在 WEB-INF 下
    File bp = new File(Core.BASE_PATH);
    if (!bp.exists()) {
        Core.BASE_PATH = bp.getParent();
    }
    /**
     * 系统属性配置 *
     */
    CoreConfig cnf = CoreConfig.getInstance("defines");
    Core.SERVER_ID = cnf.getProperty("server.id", "0");
    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 = Syno.inject(v, m);
            System.setProperty(k, v);
        }
    }
    if (4 == (4 & 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 = Syno.inject(v, m);
                System.setProperty(k, v);
            }
        }
    }
    /**
     * 提取网址前缀 *
     */
    String su = Synt.defoult((String) opts.get("BASEHREF"), System.getProperty("serv.url"));
    if (null != su) {
        Pattern pattern = Pattern.compile("^\\w+://[^/]+");
        Matcher matcher = pattern.matcher(su);
        if (matcher.find()) {
            Core.SERV_PATH = su.substring(0 + matcher.end());
            Core.SERV_HREF = su.substring(0, matcher.end());
        } else {
            Core.SERV_PATH = su;
            Core.SERV_HREF = "";
        }
    } else {
        Core.SERV_PATH = "";
        Core.SERV_HREF = "";
    }
    /**
     * 实例属性配置 *
     */
    cnf = CoreConfig.getInstance("default");
    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", Cnst.ZONE_DEF);
        }
    }
    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", Cnst.LANG_DEF);
            }
        } else {
            lang = cnf.getProperty("core.language.default", Cnst.ZONE_DEF);
        }
    } 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().set(ActionHelper.class.getName(), hlpr);
    hlpr.updateOutput(System.out, new PrintWriter(System.out));
    return args;
}
Also used : Pattern(java.util.regex.Pattern) CoreConfig(io.github.ihongs.CoreConfig) HashMap(java.util.HashMap) Matcher(java.util.regex.Matcher) ActionHelper(io.github.ihongs.action.ActionHelper) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) PrintWriter(java.io.PrintWriter)

Example 7 with ActionHelper

use of io.github.ihongs.action.ActionHelper in project HongsCORE by ihongs.

the class Unit method filter.

@Override
protected void filter(FetchCase caze, Map rd) throws HongsException {
    super.filter(caze, rd);
    // 超级管理员不做限制
    ActionHelper helper = Core.getInstance(ActionHelper.class);
    String uid = (String) helper.getSessibute(Cnst.UID_SES);
    if (Cnst.ADM_UID.equals(uid)) {
        return;
    }
    String mm = caze.getOption("MODEL_START", "");
    if ("getList".equals(mm) || "getInfo".equals(mm)) {
    // mm = "/search";
    } else if ("update".equals(mm) || "delete".equals(mm)) {
    // mm = "/" + mm ;
    } else {
        // 非常规动作不限制
        return;
    }
    // 从导航表中取单元ID
    Set<String> us = new HashSet();
    NaviMap nv = NaviMap.getInstance(centra);
    getSubUnits(nv.menus, nv.getRoleSet(), us);
    // 限制为有权限的单元
    caze.filter("`" + table.name + "`.`id` IN (?)", us);
}
Also used : ActionHelper(io.github.ihongs.action.ActionHelper) NaviMap(io.github.ihongs.action.NaviMap) HashSet(java.util.HashSet)

Example 8 with ActionHelper

use of io.github.ihongs.action.ActionHelper in project HongsCORE by ihongs.

the class Form method deleteAuthRole.

protected void deleteAuthRole(String id) throws HongsException {
    ActionHelper helper = Core.getInstance(ActionHelper.class);
    String uid = (String) helper.getSessibute(Cnst.UID_SES);
    String tan;
    // 删除权限
    tan = (String) table.getParams().get("role.table");
    if (tan != null) {
        Table tab = db.getTable(tan);
        tab.remove("`role` IN (?)", Synt.setOf(centra + "/" + id + "/search", centra + "/" + id + "/create", centra + "/" + id + "/update", centra + "/" + id + "/delete", centra + "/" + id + "/revert"));
    }
    // 更新缓存(通过改变权限更新时间)
    tan = (String) table.getParams().get("user.table");
    if (tan != null) {
        Table tab = db.getTable(tan);
        tab.update(Synt.mapOf("rtime", System.currentTimeMillis() / 1000), "`id` = ?", uid);
    }
}
Also used : Table(io.github.ihongs.db.Table) ActionHelper(io.github.ihongs.action.ActionHelper)

Example 9 with ActionHelper

use of io.github.ihongs.action.ActionHelper in project HongsCORE by ihongs.

the class Form method filter.

@Override
protected void filter(FetchCase caze, Map rd) throws HongsException {
    super.filter(caze, rd);
    // 超级管理员不做限制
    ActionHelper helper = Core.getInstance(ActionHelper.class);
    String uid = (String) helper.getSessibute(Cnst.UID_SES);
    if (Cnst.ADM_UID.equals(uid)) {
        return;
    }
    String mm = caze.getOption("MODEL_START", "");
    if ("getList".equals(mm) || "getInfo".equals(mm)) {
        mm = "/search";
    } else if ("update".equals(mm) || "delete".equals(mm)) {
        mm = "/" + mm;
    } else {
        // 非常规动作不限制
        return;
    }
    // 从权限串中取表单ID
    NaviMap nm = NaviMap.getInstance(centra);
    String pm = centra + "/";
    Set<String> ra = nm.getRoleSet();
    Set<String> rs = new HashSet();
    for (String rn : ra) {
        if (rn.startsWith(pm) && rn.endsWith(mm)) {
            rs.add(rn.substring(pm.length(), rn.length() - mm.length()));
        }
    }
    // 限制为有权限的表单
    caze.filter("`" + table.name + "`.`id` IN (?)", rs);
}
Also used : ActionHelper(io.github.ihongs.action.ActionHelper) NaviMap(io.github.ihongs.action.NaviMap) HashSet(java.util.HashSet)

Example 10 with ActionHelper

use of io.github.ihongs.action.ActionHelper in project HongsCORE by ihongs.

the class Form method insertAuthRole.

protected void insertAuthRole(String id) throws HongsException {
    ActionHelper helper = Core.getInstance(ActionHelper.class);
    String uid = (String) helper.getSessibute(Cnst.UID_SES);
    String tan;
    // 写入权限
    tan = (String) table.getParams().get("role.table");
    if (tan != null) {
        Table tab = db.getTable(tan);
        tab.insert(Synt.mapOf("user_id", uid, "role", centra + "/" + id + "/search"));
        tab.insert(Synt.mapOf("user_id", uid, "role", centra + "/" + id + "/create"));
        tab.insert(Synt.mapOf("user_id", uid, "role", centra + "/" + id + "/update"));
        tab.insert(Synt.mapOf("user_id", uid, "role", centra + "/" + id + "/delete"));
        tab.insert(Synt.mapOf("user_id", uid, "role", centra + "/" + id + "/revert"));
    }
    // 更新缓存(通过改变权限更新时间)
    tan = (String) table.getParams().get("user.table");
    if (tan != null) {
        Table tab = db.getTable(tan);
        tab.update(Synt.mapOf("rtime", System.currentTimeMillis() / 1000), "`id` = ?", uid);
    }
}
Also used : Table(io.github.ihongs.db.Table) ActionHelper(io.github.ihongs.action.ActionHelper)

Aggregations

ActionHelper (io.github.ihongs.action.ActionHelper)21 HashSet (java.util.HashSet)11 Set (java.util.Set)8 HongsException (io.github.ihongs.HongsException)7 HashMap (java.util.HashMap)7 Map (java.util.Map)7 Core (io.github.ihongs.Core)6 List (java.util.List)5 HongsExemption (io.github.ihongs.HongsExemption)4 Table (io.github.ihongs.db.Table)4 NaviMap (io.github.ihongs.action.NaviMap)3 ActionRunner (io.github.ihongs.action.ActionRunner)2 PrintWriter (java.io.PrintWriter)2 CoreConfig (io.github.ihongs.CoreConfig)1 CoreSerial (io.github.ihongs.CoreSerial)1 FormSet (io.github.ihongs.action.FormSet)1 Action (io.github.ihongs.action.anno.Action)1 Cmdlet (io.github.ihongs.cmdlet.anno.Cmdlet)1 DB (io.github.ihongs.db.DB)1 FetchCase (io.github.ihongs.db.util.FetchCase)1