3 %# Copyright (c) 1996-2003 Jesse Vincent <jesse@bestpractical.com>
5 %# (Except where explictly superceded by other copyright notices)
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
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.
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.
26 # Roll back any dangling transactions from a previous failed connection
27 $RT::Handle->ForceRollback() if $RT::Handle->TransactionDepth;
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
36 ? Encode::decode(utf8 => $_, Encode::FB_PERLQQ) :
38 ? [ map { ref($_) ? $_ : Encode::decode(utf8 => $_, Encode::FB_PERLQQ) } @$_ ] :
40 ? { map { ref($_) ? $_ : Encode::decode(utf8 => $_, Encode::FB_PERLQQ) } %$_ } : $_
45 $m->{'rt_base_time'} = [Time::HiRes::gettimeofday()];
49 $m->{'rt_base_time'} = time;
51 $m->comp('/Elements/SetupSessionCookie', %ARGS);
53 unless ($session{'CurrentUser'} && $session{'CurrentUser'}->Id) {
54 $session{'CurrentUser'} = RT::CurrentUser->new();
57 # Set the proper encoding for the current language handle
58 $r->content_type("text/html; charset=utf-8");
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/')
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;
72 $user = $ENV{'REMOTE_USER'};
73 $session{'CurrentUser'} = RT::CurrentUser->new();
74 my $load_method = $RT::WebExternalGecos ? 'LoadByGecos' : 'Load';
76 if ($^O eq 'MSWin32' and $RT::WebExternalGecos) {
77 my $NodeName = Win32::NodeName();
78 $user =~ s/^\Q$NodeName\E\\//i;
81 $session{'CurrentUser'}->$load_method($user);
83 if ($RT::WebExternalAuto and !$session{'CurrentUser'}->Id() ) {
84 # Create users on-the-fly with default attributes
86 my $UserObj = RT::User->new(RT::CurrentUser->new('root'));
88 my ($val, $msg) = $UserObj->Create(
89 %{ref($RT::AutoCreate) ? $RT::AutoCreate : {}},
95 $UserObj->SetPrivileged(1);
97 if ($^O !~ /^(?:riscos|MacOS|MSWin32|dos|os2)$/) {
98 # Populate fields with information from Unix /etc/passwd
100 my ($comments, $realname) = (getpwnam($user))[5, 6];
101 $UserObj->SetComments($comments) if defined $comments;
102 $UserObj->SetRealName($realname) if defined $realname;
104 elsif ($^O eq 'MSWin32' and eval 'use Net::AdminMisc; 1') {
105 # Populate fields with information from NT domain controller
108 $session{'CurrentUser'}->Load($user);
111 delete $session{'CurrentUser'};
112 $m->abort() unless $RT::WebFallbackToInternalAuth;
113 $m->comp('/Elements/Login', %ARGS, Error=> loc('Cannot create user: [_1]', $msg));
117 unless ( $session{'CurrentUser'}->Id() ) {
118 delete $session{'CurrentUser'};
121 if ( $RT::WebExternalOnly ) {
122 $m->comp('/Elements/Login', %ARGS, Error=> loc('You are not an authorized user'));
128 delete $session{'CurrentUser'}
129 unless $session{'CurrentUser'} and defined $session{'CurrentUser'}->Id;
131 # Process per-page authentication callbacks
132 $m->comp('/Elements/Callback', %ARGS, _CallbackName => 'Auth');
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);
139 if (!$session{'CurrentUser'}->id() ||
140 !$session{'CurrentUser'}->IsPassword($pass))
142 delete $session{'CurrentUser'};
143 $m->comp('/Elements/Login', %ARGS,
144 Error => loc('Your username or password is incorrect'));
149 # If we've got credentials, let's serve the file up.
150 if ( (defined $session{'CurrentUser'}) and
151 ( $session{'CurrentUser'}->Id) ) {
153 # Process per-page global callbacks
154 $m->comp('/Elements/Callback', %ARGS);
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');
163 $m->call_next(%ARGS);
167 # If we have no credentials
169 $m->comp('/Elements/Login', %ARGS);
173 <& /Elements/Footer, %ARGS &>