Search in sources :

Example 96 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Dept method permit.

protected void permit(String id, Map data) throws HongsException {
    String pid = null;
    if (data != null) {
        // 上级部门
        pid = (String) data.get("pid");
        if (pid == null || pid.equals("")) {
            data.remove("pid");
            pid = null;
        }
        // 权限限制, 仅能赋予当前登录用户所有的权限
        if (data.containsKey("roles")) {
            data.put("rtime", System.currentTimeMillis() / 1000);
            List list = Synt.asList(data.get("roles"));
            AuthKit.cleanDeptRoles(list, id);
            // if ( list.isEmpty() ) {
            // throw new HongsException(400)
            // .setLocalizedContent("master.user.dept.error")
            // .setLocalizedContext("master");
            // }
            data.put("roles", list);
        }
    } else {
        List list;
        Table tablx = db.getTable("dept_user");
        // 删除限制, 如果部门下有部门则中止当前操作
        list = table.fetchCase().filter("pid = ? AND state > ?", id, 0).limit(1).getAll();
        if (!list.isEmpty()) {
            throw new HongsException(400).setLocalizedContent("master.dept.have.depts").setLocalizedContext("master");
        }
        // 删除限制, 如果部门下有用户则中止当前操作
        list = tablx.fetchCase().filter("dept_id = ?", id).limit(1).getAll();
        if (!list.isEmpty()) {
            throw new HongsException(400).setLocalizedContent("master.dept.have.users").setLocalizedContext("master");
        }
    }
    if (id == null && pid == null) {
        throw new NullPointerException("id and pid cannot be all null");
    }
    if (id != null || pid != null) {
        // 超级管理员可操作任何部门
        ActionHelper helper = Core.getInstance(ActionHelper.class);
        String uid = (String) helper.getSessibute(Cnst.UID_SES);
        if (Cnst.ADM_UID.equals(uid)) {
            return;
        }
        // 超级管理组可操作任何部门
        // 但禁止操作顶级部门
        Set cur = AuthKit.getUserDepts(uid);
        if (cur.contains(Cnst.ADM_GID) && !Cnst.ADM_GID.equals(id)) {
            return;
        }
        // 仅可以操作下级部门
        for (Object gid : cur) {
            Set cld = new HashSet(this.getChildIds((String) gid, true));
            if (null != pid && (gid.equals(pid) || cld.contains(pid))) {
                return;
            }
            if (null != id && cld.contains(id)) {
                return;
            }
        }
        throw new HongsException(400).setLocalizedContent("master.dept.unit.error").setLocalizedContext("master");
    }
}
Also used : Table(io.github.ihongs.db.Table) Set(java.util.Set) HashSet(java.util.HashSet) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) List(java.util.List) HashSet(java.util.HashSet)

Example 97 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class User method permit.

protected void permit(String id, Map data) throws HongsException {
    if (data != null) {
        // 登录账号, 空串可能导致重复
        if (data.containsKey("username")) {
            String un = Synt.declare(data.get("username"), "");
            if (un.isEmpty()) {
                data.put("username", null);
            }
        }
        // 加密密码, 联动密码更新时间
        data.remove("passcode");
        if (data.containsKey("password")) {
            String pw = Synt.declare(data.get("password"), "");
            String pc = Core.newIdentity();
            pc = AuthKit.getCrypt(pw + pc);
            pw = AuthKit.getCrypt(pw + pc);
            data.put("password", pw);
            data.put("passcode", pc);
            data.put("ptime", System.currentTimeMillis() / 1000);
        }
        // 状态变更, 联动权限更新时间
        if (data.containsKey("state")) {
            data.put("rtime", System.currentTimeMillis() / 1000);
        }
        // 权限限制, 仅能赋予当前登录用户所有的权限
        if (data.containsKey("roles")) {
            data.put("rtime", System.currentTimeMillis() / 1000);
            List list = Synt.asList(data.get("roles"));
            AuthKit.cleanUserRoles(list, id);
            // if ( list.isEmpty() ) {
            // throw new HongsException(400)
            // .setLocalizedContent("master.user.role.error")
            // .setLocalizedContext("master");
            // }
            data.put("roles", list);
        }
        // 部门限制, 仅能指定当前登录用户下属的部门
        if (data.containsKey("depts")) {
            data.put("rtime", System.currentTimeMillis() / 1000);
            List list = Synt.asList(data.get("depts"));
            AuthKit.cleanUserDepts(list, id);
            if (list.isEmpty()) {
                throw new HongsException(400).setLocalizedContent("master.user.dept.error").setLocalizedContext("master");
            }
            data.put("depts", list);
        }
    }
    if (id != null) {
        // 超级管理员可操作任何用户
        // 但允许操作自身账号
        ActionHelper helper = Core.getInstance(ActionHelper.class);
        String uid = (String) helper.getSessibute(Cnst.UID_SES);
        if (Cnst.ADM_UID.equals(uid) || id.equals(uid)) {
            return;
        }
        // 超级管理组可操作任何用户
        // 但不包含超级管理员
        Set cur = AuthKit.getUserDepts(uid);
        if (cur.contains(Cnst.ADM_GID) && !Cnst.ADM_UID.equals(id)) {
            return;
        }
        // 仅可以操作下级用户
        Set tar = AuthKit.getLessDepts(id);
        Dept dept = new Dept();
        for (Object gid : cur) {
            Set cld = new HashSet(dept.getChildIds((String) gid, true));
            cld.retainAll(tar);
            if (!cld.isEmpty()) {
                return;
            }
        }
        throw new HongsException(400).setLocalizedContent("master.user.unit.error").setLocalizedContext("master");
    }
}
Also used : Set(java.util.Set) HashSet(java.util.HashSet) HongsException(io.github.ihongs.HongsException) ActionHelper(io.github.ihongs.action.ActionHelper) List(java.util.List) HashSet(java.util.HashSet)

Example 98 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class WXAction method inWap.

/**
 * 微信 WAP 登录回调
 * @param helper
 * @throws HongsException
 */
@Action("wap/create")
@CommitSuccess
public void inWap(ActionHelper helper) throws HongsException {
    CoreConfig cc = CoreConfig.getInstance("oauth2");
    String appId = cc.getProperty("oauth2.wx.wap.app.id");
    String appSk = cc.getProperty("oauth2.wx.wap.app.key");
    String code = helper.getParameter("code");
    if (appId == null || appSk == null) {
        helper.error(400, "Not support this mode");
        return;
    }
    try {
        Map info = getUserInfo(code, appId, appSk);
        String opnId = (String) info.get("opnid");
        String opuId = (String) info.get("opuid");
        String name = (String) info.get("name");
        String head = (String) info.get("head");
        Map back = AuthKit.openSign(helper, "wx", Synt.defoult(opuId, opnId), name, head);
        // 登记 openId
        if (opnId != null && opuId != null) {
            String usrId = (String) back.get(Cnst.UID_SES);
            setUserSign("wx.wap", opnId, usrId);
        }
        AuthKit.redirect(helper, back);
    } catch (HongsException ex) {
        AuthKit.redirect(helper, ex);
    }
}
Also used : CoreConfig(io.github.ihongs.CoreConfig) HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action) CommitSuccess(io.github.ihongs.action.anno.CommitSuccess)

Example 99 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class WXAction method inWeb.

/**
 * 微信 Web 登录回调
 * @param helper
 * @throws HongsException
 */
@Action("web/create")
@CommitSuccess
public void inWeb(ActionHelper helper) throws HongsException {
    CoreConfig cc = CoreConfig.getInstance("oauth2");
    String appId = cc.getProperty("oauth2.wx.web.app.id");
    String appSk = cc.getProperty("oauth2.wx.web.app.key");
    String code = helper.getParameter("code");
    if (appId == null || appSk == null) {
        helper.error(400, "Not support this mode");
        return;
    }
    try {
        Map info = getUserInfo(code, appId, appSk);
        String opnId = (String) info.get("opnid");
        String opuId = (String) info.get("opuid");
        String name = (String) info.get("name");
        String head = (String) info.get("head");
        Map back = AuthKit.openSign(helper, "wx", Synt.defoult(opuId, opnId), name, head);
        // 登记 openId
        if (opnId != null && opuId != null) {
            String usrId = (String) back.get(Cnst.UID_SES);
            setUserSign("wx.web", opnId, usrId);
        }
        AuthKit.redirect(helper, back);
    } catch (HongsException ex) {
        AuthKit.redirect(helper, ex);
    }
}
Also used : CoreConfig(io.github.ihongs.CoreConfig) HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) Map(java.util.Map) Action(io.github.ihongs.action.anno.Action) CommitSuccess(io.github.ihongs.action.anno.CommitSuccess)

Example 100 with HongsException

use of io.github.ihongs.HongsException in project HongsCORE by ihongs.

the class Data method put.

/**
 * 更新记录
 *
 * 注意:
 * 每次都产生新节点,
 * 有则更新无则添加.
 *
 * @param id
 * @param rd
 * @param ctime
 * @return 有更新为 1, 无更新为 0
 * @throws HongsException
 */
public int put(String id, Map rd, long ctime) throws HongsException {
    Map dd = get(id);
    int t = dd.isEmpty() ? 1 : 2;
    int i = padInf(dd, rd);
    // 无更新不存储
    if (i == 0) {
        return 0;
    }
    // 保存到文档库
    dd.put(Cnst.ID_KEY, id);
    Document dc = padDoc(dd);
    setDoc(id, dc);
    Table table = getTable();
    if (table == null) {
        return 1;
    }
    String uid = getUserId();
    String fid = getFormId();
    Object[] param = new String[] { id, fid, "0" };
    String where = "`id`=? AND `form_id`=? AND `etime`=?";
    Map od = table.fetchCase().filter(where, param).select("ctime,state").getOne();
    if (!od.isEmpty()) {
        if (Synt.declare(od.get("state"), 0) == 0) {
            throw new HongsException(404, "Data item '" + id + "' is removed in " + getDbName()).setLocalizedContent("matrix.item.is.removed").setLocalizedContext("matrix");
        }
        if (Synt.declare(od.get("ctime"), 0L) >= ctime) {
            throw new HongsException(400, "Wait 1 second to put '" + id + "' in " + getDbName()).setLocalizedContent("matrix.wait.one.second").setLocalizedContext("matrix");
        }
    }
    Map ud = new HashMap();
    ud.put("etime", ctime);
    Map nd = new HashMap();
    nd.put("ctime", ctime);
    nd.put("etime", 0);
    nd.put("state", t);
    nd.put("id", id);
    nd.put("form_id", fid);
    nd.put("user_id", uid);
    // 数据快照和日志标题
    nd.put("__data__", dd);
    nd.put("data", Dawn.toString(dd, true));
    nd.put("name", getText(dd, "name"));
    // 操作备注和终端代码
    if (rd.containsKey("memo")) {
        nd.put("memo", getText(rd, "memo"));
    }
    if (rd.containsKey("meno")) {
        nd.put("meno", getText(rd, "meno"));
    }
    table.update(ud, where, param);
    table.insert(nd);
    return 1;
}
Also used : Table(io.github.ihongs.db.Table) HongsException(io.github.ihongs.HongsException) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Document(org.apache.lucene.document.Document) HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Aggregations

HongsException (io.github.ihongs.HongsException)138 Map (java.util.Map)77 HashMap (java.util.HashMap)61 LinkedHashMap (java.util.LinkedHashMap)31 IOException (java.io.IOException)29 Set (java.util.Set)26 HashSet (java.util.HashSet)25 ArrayList (java.util.ArrayList)24 List (java.util.List)20 HongsExemption (io.github.ihongs.HongsExemption)14 Action (io.github.ihongs.action.anno.Action)14 LinkedHashSet (java.util.LinkedHashSet)14 SQLException (java.sql.SQLException)13 FormSet (io.github.ihongs.action.FormSet)12 Table (io.github.ihongs.db.Table)12 FileNotFoundException (java.io.FileNotFoundException)11 CoreConfig (io.github.ihongs.CoreConfig)10 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)10 Iterator (java.util.Iterator)9 DocumentBuilder (javax.xml.parsers.DocumentBuilder)9