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