[dw-free] Convert /misc/whereami to TT
[commit: http://hg.dwscoalition.org/dw-free/rev/1d3f737cd7fd]
http://bugs.dwscoalition.org/show_bug.cgi?id=2236
Simplify the controller for the whereami page TT version. This adds a new
controller method which we can use for most of the basic page patterns.
Patch by
mark.
Files modified:
http://bugs.dwscoalition.org/show_bug.cgi?id=2236
Simplify the controller for the whereami page TT version. This adds a new
controller method which we can use for most of the basic page patterns.
Patch by
![[staff profile]](https://www.dreamwidth.org/img/silk/identity/user_staff.png)
Files modified:
- cgi-bin/DW/Controller.pm
- cgi-bin/DW/Controller/Misc.pm
- cgi-bin/DW/Template/Apache2.pm
- views/misc/whereami.tt
-------------------------------------------------------------------------------- diff -r 515eb2dc34a6 -r 1d3f737cd7fd cgi-bin/DW/Controller.pm --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/cgi-bin/DW/Controller.pm Wed Dec 16 09:27:50 2009 +0000 @@ -0,0 +1,81 @@ +#!/usr/bin/perl +# +# DW::Controller +# +# Not actually a controller, but contains methods that help other controllers. +# +# Authors: +# Mark Smith <mark@dreamwidth.org> +# +# Copyright (c) 2009 by Dreamwidth Studios, LLC. +# +# This program is free software; you may redistribute it and/or modify it under +# the same terms as Perl itself. For a copy of the license, please reference +# 'perldoc perlartistic' or 'perldoc perlgpl'. +# + +package DW::Controller; + +use strict; +use warnings; +use Exporter; +use DW::Routing::Apache2; +use DW::Template::Apache2; + +our ( @ISA, @EXPORT ); +@ISA = qw/ Exporter /; +@EXPORT = qw/ needlogin error_ml controller /; + +# redirects the user to the login page to handle that eventuality +sub needlogin { + my $r = DW::Request->get; + + my $uri = $r->uri; + if ( my $qs = $r->query_string ) { + $uri .= '?' . $qs; + } + $uri = LJ::eurl( $uri ); + + $r->header_out( Location => "$LJ::SITEROOT/?returnto=$uri" ); + return $r->REDIRECT; +} + +# returns an error page using a language string +sub error_ml { + return DW::Template::Apache2->render_template( + DW::Request->get->r, 'error.tt', { message => LJ::Lang::ml( $_[0] ) } + ); +} + +# helper controller. give it a few arguments and it does nice things for you. +sub controller { + my ( %args ) = @_; + + my $vars = {}; + my $fail = sub { return ( 0, $_[0] ); }; + my $ok = sub { return ( 1, $vars ); }; + + # ensure the arguments make sense... anonymous means we cannot authas + delete $args{authas} if $args{anonymous}; + + # 'anonymous' pages must declare themselves, else we assume that a remote is + # necessary as most pages require a user + unless ( $args{anonymous} ) { + $vars->{u} = $vars->{remote} = LJ::get_remote() + or return $fail->( needlogin() ); + } + + # if a page allows authas it must declare it. authas can only happen if we are + # requiring the user to be logged in. + my $r = DW::Request->get; + if ( $args{authas} ) { + $vars->{u} = LJ::get_authas_user( $r->get_args->{authas} || $vars->{remote}->user ) + or return $fail->( error_ml( 'error.invalidauth' ) ); + $vars->{authas_html} = LJ::make_authas_select( $vars->{remote}, { authas => $vars->{u}->user } ); + } + + # everything good... let the caller know they can continue + return $ok->(); +} + +1; diff -r 515eb2dc34a6 -r 1d3f737cd7fd cgi-bin/DW/Controller/Misc.pm --- a/cgi-bin/DW/Controller/Misc.pm Wed Dec 16 03:23:02 2009 +0000 +++ b/cgi-bin/DW/Controller/Misc.pm Wed Dec 16 09:27:50 2009 +0000 @@ -21,50 +21,22 @@ package DW::Controller::Misc; use strict; use warnings; +use DW::Controller; use DW::Routing::Apache2; use DW::Template::Apache2; DW::Routing::Apache2->register_string( '/misc/whereami', \&whereami_handler, app => 1 ); -# redirects the user to the login page to handle that eventuality -sub needlogin { - my $r = DW::Request->get; - - my $uri = $r->uri; - if ( my $qs = $r->query_string ) { - $uri .= '?' . $qs; - } - $uri = LJ::eurl( $uri ); - - $r->header_out( Location => "$LJ::SITEROOT/?returnto=$uri" ); - return $r->REDIRECT; -} - -# returns an error page using a language string -sub error_ml { - return DW::Template::Apache2->render_template( - DW::Request->get->r, 'error.tt', { message => LJ::Lang::ml( $_[0] ) } - ); -} - # handles the /misc/whereami page sub whereami_handler { - my $r = DW::Request->get; + my ( $ok, $rv ) = controller( authas => 1 ); + return $rv unless $ok; - my $remote = LJ::get_remote() - or return needlogin(); - my $u = LJ::get_authas_user( $r->get_args->{authas} || $remote->user ) - or return error_ml( 'error.invalidauth' ); - - my $vars = { - user => $u, - cluster_name => $LJ::CLUSTER_NAME{$u->clusterid} || LJ::Lang::ml( '.cluster.unknown' ), - authas_html => LJ::make_authas_select( $remote, { authas => $u->user } ), + my $vars = { %$rv, + cluster_name => $LJ::CLUSTER_NAME{$rv->{u}->clusterid} || LJ::Lang::ml( '.cluster.unknown' ), }; - return DW::Template::Apache2->render_template( - $r->r, 'misc/whereami.tt', $vars, {} - ); + return DW::Template::Apache2->render_template( 'misc/whereami.tt', $vars ); } 1; diff -r 515eb2dc34a6 -r 1d3f737cd7fd cgi-bin/DW/Template/Apache2.pm --- a/cgi-bin/DW/Template/Apache2.pm Wed Dec 16 03:23:02 2009 +0000 +++ b/cgi-bin/DW/Template/Apache2.pm Wed Dec 16 09:27:50 2009 +0000 @@ -120,11 +120,9 @@ sub cached_template_string { }, $extra); } -=head2 C<< $class->render_cached_template( $r, $key, $filename, $subref, $extra ) >> +=head2 C<< $class->render_cached_template( $key, $filename, $subref, $extra ) >> Render a template inside the sitescheme or alone. - -NOTE: $r needs to be an Apache2 request, not a DW::Request. See render_template, except note that the opts hash is returned by subref if it's needed. @@ -151,20 +149,18 @@ See render_template, except note that th =cut sub render_cached_template { - my ($class, $r, $key, $filename, $subref, $opts, $extra) = @_; + my ($class, $key, $filename, $subref, $opts, $extra) = @_; $extra ||= {}; my $out = $class->cached_template_string( $key, $filename, $subref, $opts, $extra ); - return $class->render_string( $r, $out, $extra ); + return $class->render_string( $out, $extra ); } -=head2 C<< $class->render_template( $r, $filename, $opts, $extra ) >> +=head2 C<< $class->render_template( $filename, $opts, $extra ) >> Render a template inside the sitescheme or alone. - -NOTE: $r needs to be an Apache2 request, not a DW::Request. $extra can contain: @@ -183,18 +179,16 @@ NOTE: $r needs to be an Apache2 request, =cut sub render_template { - my ( $class, $r, $filename, $opts, $extra ) = @_; + my ( $class, $filename, $opts, $extra ) = @_; my $out = $class->template_string( $filename, $opts ); - return $class->render_string( $r, $out, $extra ); + return $class->render_string( $out, $extra ); } -=head2 C<< $class->render_string( $r, $string, $extra ) >> +=head2 C<< $class->render_string( $string, $extra ) >> Render a string inside the sitescheme or alone. - -NOTE: $r needs to be an Apache2 request, not a DW::Request. $extra can contain: @@ -211,9 +205,11 @@ NOTE: $r needs to be an Apache2 request, =back =cut + sub render_string { - my ( $class, $r, $out, $extra ) = @_; + my ( $class, $out, $extra ) = @_; + my $r = DW::Request->get->r; $r->status( $extra->{status} ) if $extra->{status}; $r->content_type( $extra->{content_type} ) if $extra->{content_type}; diff -r 515eb2dc34a6 -r 1d3f737cd7fd views/misc/whereami.tt --- a/views/misc/whereami.tt Wed Dec 16 03:23:02 2009 +0000 +++ b/views/misc/whereami.tt Wed Dec 16 09:27:50 2009 +0000 @@ -6,4 +6,4 @@ [% authas_html %] </form><br /> -<p>[% '.cluster' | ml(user = user.ljuser_display, cluster = cluster_name) %]</p> +<p>[% '.cluster' | ml(user = u.ljuser_display, cluster = cluster_name) %]</p> --------------------------------------------------------------------------------