auto-create selfservice user in RT, #13199
[freeside.git] / FS / FS / TicketSystem.pm
1 package FS::TicketSystem;
2
3 use strict;
4 use vars qw( $conf $system $AUTOLOAD );
5 use FS::Conf;
6 use FS::UID qw( dbh driver_name );
7
8 FS::UID->install_callback( sub { 
9   $conf = new FS::Conf;
10   $system = $conf->config('ticket_system');
11 } );
12
13 sub AUTOLOAD {
14   my $self = shift;
15
16   my($sub)=$AUTOLOAD;
17   $sub =~ s/.*://;
18
19   my $conf = new FS::Conf;
20   die "FS::TicketSystem::$AUTOLOAD called, but no ticket system configured\n"
21     unless $system;
22
23   eval "use FS::TicketSystem::$system;";
24   die $@ if $@;
25
26   $self .= "::$system";
27   $self->$sub(@_);
28 }
29
30 sub _upgrade_data {
31   return if !defined($system) || $system ne 'RT_Internal';
32   my ($class, %opts) = @_;
33
34   # go ahead and use the RT API for this
35   
36   FS::TicketSystem->init;
37   my $session = FS::TicketSystem->session();
38   # bypass RT ACLs--we're going to do lots of things
39   my $CurrentUser = $RT::SystemUser;
40
41   # selfservice user
42   my $User = RT::User->new($CurrentUser);
43   $User->Load('%%%SELFSERVICE_USER%%%');
44   if (!defined($User->Id)) {
45     my ($val, $msg) = $User->Create(
46       'Name' => '%%%SELFSERVICE_USER%%%',
47       'Gecos' => '%%%SELFSERVICE_USER%%%',
48       'Privileged' => 1,
49       # any other fields needed?
50     );
51     die $msg if !$val;
52   }
53   my $Principal = $User->PrincipalObj; # can this ever fail?
54   my @rights = ( qw(ShowTicket SeeQueue ModifyTicket ReplyToTicket) );
55   foreach (@rights) {
56     next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
57     my ($val, $msg) = $Principal->GrantRight(
58       'Right' => $_,
59       'Object' => $RT::System,
60     );
61     die $msg if !$val;
62   }
63
64   # EscalateQueue custom field and friends
65   my $CF = RT::CustomField->new($CurrentUser);
66   $CF->Load('EscalateQueue');
67   if (!defined($CF->Id)) {
68     my ($val, $msg) = $CF->Create(
69       'Name' => 'EscalateQueue',
70       'Type' => 'Select',
71       'MaxValues' => 1,
72       'LookupType' => 'RT::Queue',
73       'Description' => 'Escalate to Queue',
74       'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
75     );
76     die $msg if !$val;
77     my $OCF = RT::ObjectCustomField->new($CurrentUser);
78     ($val, $msg) = $OCF->Create(
79       'CustomField' => $CF->Id,
80       'ObjectId' => 0,
81     );
82     die $msg if !$val;
83   }
84
85   # Load from RT data file
86   our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
87        @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
88   my $datafile = '%%%RT_PATH%%%/etc/initialdata';
89   eval { require $datafile };
90   if ( $@ ) {
91     warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
92     return;
93   }
94
95   # Cache existing ScripCondition, ScripAction, and Template IDs
96   my $search = RT::ScripConditions->new($CurrentUser);
97   $search->UnLimit;
98   my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
99
100   $search = RT::ScripActions->new($CurrentUser);
101   $search->UnLimit;
102   my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
103
104   $search = RT::Templates->new($CurrentUser);
105   $search->UnLimit;
106   my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
107
108   # ScripConditions
109   my $ScripCondition = RT::ScripCondition->new($CurrentUser);
110   foreach my $sc (@ScripConditions) {
111     # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
112     next if exists( $condition{ lc($sc->{Name}) } );
113     my ($val, $msg) = $ScripCondition->Create( %$sc );
114     die $msg if !$val;
115     $condition{ lc($ScripCondition->Name) } = $ScripCondition->Id;
116   }
117
118   # ScripActions
119   my $ScripAction = RT::ScripAction->new($CurrentUser);
120   foreach my $sa (@ScripActions) {
121     # $sa: Name, Description, ExecModule, Argument
122     next if exists( $action{ lc($sa->{Name}) } );
123     my ($val, $msg) = $ScripAction->Create( %$sa );
124     die $msg if !$val;
125     $action{ lc($ScripAction->Name) } = $ScripAction->Id;
126   }
127
128   # Templates
129   my $Template = RT::Template->new($CurrentUser);
130   foreach my $t (@Templates) {
131     # $t: Queue, Name, Description, Content
132     next if exists( $template{ lc($t->{Name}) } );
133     my ($val, $msg) = $Template->Create( %$t );
134     die $msg if !$val;
135     $template{ lc($Template->Name) } = $Template->Id;
136   }
137
138   # Scrips
139   my $Scrip = RT::Scrip->new($CurrentUser);
140   foreach my $s ( @Scrips ) {
141     my $desc = $s->{'Description'};
142     my ($c, $a, $t) = map lc,
143       @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
144     if ( !$condition{$c} ) {
145       warn "ScripCondition '$c' not found.\n";
146       next;
147     }
148     if ( !$action{$a} ) {
149       warn "ScripAction '$a' not found.\n";
150       next;
151     }
152     if ( !$template{$t} ) {
153       warn "Template '$t' not found.\n";
154       next;
155     }
156     my %param = (
157       ScripCondition => $condition{$c},
158       ScripAction => $action{$a},
159       Template => $template{$t},
160       Queue => 0,
161     );
162     $Scrip->LoadByCols(%param);
163     if (!defined($Scrip->Id)) {
164       my ($val, $msg) = $Scrip->Create(%param, Description => $desc);
165       die $msg if !$val;
166     }
167   } #foreach (@Scrips)
168
169   return;
170 }
171
172 1;