Search in sources :

Example 1 with DB

use of io.github.ihongs.db.DB 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 DB

use of io.github.ihongs.db.DB 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 3 with DB

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

the class UserCmdlet method uproot.

/**
 * 归并命令
 * @param args
 * @throws HongsException
 */
@Cmdlet("uproot")
public static void uproot(String[] args) throws HongsException {
    Map opts = CmdletHelper.getOpts(args, "uid=s", "uids=s", "!A", "!U", "?Usage: attach --uid UID --uids UID1,UID2...");
    String uid = (String) opts.get("uid");
    String uidz = (String) opts.get("uids");
    Set<String> uids = Synt.toSet(uidz);
    DB db = DB.getInstance("master");
    try {
        db.begin();
        uproot(uid, uids);
        db.commit();
    } catch (HongsException ex) {
        db.revert();
        throw ex;
    }
}
Also used : HongsException(io.github.ihongs.HongsException) Map(java.util.Map) HashMap(java.util.HashMap) DB(io.github.ihongs.db.DB) Cmdlet(io.github.ihongs.cmdlet.anno.Cmdlet)

Example 4 with DB

use of io.github.ihongs.db.DB 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 5 with DB

use of io.github.ihongs.db.DB 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

DB (io.github.ihongs.db.DB)12 Map (java.util.Map)8 HongsException (io.github.ihongs.HongsException)7 Table (io.github.ihongs.db.Table)6 HashMap (java.util.HashMap)4 FetchCase (io.github.ihongs.db.util.FetchCase)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Matcher (java.util.regex.Matcher)3 Cmdlet (io.github.ihongs.cmdlet.anno.Cmdlet)2 Loop (io.github.ihongs.db.link.Loop)2 ArrayList (java.util.ArrayList)2 Pattern (java.util.regex.Pattern)2 CoreConfig (io.github.ihongs.CoreConfig)1 ActionHelper (io.github.ihongs.action.ActionHelper)1 VerifyHelper (io.github.ihongs.action.VerifyHelper)1 Action (io.github.ihongs.action.anno.Action)1 CommitSuccess (io.github.ihongs.action.anno.CommitSuccess)1 Verify (io.github.ihongs.action.anno.Verify)1 RoleSet (io.github.ihongs.serv.auth.RoleSet)1