use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class MineAction method sameName.
@Action("same")
@Preset(conf = "master", form = "mine")
public void sameName(ActionHelper ah) throws HongsException {
Object id = ah.getSessibute(Cnst.UID_SES);
if (id == null || "".equals(id)) {
throw new HongsException(401, "");
}
UserAction ua = new UserAction();
Map rd = ah.getRequestData();
rd.put("id", id);
ua.isUnique(ah);
}
use of io.github.ihongs.HongsException 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);
}
}
use of io.github.ihongs.HongsException 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;
}
}
use of io.github.ihongs.HongsException in project HongsCORE by ihongs.
the class WXAction method getUserInfo.
public static Map getUserInfo(String code, String appId, String appSk) throws HongsException {
Map req;
Map rsp;
int err;
String url;
String token;
String opnId;
String opuId;
url = "https://api.weixin.qq.com/sns/oauth2/access_token";
req = new HashMap();
req.put("code", code);
req.put("appid", appId);
req.put("secret", appSk);
req.put("grant_type", "authorization_code");
rsp = Remote.parseData(Remote.get(url, req));
err = Synt.declare(rsp.get("errcode"), 0);
if (err != 0) {
throw new HongsException("Get token error\r\n" + Dawn.toString(rsp));
}
token = (String) rsp.get("access_token");
opnId = (String) rsp.get("openid");
url = "https://api.weixin.qq.com/sns/userinfo";
req = new HashMap();
req.put("openid", opnId);
req.put("access_token", token);
req.put("lang", "zh_CN");
rsp = Remote.parseData(Remote.get(url, req));
err = Synt.declare(rsp.get("errcode"), 0);
if (err != 0) {
throw new HongsException("Get user info error\r\n" + Dawn.toString(rsp));
}
opuId = (String) rsp.get("unionid");
req = new HashMap();
req.put("opnid", opnId);
req.put("opuid", opuId);
req.put("name", rsp.get("nickname"));
req.put("head", rsp.get("headimgurl"));
return req;
}
use of io.github.ihongs.HongsException 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;
}
}
Aggregations