Search in sources :

Example 6 with DB

use of app.hongs.db.DB in project HongsCORE by ihongs.

the class AuthKit method userSign.

/**
 * 自运营登录
 * @param ah
 * @param place
 * @param appid
 * @param usrid
 * @param uname 名称
 * @param uhead 头像
 * @param utime 用户信息更新时间
 * @return
 * @throws HongsException
 */
public static Map userSign(ActionHelper ah, String place, String appid, String usrid, String uname, String uhead, long utime) throws HongsException {
    HttpSession sd = ah.getRequest().getSession(false);
    long stime = System.currentTimeMillis() / 1000;
    // 登录时哪些会话数据需要保留
    String sesmk = CoreConfig.getInstance("master").getProperty("core.keep.sess", "");
    // 重建会话
    if (sd != null) {
        Map<String, Object> xs = new HashMap();
        Set<String> ks = Synt.toTerms(sesmk);
        ks.add(Cnst.SAE_SES);
        for (String kn : ks) {
            Object kv = sd.getAttribute(kn);
            if (null != kv)
                xs.put(kn, kv);
        }
        sd.invalidate();
        sd = ah.getRequest().getSession(true);
        for (Map.Entry<String, Object> et : xs.entrySet()) {
            sd.setAttribute(et.getKey(), et.getValue());
        }
    } else {
        sd = ah.getRequest().getSession(true);
    }
    String sesid = sd.getId();
    // 设置会话
    if (place != null && 0 < place.length()) {
        Set s = Synt.asSet(sd.getAttribute(Cnst.SAE_SES));
        if (s == null) {
            s = new HashSet();
        }
        s.add(place);
        // 仅保留拥有的区域
        s.retainAll(RoleSet.getInstance(usrid));
        sd.setAttribute(Cnst.SAE_SES, s);
    }
    sd.setAttribute(Cnst.STM_SES, stime);
    sd.setAttribute(Cnst.UID_SES, usrid);
    sd.setAttribute("appid", appid);
    sd.setAttribute("uname", uname);
    sd.setAttribute("uhead", uhead);
    sd.setAttribute("utime", utime);
    // 返回数据
    Map rd = new HashMap();
    rd.put(Cnst.STM_SES, stime);
    rd.put(Cnst.UID_SES, usrid);
    rd.put("appid", appid);
    rd.put("place", place);
    rd.put("uname", uname);
    rd.put("uhead", uhead);
    rd.put("utime", utime);
    rd.put("sesid", sesid);
    // 记录登录
    DB db = DB.getInstance("master");
    Table tb = db.getTable("user_sign");
    tb.remove("(`user_id` = ? AND `appid` = ?) OR `sesid` = ?", usrid, appid, sesid);
    Map ud = new HashMap();
    ud.put("user_id", usrid);
    ud.put("sesid", sesid);
    ud.put("appid", appid);
    ud.put("ctime", stime);
    tb.insert(ud);
    return rd;
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) Table(app.hongs.db.Table) HashMap(java.util.HashMap) HttpSession(javax.servlet.http.HttpSession) HashMap(java.util.HashMap) Map(java.util.Map) DB(app.hongs.db.DB) HashSet(java.util.HashSet)

Example 7 with DB

use of app.hongs.db.DB in project HongsCORE by ihongs.

the class AuthKit method openSign.

/**
 * 第三方登录
 * @param ah
 * @param place
 * @param appid
 * @param opnid
 * @param uname 名称
 * @param uhead 头像
 * @param utime 用户信息更新时间
 * @return
 * @throws HongsException
 */
public static Map openSign(ActionHelper ah, String place, String appid, String opnid, String uname, String uhead, long utime) throws HongsException {
    DB db = DB.getInstance("master");
    Table tb = db.getTable("user_open");
    Map ud = tb.fetchCase().filter("`opnid` =? AND `appid` = ?", opnid, appid).select("`user_id`").one();
    // 记录关联
    String usrid;
    if (ud != null && !ud.isEmpty()) {
        usrid = ud.get("user_id").toString();
    } else {
        ud = new HashMap();
        ud.put("name", uname);
        ud.put("head", uhead);
        usrid = db.getModel("user").add(ud);
        // 第三方登录项
        ud = new HashMap();
        ud.put("user_id", usrid);
        ud.put("appid", appid);
        ud.put("opnid", opnid);
        db.getTable("user_open").insert(ud);
        // 赋予公共权限
        ud = new HashMap();
        ud.put("user_id", usrid);
        ud.put("role", "public");
        db.getTable("user_role").insert(ud);
        // 加入公共部门
        ud = new HashMap();
        ud.put("user_id", usrid);
        ud.put("dept_id", "PUBLIC");
        db.getTable("user_dept").insert(ud);
    }
    return userSign(ah, place, appid, usrid, uname, uhead, utime);
}
Also used : Table(app.hongs.db.Table) HashMap(java.util.HashMap) HashMap(java.util.HashMap) Map(java.util.Map) DB(app.hongs.db.DB)

Example 8 with DB

use of app.hongs.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;
    FetchCase fc;
    List<Map> rz;
    db = DB.getInstance("master");
    // ** 查询用户权限 **/
    tb = db.getTable("user_role");
    fc = new FetchCase().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("user_dept");
    fc = new FetchCase().from(tb.tableName, tb.name).select(tb.name + ".role");
    fc.join(td.tableName, td.name).on(td.name + ".dept_id = " + tb.name + ".dept_id").filter(td.name + ".user_id = ?", userId);
    rz = db.fetchMore(fc);
    for (Map rm : rz) {
        roles.add((String) rm.get("role"));
    }
    // ** 当前保存时间 **/
    rtime = (int) (System.currentTimeMillis() / 1000);
}
Also used : Table(app.hongs.db.Table) FetchCase(app.hongs.db.util.FetchCase) Map(java.util.Map) DB(app.hongs.db.DB) HashSet(java.util.HashSet)

Example 9 with DB

use of app.hongs.db.DB in project HongsCORE by ihongs.

the class FetchMore method join.

/**
 * 获取关联数据
 * @param table 关联表
 * @param caze  附加查询
 * @param map   映射关系
 * @param col   关联字段
 * @throws app.hongs.HongsException
 */
public void join(Table table, FetchCase caze, Map<String, List> map, String col) throws HongsException {
    if (map.isEmpty()) {
        return;
    }
    DB db = table.db;
    String name = table.name;
    String tableName = table.tableName;
    boolean multi = caze.getOption("ASSOC_MULTI", false);
    boolean merge = caze.getOption("ASSOC_MERGE", false);
    boolean fills = caze.getOption("ASSOC_FILLS", false);
    if (null != caze.name && 0 != caze.name.length()) {
        name = caze.name;
    }
    // 获取id及行号
    Set ids = map.keySet();
    if (ids.isEmpty()) {
        // throw new HongsException(0x10c0, "Ids map is empty");
        return;
    }
    // 识别字段别名
    String rel = col;
    if (table.getFields().containsKey(col)) {
        col = "`" + name + "`.`" + col + "`";
    } else {
        Pattern pattern;
        Matcher matcher;
        do {
            pattern = Pattern.compile("^(.+?)(?:\\s+AS)?\\s+`?(.+?)`?$", Pattern.CASE_INSENSITIVE);
            matcher = pattern.matcher(col);
            if (matcher.find()) {
                col = matcher.group(1);
                rel = matcher.group(2);
                break;
            }
            pattern = Pattern.compile("^(.+?)\\.\\s*`?(.+?)`?$");
            matcher = pattern.matcher(col);
            if (matcher.find()) {
                col = matcher.group(0);
                rel = matcher.group(2);
                break;
            }
        } while (false);
    }
    // 构建查询结构
    caze.filter(col + " IN (?)", ids).from(tableName, name);
    // 获取关联数据
    Loop rs = db.queryMore(caze);
    /**
     * 根据之前的 ID=>行 关系以表名为键放入列表中
     */
    String sid;
    List lst;
    Map row, sub;
    // 登记已关联上的ID
    Set idz = new HashSet();
    // 暂存字段类型字典
    Map tdz = rs.getTypeDict();
    if (!multi) {
        while ((sub = rs.next()) != null) {
            sid = Synt.asString(sub.get(rel));
            lst = map.get(sid);
            idz.add(sid);
            if (lst == null) {
                // throw new HongsException(0x10c0, "Line nums is null");
                continue;
            }
            Iterator it = lst.iterator();
            while (it.hasNext()) {
                row = (Map) it.next();
                if (!merge) {
                    row.put(name, sub);
                } else {
                    sub.putAll(row);
                    row.putAll(sub);
                }
            }
        }
    } else {
        while ((sub = rs.next()) != null) {
            sid = Synt.asString(sub.get(rel));
            lst = map.get(sid);
            idz.add(sid);
            if (lst == null) {
                // throw new HongsException(0x10c0, "Line nums is null");
                continue;
            }
            Iterator it = lst.iterator();
            while (it.hasNext()) {
                row = (Map) it.next();
                if (row.containsKey(name)) {
                    ((List) row.get(name)).add(sub);
                } else {
                    List lzt = new ArrayList();
                    row.put(name, lzt);
                    lzt.add(sub);
                }
            }
        }
    }
    if (!fills) {
        return;
    }
    if (!multi && merge) {
        Set<String> padSet = tdz.keySet();
        for (Map.Entry<String, List> et : map.entrySet()) {
            String colKey = et.getKey();
            if (idz.contains(colKey)) {
                continue;
            }
            List<Map> mapLst = et.getValue();
            for (Map mapRow : mapLst) {
                for (String k : padSet) {
                    if (!mapRow.containsKey(k)) {
                        mapRow.put(k, null);
                    }
                }
            }
        }
    } else {
        Object padDat = !multi ? new HashMap() : new ArrayList();
        for (Map.Entry<String, List> et : map.entrySet()) {
            String colKey = et.getKey();
            if (idz.contains(colKey)) {
                continue;
            }
            List<Map> mapLst = et.getValue();
            for (Map mapRow : mapLst) {
                if (!mapRow.containsKey(name)) {
                    mapRow.put(name, padDat);
                }
            }
        }
    }
}
Also used : Loop(app.hongs.db.link.Loop) Pattern(java.util.regex.Pattern) Set(java.util.Set) HashSet(java.util.HashSet) Matcher(java.util.regex.Matcher) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Iterator(java.util.Iterator) ArrayList(java.util.ArrayList) List(java.util.List) HashMap(java.util.HashMap) Map(java.util.Map) DB(app.hongs.db.DB) HashSet(java.util.HashSet)

Aggregations

DB (app.hongs.db.DB)9 Map (java.util.Map)6 Table (app.hongs.db.Table)5 HongsException (app.hongs.HongsException)3 HashMap (java.util.HashMap)3 HashSet (java.util.HashSet)3 Set (java.util.Set)3 Matcher (java.util.regex.Matcher)3 FetchCase (app.hongs.db.util.FetchCase)2 Pattern (java.util.regex.Pattern)2 Action (app.hongs.action.anno.Action)1 Verify (app.hongs.action.anno.Verify)1 Cmdlet (app.hongs.cmdlet.anno.Cmdlet)1 Loop (app.hongs.db.link.Loop)1 RoleSet (app.hongs.serv.auth.RoleSet)1 FileInputStream (java.io.FileInputStream)1 FileNotFoundException (java.io.FileNotFoundException)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Date (java.util.Date)1