use of io.github.ihongs.db.Table in project HongsCORE by ihongs.
the class AuthKit method getLessDepts.
/**
* 获取所在的顶层分组
* @param uid
* @return
* @throws HongsException
*/
public static Set getLessDepts(String uid) throws HongsException {
Table rel = DB.getInstance("master").getTable("dept_user");
List<Map> lst = rel.fetchCase().filter("user_id = ?", uid).select("dept_id").getAll();
Set set = new TreeSet();
Dept dp = new Dept();
for (Map row : lst) {
String id = (String) row.get("dept_id");
set.add(getDeptPath(id, dp));
}
return getPeakPids(set);
}
use of io.github.ihongs.db.Table 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");
}
}
use of io.github.ihongs.db.Table 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;
}
use of io.github.ihongs.db.Table in project HongsCORE by ihongs.
the class SignAction method signCreate.
/**
* 登录
* @param ah
* @throws HongsException
*/
@Action("create")
@Verify(conf = "master", form = "sign")
@CommitSuccess
public void signCreate(ActionHelper ah) throws HongsException {
String place = Synt.declare(ah.getParameter("place"), "centre");
String username = Synt.declare(ah.getParameter("username"), "");
String password = Synt.declare(ah.getParameter("password"), "");
String passcode;
DB db = DB.getInstance("master");
Table tb = db.getTable("user");
FetchCase fc;
Map ud;
String id;
String tt;
int at;
int rt;
// 检查账号
fc = new FetchCase().from(tb.tableName).filter("username = ?", username).select("password, passcode, id, name, head, state");
ud = db.fetchLess(fc);
if (ud.isEmpty()) {
ah.reply(AuthKit.getWrong("username", "core.username.invalid"));
return;
}
// 重试限制
CoreConfig cc = CoreConfig.getInstance("master");
id = (String) ud.get("id");
tt = Synt.declare(cc.getProperty("core.sign.retry.token"), "");
at = Synt.declare(cc.getProperty("core.sign.retry.times"), 5);
if (Synt.declare(Record.get("sign.retry.allow." + id), false)) {
tt = "id";
}
switch(tt) {
case "id":
break;
case "ip":
id = Core.CLIENT_ADDR.get();
break;
default:
id = id + "-" + Core.CLIENT_ADDR.get();
}
rt = Synt.declare(Record.get("sign.retry.times." + id), 0);
if (rt >= at) {
ah.reply(AuthKit.getWrong("password", "core.password.timeout"));
ah.getResponseData().put("allow_times", at);
ah.getResponseData().put("retry_times", rt);
return;
} else {
rt++;
}
// 校验密码
passcode = Synt.declare(ud.get("passcode"), "");
password = AuthKit.getCrypt(password + passcode);
if (!password.equals(ud.get("password"))) {
ah.reply(AuthKit.getWrong("password", "core.password.invalid"));
ah.getResponseData().put("allow_times", at);
ah.getResponseData().put("retry_times", rt);
// 记录错误次数
Calendar ca;
long et;
ca = Calendar.getInstance(Core.getTimezone());
ca.setTimeInMillis(Core.ACTION_TIME.get());
ca.set(Calendar.HOUR_OF_DAY, 23);
ca.set(Calendar.MINUTE, 59);
ca.set(Calendar.SECOND, 59);
et = ca.getTimeInMillis() / 1000 + 1;
Record.set("sign.retry.times." + id, rt, et);
return;
} else {
Record.del("sign.retry.times." + id);
}
String uuid = (String) ud.get("id");
String uname = (String) ud.get("name");
String uhead = (String) ud.get("head");
int state = Synt.declare(ud.get("state"), 0);
// 验证状态
if (0 >= state) {
ah.reply(AuthKit.getWrong("state", "core.sign.state.invalid"));
return;
}
// 规避自定 RoleSet 附加判断
ah.setSessibute(Cnst.UID_SES, null);
ah.setSessibute(Cnst.USK_SES, null);
ah.setSessibute(Cnst.UST_SES, null);
// 验证区域
Set rs = RoleSet.getInstance(uuid);
if (rs != null && !place.isEmpty() && !rs.contains(place)) {
ah.reply(AuthKit.getWrong("place", "core.sign.place.invalid"));
return;
}
// * 表示密码登录
Map sd = AuthKit.userSign(ah, "*", uuid, uname, uhead);
ah.reply(Synt.mapOf("info", sd));
}
Aggregations