import of rt 3.0.4
[freeside.git] / rt / html / autohandler
1 %# BEGIN LICENSE BLOCK
2 %# 
3 %# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
4 %# 
5 %# (Except where explictly superceded by other copyright notices)
6 %# 
7 %# This work is made available to you under the terms of Version 2 of
8 %# the GNU General Public License. A copy of that license should have
9 %# been provided with this software, but in any event can be snarfed
10 %# from www.gnu.org.
11 %# 
12 %# This work is distributed in the hope that it will be useful, but
13 %# WITHOUT ANY WARRANTY; without even the implied warranty of
14 %# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15 %# General Public License for more details.
16 %# 
17 %# Unless otherwise specified, all modifications, corrections or
18 %# extensions to this work which alter its source code become the
19 %# property of Best Practical Solutions, LLC when submitted for
20 %# inclusion in the work.
21 %# 
22 %# 
23 %# END LICENSE BLOCK
24 <%INIT>
25
26 # Roll back any dangling transactions from a previous failed connection
27 $RT::Handle->ForceRollback() if $RT::Handle->TransactionDepth;
28
29
30 local *session;
31 %ARGS = map {
32     # if they've passed multiple values, they'll be an array. if they've passed just one, a scalar
33     # whatever they are, mark them as utf8
34     my $type = ref($_);
35     (!$type)
36         ? Encode::decode(utf8 => $_, Encode::FB_PERLQQ) :
37     ($type eq 'ARRAY')
38         ? [ map { ref($_) ? $_ : Encode::decode(utf8 => $_, Encode::FB_PERLQQ) } @$_ ] :
39     ($type eq 'HASH')
40         ? { map { ref($_) ? $_ : Encode::decode(utf8 => $_, Encode::FB_PERLQQ) } %$_ } : $_
41 } %ARGS;
42
43 if ($ARGS{'Debug'}) {
44         require Time::HiRes;
45         $m->{'rt_base_time'} = [Time::HiRes::gettimeofday()];
46         
47 }
48 else {
49         $m->{'rt_base_time'} = time;
50 }
51 $m->comp('/Elements/SetupSessionCookie', %ARGS);
52
53 unless ($session{'CurrentUser'} && $session{'CurrentUser'}->Id) {
54     $session{'CurrentUser'} = RT::CurrentUser->new();
55 }
56
57 # Set the proper encoding for the current language handle
58 $r->content_type("text/html; charset=utf-8");
59
60 # If it's a noauth file, don't ask for auth.
61 if ($m->base_comp->path =~ '^/+NoAuth/' ||
62     $m->base_comp->path =~ '^/+REST/\d+\.\d+/NoAuth/')
63 {
64     $m->call_next(%ARGS);
65     $m->abort();
66 }
67
68 # If RT is configured for external auth, let's get REMOTE_USER
69 elsif ($RT::WebExternalAuth and length($ENV{'REMOTE_USER'})) {
70     my $orig_user = $user;
71
72     $user = $ENV{'REMOTE_USER'};
73     $session{'CurrentUser'} = RT::CurrentUser->new();
74     my $load_method = $RT::WebExternalGecos ? 'LoadByGecos' : 'Load';
75     
76     if ($^O eq 'MSWin32' and $RT::WebExternalGecos) {
77         my $NodeName = Win32::NodeName();
78         $user =~ s/^\Q$NodeName\E\\//i;
79     }
80
81     $session{'CurrentUser'}->$load_method($user);
82
83     if ($RT::WebExternalAuto and !$session{'CurrentUser'}->Id() ) {
84         # Create users on-the-fly with default attributes
85
86         my $UserObj = RT::User->new(RT::CurrentUser->new('root'));
87
88         my ($val, $msg) = $UserObj->Create(
89             %{ref($RT::AutoCreate) ? $RT::AutoCreate : {}},
90             Name         => $user,
91             Gecos        => $user,
92         );
93
94         if ($val) {
95             $UserObj->SetPrivileged(1);
96
97             if ($^O !~ /^(?:riscos|MacOS|MSWin32|dos|os2)$/) {
98                 # Populate fields with information from Unix /etc/passwd
99
100                 my ($comments, $realname) = (getpwnam($user))[5, 6];
101                 $UserObj->SetComments($comments) if defined $comments;
102                 $UserObj->SetRealName($realname) if defined $realname;
103             }
104             elsif ($^O eq 'MSWin32' and eval 'use Net::AdminMisc; 1') {
105                 # Populate fields with information from NT domain controller
106             }
107
108             $session{'CurrentUser'}->Load($user);
109         }
110         else {
111             delete $session{'CurrentUser'};
112             $m->abort() unless $RT::WebFallbackToInternalAuth;
113             $m->comp('/Elements/Login', %ARGS, Error=> loc('Cannot create user: [_1]', $msg));
114         }
115     }
116
117     unless ( $session{'CurrentUser'}->Id() ) {
118         delete $session{'CurrentUser'};
119         $user = $orig_user;
120
121         if ( $RT::WebExternalOnly ) {           
122             $m->comp('/Elements/Login', %ARGS, Error=> loc('You are not an authorized user'));
123             $m->abort();
124         }
125     }
126 }
127
128 delete $session{'CurrentUser'}
129     unless $session{'CurrentUser'} and defined $session{'CurrentUser'}->Id;
130
131 # Process per-page authentication callbacks
132 $m->comp('/Elements/Callback', %ARGS, _CallbackName => 'Auth');
133
134 # If the user is logging in, let's authenticate
135 if (!$session{'CurrentUser'} && defined ($user) && defined ($pass) ){
136     $session{'CurrentUser'} = RT::CurrentUser->new();
137     $session{'CurrentUser'}->Load($user);
138
139     if (!$session{'CurrentUser'}->id() ||
140         !$session{'CurrentUser'}->IsPassword($pass))
141     {
142         delete $session{'CurrentUser'};
143         $m->comp('/Elements/Login', %ARGS,
144                  Error => loc('Your username or password is incorrect'));
145         $m->abort();
146     }
147 }
148   
149 # If we've got credentials, let's serve the file up.
150 if ( (defined $session{'CurrentUser'}) and 
151      ( $session{'CurrentUser'}->Id) ) {
152     
153     # Process per-page global callbacks
154     $m->comp('/Elements/Callback', %ARGS);
155
156     # If the user isn't privileged, they can only see SelfService
157     if ((! $session{'CurrentUser'}->Privileged) and
158         ($m->base_comp->path !~ '^(/+)SelfService/') ) {
159         $m->comp('/SelfService/index.html');
160         $m->abort();
161     }
162     else {
163         $m->call_next(%ARGS);
164     }
165 }
166
167 # If we have no credentials
168 else {
169     $m->comp('/Elements/Login', %ARGS);
170     $m->abort();
171 }
172 </%INIT>
173 <& /Elements/Footer, %ARGS &>
174 <%ARGS>
175 $user => undef
176 $pass => undef
177 $menu => undef
178 </%ARGS>