add edit admin permissions
This commit is contained in:
@@ -909,6 +909,10 @@ public class ActionHandler {
|
||||
}
|
||||
System.out.println("10. View Profile");
|
||||
|
||||
if (isOwner) {
|
||||
System.out.println("11. Edit Admin Permissions");
|
||||
}
|
||||
|
||||
System.out.println("0. Back to Chat List");
|
||||
|
||||
String input = scanner.nextLine();
|
||||
@@ -952,6 +956,11 @@ public class ActionHandler {
|
||||
case "10" ->{
|
||||
GroupInfo(chat.getId());
|
||||
}
|
||||
case "11" -> {
|
||||
if (isOwner) {
|
||||
editAdminPermissions(chat.getId(), "group");
|
||||
}
|
||||
}
|
||||
|
||||
case "0" -> {
|
||||
return false;
|
||||
@@ -1015,6 +1024,10 @@ public class ActionHandler {
|
||||
}
|
||||
|
||||
System.out.println("10. View Profile");
|
||||
if (isOwner) {
|
||||
System.out.println("11. Edit Admin Permissions");
|
||||
}
|
||||
|
||||
System.out.println("0. Back to Chat List");
|
||||
|
||||
String input = scanner.nextLine();
|
||||
@@ -1088,6 +1101,13 @@ public class ActionHandler {
|
||||
case "10"->{
|
||||
ChannelInfo(chat.getId());
|
||||
}
|
||||
case "11" -> {
|
||||
if (isOwner) {
|
||||
editAdminPermissions(chat.getId(), "channel");
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
case "0" -> {
|
||||
return false;
|
||||
}
|
||||
@@ -2113,33 +2133,100 @@ public class ActionHandler {
|
||||
}
|
||||
|
||||
|
||||
private void editChannelAdminPermissions(UUID channelId) {
|
||||
System.out.print("Enter user_id of the admin to edit: ");
|
||||
String userId = scanner.nextLine().trim();
|
||||
|
||||
JSONObject permissions = new JSONObject();
|
||||
System.out.print("Can post? (true/false): ");
|
||||
permissions.put("can_post", Boolean.parseBoolean(scanner.nextLine()));
|
||||
System.out.print("Can edit channel info? (true/false): ");
|
||||
permissions.put("can_edit_channel", Boolean.parseBoolean(scanner.nextLine()));
|
||||
System.out.print("Can add members? (true/false): ");
|
||||
permissions.put("can_add_members", Boolean.parseBoolean(scanner.nextLine()));
|
||||
System.out.print("Can remove members? (true/false): ");
|
||||
permissions.put("can_remove_members", Boolean.parseBoolean(scanner.nextLine()));
|
||||
System.out.print("Can add admins? (true/false): ");
|
||||
permissions.put("can_add_admins", Boolean.parseBoolean(scanner.nextLine()));
|
||||
System.out.print("Can remove admins? (true/false): ");
|
||||
permissions.put("can_remove_admins", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
private void editAdminPermissions(UUID chatId, String chatType) {
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("action", "edit_channel_admin_permissions");
|
||||
req.put("channel_id", channelId.toString());
|
||||
req.put("user_id", userId);
|
||||
req.put("permissions", permissions);
|
||||
req.put("action", chatType.equals("group") ? "view_group_admins" : "view_channel_admins");
|
||||
req.put(chatType + "_id", chatId.toString());
|
||||
|
||||
JSONObject res = sendWithResponse(req);
|
||||
if (res != null)
|
||||
System.out.println(res.getString("message"));
|
||||
if (res == null || !res.getString("status").equals("success")) {
|
||||
System.out.println("❌ Failed to fetch admins.");
|
||||
return;
|
||||
}
|
||||
|
||||
JSONArray admins = res.getJSONObject("data").getJSONArray("admins");
|
||||
List<JSONObject> editableAdmins = new ArrayList<>();
|
||||
|
||||
System.out.println("\n--- Admins List ---");
|
||||
for (int i = 0; i < admins.length(); i++) {
|
||||
JSONObject admin = admins.getJSONObject(i);
|
||||
if (!admin.getString("role").equals("owner")) {
|
||||
editableAdmins.add(admin);
|
||||
System.out.printf("%d. %s (%s)\n", editableAdmins.size(),
|
||||
admin.getString("profile_name"),
|
||||
admin.getString("user_id"));
|
||||
}
|
||||
}
|
||||
|
||||
if (editableAdmins.isEmpty()) {
|
||||
System.out.println("⚠️ No editable admins found.");
|
||||
return;
|
||||
}
|
||||
|
||||
System.out.print("Select an admin to edit permissions: ");
|
||||
int choice;
|
||||
try {
|
||||
choice = Integer.parseInt(scanner.nextLine()) - 1;
|
||||
} catch (Exception e) {
|
||||
System.out.println("❌ Invalid input.");
|
||||
return;
|
||||
}
|
||||
|
||||
if (choice < 0 || choice >= editableAdmins.size()) {
|
||||
System.out.println("❌ Invalid selection.");
|
||||
return;
|
||||
}
|
||||
|
||||
JSONObject selected = editableAdmins.get(choice);
|
||||
String adminId = selected.getString("user_id");
|
||||
|
||||
JSONObject permissions = new JSONObject();
|
||||
|
||||
if (chatType.equals("channel")) {
|
||||
System.out.print("Can post? (true/false): ");
|
||||
permissions.put("can_post", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can edit channel info? (true/false): ");
|
||||
permissions.put("can_edit_channel", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can add members? (true/false): ");
|
||||
permissions.put("can_add_members", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can remove members? (true/false): ");
|
||||
permissions.put("can_remove_members", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can add admins? (true/false): ");
|
||||
permissions.put("can_add_admins", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can remove admins? (true/false): ");
|
||||
permissions.put("can_remove_admins", Boolean.parseBoolean(scanner.nextLine()));
|
||||
} else {
|
||||
System.out.print("Can add members? (true/false): ");
|
||||
permissions.put("can_add_members", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can remove members? (true/false): ");
|
||||
permissions.put("can_remove_members", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can add admins? (true/false): ");
|
||||
permissions.put("can_add_admins", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can remove admins? (true/false): ");
|
||||
permissions.put("can_remove_admins", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
System.out.print("Can edit group info? (true/false): ");
|
||||
permissions.put("can_edit_group", Boolean.parseBoolean(scanner.nextLine()));
|
||||
}
|
||||
|
||||
JSONObject updateReq = new JSONObject();
|
||||
updateReq.put("action", "edit_admin_permissions");
|
||||
updateReq.put("chat_id", chatId.toString());
|
||||
updateReq.put("chat_type", chatType);
|
||||
updateReq.put("admin_id", adminId);
|
||||
updateReq.put("permissions", permissions);
|
||||
|
||||
JSONObject updateRes = sendWithResponse(updateReq);
|
||||
if (updateRes != null)
|
||||
System.out.println(updateRes.getString("message"));
|
||||
}
|
||||
|
||||
|
||||
@@ -2181,26 +2268,7 @@ public class ActionHandler {
|
||||
}
|
||||
|
||||
|
||||
public void editAdminPermissions(String type, UUID entityId) {
|
||||
System.out.print("Enter user ID of the admin to edit: ");
|
||||
String targetUserId = scanner.nextLine().trim();
|
||||
|
||||
JSONObject permissions = new JSONObject();
|
||||
System.out.print("Can send messages? (true/false): ");
|
||||
permissions.put("can_send", Boolean.parseBoolean(scanner.nextLine()));
|
||||
System.out.print("Can edit info? (true/false): ");
|
||||
permissions.put("can_edit", Boolean.parseBoolean(scanner.nextLine()));
|
||||
|
||||
JSONObject req = new JSONObject();
|
||||
req.put("action", type.equals("group") ? "edit_group_admin_permissions" : "edit_channel_admin_permissions");
|
||||
req.put(type + "_id", entityId.toString());
|
||||
req.put("target_user_id", targetUserId);
|
||||
req.put("permissions", permissions);
|
||||
|
||||
send(req);
|
||||
JSONObject res = getResponse();
|
||||
System.out.println(res.getString("message"));
|
||||
}
|
||||
|
||||
|
||||
public void viewAdmins(String type, UUID entityId) {
|
||||
|
||||
@@ -79,7 +79,7 @@ public class IncomingMessageListener implements Runnable {
|
||||
"update_group_or_channel", "chat_deleted",
|
||||
"blocked_by_user", "unblocked_by_user", "message_seen",
|
||||
"removed_from_group", "removed_from_channel",
|
||||
"became_admin", "removed_admin", "ownership_transferred" -> true;
|
||||
"became_admin", "removed_admin", "ownership_transferred","admin_permissions_updated" -> true;
|
||||
default -> false;
|
||||
};
|
||||
}
|
||||
@@ -117,7 +117,7 @@ public class IncomingMessageListener implements Runnable {
|
||||
}).start();
|
||||
}
|
||||
|
||||
case "became_admin", "removed_admin", "ownership_transferred" -> {
|
||||
case "became_admin", "removed_admin", "ownership_transferred","admin_permissions_updated" -> {
|
||||
System.out.println("🧩 Detected admin/owner role change. Calling handler...");
|
||||
new Thread(() -> {
|
||||
try {
|
||||
|
||||
@@ -636,4 +636,18 @@ public class ChannelDatabase {
|
||||
return subscriberIds;
|
||||
}
|
||||
|
||||
public static boolean updateAdminPermissions(UUID channelId, UUID userId, JSONObject permissions) {
|
||||
String sql = "UPDATE channel_subscribers SET permissions = ?::jsonb WHERE channel_id = ? AND user_id = ? AND role = 'admin'";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, permissions.toString());
|
||||
stmt.setObject(2, channelId);
|
||||
stmt.setObject(3, userId);
|
||||
return stmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -615,4 +615,18 @@ public class GroupDatabase {
|
||||
return memberIds;
|
||||
}
|
||||
|
||||
public static boolean updateAdminPermissions(UUID groupId, UUID userId, JSONObject permissions) {
|
||||
String sql = "UPDATE group_members SET permissions = ?::jsonb WHERE group_id = ? AND user_id = ? AND role = 'admin'";
|
||||
try (Connection conn = ConnectionDb.connect();
|
||||
PreparedStatement stmt = conn.prepareStatement(sql)) {
|
||||
stmt.setString(1, permissions.toString());
|
||||
stmt.setObject(2, groupId);
|
||||
stmt.setObject(3, userId);
|
||||
return stmt.executeUpdate() > 0;
|
||||
} catch (SQLException e) {
|
||||
e.printStackTrace();
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
@@ -1628,6 +1628,50 @@ public class ClientHandler implements Runnable {
|
||||
break;
|
||||
}
|
||||
|
||||
case "edit_admin_permissions": {
|
||||
if (currentUser == null) {
|
||||
response = new ResponseModel("error", "Unauthorized. Please login first.");
|
||||
break;
|
||||
}
|
||||
|
||||
UUID chatId = UUID.fromString(requestJson.getString("chat_id"));
|
||||
String chatType = requestJson.getString("chat_type");
|
||||
UUID adminId = UUID.fromString(requestJson.getString("admin_id"));
|
||||
JSONObject newPermissions = requestJson.getJSONObject("permissions");
|
||||
|
||||
boolean success = false;
|
||||
|
||||
if (chatType.equals("group")) {
|
||||
success = GroupDatabase.updateAdminPermissions(chatId, adminId, newPermissions);
|
||||
} else if (chatType.equals("channel")) {
|
||||
success = ChannelDatabase.updateAdminPermissions(chatId, adminId, newPermissions);
|
||||
} else {
|
||||
response = new ResponseModel("error", "Invalid chat type.");
|
||||
break;
|
||||
}
|
||||
|
||||
if (success) {
|
||||
JSONObject data = new JSONObject();
|
||||
data.put("chat_id", chatId.toString());
|
||||
data.put("chat_type", chatType);
|
||||
data.put("permissions", newPermissions);
|
||||
|
||||
JSONObject event = new JSONObject();
|
||||
event.put("action", "admin_permissions_updated");
|
||||
event.put("data", data);
|
||||
|
||||
RealTimeEventDispatcher.sendToUser(adminId, event);
|
||||
}
|
||||
|
||||
|
||||
response = success
|
||||
? new ResponseModel("success", "Permissions updated successfully.")
|
||||
: new ResponseModel("error", "Failed to update permissions.");
|
||||
break;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user