Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow admins to make users join factions #708

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/roles.md
Original file line number Diff line number Diff line change
Expand Up @@ -104,7 +104,8 @@ roleID {
| `faction.create` | `/factions` | Create faction | user |
| `faction.data` | `/factions/{fid}` | Access faction data | user |
| `faction.delete` | `/factions/{fid}` | Delete own faction | user |
| `faction.delete.other` | `/admin/faction/delete` | Delete other factions | staff |
| `faction.delete.other` | `/admin/faction/delete` | Delete other factions | administrator |
| `faction.join.other` | `/admin/faction/join` | Make other user join a faction | administrator |
| `faction.edit` | `/factions/{fid}` | Edit own faction | user |
| `faction.edit.other` | `/admin/faction/edit` | Edit other factions | staff |
| `faction.setblocked` | `/admin/setFactionBlocked` | Set block status on factions | staff |
Expand Down
1 change: 1 addition & 0 deletions resources/roles-reference.conf
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,7 @@ administrator {
board.placemap.ignore
user.shadowban
faction.delete.other
faction.join.other
faction.edit.other
faction.setblocked
notification.create
Expand Down
1 change: 1 addition & 0 deletions src/main/java/space/pxls/server/UndertowServer.java
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ public void start() {
.addPermGatedPrefixPath("/admin/forceNameChange", "user.namechange.force", webHandler::forceNameChange)
.addPermGatedPrefixPath("/admin/faction/edit", "faction.edit.other", new JsonReader(webHandler::adminEditFaction))
.addPermGatedPrefixPath("/admin/faction/delete", "faction.delete.other", new JsonReader(webHandler::adminDeleteFaction))
.addPermGatedPrefixPath("/admin/faction/join", "faction.join.other", new JsonReader(webHandler::adminJoinFaction))
.addPermGatedPrefixPath("/admin/setFactionBlocked", "faction.setblocked", new AllowedMethodsHandler(webHandler::setFactionBlocked, Methods.POST))
.addPermGatedPrefixPath("/createNotification", "notification.create", webHandler::createNotification)
.addPermGatedPrefixPath("/sendNotificationToDiscord", "notification.discord", webHandler::sendNotificationToDiscord)
Expand Down
57 changes: 57 additions & 0 deletions src/main/java/space/pxls/server/WebHandler.java
Original file line number Diff line number Diff line change
Expand Up @@ -381,6 +381,63 @@ public void adminDeleteFaction(HttpServerExchange exchange) {
send(200, exchange, "OK");
}

public void adminJoinFaction(HttpServerExchange exchange) {
User admin_user = exchange.getAttachment(AuthReader.USER);
if (admin_user == null) {
sendBadRequest(exchange, "No authenticated users found");
return;
}

FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
String fid_str;
String username;
try {
fid_str = data.getFirst("fid").getValue();
username = data.getFirst("user").getValue();
} catch (NullPointerException npe) {
sendBadRequest(exchange, "Missing fid/user");
return;
}

int fid;
try {
fid = Integer.parseInt(fid_str);
} catch (NumberFormatException ex) {
sendBadRequest(exchange, "Faction ID is not a number");
return;
}

Optional<Faction> _faction = FactionManager.getInstance().getByID(fid);
if (!_faction.isPresent()) {
sendBadRequest(exchange, "Faction with that ID doesn't exist");
return;
}
Faction faction = _faction.get();

User user = App.getUserManager().getByName(username);
if (user == null) {
sendBadRequest(exchange, "Invalid user provided");
return;
}

if (faction.fetchMembers().stream().anyMatch(fUser -> fUser.getId() == user.getId())) {
sendBadRequest(exchange, "User is already a member of this faction");
return;
}

if (faction.fetchBans().stream().anyMatch(fUser -> fUser.getId() == user.getId())) {
sendBadRequest(exchange, "User is banned from this faction");
return;
}

FactionManager.getInstance().joinFaction(fid, user.getId());
user.setDisplayedFactionMaybe(fid);

App.getDatabase().insertAdminLog(admin_user.getId(), String.format("Made %s join faction %d", user.getName(), fid));

send(200, exchange, "OK");
}

public void adminEditFaction(HttpServerExchange exchange) {
if (!exchange.getRequestMethod().equals(Methods.POST)) {
send(StatusCodes.METHOD_NOT_ALLOWED, exchange, StatusCodes.METHOD_NOT_ALLOWED_STRING);
Expand Down