File Coverage

blib/lib/Doorman/PlackMiddleware.pm
Criterion Covered Total %
statement 64 68 94.1
branch 11 12 91.6
condition n/a
subroutine 18 19 94.7
pod 1 10 10.0
total 94 109 86.2


line stmt bran cond sub pod time code
1             package Doorman::PlackMiddleware;
2 4     4   3160 use 5.010;
  4         13  
3 4     4   17 use parent 'Plack::Middleware';
  4         7  
  4         26  
4 4     4   50051 use Plack::Session;
  4         1723  
  4         117  
5 4     4   20 use Plack::Util::Accessor qw(root_url scope);
  4         8  
  4         17  
6              
7 4     4   2010 use Doorman;
  4         12  
  4         195  
8             our $VERSION = $Doorman::VERSION;
9             our $AUTHORITY = $Doorman::AUTHORITY;
10              
11 4     4   1465 use Doorman::Scope;
  4         8  
  4         103  
12 4     4   23 use Scalar::Util qw(weaken);
  4         7  
  4         2408  
13              
14             sub prepare_app {
15 6     6 1 3847 my $self = shift;
16 6 100       30 $self->scope('users') unless $self->scope;
17 6         269 return $self;
18             }
19              
20             sub scope_object {
21 20     20 0 35 my ($self, $obj) = @_;
22              
23 20 100       47 if ($obj) {
24 2         5 $self->{scope_object} = $obj;
25 2         5 return $obj;
26             }
27              
28 18 100       55 unless ($self->{scope_object}) {
29 1         5 my $obj = Doorman::Scope->new( name => $self->scope, root_url => $self->root_url );
30 1         4 $self->{scope_object} = $obj;
31             }
32              
33 18         75 return $self->{scope_object};
34             }
35              
36             # Fully Qualified
37             sub fq {
38 22     22 0 41 my ($self, $name) = @_;
39 22         49 my $class_name = ref($self);
40 22         114 my ($mwname) = lc($class_name) =~ /^Plack::Middleware::Doorman(.+)$/i;
41 22 100       66 return "doorman." . $self->scope . ".${mwname}" . ($name ? ".${name}" : "");
42             }
43              
44             sub env_get {
45 2     2 0 13 my ($self, $name) = @_;
46 2         7 return $self->{env}->{ $self->fq($name) };
47             }
48              
49             sub env_set {
50 2     2 0 7 my ($self, $name, $value) = @_;
51 2         9 $self->{env}->{ $self->fq($name) } = $value;
52             }
53              
54             sub session_get {
55 6     6 0 7 my ($self, $name) = @_;
56 6         30 my $session = Plack::Session->new($self->{env});
57 6         46 return $session->get( $self->fq($name) );
58             }
59              
60             sub session_set {
61 1     1 0 2 my ($self, $name, $value) = @_;
62 1         8 my $session = Plack::Session->new($self->{env});
63 1         8 return $session->set( $self->fq($name), $value );
64             }
65              
66             sub session_remove {
67 1     1 0 2 my ($self, $name, $value) = @_;
68 1         7 my $session = Plack::Session->new($self->{env});
69 1         8 $session->remove( $self->fq($name) );
70             }
71              
72              
73             # STUB
74             sub is_sign_in {
75 0     0 0 0 my ($self) = @_;
76 0         0 die "Unimplemented: @{[ ref($self )]}->is_sign_in must be implemented.";
  0         0  
77             }
78              
79             # DELEGATE
80             {
81 4     4   24 no strict 'refs';
  4         6  
  4         1178  
82             for my $method (qw(scope_url sign_in_url sign_out_url scope_path sign_in_path sign_out_path)) {
83             *{ __PACKAGE__ . "::$method" } = sub {
84 12     12   10870 $_[0]->scope_object->$method;
85             };
86             }
87             }
88              
89             sub prepare_call {
90 6     6 0 10 my ($self, $env) = @_;
91 6         36 my $request = Plack::Request->new($env);
92              
93 6         46 $self->{env} = $env;
94 6         24 weaken($self->{env});
95              
96 6 100       23 if (!$self->root_url) {
97 1         11 my $root_uri = $request->uri->clone;
98 1         408 $root_uri->path("");
99 1         47 $root_uri->query(undef);
100 1         19 $self->root_url($root_uri->as_string);
101             }
102              
103 6 50       48 if ($env->{"doorman." . $self->scope}) {
104 0         0 $self->scope_object($env->{"doorman." . $self->scope});
105             }
106             else {
107 6         43 $env->{"doorman." . $self->scope} = $self->scope_object;
108             }
109              
110 6         54 return $self;
111             }
112              
113             1;