Search in sources :

Example 1 with Wrong

use of io.github.ihongs.util.verify.Wrong 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 Wrong

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

the class UploadHelper method upload.

/**
 * 检查文件流并写入目标目录
 * @param xis  上传文件输入流
 * @param type 上传文件类型
 * @param extn 上传文件扩展
 * @param subn 目标文件名称
 * @return
 * @throws Wrong
 */
public File upload(InputStream xis, String type, String extn, String subn) throws Wrong {
    if (type.contains(";")) {
        type = type.substring(0, type.indexOf(";"));
    }
    if (extn.contains(".")) {
        extn = extn.substring(1 + extn.lastIndexOf('.'));
    }
    chkTypeOrExtn(type, extn);
    setResultName(subn, extn);
    File file = new File(getResultPath());
    File fdir = file.getParentFile();
    if (!fdir.exists()) {
        fdir.mkdirs();
    }
    // 拷贝数据
    try {
        try (FileOutputStream fos = new FileOutputStream(file);
            BufferedInputStream bis = new BufferedInputStream(xis);
            BufferedOutputStream bos = new BufferedOutputStream(fos)) {
            byte[] buf = new byte[1024];
            int ovr;
            while ((ovr = bis.read(buf)) != -1) {
                bos.write(buf, 0, ovr);
            }
        }
    } catch (IOException ex) {
        throw new Wrong(ex, "fore.form.upload.failed");
    }
    return file;
}
Also used : Wrong(io.github.ihongs.util.verify.Wrong) BufferedInputStream(java.io.BufferedInputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File) BufferedOutputStream(java.io.BufferedOutputStream)

Example 3 with Wrong

use of io.github.ihongs.util.verify.Wrong 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)

Example 4 with Wrong

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

the class UploadHelper method upload.

/**
 * 检查上传对象并写入目标目录
 * @param part
 * @param subn
 * @return
 * @throws Wrong
 */
public File upload(Part part, String subn) throws Wrong {
    if (part == null) {
        setResultName("", null);
        return null;
    }
    /**
     * 从上传项中获取类型并提取扩展名
     */
    String type = part.getContentType();
    type = getTypeByMime(type);
    String extn = part.getSubmittedFileName();
    extn = getExtnByName(extn);
    try {
        return upload(part.getInputStream(), type, extn, subn);
    } catch (IOException ex) {
        throw new Wrong(ex, "fore.form.upload.failed");
    }
}
Also used : Wrong(io.github.ihongs.util.verify.Wrong) IOException(java.io.IOException)

Example 5 with Wrong

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

the class UploadHelper method upload.

/**
 * 检查临时文件或目标链接情况
 * 参数为临时文件名或结果链接
 * 注意: 如果有 URL encode 务必先 decode
 * @param name
 * @return
 * @throws Wrong
 */
public File upload(String name) throws Wrong {
    if (name == null || name.length() == 0) {
        setResultName("", null);
        return null;
    }
    // 避免 Windows 异常
    name = name.replace('\\', '/');
    /**
     * 值与目标网址的前导路径如果一致,
     * 视为修改表单且没有重新上传文件;
     * 但需规避用相对路径访问敏感文件,
     * 目录不得以点结尾, 如 ./ 和 ../
     *
     * 反之将其视为传递来的临时文件名,
     * 此时不得含斜杠从而规避同上问题.
     */
    String subn;
    do {
        String href = getResultHref(uploadHref) + "/";
        String path = getResultPath(uploadPath) + "/";
        if (name.startsWith(href)) {
            subn = name.substring(href.length());
            if (subn.contains("./")) {
                throw new Wrong("core.file.upload.not.allows");
            }
            name = path + subn;
            break;
        }
        if (name.startsWith(path)) {
            subn = name.substring(path.length());
            if (subn.contains("./")) {
                throw new Wrong("core.file.upload.not.allows");
            }
            // name = path + subn;
            break;
        }
        String temp = getUploadTemp(uploadTemp) + "/";
        {
            if (name.contains("./")) {
                throw new Wrong("core.file.upload.not.allows");
            }
            name = temp + name;
            subn = getDigestName(new File(name));
            break;
        }
    } while (false);
    return upload(new File(name), subn);
}
Also used : Wrong(io.github.ihongs.util.verify.Wrong) File(java.io.File)

Aggregations

Wrong (io.github.ihongs.util.verify.Wrong)6 File (java.io.File)3 Wrongs (io.github.ihongs.util.verify.Wrongs)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 Map (java.util.Map)2 CoreLocale (io.github.ihongs.CoreLocale)1 VerifyHelper (io.github.ihongs.action.VerifyHelper)1 DB (io.github.ihongs.db.DB)1 Table (io.github.ihongs.db.Table)1 BufferedInputStream (java.io.BufferedInputStream)1 BufferedOutputStream (java.io.BufferedOutputStream)1 FileOutputStream (java.io.FileOutputStream)1