Skip to content

Commit

Permalink
[biography] allow admins to edit other people's bios
Browse files Browse the repository at this point in the history
  • Loading branch information
shish committed Feb 12, 2025
1 parent 95b0092 commit ced4f33
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 14 deletions.
23 changes: 14 additions & 9 deletions ext/biography/main.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ public function onUserPageBuilding(UserPageBuildingEvent $event): void
$duser = $event->display_user;
$bio = $duser->get_config()->get_string("biography", "");

if ($user->id == $duser->id) {
$this->theme->display_composer($page, $bio);
if ($user->id == $duser->id || $user->can(Permissions::EDIT_USER_INFO)) {
$this->theme->display_composer($page, $duser, $bio);
} else {
$this->theme->display_biography($page, $bio);
}
Expand All @@ -25,13 +25,18 @@ public function onUserPageBuilding(UserPageBuildingEvent $event): void
public function onPageRequest(PageRequestEvent $event): void
{
global $page, $user;
if ($event->page_matches("biography", method: "POST")) {
$bio = $event->get_POST('biography');
log_info("biography", "Set biography to $bio");
$user->get_config()->set_string("biography", $bio);
$page->flash("Bio Updated");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(referer_or(make_link()));
if ($event->page_matches("user/{name}/biography", method: "POST")) {
$duser = User::by_name($event->get_arg("name"));
if ($user->id == $duser->id || $user->can(Permissions::EDIT_USER_INFO)) {
$bio = $event->req_POST('biography');
log_info("biography", "Set biography to $bio");
$duser->get_config()->set_string("biography", $bio);
$page->flash("Bio Updated");
$page->set_mode(PageMode::REDIRECT);
$page->set_redirect(referer_or(make_link()));
} else {
throw new PermissionDenied("You do not have permission to edit this user's biography");
}
}
}
}
2 changes: 1 addition & 1 deletion ext/biography/test.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ class BiographyTest extends ShimmiePHPUnitTestCase
public function testBio(): void
{
$this->log_in_as_user();
$this->post_page("biography", ["biography" => "My bio goes here"]);
$this->post_page("user/" . self::USER_NAME . "/biography", ["biography" => "My bio goes here"]);
$this->get_page("user/" . self::USER_NAME);
$this->assert_text("My bio goes here");

Expand Down
14 changes: 10 additions & 4 deletions ext/biography/theme.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,9 @@
namespace Shimmie2;

use function MicroHTML\{TEXTAREA,rawHTML};
use function MicroHTML\TABLE;
use function MicroHTML\TD;
use function MicroHTML\TR;

class BiographyTheme extends Themelet
{
Expand All @@ -13,12 +16,15 @@ public function display_biography(Page $page, string $bio): void
$page->add_block(new Block("About Me", rawHTML(format_text($bio)), "main", 30, "about-me"));
}

public function display_composer(Page $page, string $bio): void
public function display_composer(Page $page, User $duser, string $bio): void
{
$html = SHM_SIMPLE_FORM(
make_link("biography"),
TEXTAREA(["style" => "width: 100%", "rows" => "6", "name" => "biography"], $bio),
SHM_SUBMIT("Save")
"user/{$duser->name}/biography",
TABLE(
["class" => "form", "style" => "width: 100%"],
TR(TD(TEXTAREA(["rows" => "6", "name" => "biography"], $bio))),
TR(TD(SHM_SUBMIT("Save")))
),
);

$page->add_block(new Block("About Me", $html, "main", 30));
Expand Down

0 comments on commit ced4f33

Please sign in to comment.