|
|
|
@ -9,6 +9,7 @@ import com.jfinal.plugin.activerecord.Page;
|
|
|
|
|
import com.jfinal.plugin.activerecord.Record;
|
|
|
|
|
import com.jfinal.plugin.activerecord.SqlPara;
|
|
|
|
|
|
|
|
|
|
import java.util.ArrayList;
|
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
|
|
public class DataShareModel {
|
|
|
|
@ -60,10 +61,32 @@ public class DataShareModel {
|
|
|
|
|
* @param system_id
|
|
|
|
|
*/
|
|
|
|
|
public void delSystem(int system_id) {
|
|
|
|
|
//此系统的用户
|
|
|
|
|
//1、删除此系统的用户
|
|
|
|
|
String user_name = getSystemById(system_id).getStr("user_name");
|
|
|
|
|
PgUtil.delUser(user_name);
|
|
|
|
|
//2、删除数据表记录
|
|
|
|
|
Db.deleteById("t_datashare_system", "system_id", system_id);
|
|
|
|
|
//3、删除此系统共享表
|
|
|
|
|
String sql = "delete from t_datashare_table where system_id=?";
|
|
|
|
|
Db.update(sql, system_id);
|
|
|
|
|
//4、删除所有其它系统对此系统的订阅
|
|
|
|
|
sql = "select * from t_datashare_system";
|
|
|
|
|
List<Record> list = Db.find(sql);
|
|
|
|
|
for (Record record : list) {
|
|
|
|
|
int sub_system_id = record.getInt("system_id");
|
|
|
|
|
String subscribe_system_ids = record.getStr("subscribe_system_ids");
|
|
|
|
|
String new_subscribe_system_ids = "";
|
|
|
|
|
if (!StrKit.isBlank(subscribe_system_ids)) {
|
|
|
|
|
for (String s : subscribe_system_ids.split(",")) {
|
|
|
|
|
if (!s.equals(system_id + "")) new_subscribe_system_ids += s + ",";
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
if (!new_subscribe_system_ids.isEmpty()) {
|
|
|
|
|
new_subscribe_system_ids = new_subscribe_system_ids.substring(0, new_subscribe_system_ids.length() - 1);//去最后的逗号
|
|
|
|
|
}
|
|
|
|
|
sql = "update t_datashare_system set subscribe_system_ids=? where system_id=?";
|
|
|
|
|
Db.update(sql, new_subscribe_system_ids, sub_system_id);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
@ -94,35 +117,59 @@ public class DataShareModel {
|
|
|
|
|
* @param table_names
|
|
|
|
|
*/
|
|
|
|
|
public void share(int system_id, String table_names) {
|
|
|
|
|
//系统的数据库账号
|
|
|
|
|
Record record = getSystemById(system_id);
|
|
|
|
|
String user_name = record.getStr("user_name");
|
|
|
|
|
String subscribe_system_ids=record.getStr("subscribe_system_ids");
|
|
|
|
|
//0、系统的ID与数据库用户名
|
|
|
|
|
Record systemRecord = getSystemById(system_id);
|
|
|
|
|
String user_name = systemRecord.getStr("user_name");
|
|
|
|
|
String subscribe_system_ids = systemRecord.getStr("subscribe_system_ids");
|
|
|
|
|
|
|
|
|
|
//原来共享的表
|
|
|
|
|
List<Record> oldTables = getShareTable(system_id);
|
|
|
|
|
|
|
|
|
|
//1、回收原来共享表的写权限
|
|
|
|
|
for (Record r : getShareTable(system_id)) {//原来有哪些共享表?
|
|
|
|
|
for (Record r : oldTables) {//原来有哪些共享表?
|
|
|
|
|
String table_name = r.getStr("table_name");
|
|
|
|
|
PgUtil.revokeUserPrivilege(user_name, table_name, PgUtil.WRITE);
|
|
|
|
|
}
|
|
|
|
|
//2、重新分配共享表的写权限
|
|
|
|
|
|
|
|
|
|
//2、重新分配新共享表的写权限
|
|
|
|
|
for (String s : table_names.split(",")) {
|
|
|
|
|
PgUtil.grantUserPrivilege(user_name, s, PgUtil.WRITE);
|
|
|
|
|
}
|
|
|
|
|
//3、原来订阅这个系统有第三方系统有哪些,它们的用户都需要取消对原来表的读授权
|
|
|
|
|
|
|
|
|
|
//3、原来订阅这个系统有第三方系统有哪些,它们的用户都需要取消对原来表的读授权
|
|
|
|
|
List<String> listUser = new ArrayList<>();
|
|
|
|
|
if (!StrKit.isBlank(subscribe_system_ids)) {
|
|
|
|
|
for (String s : subscribe_system_ids.split(",")) {
|
|
|
|
|
String sub_user_name = getSystemById(Integer.parseInt(s)).getStr("user_name");
|
|
|
|
|
listUser.add(sub_user_name);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//4、删除旧数据
|
|
|
|
|
for (Record r : oldTables) {//原来有哪些共享表?
|
|
|
|
|
String table_name = r.getStr("table_name");
|
|
|
|
|
for (String s : listUser) {
|
|
|
|
|
PgUtil.revokeUserPrivilege(s, table_name, PgUtil.READ);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//4、原来访问的这个系统的第三方系统,它们的用户都需要授予新表的读权限
|
|
|
|
|
if (!StrKit.isBlank(table_names)) {
|
|
|
|
|
for (String table_name : table_names.split(",")) {
|
|
|
|
|
for (String user : listUser) {
|
|
|
|
|
PgUtil.revokeUserPrivilege(user, table_name, PgUtil.READ);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
//5、删除旧数据
|
|
|
|
|
String sql = "delete from t_datashare_table where system_id=?";
|
|
|
|
|
Db.update(sql, system_id);
|
|
|
|
|
//5、保存新数据
|
|
|
|
|
|
|
|
|
|
//6、保存新数据
|
|
|
|
|
for (String s : table_names.split(",")) {
|
|
|
|
|
Record record = new Record();
|
|
|
|
|
record.set("system_id", system_id);
|
|
|
|
|
record.set("table_name", s);
|
|
|
|
|
Db.save("t_datashare_table", "id", record);
|
|
|
|
|
systemRecord = new Record();
|
|
|
|
|
systemRecord.set("system_id", system_id);
|
|
|
|
|
systemRecord.set("table_name", s);
|
|
|
|
|
Db.save("t_datashare_table", "id", systemRecord);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//TODO
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
|