1 package FS::TicketSystem::RT_Internal;
4 use vars qw( @ISA $DEBUG $me );
8 use FS::CGI qw(popurl);
9 use FS::TicketSystem::RT_Libs;
12 @ISA = qw( FS::TicketSystem::RT_Libs );
15 $me = '[FS::TicketSystem::RT_Internal]';
17 sub sql_num_customer_tickets {
18 "( select count(*) from Tickets
19 join Links on ( Tickets.id = Links.LocalBase )
20 where ( Status = 'new' or Status = 'open' or Status = 'stalled' )
21 and Target = 'freeside://freeside/cust_main/' || custnum
27 if ( $RT::URI::freeside::URL ) {
28 $RT::URI::freeside::URL. '/rt/';
30 'http://you_need_to_set_RT_URI_freeside_URL_in_SiteConfig.pm/';
35 #ShowConfigTab ModifySelf
37 my( $self, $session, $right ) = @_;
39 #return '' unless $conf->config('ticket_system');
40 return '' unless FS::Conf->new->config('ticket_system');
42 $session = $self->session($session);
44 #warn "$me access_right: CurrentUser ". $session->{'CurrentUser'}. ":\n".
45 # ( $DEBUG>1 ? Dumper($session->{'CurrentUser'}) : '' )
48 $session->{'CurrentUser'}->HasRight( Right => $right,
49 Object => $RT::System );
53 my( $self, $session ) = @_;
55 if ( $session && $session->{'Current_User'} ) {
56 warn "$me session: using existing session and CurrentUser: \n".
57 Dumper($session->{'CurrentUser'})
60 warn "$me session: loading session and CurrentUser\n" if $DEBUG > 1;
61 $session = $self->_web_external_auth($session);
70 warn "$me init: loading RT libraries\n" if $DEBUG;
72 use lib ( "/opt/rt3/local/lib", "/opt/rt3/lib" );
74 #it looks like the rest are taken care of these days in RT::InitClasses
76 #use RT::Transactions;
81 #use RT::ScripActions;
82 #use RT::ScripConditions;
85 #use RT::GroupMembers;
86 #use RT::CustomFields;
87 #use RT::CustomFieldValues;
88 #use RT::ObjectCustomFieldValues;
90 #for web external auth...
91 use RT::Interface::Web;
95 warn "$me init: loading RT config\n" if $DEBUG;
98 eval 'RT::LoadConfig();';
102 warn "$me init: initializing RT\n" if $DEBUG;
105 eval 'RT::Init("NoSignalHandlers"=>1);';
109 warn "$me init: complete" if $DEBUG;
112 =item create_ticket SESSION_HASHREF, OPTION => VALUE ...
114 Class method. Creates a ticket. If there is an error, returns the scalar
115 error, otherwise returns the newly created RT::Ticket object.
117 Accepts the following options:
131 Requestor email address or arrayref of addresses
135 Cc: email address or arrayref of addresses
143 MIME type to use for message. Defaults to text/plain. Specifying text/html
144 can be useful to use HTML markup in message.
148 Customer number (see L<FS::cust_main>) to associate with ticket.
152 Service number (see L<FS::cust_svc>) to associate with ticket. Will also
153 associate the customer who has this service (unless the service is unlinked).
160 my($self, $session, %param) = @_;
162 $session = $self->session($session);
164 my $Queue = RT::Queue->new($session->{'CurrentUser'});
165 $Queue->Load( $param{'queue'} );
167 my $req = ref($param{'requestor'})
168 ? $param{'requestor'}
169 : ( $param{'requestor'} ? [ $param{'requestor'} ] : [] );
171 my $cc = ref($param{'cc'})
173 : ( $param{'cc'} ? [ $param{'cc'} ] : [] );
175 my $mimeobj = MIME::Entity->build(
176 'Data' => $param{'message'},
177 'Type' => ( $param{'mime_type'} || 'text/plain' ),
181 'Queue' => $Queue->Id,
182 'Subject' => $param{'subject'},
185 'MIMEObj' => $mimeobj,
187 warn Dumper(\%ticket) if $DEBUG > 1;
189 my $Ticket = RT::Ticket->new($session->{'CurrentUser'});
190 my( $id, $Transaction, $ErrStr );
193 ( $id, $Transaction, $ErrStr ) = $Ticket->Create( %ticket );
195 return $ErrStr if $id == 0;
197 warn "ticket got id $id\n" if $DEBUG;
199 #XXX check errors adding custnum/svcnum links (put it in a transaction)...
200 # but we do already know they're good
202 if ( $param{'custnum'} ) {
203 my( $val, $msg ) = $Ticket->_AddLink(
204 'Type' => 'MemberOf',
205 'Target' => 'freeside://freeside/cust_main/'. $param{'custnum'},
209 if ( $param{'svcnum'} ) {
210 my( $val, $msg ) = $Ticket->_AddLink(
211 'Type' => 'MemberOf',
212 'Target' => 'freeside://freeside/cust_svc/'. $param{'svcnum'},
219 #shameless false laziness w/RT::Interface::Web::AttemptExternalAuth
220 # to get logged into RT from afar
221 sub _web_external_auth {
222 my( $self, $session ) = @_;
224 my $user = $FS::CurrentUser::CurrentUser->username;
227 $session->{'CurrentUser'} = RT::CurrentUser->new();
229 warn "$me _web_external_auth loading RT user for $user\n"
232 $session->{'CurrentUser'}->Load($user);
234 if ( ! $session->{'CurrentUser'}->Id() ) {
236 # Create users on-the-fly
238 warn "can't load RT user for $user; auto-creating\n"
241 my $UserObj = RT::User->new( RT::CurrentUser->new('RT_System') );
243 my ( $val, $msg ) = $UserObj->Create(
244 %{ ref($RT::AutoCreate) ? $RT::AutoCreate : {} },
251 # now get user specific information, to better create our user.
253 = RT::Interface::Web::WebExternalAutoInfo($user);
255 # set the attributes that have been defined.
256 # FIXME: this is a horrible kludge. I'm sure there's something cleaner
257 foreach my $attribute (
259 'Signature', 'EmailAddress',
260 'PagerEmailAddress', 'FreeformContactInfo',
261 'Organization', 'Disabled',
262 'Privileged', 'RealName',
264 'EmailEncoding', 'WebEncoding',
265 'ExternalContactInfoId', 'ContactInfoSystem',
266 'ExternalAuthId', 'Gecos',
267 'HomePhone', 'WorkPhone',
268 'MobilePhone', 'PagerPhone',
269 'Address1', 'Address2',
275 #$m->comp( '/Elements/Callback', %ARGS,
276 # _CallbackName => 'NewUser' );
278 my $method = "Set$attribute";
279 $UserObj->$method( $new_user_info->{$attribute} )
280 if ( defined $new_user_info->{$attribute} );
282 $session->{'CurrentUser'}->Load($user);
286 # we failed to successfully create the user. abort abort abort.
287 delete $session->{'CurrentUser'};
289 die "can't auto-create RT user"; #an error message would be nice :/
290 #$m->abort() unless $RT::WebFallbackToInternalAuth;
291 #$m->comp( '/Elements/Login', %ARGS,
292 # Error => loc( 'Cannot create user: [_1]', $msg ) );
296 unless ( $session->{'CurrentUser'}->Id() ) {
297 delete $session->{'CurrentUser'};
299 die "can't auto-create RT user";
302 #if ($RT::WebExternalOnly) {
303 # $m->comp( '/Elements/Login', %ARGS,
304 # Error => loc('You are not an authorized user') );