attempt to get more information on errors using an RT-instansiated session??
[freeside.git] / FS / FS / TicketSystem / RT_Internal.pm
1 package FS::TicketSystem::RT_Internal;
2
3 use strict;
4 use vars qw( @ISA $DEBUG $me );
5 use Data::Dumper;
6 use FS::UID qw(dbh);
7 use FS::CGI qw(popurl);
8 use FS::TicketSystem::RT_Libs;
9 use RT::CurrentUser;
10
11 @ISA = qw( FS::TicketSystem::RT_Libs );
12
13 $DEBUG = 1;
14 $me = '[FS::TicketSystem::RT_Internal]';
15
16 sub sql_num_customer_tickets {
17   "( select count(*) from tickets
18                      join links on ( tickets.id = links.localbase )
19      where ( status = 'new' or status = 'open' or status = 'stalled' )
20        and target = 'freeside://freeside/cust_main/' || custnum
21    )";
22 }
23
24 sub baseurl {
25   #my $self = shift;
26   if ( $RT::URI::freeside::URL ) {
27     $RT::URI::freeside::URL. '/rt/';
28   } else {
29     'http://you_need_to_set_RT_URI_freeside_URL_in_SiteConfig.pm/';
30   }
31 }
32
33 #mapping/genericize??
34 #ShowConfigTab ModifySelf
35 sub access_right {
36   my( $self, $session, $right ) = @_;
37
38   #return '' unless $conf->config('ticket_system');
39   return '' unless FS::Conf->new->config('ticket_system');
40
41   if ( $session && $session->{'Current_User'} ) {
42     warn "$me access_right: using existing session and CurrentUser: \n".
43          Dumper($session->{'CurrentUser'})
44       if $DEBUG;
45  } else {
46     warn "$me access_right: loading session and CurrentUser\n" if $DEBUG > 1;
47     $self->_web_external_auth($session);
48   }
49
50   #warn "$me access_right: CurrentUser ". $session->{'CurrentUser'}. ":\n".
51   #     ( $DEBUG>1 ? Dumper($session->{'CurrentUser'}) : '' )
52   #  if $DEBUG > 1;
53
54   $session->{'CurrentUser'}->HasRight( Right  => $right,
55                                        Object => $RT::System );
56 }
57
58 #shameless false laziness w/RT::Interface::Web::AttemptExternalAuth
59 # to get logged into RT from afar
60 sub _web_external_auth {
61   my( $self, $session ) = @_;
62
63   my $user = $FS::CurrentUser::CurrentUser->username;
64
65   $session->{'CurrentUser'} = RT::CurrentUser->new();
66
67   warn "$me _web_external_auth loading RT user for $user\n"
68     if $DEBUG > 1;
69
70   $session->{'CurrentUser'}->Load($user);
71
72   if ( ! $session->{'CurrentUser'}->Id() ) {
73
74       # Create users on-the-fly
75
76       warn "can't load RT user for $user; auto-creating\n"
77         if $DEBUG;
78
79       my $UserObj = RT::User->new( RT::CurrentUser->new('RT_System') );
80
81       my ( $val, $msg ) = $UserObj->Create(
82           %{ ref($RT::AutoCreate) ? $RT::AutoCreate : {} },
83           Name  => $user,
84           Gecos => $user,
85       );
86
87       if ($val) {
88
89           # now get user specific information, to better create our user.
90           my $new_user_info
91               = RT::Interface::Web::WebExternalAutoInfo($user);
92
93           # set the attributes that have been defined.
94           # FIXME: this is a horrible kludge. I'm sure there's something cleaner
95           foreach my $attribute (
96               'Name',                  'Comments',
97               'Signature',             'EmailAddress',
98               'PagerEmailAddress',     'FreeformContactInfo',
99               'Organization',          'Disabled',
100               'Privileged',            'RealName',
101               'NickName',              'Lang',
102               'EmailEncoding',         'WebEncoding',
103               'ExternalContactInfoId', 'ContactInfoSystem',
104               'ExternalAuthId',        'Gecos',
105               'HomePhone',             'WorkPhone',
106               'MobilePhone',           'PagerPhone',
107               'Address1',              'Address2',
108               'City',                  'State',
109               'Zip',                   'Country'
110               )
111           {
112               #uhh, wrong root
113               #$m->comp( '/Elements/Callback', %ARGS,
114               #    _CallbackName => 'NewUser' );
115
116               my $method = "Set$attribute";
117               $UserObj->$method( $new_user_info->{$attribute} )
118                   if ( defined $new_user_info->{$attribute} );
119           }
120           $session->{'CurrentUser'}->Load($user);
121       }
122       else {
123
124          # we failed to successfully create the user. abort abort abort.
125           delete $session->{'CurrentUser'};
126
127           die "can't auto-create RT user"; #an error message would be nice :/
128           #$m->abort() unless $RT::WebFallbackToInternalAuth;
129           #$m->comp( '/Elements/Login', %ARGS,
130           #    Error => loc( 'Cannot create user: [_1]', $msg ) );
131       }
132   }
133
134   unless ( $session->{'CurrentUser'}->Id() ) {
135       delete $session->{'CurrentUser'};
136
137       die "can't auto-create RT user";
138       #$user = $orig_user;
139       # 
140       #if ($RT::WebExternalOnly) {
141       #    $m->comp( '/Elements/Login', %ARGS,
142       #        Error => loc('You are not an authorized user') );
143       #    $m->abort();
144       #}
145   }
146
147 }
148
149 1;
150