grant all needed rights to fs_selfservice, #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                     CreateTicket SeeCustomField) );
56   foreach (@rights) {
57     next if $Principal->HasRight( 'Right' => $_, Object => $RT::System );
58     my ($val, $msg) = $Principal->GrantRight(
59       'Right' => $_,
60       'Object' => $RT::System,
61     );
62     die $msg if !$val;
63   }
64
65   # EscalateQueue custom field and friends
66   my $CF = RT::CustomField->new($CurrentUser);
67   $CF->Load('EscalateQueue');
68   if (!defined($CF->Id)) {
69     my ($val, $msg) = $CF->Create(
70       'Name' => 'EscalateQueue',
71       'Type' => 'Select',
72       'MaxValues' => 1,
73       'LookupType' => 'RT::Queue',
74       'Description' => 'Escalate to Queue',
75       'ValuesClass' => 'RT::CustomFieldValues::Queues', #magic!
76     );
77     die $msg if !$val;
78     my $OCF = RT::ObjectCustomField->new($CurrentUser);
79     ($val, $msg) = $OCF->Create(
80       'CustomField' => $CF->Id,
81       'ObjectId' => 0,
82     );
83     die $msg if !$val;
84   }
85
86   # Load from RT data file
87   our (@Groups, @Users, @ACL, @Queues, @ScripActions, @ScripConditions,
88        @Templates, @CustomFields, @Scrips, @Attributes, @Initial, @Final);
89   my $datafile = '%%%RT_PATH%%%/etc/initialdata';
90   eval { require $datafile };
91   if ( $@ ) {
92     warn "Couldn't load RT data from '$datafile': $@\n(skipping)\n";
93     return;
94   }
95
96   # Cache existing ScripCondition, ScripAction, and Template IDs
97   my $search = RT::ScripConditions->new($CurrentUser);
98   $search->UnLimit;
99   my %condition = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
100
101   $search = RT::ScripActions->new($CurrentUser);
102   $search->UnLimit;
103   my %action = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
104
105   $search = RT::Templates->new($CurrentUser);
106   $search->UnLimit;
107   my %template = map { lc($_->Name), $_->Id } @{ $search->ItemsArrayRef };
108
109   # ScripConditions
110   my $ScripCondition = RT::ScripCondition->new($CurrentUser);
111   foreach my $sc (@ScripConditions) {
112     # $sc: Name, Description, ApplicableTransTypes, ExecModule, Argument
113     next if exists( $condition{ lc($sc->{Name}) } );
114     my ($val, $msg) = $ScripCondition->Create( %$sc );
115     die $msg if !$val;
116     $condition{ lc($ScripCondition->Name) } = $ScripCondition->Id;
117   }
118
119   # ScripActions
120   my $ScripAction = RT::ScripAction->new($CurrentUser);
121   foreach my $sa (@ScripActions) {
122     # $sa: Name, Description, ExecModule, Argument
123     next if exists( $action{ lc($sa->{Name}) } );
124     my ($val, $msg) = $ScripAction->Create( %$sa );
125     die $msg if !$val;
126     $action{ lc($ScripAction->Name) } = $ScripAction->Id;
127   }
128
129   # Templates
130   my $Template = RT::Template->new($CurrentUser);
131   foreach my $t (@Templates) {
132     # $t: Queue, Name, Description, Content
133     next if exists( $template{ lc($t->{Name}) } );
134     my ($val, $msg) = $Template->Create( %$t );
135     die $msg if !$val;
136     $template{ lc($Template->Name) } = $Template->Id;
137   }
138
139   # Scrips
140   my $Scrip = RT::Scrip->new($CurrentUser);
141   foreach my $s ( @Scrips ) {
142     my $desc = $s->{'Description'};
143     my ($c, $a, $t) = map lc,
144       @{ $s }{'ScripCondition', 'ScripAction', 'Template'};
145     if ( !$condition{$c} ) {
146       warn "ScripCondition '$c' not found.\n";
147       next;
148     }
149     if ( !$action{$a} ) {
150       warn "ScripAction '$a' not found.\n";
151       next;
152     }
153     if ( !$template{$t} ) {
154       warn "Template '$t' not found.\n";
155       next;
156     }
157     my %param = (
158       ScripCondition => $condition{$c},
159       ScripAction => $action{$a},
160       Template => $template{$t},
161       Queue => 0,
162     );
163     $Scrip->LoadByCols(%param);
164     if (!defined($Scrip->Id)) {
165       my ($val, $msg) = $Scrip->Create(%param, Description => $desc);
166       die $msg if !$val;
167     }
168   } #foreach (@Scrips)
169
170   return;
171 }
172
173 1;