Search in sources :

Example 11 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class Model method field.

// ** 查询字段处理 **/
/**
 * 绑定许可字段
 * @param caze  查询用例
 * @param rb    排序字段, 结构 { FIELD1, FIELD2... }, 后跟 ! 号或前跟 - 号表逆序
 */
protected final void field(FetchCase caze, Set rb) {
    if (rb == null) {
        rb = new HashSet();
    }
    // 许可的字段
    Map<String, Object[]> af = new LinkedHashMap();
    // 通配符字段
    Map<String, Set<String>> cf = new HashMap();
    // 可关联的表
    Set<String> tc = caze.getOption("ASSOCS", new HashSet());
    // 包含字段
    Set<String> ic = new LinkedHashSet();
    // 排除字段
    Set<String> ec = new LinkedHashSet();
    Set<String> xc;
    allow(caze, af);
    // 整理出层级结构, 方便处理通配符
    for (Map.Entry<String, Object[]> et : af.entrySet()) {
        String fn = et.getKey();
        int p = fn.lastIndexOf(".");
        String k;
        if (p > -1) {
            k = fn.substring(0, p) + ".*";
        } else {
            k = "*";
        }
        Set<String> fs = cf.get(k);
        if (fs == null) {
            fs = new LinkedHashSet();
            cf.put(k, fs);
            // 当字段别名有点时表示是 JOIN 关联, 这种情况总是需要查询
            Object[] fa = et.getValue();
            // 别名
            String ln = (String) fa[1];
            // 用例
            FetchCase fc = (FetchCase) fa[2];
            if (ln.contains(".")) {
                tc.add(fc.getName());
            }
        }
        fs.add(fn);
    }
    for (Object fo : rb) {
        String fn = fo.toString();
        if (fn.endsWith("!")) {
            // 新的排除后缀
            fn = fn.substring(0, fn.length() - 1);
            xc = ec;
        } else if (fn.startsWith("-")) {
            // 旧的排除前缀
            fn = fn.substring(1);
            xc = ec;
        } else {
            xc = ic;
        }
        if (cf.containsKey(fn)) {
            xc.addAll(cf.get(fn));
        } else if (af.containsKey(fn)) {
            xc.add(fn);
            // 排除时, 先在包含中增加全部
            if (xc == ec) {
                int p = fn.lastIndexOf(".");
                if (p != -1) {
                    fn = fn.substring(0, p) + ".*";
                } else {
                    fn = "*";
                }
                ic.addAll(cf.get(fn));
            }
        }
    }
    // 默认没给就是全部
    if (ic.isEmpty() == true) {
        ic.addAll(af.keySet());
    }
    // 排除字段即取差集
    if (ec.isEmpty() == false) {
        ic.removeAll(ec);
    }
    for (String kn : ic) {
        Object[] fa = af.get(kn);
        // 字段
        String fn = (String) fa[0];
        // 别名
        String ln = (String) fa[1];
        // 用例
        FetchCase fc = (FetchCase) fa[2];
        if (fn != null && ln != null) {
            fc.select(fn + " AS `" + ln + "`");
        }
        tc.add(fc.getName());
    }
    caze.setOption("ASSOCS", tc);
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Set(java.util.Set) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet) FetchCase(io.github.ihongs.db.util.FetchCase) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map) HashSet(java.util.HashSet) LinkedHashSet(java.util.LinkedHashSet)

Example 12 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class SignAction method signCreate.

/**
 * 登录
 * @param ah
 * @throws HongsException
 */
@Action("create")
@Verify(conf = "master", form = "sign")
@CommitSuccess
public void signCreate(ActionHelper ah) throws HongsException {
    String place = Synt.declare(ah.getParameter("place"), "centre");
    String username = Synt.declare(ah.getParameter("username"), "");
    String password = Synt.declare(ah.getParameter("password"), "");
    String passcode;
    DB db = DB.getInstance("master");
    Table tb = db.getTable("user");
    FetchCase fc;
    Map ud;
    String id;
    String tt;
    int at;
    int rt;
    // 检查账号
    fc = new FetchCase().from(tb.tableName).filter("username = ?", username).select("password, passcode, id, name, head, state");
    ud = db.fetchLess(fc);
    if (ud.isEmpty()) {
        ah.reply(AuthKit.getWrong("username", "core.username.invalid"));
        return;
    }
    // 重试限制
    CoreConfig cc = CoreConfig.getInstance("master");
    id = (String) ud.get("id");
    tt = Synt.declare(cc.getProperty("core.sign.retry.token"), "");
    at = Synt.declare(cc.getProperty("core.sign.retry.times"), 5);
    if (Synt.declare(Record.get("sign.retry.allow." + id), false)) {
        tt = "id";
    }
    switch(tt) {
        case "id":
            break;
        case "ip":
            id = Core.CLIENT_ADDR.get();
            break;
        default:
            id = id + "-" + Core.CLIENT_ADDR.get();
    }
    rt = Synt.declare(Record.get("sign.retry.times." + id), 0);
    if (rt >= at) {
        ah.reply(AuthKit.getWrong("password", "core.password.timeout"));
        ah.getResponseData().put("allow_times", at);
        ah.getResponseData().put("retry_times", rt);
        return;
    } else {
        rt++;
    }
    // 校验密码
    passcode = Synt.declare(ud.get("passcode"), "");
    password = AuthKit.getCrypt(password + passcode);
    if (!password.equals(ud.get("password"))) {
        ah.reply(AuthKit.getWrong("password", "core.password.invalid"));
        ah.getResponseData().put("allow_times", at);
        ah.getResponseData().put("retry_times", rt);
        // 记录错误次数
        Calendar ca;
        long et;
        ca = Calendar.getInstance(Core.getTimezone());
        ca.setTimeInMillis(Core.ACTION_TIME.get());
        ca.set(Calendar.HOUR_OF_DAY, 23);
        ca.set(Calendar.MINUTE, 59);
        ca.set(Calendar.SECOND, 59);
        et = ca.getTimeInMillis() / 1000 + 1;
        Record.set("sign.retry.times." + id, rt, et);
        return;
    } else {
        Record.del("sign.retry.times." + id);
    }
    String uuid = (String) ud.get("id");
    String uname = (String) ud.get("name");
    String uhead = (String) ud.get("head");
    int state = Synt.declare(ud.get("state"), 0);
    // 验证状态
    if (0 >= state) {
        ah.reply(AuthKit.getWrong("state", "core.sign.state.invalid"));
        return;
    }
    // 规避自定 RoleSet 附加判断
    ah.setSessibute(Cnst.UID_SES, null);
    ah.setSessibute(Cnst.USK_SES, null);
    ah.setSessibute(Cnst.UST_SES, null);
    // 验证区域
    Set rs = RoleSet.getInstance(uuid);
    if (rs != null && !place.isEmpty() && !rs.contains(place)) {
        ah.reply(AuthKit.getWrong("place", "core.sign.place.invalid"));
        return;
    }
    // * 表示密码登录
    Map sd = AuthKit.userSign(ah, "*", uuid, uname, uhead);
    ah.reply(Synt.mapOf("info", sd));
}
Also used : Table(io.github.ihongs.db.Table) FetchCase(io.github.ihongs.db.util.FetchCase) Set(java.util.Set) RoleSet(io.github.ihongs.serv.auth.RoleSet) CoreConfig(io.github.ihongs.CoreConfig) Calendar(java.util.Calendar) Map(java.util.Map) DB(io.github.ihongs.db.DB) Action(io.github.ihongs.action.anno.Action) CommitSuccess(io.github.ihongs.action.anno.CommitSuccess) Verify(io.github.ihongs.action.anno.Verify)

Example 13 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class DeptAction method isUnique.

@Action("unique")
public void isUnique(ActionHelper helper) throws HongsException {
    Map rd = helper.getRequestData();
    FetchCase fc = model.fetchCase();
    fc.setOption("INCLUDE_REMOVED", Synt.declare(rd.get("include-removed"), false));
    boolean rv = model.unique(rd, fc);
    helper.reply(null, rv ? 1 : 0);
}
Also used : FetchCase(io.github.ihongs.db.util.FetchCase) HashMap(java.util.HashMap) NaviMap(io.github.ihongs.action.NaviMap) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action)

Example 14 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class TestDB method testAssocCase.

@Test
public void testAssocCase() throws HongsException {
    FetchCase fc = new FetchCase();
    AssocCase ac = new AssocCase(fc);
    fc.from("a_table", "table1").join("b_table", "table2", "table1.id=table2.a_id");
    ac.allow(AssocCase.LISTABLE, "id", "name", "ctime", "mtime", "state", "t2_id:table2.id", "t2_name:table2.name");
    ac.allow(AssocCase.UNSTORED, "state");
    ac.parse(Synt.mapOf("id", Synt.setOf("1", "2", "3"), "rb", "*,table2.name", "ob", "ctime!,state", "state", Synt.mapOf("gt", 1)));
    assertEquals(fc.toString(), "SELECT `table1`.`id` AS `id`, `table1`.`name` AS `name`, `table1`.`ctime` AS `ctime`, `table1`.`mtime` AS `mtime`, `table1`.`state` AS `state`, table2.id AS `t2_id`, table2.name AS `t2_name` FROM `a_table` AS `table1` INNER JOIN `b_table` AS `table2` ON table1.id=table2.a_id WHERE `table1`.`id` IN ('1','2','3') AND `table1`.`state` > 1 ORDER BY `table1`.`ctime` DESC, `table1`.`state`");
    // System.out.println(fc);
    Map ss = ac.saves(Synt.mapOf("id", "123", "name", "hh", "note", "nn", "state", 6));
    Map zz = new HashMap(Synt.mapOf("id", "123", "name", "hh"));
    assertEquals(ss.toString(), zz.toString());
// System.out.println(ss);
}
Also used : FetchCase(io.github.ihongs.db.util.FetchCase) AssocCase(io.github.ihongs.db.util.AssocCase) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Test(org.junit.Test)

Example 15 with FetchCase

use of io.github.ihongs.db.util.FetchCase in project HongsCORE by ihongs.

the class DBAction method isExists.

@Action("exists")
public void isExists(ActionHelper helper) throws HongsException {
    Model ett = getEntity(helper);
    Map req = helper.getRequestData();
    req = getReqMap(helper, ett, "exists", req);
    FetchCase c = new FetchCase();
    c.setOption("INCLUDE_REMOVED", Synt.declare(req.get("include-removed"), false));
    boolean val = ett.exists(req, c);
    helper.reply(null, val ? 1 : 0);
}
Also used : FetchCase(io.github.ihongs.db.util.FetchCase) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action) IAction(io.github.ihongs.dh.IAction)

Aggregations

FetchCase (io.github.ihongs.db.util.FetchCase)15 Map (java.util.Map)12 HashMap (java.util.HashMap)7 Action (io.github.ihongs.action.anno.Action)5 LinkedHashMap (java.util.LinkedHashMap)4 DB (io.github.ihongs.db.DB)3 Table (io.github.ihongs.db.Table)3 LinkedHashSet (java.util.LinkedHashSet)3 HongsException (io.github.ihongs.HongsException)2 NaviMap (io.github.ihongs.action.NaviMap)2 IAction (io.github.ihongs.dh.IAction)2 Collection (java.util.Collection)2 HashSet (java.util.HashSet)2 Set (java.util.Set)2 Test (org.junit.Test)2 CoreConfig (io.github.ihongs.CoreConfig)1 HongsExemption (io.github.ihongs.HongsExemption)1 ActionHelper (io.github.ihongs.action.ActionHelper)1 CommitSuccess (io.github.ihongs.action.anno.CommitSuccess)1 Verify (io.github.ihongs.action.anno.Verify)1