Skip to content

Instantly share code, notes, and snippets.

@yko
Created December 16, 2010 10:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save yko/743265 to your computer and use it in GitHub Desktop.
Save yko/743265 to your computer and use it in GitHub Desktop.
#!/usr/bin/env perl
use Mojolicious::Lite;
get '/' => 'index';
get '/set-admin' => sub {
my $self = shift;
$self->session->{this_user_is_admin} = 1;
$self->redirect_to('/');
} => 'became admin';
get '/unset-admin' => sub {
my $self = shift;
$self->session->{this_user_is_admin} = 0;
$self->redirect_to('/');
} => 'logout';
app->helper(
admin => sub {
my $c = shift;
my $cb = shift;
# By default return 1 on success
$cb ||= sub {1};
if ($c->session->{this_user_is_admin} == 1) {
return Mojo::ByteStream->new($cb->(@_));
}
return undef;
}
);
app->start;
__DATA__
@@ index.html.ep
% layout 'default';
<p>Welcome to Mojolicious!</p>
% # You can use 'admin' helper as boolean
% if (!admin) {
You can <%= link_to 'became admin' => begin %>obtain adminship<%end%> (only today!)
% }
% # And format pretty block of code with new begin/end block!
<%= admin begin %>
You are admin, but you can <%= link_to logout => begin %>logout<%end%> anytime.
<% end %>
@@ layouts/default.html.ep
<!doctype html><html>
<head><title>Welcome!</title></head>
<body>
<%== content %>
</body>
</html>
@ChuZi39
Copy link

ChuZi39 commented Jan 30, 2022

a

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment