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);
}
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));
}
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);
}
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);
}
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);
}
Aggregations