598 lines
17 KiB
Java
598 lines
17 KiB
Java
package Manager;
|
|
|
|
import CORE.Core;
|
|
import Utility.User.GuestHandle;
|
|
import Utility.User.MyUser;
|
|
import Utility.User.Roles;
|
|
import Utility.User.UserPermission;
|
|
import Utility.jsonUtils;
|
|
|
|
import org.json.simple.JSONArray;
|
|
import org.json.simple.JSONObject;
|
|
|
|
import org.slf4j.Logger;
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
import org.telegram.telegrambots.meta.api.methods.groupadministration.KickChatMember;
|
|
import org.telegram.telegrambots.meta.api.methods.groupadministration.UnbanChatMember;
|
|
import org.telegram.telegrambots.meta.api.objects.User;
|
|
import org.telegram.telegrambots.meta.exceptions.TelegramApiException;
|
|
|
|
import java.util.ArrayList;
|
|
import java.util.List;
|
|
import java.util.Timer;
|
|
|
|
public class UserManager {
|
|
|
|
private static Logger log = LoggerFactory.getLogger(UserManager.class);
|
|
|
|
private static final String WHITELIST_FILENAME = "whitelist.json";
|
|
private static final String BLOCKLIST_FILENAME = "blocklist.json";
|
|
|
|
/**
|
|
* Put User in Whitelist with specific role
|
|
* @param user user to add
|
|
*/
|
|
public static void whitelistUser(MyUser user, Core core) {
|
|
log.info("Adding whitelist " + Core.getUserAsMention(user) + " - " + user.getId());
|
|
|
|
//Check if user is not already on whitelist
|
|
if (checkWhitelist(user.getId())) {
|
|
log.error("Adding whitelist error - already on whitelist");
|
|
return;
|
|
}
|
|
|
|
//Check if user is not already on blocklist
|
|
if (checkBlacklist(user.getId())) {
|
|
log.error("Adding whitelist error - already on blacklist");
|
|
return;
|
|
}
|
|
|
|
/*
|
|
* ROLES
|
|
* Admin
|
|
* User
|
|
* Guest
|
|
*/
|
|
|
|
// Save user to whitelist
|
|
JSONObject new_user = new JSONObject();
|
|
new_user.put("id", user.getId());
|
|
new_user.put("user_name", Core.getUserAsMention(user));
|
|
new_user.put("role", user.getRole().name());
|
|
new_user.put("permissions", new JSONArray());
|
|
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
whitelist.add(new_user);
|
|
jsonUtils.saveJson(whitelist, WHITELIST_FILENAME);
|
|
|
|
// Update Usermap
|
|
Core.userMap.put((long)user.getId(), user);
|
|
|
|
//Start Guesttimer
|
|
if (user.getRole() == Roles.GUEST)
|
|
new Timer().schedule(new GuestHandle(core, user.getId()), Core.GUEST_TIME);
|
|
}
|
|
|
|
/**
|
|
* Change user
|
|
* @param user user to change
|
|
*/
|
|
public static void changeUser(MyUser user, Core core) {
|
|
log.info("Changing user" + Core.getUserAsMention(user) + " - " + user.getId());
|
|
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
|
|
if (!checkWhitelist(user.getId())){
|
|
//TODO ERROR
|
|
log.debug("User not in whitelist!");
|
|
return;
|
|
}
|
|
|
|
//Find user entry
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject search = (JSONObject) o;
|
|
long searchId = (long) search.get("id");
|
|
|
|
//if user entry found:
|
|
if (searchId == user.getId()) {
|
|
//remove old entry
|
|
whitelist.remove(o);
|
|
|
|
//add user with new Role
|
|
JSONObject changed = new JSONObject();
|
|
|
|
changed.put("id", user.getId());
|
|
changed.put("user_name", Core.getUserAsMention(user));
|
|
changed.put("role", user.getRole().name());
|
|
|
|
JSONArray permissions = new JSONArray();
|
|
|
|
if (user.getPermission() != null && user.getPermission().size() > 0) {
|
|
for (UserPermission x : user.getPermission())
|
|
permissions.add(x.name());
|
|
}
|
|
|
|
changed.put("permissions", permissions);
|
|
|
|
whitelist.add(changed);
|
|
|
|
//Start Guesttimer
|
|
if (user.getRole() == Roles.GUEST)
|
|
new Timer().schedule(new GuestHandle(core, user.getId()), Core.GUEST_TIME);
|
|
|
|
// Update Usermap
|
|
Core.userMap.put((long)user.getId(), user);
|
|
|
|
break;
|
|
}
|
|
}
|
|
|
|
jsonUtils.saveJson(whitelist, WHITELIST_FILENAME);
|
|
}
|
|
|
|
/**
|
|
* Remove user from Whitelist and write to Blacklist
|
|
* @param user user to block
|
|
*/
|
|
public static void blockUser(User user, Core core) {
|
|
log.info("Adding Blacklist " + Core.getUserAsMention(user) + " - " + user.getId());
|
|
|
|
//Load id and username
|
|
long id = user.getId();
|
|
|
|
if (checkWhitelist(user.getId())) {
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
//Remove user From Whitelist
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject userjson = (JSONObject) o;
|
|
long userjson_id = (long) userjson.get("id");
|
|
|
|
//if user id is matching remove entry
|
|
if (userjson_id == id) {
|
|
whitelist.remove(o);
|
|
break;
|
|
}
|
|
}
|
|
|
|
//save new list
|
|
jsonUtils.saveJson(whitelist, WHITELIST_FILENAME);
|
|
|
|
//Block in chats
|
|
//kick user from all chats
|
|
for (long x : core.getWatchingChats()) {
|
|
KickChatMember kick = new KickChatMember()
|
|
.setChatId(x)
|
|
.setUserId(user.getId());
|
|
try {
|
|
kick.validate();
|
|
core.execute(kick);
|
|
} catch (TelegramApiException e) {
|
|
log.error(e.toString());
|
|
}
|
|
}
|
|
}
|
|
|
|
JSONArray blocklist = jsonUtils.loadJson(BLOCKLIST_FILENAME);
|
|
|
|
// Save user to blocklist
|
|
JSONObject new_user = new JSONObject();
|
|
new_user.put("id", id);
|
|
new_user.put("user_name", Core.getUserAsMention(user));
|
|
blocklist.add(new_user);
|
|
|
|
jsonUtils.saveJson(blocklist, BLOCKLIST_FILENAME);
|
|
|
|
// Update Usermap
|
|
Core.userMap.remove((long)user.getId());
|
|
}
|
|
|
|
/**
|
|
* Remove user from Blacklist
|
|
* @param userId user id to remove
|
|
*/
|
|
public static void unblock(long userId, Core core) {
|
|
|
|
log.info("Remove from Blocklist " + userId);
|
|
|
|
JSONArray blocklist = jsonUtils.loadJson(BLOCKLIST_FILENAME);
|
|
|
|
//search user entry
|
|
for (Object o : blocklist) {
|
|
JSONObject userjson = (JSONObject) o;
|
|
|
|
long user_id = (long) userjson.get("id");
|
|
|
|
//remove if id is equal
|
|
if (user_id == userId) {
|
|
blocklist.remove(o);
|
|
break;
|
|
}
|
|
}
|
|
|
|
//safe new blocklist
|
|
jsonUtils.saveJson(blocklist, BLOCKLIST_FILENAME);
|
|
|
|
//unBlock in chats
|
|
//kick user from all chats
|
|
for (long x : core.getWatchingChats()) {
|
|
UnbanChatMember unban = new UnbanChatMember()
|
|
.setChatId(x)
|
|
.setUserId((int) userId);
|
|
try {
|
|
unban.validate();
|
|
core.execute(unban);
|
|
} catch (TelegramApiException e) {
|
|
log.error(e.toString());
|
|
}
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Remove user From Whitelist
|
|
* @param userID user id to remove
|
|
*/
|
|
public static void removeUser(long userID){
|
|
log.info("Remove from Whitelist " + userID);
|
|
|
|
// Update Usermap
|
|
Core.userMap.remove(userID);
|
|
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
|
|
//search User
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject search = (JSONObject) o;
|
|
long searchId = (long) search.get("id");
|
|
|
|
//remove if id is equal
|
|
if (searchId == userID) {
|
|
whitelist.remove(o);
|
|
break;
|
|
}
|
|
}
|
|
|
|
jsonUtils.saveJson(whitelist, WHITELIST_FILENAME);
|
|
}
|
|
|
|
/**
|
|
* Check if user is Admin
|
|
* @param userId user id to check
|
|
* @return boolean true if admin
|
|
*/
|
|
public static boolean checkAdmin(long userId) {
|
|
log.debug("Checking if user " + userId + " is admin");
|
|
|
|
//first lookup hashmap
|
|
if (Core.userMap.containsKey(userId))
|
|
return Core.userMap.get(userId).getRole() == Roles.ADMIN;
|
|
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
|
|
//search userid
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject user = (JSONObject) o;
|
|
long user_id = (long) user.get("id");
|
|
if (user_id == userId) {
|
|
String role = (String) user.get("role");
|
|
if (role.equalsIgnoreCase(Roles.ADMIN.name())){
|
|
return true;
|
|
}
|
|
|
|
break;
|
|
}
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Load all Admins out of whitelist
|
|
* @return Arraylist out of admins
|
|
*/
|
|
public static ArrayList<MyUser> buildAdminList() {
|
|
|
|
log.debug("Build admin list");
|
|
ArrayList<MyUser> admins = new ArrayList<>();
|
|
|
|
for (long key : Core.userMap.keySet()){
|
|
if (Core.userMap.get(key).getRole() == Roles.ADMIN)
|
|
admins.add(Core.userMap.get(key));
|
|
}
|
|
|
|
return admins;
|
|
|
|
//OLD
|
|
|
|
/*
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
ArrayList<MyUser> admins = new ArrayList<>();
|
|
|
|
//go through whitelist
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject userjson = (JSONObject) o;
|
|
String role = (String) userjson.get("role");
|
|
|
|
if (role.equalsIgnoreCase(Roles.ADMIN.name())) {
|
|
long user_id = (long) userjson.get("id");
|
|
String user_name = (String) userjson.get("user_name");
|
|
|
|
admins.add(new MyUser((int)user_id, null, false, null, user_name, "DE", Roles.ADMIN));
|
|
}
|
|
}
|
|
|
|
return admins;
|
|
*/
|
|
}
|
|
|
|
|
|
/**
|
|
* Load all user out of Whitelist
|
|
* @return List of Users
|
|
*/
|
|
public static ArrayList<MyUser> buildUserList() {
|
|
log.debug("Build user list");
|
|
|
|
ArrayList<MyUser> users = new ArrayList<>();
|
|
|
|
for (long key : Core.userMap.keySet()){
|
|
if (Core.userMap.get(key).getRole() == Roles.USER)
|
|
users.add(Core.userMap.get(key));
|
|
}
|
|
|
|
return users;
|
|
|
|
/*
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
|
|
ArrayList<MyUser> userList = new ArrayList<>();
|
|
//go through whitelist
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject userjson = (JSONObject) o;
|
|
String role = (String) userjson.get("role");
|
|
if (role.equalsIgnoreCase(Roles.USER.name())) {
|
|
long user_id = (long) userjson.get("id");
|
|
String user_name = (String) userjson.get("user_name");
|
|
JSONArray permissions = (JSONArray) userjson.get("permissions");
|
|
|
|
MyUser user = new MyUser((int) user_id, null, false, null, user_name, "DE", Roles.USER);
|
|
|
|
List<UserPermission> perms = new ArrayList<>();
|
|
for (Object x : permissions)
|
|
perms.add(UserPermission.valueOf((String) x));
|
|
|
|
user.setPermission(perms);
|
|
|
|
userList.add(user);
|
|
}
|
|
}
|
|
return userList;
|
|
*/
|
|
}
|
|
|
|
|
|
/**
|
|
* load all Guests ou tof Whitelist
|
|
* @return List of Guests
|
|
*/
|
|
public static ArrayList<MyUser> buildGuestList() {
|
|
log.debug("Build guest list");
|
|
|
|
ArrayList<MyUser> guests = new ArrayList<>();
|
|
|
|
for (long key : Core.userMap.keySet()){
|
|
if (Core.userMap.get(key).getRole() == Roles.GUEST)
|
|
guests.add(Core.userMap.get(key));
|
|
}
|
|
|
|
return guests;
|
|
|
|
/*
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
ArrayList<MyUser> guests = new ArrayList<>();
|
|
|
|
//go through whitelist
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject userjson = (JSONObject) o;
|
|
String role = (String) userjson.get("role");
|
|
if (role.equalsIgnoreCase(Roles.GUEST.name())) {
|
|
long user_id = (long) userjson.get("id");
|
|
String user_name = (String) userjson.get("user_name");
|
|
|
|
MyUser user = new MyUser((int) user_id, null, false, null, user_name, "DE", Roles.USER);
|
|
|
|
guests.add(user);
|
|
}
|
|
}
|
|
return guests;
|
|
|
|
*/
|
|
}
|
|
|
|
//Method to load all blocked to a Userlist
|
|
public static ArrayList<MyUser> buildBlockList() {
|
|
log.debug("Build block list");
|
|
JSONArray blocklist = jsonUtils.loadJson(BLOCKLIST_FILENAME);
|
|
ArrayList<MyUser> blocked = new ArrayList<>();
|
|
|
|
//go through whitelist
|
|
for (Object o : blocklist) {
|
|
|
|
JSONObject userjson = (JSONObject) o;
|
|
|
|
long user_id = (long) userjson.get("id");
|
|
String user_name = (String) userjson.get("user_name");
|
|
|
|
MyUser user = new MyUser((int) user_id, null, false, null, user_name, "DE", Roles.BLOCKED);
|
|
|
|
blocked.add(user);
|
|
}
|
|
|
|
return blocked;
|
|
}
|
|
|
|
/**
|
|
* Get user from Whitelist by ID
|
|
* @param userID id of User
|
|
* @return MyUser object
|
|
*/
|
|
public static MyUser loadUser(long userID){
|
|
log.debug("load user: " + userID);
|
|
|
|
//First lookup user
|
|
if (Core.userMap.containsKey(userID))
|
|
return Core.userMap.get(userID);
|
|
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
|
|
for (Object o : whitelist){
|
|
|
|
JSONObject x = (JSONObject) o;
|
|
|
|
if ((long)x.get("id") == userID){
|
|
MyUser user = new MyUser((int)(long)x.get("id"), null, false, null, (String)x.get("user_name"), "DE", Roles.valueOf(((String)x.get("role")).toUpperCase()));
|
|
|
|
List<UserPermission> perms = new ArrayList<>();
|
|
if (x.containsKey("permissions")) {
|
|
for (Object z : (JSONArray) x.get("permissions")) {
|
|
//TODO work that shit!
|
|
perms.add(UserPermission.valueOf((String) z));
|
|
|
|
}
|
|
}
|
|
|
|
user.setPermission(perms);
|
|
|
|
//save to Hashmap
|
|
//Core.userMap.put(userID, user);
|
|
|
|
if (user.getRole() == Roles.ADMIN)
|
|
Core.userMap.put(userID, user);
|
|
|
|
return user;
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/**
|
|
* Get user from Blocklist by ID
|
|
* @param userID id of User
|
|
* @return MyUser object
|
|
*/
|
|
public static MyUser loadBlockedUser(long userID){
|
|
log.debug("load blocked user: " + userID);
|
|
|
|
JSONArray blocklist = jsonUtils.loadJson(BLOCKLIST_FILENAME);
|
|
|
|
for (Object o : blocklist){
|
|
|
|
JSONObject x = (JSONObject) o;
|
|
|
|
if ((long)x.get("id") == userID){
|
|
return new MyUser((int)(long)x.get("id"), null, false, null, (String)x.get("user_name"), "DE", Roles.BLOCKED);
|
|
}
|
|
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
/*------------------------------------------- UTIL ---------------------------------------------------------------*/
|
|
|
|
/**
|
|
* Check Whitelist for Given user if list contains user return true
|
|
* @param userID userID to check
|
|
* @return boolean user is in whitelist
|
|
*/
|
|
public static boolean checkWhitelist(long userID){
|
|
|
|
//First look in Hashmap
|
|
if (Core.userMap.containsKey(userID))
|
|
return true;
|
|
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
|
|
//go through Whitelist
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject test = (JSONObject) o;
|
|
long testId = (long) test.get("id");
|
|
|
|
//if IDis quall user is in Whitelist
|
|
if (testId == userID) {
|
|
return true;
|
|
}
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Check Blacklist for Given user if list contains user return true
|
|
* @param userID userID to check
|
|
* @return boolean user is in Blacklist
|
|
*/
|
|
public static boolean checkBlacklist(long userID){
|
|
|
|
JSONArray blacklist = jsonUtils.loadJson(BLOCKLIST_FILENAME);
|
|
|
|
//go through Blacklist
|
|
for (Object o : blacklist) {
|
|
|
|
JSONObject test = (JSONObject) o;
|
|
long testId = (long) test.get("id");
|
|
|
|
//if IDis quall user is in Whitelist
|
|
if (testId == userID) {
|
|
return true;
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
}
|
|
|
|
public static void loadUserList() {
|
|
log.info("load userList");
|
|
|
|
JSONArray whitelist = jsonUtils.loadJson(WHITELIST_FILENAME);
|
|
|
|
for (Object o : whitelist) {
|
|
|
|
JSONObject x = (JSONObject) o;
|
|
|
|
log.debug("JSON: " + x.toString());
|
|
|
|
MyUser user = new MyUser((int)(long)x.get("id"),
|
|
null, false, null,
|
|
(String)x.get("user_name"), "DE",
|
|
Roles.valueOf(((String)x.get("role")).toUpperCase()));
|
|
|
|
|
|
List<UserPermission> perms = new ArrayList<>();
|
|
if (x.containsKey("permissions")) {
|
|
JSONArray arr = (JSONArray) x.get("permissions");
|
|
for (Object z : arr) {
|
|
perms.add(UserPermission.valueOf((String) z));
|
|
|
|
}
|
|
user.setPermission(perms);
|
|
}
|
|
|
|
//save to Hashmap
|
|
Core.userMap.put((long)user.getId(), user);
|
|
|
|
log.debug("HASH: " + user.toStringEXTRA());
|
|
}
|
|
}
|
|
}
|