Search in sources :

Example 6 with Table

use of io.github.ihongs.db.Table in project HongsCORE by ihongs.

the class MineAction method mineSave.

@Action("save")
@Preset(conf = "master", form = "mine")
@Verify(conf = "master", form = "mine", type = 1, trim = 1)
@CommitSuccess
public void mineSave(ActionHelper ah) throws HongsException {
    Object id = ah.getSessibute(Cnst.UID_SES);
    if (id == null || "".equals(id)) {
        throw new HongsException(401, "");
    }
    Map rd = ah.getRequestData();
    rd.put("id", id);
    // 禁止危险修改, 其实校验已经做过限制了. 这是以防万一
    rd.remove("depts");
    rd.remove("roles");
    rd.remove("state");
    // 验证原始密码
    Table ut = DB.getInstance("master").getTable("user");
    String pw = (String) rd.get("password");
    String po = (String) rd.get("passolde");
    if (pw != null && !"".equals(pw)) {
        Map xd = new HashMap();
        Map ed = new HashMap();
        xd.put("ok", false);
        xd.put("errs", ed);
        xd.put("msg", CoreLocale.getInstance().translate("fore.form.invalid"));
        if (po != null && !"".equals(po)) {
            Map row = ut.fetchCase().filter("id = ?", id).select("password , passcode").getOne();
            String ps = (String) row.get("password");
            String pc = (String) row.get("passcode");
            if (pc != null)
                po += pc;
            po = AuthKit.getCrypt(po);
            if (!po.equals(ps)) {
                ed.put("passolde", "旧密码不正确");
                ah.reply(xd);
                return;
            }
        } else {
            ed.put("passolde", "请填写旧密码");
            ah.reply(xd);
            return;
        }
    }
    // 附加验证标识, 当要验证的字段值改变时, 重设为未验证
    Map<String, Object> fs = ut.getFields();
    Map<String, Object> fz = new HashMap();
    for (String fn : fs.keySet()) {
        String fx;
        if (fn.endsWith("_checked")) {
            fx = fn.substring(0, fn.length() - 8);
            if (fs.containsKey(fx) && rd.containsKey(fx)) {
                fz.put(fx, rd.get(fx));
            }
        }
    }
    if (!fz.isEmpty()) {
        StringBuilder sb = new StringBuilder();
        for (String fn : fz.keySet()) {
            sb.append(",`").append(fn).append("`");
        }
        Map ud = ut.fetchCase().filter("`id` = ?", id).select(sb.substring(1)).getOne();
        for (Map.Entry<String, Object> et : fz.entrySet()) {
            String fn = et.getKey();
            Object fv = et.getValue();
            Object fo = ud.get(fn);
            if (fv == null || fv.equals("") || !fv.equals(fo)) {
                rd.put(et.getKey() + "_checked", "0");
            }
        }
    }
    UserAction ua = new UserAction();
    ua.doSave(ah);
}
Also used : UserAction(io.github.ihongs.serv.master.UserAction) Table(io.github.ihongs.db.Table) HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action) UserAction(io.github.ihongs.serv.master.UserAction) CommitSuccess(io.github.ihongs.action.anno.CommitSuccess) Preset(io.github.ihongs.action.anno.Preset) Verify(io.github.ihongs.action.anno.Verify)

Example 7 with Table

use of io.github.ihongs.db.Table in project HongsCORE by ihongs.

the class UserCmdlet method uproot.

/**
 * 归并账号
 * @param uid  目标账号
 * @param uids 被并账号
 * @throws HongsException
 */
public static void uproot(String uid, Set<String> uids) throws HongsException {
    DB db;
    Table tb;
    Loop lo;
    db = DB.getInstance("master");
    // ** 关联登录 **/
    tb = db.getTable("user_sign");
    tb.update(Synt.mapOf("user_id", uid), "`user_id` IN (?)", uids);
    // ** 用户权限 **/
    tb = db.getTable("user_role");
    lo = tb.fetchCase().filter("`user_id` = ?", uid).select("`role`").select();
    Set rids = new HashSet();
    for (Map ro : lo) {
        rids.add(ro.get("role"));
    }
    lo = tb.fetchCase().filter("`user_id` IN (?) AND `role` NOT IN (?)", uids, rids).select("`role`").select();
    rids.clear();
    for (Map ro : lo) {
        rids.add(ro.get("role"));
    }
    for (Object rid : rids) {
        tb.insert(Synt.mapOf("role", rid, "user_id", uid));
    }
    // ** 用户分组 **/
    tb = db.getTable("dept_user");
    lo = tb.fetchCase().filter("`user_id` = ?", uid).select("`dept_id`").select();
    Set dids = new HashSet();
    for (Map ro : lo) {
        dids.add(ro.get("dept_id"));
    }
    lo = tb.fetchCase().filter("`user_id` IN (?) AND `dept_id` NOT IN (?)", uids, dids).select("`dept_id`").select();
    dids.clear();
    for (Map ro : lo) {
        dids.add(ro.get("dept_id"));
    }
    for (Object did : dids) {
        tb.insert(Synt.mapOf("dept_id", did, "user_id", uid));
    }
    // ** 用户资料 **/
    tb = db.getTable("user");
    lo = tb.fetchCase().filter("`id` = ?", uid).select("`phone`,`phone_checked`,`email`,`email_checked`,`username`").select();
    Map info = new HashMap();
    boolean phoneChecked = false;
    boolean emailChecked = false;
    boolean loginChecked = false;
    for (Map ro : lo) {
        info.putAll(ro);
        Object phone = info.get("phone");
        if (Synt.declare(ro.get("phone_checked"), false) && phone != null && !phone.equals("")) {
            phoneChecked = true;
        }
        Object email = info.get("email");
        if (Synt.declare(ro.get("email_checked"), false) && email != null && !email.equals("")) {
            emailChecked = true;
        }
        Object login = info.get("username");
        if (login != null && !login.equals("")) {
            loginChecked = true;
        }
    }
    lo = tb.fetchCase().filter("`id` IN (?)", uids).assort("`ctime` DESC, `mtime` DESC").select("`phone`,`phone_checked`,`email`,`email_checked`,`username`,`password`,`passcode`").select();
    for (Map ro : lo) {
        if (!phoneChecked) {
            Object phone = ro.get("phone");
            if (Synt.declare(ro.get("phone_checked"), false) && phone != null && !phone.equals("")) {
                phoneChecked = true;
                info.put("phone_checked", 1);
                info.put("phone", phone);
            }
        }
        if (!emailChecked) {
            Object email = ro.get("email");
            if (Synt.declare(ro.get("email_checked"), false) && email != null && !email.equals("")) {
                emailChecked = true;
                info.put("email_checked", 1);
                info.put("email", email);
            }
        }
        if (!loginChecked) {
            Object login = ro.get("username");
            if (login != null && !login.equals("")) {
                loginChecked = true;
                info.put("username", login);
                info.put("password", info.get("password"));
                info.put("passcode", info.get("passcode"));
            }
        }
    }
    // 更新资料和权限时间
    long now = System.currentTimeMillis() / 1000;
    info.put("rtime", now);
    info.put("mtime", now);
    tb.update(info, "`id`  =  ? ", uid);
    // 其他用户标记为删除
    info.clear();
    info.put("state", 0);
    info.put("rtime", now);
    info.put("mtime", now);
    tb.update(info, "`id` IN (?)", uids);
    // ** 其他关联 **/
    /**
     * 仅能更新普通的关联到用户
     * 对那些有额外唯一约束的表
     * 请自行处理
     */
    db = DB.getInstance();
    String u = CoreConfig.getInstance("master").getProperty("core.master.uproot");
    if (null != u && !u.isEmpty())
        for (String n : u.split(",")) {
            int p = n.indexOf(":");
            if (p < 0) {
                throw new HongsException("Config item 'core.master.uproot' must be '[DB.]TABLE:FIELD'");
            }
            String t = n.substring(0, p).trim();
            String f = n.substring(1 + p).trim();
            tb = db.getTable(t);
            tb.db.execute("UPDATE `" + tb.tableName + "` SET `" + f + "` = ? WHERE `" + f + "` IN (?)", uid, uids);
        }
}
Also used : Loop(io.github.ihongs.db.link.Loop) Table(io.github.ihongs.db.Table) HashSet(java.util.HashSet) Set(java.util.Set) HashMap(java.util.HashMap) HongsException(io.github.ihongs.HongsException) Map(java.util.Map) HashMap(java.util.HashMap) DB(io.github.ihongs.db.DB) HashSet(java.util.HashSet)

Example 8 with Table

use of io.github.ihongs.db.Table in project HongsCORE by ihongs.

the class WXAction method setUserSign.

public static void setUserSign(String unit, String code, String uid) throws HongsException {
    Table tab = DB.getInstance("master").getTable("user_sign");
    Map row = tab.fetchCase().filter("user_id = ?", uid).filter("unit = ?", unit).filter("code = ?", code).select("1").getOne();
    if (row == null || row.isEmpty()) {
        tab.insert(Synt.mapOf("user_id", uid, "unit", unit, "code", code));
    }
}
Also used : Table(io.github.ihongs.db.Table) HashMap(java.util.HashMap) Map(java.util.Map)

Example 9 with Table

use of io.github.ihongs.db.Table in project HongsCORE by ihongs.

the class RoleSet method expires.

@Override
protected byte expires(File f) throws HongsException {
    DB db;
    Table tb;
    Table td;
    FetchCase fc;
    Map rs;
    int st;
    long rt;
    long ot;
    long pt;
    db = DB.getInstance("master");
    tb = db.getTable("user");
    fc = new FetchCase(FetchCase.STRICT).from(tb.tableName, tb.name).select(tb.name + ".state, " + tb.name + ".rtime, " + tb.name + ".ptime").filter(tb.name + ".id = ?", userId);
    rs = db.fetchLess(fc);
    st = Synt.declare(rs.get("state"), 0);
    rt = Synt.declare(rs.get("rtime"), 0L);
    pt = Synt.declare(rs.get("ptime"), 0L);
    if (st <= 0) {
        // 用户不存在或已锁定,则删除
        return -1;
    }
    /**
     * 使用密码登录
     * 当密码变更时(登录时间小于密码修改时间)
     * 需要重新登录
     */
    USK: {
        ActionHelper ah;
        try {
            ah = ActionHelper.getInstance();
        } catch (UnsupportedOperationException e) {
            // 不理会非动作环境
            break USK;
        }
        if (!"*".equals(ah.getSessibute(Cnst.USK_SES))) {
            // 不理会非密码登录
            break USK;
        }
        ot = Synt.declare(ah.getSessibute(Cnst.UST_SES), 0L);
        if (ot < pt && 0 < ot && 0 < pt) {
            throw new HongsException(401, "Password changed").setLocalizedContent("core.password.changed").setLocalizedContext("master");
        }
    }
    tb = db.getTable("dept");
    td = db.getTable("dept_user");
    fc = new FetchCase(FetchCase.STRICT).from(tb.tableName, tb.name).join(td.tableName, td.name, td.name + ".dept_id = " + tb.name + ".id").select("MAX(" + tb.name + ".state) AS state, MAX(" + tb.name + ".rtime) AS rtime").filter(td.name + ".user_id = ?", userId).gather(td.name + ".user_id");
    rs = db.fetchLess(fc);
    st = Synt.declare(rs.get("state"), 1);
    ot = Synt.declare(rs.get("rtime"), 0L);
    if (st <= 0) {
        // 所在的分组均已锁定,则删除
        return -1;
    }
    /**
     * 比较文件修改时间和权限变更时间
     * 还没有过期则从缓存文件载入即可
     */
    if (rt < ot) {
        rt = ot;
    }
    if (f.exists() && f.lastModified() >= rt * 1000L) {
        return 1;
    } else {
        return 0;
    }
}
Also used : Table(io.github.ihongs.db.Table) FetchCase(io.github.ihongs.db.util.FetchCase) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) Map(java.util.Map) DB(io.github.ihongs.db.DB)

Example 10 with Table

use of io.github.ihongs.db.Table in project HongsCORE by ihongs.

the class RoleSet method imports.

@Override
protected void imports() throws HongsException {
    roles = new HashSet();
    DB db;
    Table tb;
    Table td;
    Table tt;
    FetchCase fc;
    List<Map> rz;
    db = DB.getInstance("master");
    // ** 查询用户权限 **/
    tb = db.getTable("user_role");
    fc = new FetchCase(FetchCase.STRICT).from(tb.tableName, tb.name).select(tb.name + ".role").filter(tb.name + ".user_id = ?", userId);
    rz = db.fetchMore(fc);
    for (Map rm : rz) {
        roles.add((String) rm.get("role"));
    }
    // ** 查询部门权限 **/
    tb = db.getTable("dept_role");
    td = db.getTable("dept_user");
    tt = db.getTable("dept");
    fc = new FetchCase(FetchCase.STRICT).from(tb.tableName, tb.name).join(td.tableName, td.name, tb.name + ".dept_id = " + td.name + ".dept_id").join(tt.tableName, tt.name, td.name + ".dept_id = " + tt.name + ".id").select(tb.name + ".role").filter(td.name + ".user_id = ?", userId).filter(tt.name + ".state > 0");
    rz = db.fetchMore(fc);
    for (Map rm : rz) {
        roles.add((String) rm.get("role"));
    }
}
Also used : Table(io.github.ihongs.db.Table) FetchCase(io.github.ihongs.db.util.FetchCase) Map(java.util.Map) DB(io.github.ihongs.db.DB) HashSet(java.util.HashSet)

Aggregations

Table (io.github.ihongs.db.Table)29 Map (java.util.Map)26 HashMap (java.util.HashMap)22 HongsException (io.github.ihongs.HongsException)12 HashSet (java.util.HashSet)11 Set (java.util.Set)11 DB (io.github.ihongs.db.DB)6 LinkedHashMap (java.util.LinkedHashMap)6 List (java.util.List)5 TreeSet (java.util.TreeSet)5 ActionHelper (io.github.ihongs.action.ActionHelper)4 ArrayList (java.util.ArrayList)4 Document (org.apache.lucene.document.Document)4 FetchCase (io.github.ihongs.db.util.FetchCase)3 Iterator (java.util.Iterator)3 Action (io.github.ihongs.action.anno.Action)2 CommitSuccess (io.github.ihongs.action.anno.CommitSuccess)2 Verify (io.github.ihongs.action.anno.Verify)2 Loop (io.github.ihongs.db.link.Loop)2 Dept (io.github.ihongs.serv.master.Dept)2