Search in sources :

Example 1 with VerifyHelper

use of io.github.ihongs.action.VerifyHelper 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 VerifyHelper

use of io.github.ihongs.action.VerifyHelper 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 VerifyHelper

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

the class IsForm method verify.

@Override
public Object verify(Value watch) throws Wrong, Wrongs {
    // 跳过空值和空串
    Object value = watch.get();
    if (value == null) {
        return STAND;
    }
    if (value.equals("")) {
        return null;
    }
    String conf = Synt.asString(getParam("conf"));
    String name = Synt.asString(getParam("form"));
    if (conf == null || "".equals(conf)) {
        conf = Synt.asString(getParam("__conf__"));
    }
    if (name == null || "".equals(name)) {
        name = Synt.asString(getParam("__name__"));
    }
    VerifyHelper hlpr = new VerifyHelper();
    boolean update = watch.isUpdate();
    boolean prompt = watch.isPrompt();
    /**
     * 字段外部可能会有特殊要求
     * 如下层总是要作为整体保存
     */
    Set<String> flag = Synt.toTerms(getParam("form-in"));
    if (flag != null) {
        if (flag.contains("create")) {
            update = false;
        } else if (flag.contains("update")) {
            update = true;
        }
        if (flag.contains("prompt")) {
            prompt = true;
        }
    }
    /**
     * 填充字段配置所指定的规则
     */
    try {
        hlpr.addRulesByForm(conf, name);
    } catch (HongsException ex) {
        throw ex.toExemption();
    }
    return hlpr.verify(Synt.asMap(value), update, prompt);
}
Also used : VerifyHelper(io.github.ihongs.action.VerifyHelper) HongsException(io.github.ihongs.HongsException)

Aggregations

VerifyHelper (io.github.ihongs.action.VerifyHelper)3 HongsException (io.github.ihongs.HongsException)2 Wrongs (io.github.ihongs.util.verify.Wrongs)2 Map (java.util.Map)2 FormSet (io.github.ihongs.action.FormSet)1 DB (io.github.ihongs.db.DB)1 Table (io.github.ihongs.db.Table)1 Wrong (io.github.ihongs.util.verify.Wrong)1 HashMap (java.util.HashMap)1 Set (java.util.Set)1