Search in sources :

Example 1 with Wrongs

use of io.github.ihongs.util.verify.Wrongs in project HongsCORE by ihongs.

the class AuthKit method openSign.

/**
 * 第三方登录
 * @param ah
 * @param unit
 * @param code
 * @param uname 名称
 * @param uhead 头像
 * @return
 * @throws HongsException
 */
public static Map openSign(ActionHelper ah, String unit, String code, String uname, String uhead) throws HongsException {
    DB db = DB.getInstance("master");
    Table tb = db.getTable("user_sign");
    Table ub = db.getTable("user");
    Map ud = tb.fetchCase().from(tb.tableName, "s").join(ub.tableName, "u", "`u`.`id` = `s`.`user_id`").filter("`s`.`unit` = ? AND `s`.`code` = ?", unit, code).select("`u`.`id`, `u`.`name`, `u`.`head`, `u`.`state`").getOne();
    int stat = Synt.declare(ud.get("state"), 0);
    String uuid;
    if (!ud.isEmpty()) {
        // 锁定或系统账号
        if (stat <= 0) {
            throw new Wrongs(Synt.mapOf("state", new Wrong("core.sign.state.invalid"))).setLocalizedContext("master");
        // .setLocalizedOptions(  stat  );
        }
        uuid = (String) ud.get("id");
        uname = (String) ud.get("name");
        uhead = (String) ud.get("head");
    } else {
        ud = new HashMap();
        ud.put("name", uname);
        ud.put("head", uhead);
        // 校验及下载头像
        ud = new VerifyHelper().addRulesByForm("master", "user").verify(ud, true, true);
        uuid = db.getModel("user").add(ud);
        uname = (String) ud.get("name");
        uhead = (String) ud.get("head");
        // 第三方登录项
        ud = new HashMap();
        ud.put("user_id", uuid);
        ud.put("unit", unit);
        ud.put("code", code);
        db.getTable("user_sign").insert(ud);
        // 加入公共部门
        ud = new HashMap();
        ud.put("user_id", uuid);
        ud.put("dept_id", "CENTRE");
        db.getTable("dept_user").insert(ud);
    // 赋予公共权限. 仅用部门即可(2019/02/28)
    // ud  =  new HashMap( );
    // ud.put("user_id",  uuid   );
    // ud.put("role"   , "centre");
    // db.getTable("user_role").insert(ud);
    }
    ud = userSign(ah, unit, uuid, uname, uhead);
    ud.put("unit", /**/
    unit);
    ud.put("regs", 0 == stat);
    return ud;
}
Also used : VerifyHelper(io.github.ihongs.action.VerifyHelper) Wrong(io.github.ihongs.util.verify.Wrong) Table(io.github.ihongs.db.Table) HashMap(java.util.HashMap) Wrongs(io.github.ihongs.util.verify.Wrongs) HashMap(java.util.HashMap) Map(java.util.Map) DB(io.github.ihongs.db.DB)

Example 2 with Wrongs

use of io.github.ihongs.util.verify.Wrongs in project HongsCORE by ihongs.

the class VerifyInvoker method invoke.

@Override
public void invoke(ActionHelper helper, ActionRunner chains, Annotation anno) throws HongsException {
    Verify ann = (Verify) anno;
    String conf = ann.conf();
    String form = ann.form();
    byte mode = ann.mode();
    byte type = ann.type();
    byte trim = ann.trim();
    // 准备数据
    Map<String, Object> dat = helper.getRequestData();
    if (mode == -1) {
        Set ab = Synt.toTerms(helper.getRequestData().get(Cnst.AB_KEY));
        if (ab != null) {
            if (ab.contains(".errs")) {
                mode = 1;
            } else if (ab.contains("!errs")) {
                mode = 2;
            }
        }
    }
    if (type == -1) {
        Boolean up = Synt.asBool(helper.getAttribute(Cnst.UPDATE_MODE));
        if (up == null) {
            Object hd = chains.getHandle();
            Object id = dat.get(Cnst.ID_KEY);
            type = /**
             */
            "update".equals(hd) || (null != id && !"".equals(id)) ? (byte) 1 : (byte) 0;
        } else {
            type = up ? (byte) 1 : (byte) 0;
        }
    }
    boolean prp = mode <= 0;
    boolean upd = type == 1;
    boolean cln = trim == 1;
    // 识别路径
    if (form.length() == 0) {
        form = chains.getEntity();
    }
    if (conf.length() == 0) {
        conf = chains.getModule();
    // 照顾 Module Action 的配置规则. 2018/7/7 改为完全由外部预判
    // if (FormSet.hasConfFile(conf+"/"+form)) {
    // conf = conf+"/"+form ;
    // }
    }
    // 执行校验
    try {
        Map data = (Map) helper.getAttribute("form:" + conf + "!" + form);
        if (data == null) {
            data = FormSet.getInstance(conf).getForm(form);
        }
        VerifyHelper ver = new VerifyHelper();
        ver.addRulesByForm(conf, form, data);
        Map vls = ver.verify(dat, upd, prp);
        if (cln)
            dat.clear();
        dat.putAll(vls);
    } catch (Wrongs err) {
        dat = err.toReply(prp ? 0 : mode);
        helper.reply(dat);
        // Servlet 环境下设置状态码为 400 (错误的请求)
        if (helper.getResponse() != null) {
            helper.getResponse().setStatus(SC_BAD_REQUEST);
        }
        // 记录可能引起错误的原因
        if (0 != Core.DEBUG) {
            Throwable tr = err.getCause();
            if (null != tr) {
                CoreLogger.error(tr);
            }
        }
        return;
    } catch (HongsException ex) {
        int ec = ex.getErrno();
        if (ec != 910 && ec != 911 && ec != 912) {
            // 非表单缺失
            throw ex;
        }
    }
    chains.doAction();
}
Also used : FormSet(io.github.ihongs.action.FormSet) Set(java.util.Set) VerifyHelper(io.github.ihongs.action.VerifyHelper) HongsException(io.github.ihongs.HongsException) Wrongs(io.github.ihongs.util.verify.Wrongs) Map(java.util.Map)

Example 3 with Wrongs

use of io.github.ihongs.util.verify.Wrongs in project HongsCORE by ihongs.

the class TestVerify method testVerify.

@Test
public void testVerify() throws IOException {
    // 避免路径缺失致写入项目主目录
    CmdletRunner.init(new String[] { "--COREPATH", "target" });
    try {
        Verify verify = new Verify().addRule("name", new Required(), new IsString().config(Synt.mapOf("minlength", 3, "maxlength", 8))).addRule("size", new Repeated().config(Synt.mapOf("slice", ",")), new IsNumber().config(Synt.mapOf("min", 10, "max", 99)));
        Map values = Synt.mapOf("name", "abcdef", "size", "11,22");
        Map cleans = verify.verify(values, false, false);
        Map cleanz = Synt.mapOf("name", "abcdef", "size", Arrays.asList(11, 22));
        // System.out.println(Dawn.toString(cleans));
        // System.out.println(Dawn.toString(cleanz));
        assertEquals(Dawn.toString(cleans, true), Dawn.toString(cleanz, true));
    } catch (Wrongs wr) {
        Core.ACTION_LANG.set(Cnst.LANG_DEF);
        fail(Dawn.toString(wr.getErrors()));
    }
}
Also used : IsNumber(io.github.ihongs.util.verify.IsNumber) Required(io.github.ihongs.util.verify.Required) Wrongs(io.github.ihongs.util.verify.Wrongs) IsString(io.github.ihongs.util.verify.IsString) Repeated(io.github.ihongs.util.verify.Repeated) Verify(io.github.ihongs.util.verify.Verify) Map(java.util.Map) Test(org.junit.Test)

Example 4 with Wrongs

use of io.github.ihongs.util.verify.Wrongs in project HongsCORE by ihongs.

the class AuthKit method getWrong.

/**
 * 快速输出登录的错误
 * @param k 字段
 * @param w 错误
 * @return
 */
public static Map getWrong(String k, String w) {
    CoreLocale l = CoreLocale.getInstance("master");
    Map e = new HashMap();
    if (k != null && !"".equals(k)) {
        Map m = new HashMap();
        m.put(k, new Wrong(w));
        e.put("errs", new Wrongs(m).setLocalizedContext(l).getErrors());
        e.put("msg", l.translate(w));
    } else {
        e.put("msg", l.translate(w));
    }
    e.put("ok", false);
    return e;
}
Also used : CoreLocale(io.github.ihongs.CoreLocale) Wrong(io.github.ihongs.util.verify.Wrong) HashMap(java.util.HashMap) Wrongs(io.github.ihongs.util.verify.Wrongs) HashMap(java.util.HashMap) Map(java.util.Map)

Aggregations

Wrongs (io.github.ihongs.util.verify.Wrongs)4 Map (java.util.Map)4 VerifyHelper (io.github.ihongs.action.VerifyHelper)2 Wrong (io.github.ihongs.util.verify.Wrong)2 HashMap (java.util.HashMap)2 CoreLocale (io.github.ihongs.CoreLocale)1 HongsException (io.github.ihongs.HongsException)1 FormSet (io.github.ihongs.action.FormSet)1 DB (io.github.ihongs.db.DB)1 Table (io.github.ihongs.db.Table)1 IsNumber (io.github.ihongs.util.verify.IsNumber)1 IsString (io.github.ihongs.util.verify.IsString)1 Repeated (io.github.ihongs.util.verify.Repeated)1 Required (io.github.ihongs.util.verify.Required)1 Verify (io.github.ihongs.util.verify.Verify)1 Set (java.util.Set)1 Test (org.junit.Test)1