Created action for creating new mods

This commit is contained in:
swagg boi 2023-04-15 00:57:45 -04:00
parent 829be876c1
commit 25f47ce80d
6 changed files with 95 additions and 20 deletions

View file

@ -32,7 +32,8 @@ Run the tests locally (against development environment):
## TODOs ## TODOs
1. Actions for creating moderators and resetting passwords 1. Action for resetting passwords
1. Action for locking/unlocking accounts
1. CSS 1. CSS
1. "All new posts flagged" mode (require approval for new posts) 1. "All new posts flagged" mode (require approval for new posts)

View file

@ -58,10 +58,6 @@ sub startup($self) {
return undef; return undef;
}); });
$self->helper(is_admin => sub ($c) {
$self->session->{'is_admin'} || undef
});
# Finish configuring some things # Finish configuring some things
$self->secrets($self->config->{'secrets'}) || die $@; $self->secrets($self->config->{'secrets'}) || die $@;
@ -160,6 +156,10 @@ sub startup($self) {
->to('moderator#hidden') ->to('moderator#hidden')
->name('hidden_list'); ->name('hidden_list');
$moderator->any([qw{GET POST}], '/create')
->to('moderator#create')
->name('create_mod');
my $mod_thread = $moderator->under('/thread'); my $mod_thread = $moderator->under('/thread');
$mod_thread->get('/unflag/:thread_id', [thread_id => qr/\d+/]) $mod_thread->get('/unflag/:thread_id', [thread_id => qr/\d+/])
@ -188,9 +188,6 @@ sub startup($self) {
->to('moderator#unhide_remark') ->to('moderator#unhide_remark')
->name('unhide_remark'); ->name('unhide_remark');
# under() for admins
# Actions to create mods...
} }
1; 1;

View file

@ -91,7 +91,7 @@ sub unflag_thread($self) {
my $redirect_url = $self->url_for('threads_list')->fragment('info')->to_abs; my $redirect_url = $self->url_for('threads_list')->fragment('info')->to_abs;
$self->moderator->unflag_thread($thread_id); $self->moderator->unflag_thread($thread_id);
$self->flash(info => "Thread #$thread_id has been unflagged. ◀"); $self->flash(info => "Thread #$thread_id has been unflagged. ◀");
$self->redirect_to($redirect_url); $self->redirect_to($redirect_url);
} }
@ -124,7 +124,7 @@ sub unflag_remark($self) {
->fragment('info')->to_abs; ->fragment('info')->to_abs;
$self->moderator->unflag_remark($remark_id); $self->moderator->unflag_remark($remark_id);
$self->flash(info => "Remark #$remark_id has been unflagged. ◀"); $self->flash(info => "Remark #$remark_id has been unflagged. ◀");
$self->redirect_to($redirect_url); $self->redirect_to($redirect_url);
} }
@ -152,4 +152,32 @@ sub unhide_remark($self) {
$self->redirect_to($redirect_url); $self->redirect_to($redirect_url);
} }
sub create($self) {
my $v;
$v = $self->validation if $self->req->method eq 'POST';
if ($v && $v->has_data) {
$v->required('name' );
$v->required('email' );
$v->required('password');
if ($v->has_error) {
$self->stash(status => 400)
}
else {
my ($name, $email, $password);
$name = $self->param('name');
$email = $self->param('email');
$password = $self->param('password');
$self->moderator->create($name, $email, $password);
$self->stash(info => "Created moderator account for $name 🧑‍🏭");
}
}
return $self->render;
}
1; 1;

View file

@ -83,6 +83,7 @@ subtest Login => sub {
->element_exists('a[href*="/moderator/flagged"]') ->element_exists('a[href*="/moderator/flagged"]')
->element_exists('a[href*="/moderator/hidden"]' ) ->element_exists('a[href*="/moderator/hidden"]' )
->element_exists('a[href*="/logout"]' ) ->element_exists('a[href*="/logout"]' )
->element_exists('a[href*="/moderator/create"]' )
}; };
subtest Hidden => sub { subtest Hidden => sub {
@ -92,6 +93,17 @@ subtest Login => sub {
->element_exists('a[href*="/moderator/flagged"]') ->element_exists('a[href*="/moderator/flagged"]')
->element_exists('a[href*="/moderator/hidden"]' ) ->element_exists('a[href*="/moderator/hidden"]' )
->element_exists('a[href*="/logout"]' ) ->element_exists('a[href*="/logout"]' )
->element_exists('a[href*="/moderator/create"]' )
};
subtest Create => sub {
$t->get_ok('/moderator/create')
->status_is(200)
->text_like(h2 => qr/Create Moderator/)
->element_exists('a[href*="/moderator/flagged"]')
->element_exists('a[href*="/moderator/hidden"]' )
->element_exists('a[href*="/logout"]' )
->element_exists('a[href*="/moderator/create"]' )
}; };
# Mod session ends # Mod session ends
@ -125,6 +137,10 @@ subtest Login => sub {
$t->get_ok('/moderator/hidden') $t->get_ok('/moderator/hidden')
->status_is(302) ->status_is(302)
->header_like(Location => qr/login/); ->header_like(Location => qr/login/);
$t->get_ok('/moderator/create')
->status_is(302)
->header_like(Location => qr/login/);
}; };
}; };

View file

@ -7,16 +7,28 @@
<body> <body>
<h1>Post::Text</h1> <h1>Post::Text</h1>
<nav> <nav>
<%= link_to List => 'threads_list' %> <div>
<%= link_to New => 'post_thread' %> <span>Navigate:</span>
<% unless (is_mod) { =%> <%= link_to List => 'threads_list' %>
<%= link_to Login => 'mod_login' %> <%= link_to New => 'post_thread' %>
<% } =%> <% unless (is_mod) { =%>
<% if (is_mod) { =%> <%= link_to Login => 'mod_login' %>
<%= link_to Flagged => 'flagged_list' %> <% } =%>
<%= link_to Hidden => 'hidden_list' %> </div>
<%= link_to Logout => 'mod_logout' %> <div>
<% } =%> <% if (is_mod) { =%>
<span>Moderate:</span>
<%= link_to Flagged => 'flagged_list' %>
<%= link_to Hidden => 'hidden_list' %>
<%= link_to Logout => 'mod_logout' %>
<% } =%>
</div>
<div>
<% if (is_mod && session->{'is_admin'}) { =%>
<span>Admin:</span>
<%= link_to Create => 'create_mod' %>
<% } =%>
</div>
</nav> </nav>
<hr> <hr>
<% if (flash 'error') { =%> <% if (flash 'error') { =%>
@ -27,6 +39,9 @@
<% if (flash 'info') { =%> <% if (flash 'info') { =%>
<p class="field-with-info" id="info"><%= flash 'info' %></p> <p class="field-with-info" id="info"><%= flash 'info' %></p>
<% } =%> <% } =%>
<% if (stash 'info') { =%>
<p class="field-with-info" id="info"><%= stash 'info' %></p>
<% } =%>
<%= content =%> <%= content =%>
<footer> <footer>
<p>In UTF-8 we trust. 🫡</p> <p>In UTF-8 we trust. 🫡</p>

View file

@ -0,0 +1,18 @@
% layout 'default';
% title 'Create Moderator';
<h2><%= title %></h2>
<form method="post">
<div class="name field">
<%= label_for name => 'Name' %>
<%= text_field 'name' %>
</div>
<div class="email field">
<%= label_for email => 'Email' %>
<%= email_field 'email' %>
</div>
<div class="password field">
<%= label_for password => 'Password' %>
<%= password_field 'password' %>
</div>
<%= submit_button 'Create' %>
</form>