suspend and unsuspend whole customer action, RT#17841
[freeside.git] / FS / FS / access_right.pm
1 package FS::access_right;
2
3 use strict;
4 use vars qw( @ISA );
5 use FS::Record qw( qsearch qsearchs );
6 use FS::upgrade_journal;
7
8 @ISA = qw(FS::Record);
9
10 =head1 NAME
11
12 FS::access_right - Object methods for access_right records
13
14 =head1 SYNOPSIS
15
16   use FS::access_right;
17
18   $record = new FS::access_right \%hash;
19   $record = new FS::access_right { 'column' => 'value' };
20
21   $error = $record->insert;
22
23   $error = $new_record->replace($old_record);
24
25   $error = $record->delete;
26
27   $error = $record->check;
28
29 =head1 DESCRIPTION
30
31 An FS::access_right object represents a granted access right.  FS::access_right
32 inherits from FS::Record.  The following fields are currently supported:
33
34 =over 4
35
36 =item rightnum - primary key
37
38 =item righttype - 
39
40 =item rightobjnum - 
41
42 =item rightname - 
43
44
45 =back
46
47 =head1 METHODS
48
49 =over 4
50
51 =item new HASHREF
52
53 Creates a new right.  To add the right to the database, see L<"insert">.
54
55 Note that this stores the hash reference, not a distinct copy of the hash it
56 points to.  You can ask the object for a copy with the I<hash> method.
57
58 =cut
59
60 # the new method can be inherited from FS::Record, if a table method is defined
61
62 sub table { 'access_right'; }
63
64 =item insert
65
66 Adds this record to the database.  If there is an error, returns the error,
67 otherwise returns false.
68
69 =cut
70
71 # the insert method can be inherited from FS::Record
72
73 =item delete
74
75 Delete this record from the database.
76
77 =cut
78
79 # the delete method can be inherited from FS::Record
80
81 =item replace OLD_RECORD
82
83 Replaces the OLD_RECORD with this one in the database.  If there is an error,
84 returns the error, otherwise returns false.
85
86 =cut
87
88 # the replace method can be inherited from FS::Record
89
90 =item check
91
92 Checks all fields to make sure this is a valid right.  If there is
93 an error, returns the error, otherwise returns false.  Called by the insert
94 and replace methods.
95
96 =cut
97
98 # the check method should currently be supplied - FS::Record contains some
99 # data checking routines
100
101 sub check {
102   my $self = shift;
103
104   my $error = 
105     $self->ut_numbern('rightnum')
106     || $self->ut_text('righttype')
107     || $self->ut_text('rightobjnum')
108     || $self->ut_text('rightname')
109   ;
110   return $error if $error;
111
112   $self->SUPER::check;
113 }
114
115 # _upgrade_data
116 #
117 # Used by FS::Upgrade to migrate to a new database.
118
119 sub _upgrade_data { # class method
120   my ($class, %opts) = @_;
121
122   my @unmigrated = ( qsearch( 'access_right',
123                               { 'righttype'=>'FS::access_group',
124                                 'rightname'=>'Engineering configuration',
125                               }
126                             ), 
127                      qsearch( 'access_right',
128                               { 'righttype'=>'FS::access_group',
129                                 'rightname'=>'Engineering global configuration',
130                               }
131                             )
132                    ); 
133   foreach ( @unmigrated ) {
134     my $rightname = $_->rightname;
135     $rightname =~ s/Engineering/Dialup/;
136     $_->rightname($rightname);
137     my $error = $_->replace;
138     die "Failed to update access right: $error"
139       if $error;
140     my $broadband = new FS::access_right { $_->hash };
141     $rightname =~ s/Dialup/Broadband/;
142     $broadband->rightnum('');
143     $broadband->rightname($rightname);
144     $error = $broadband->insert;
145     die "Failed to insert access right: $error"
146       if $error;
147   }
148
149   my %migrate = (
150     'Post payment'    => [ 'Post check payment', 'Post cash payment' ],
151     'Process payment' => [ 'Process credit card payment', 'Process Echeck payment' ],
152     'Post refund'     => [ 'Post check refund', 'Post cash refund' ],
153     'Refund payment'  => [ 'Refund credit card payment', 'Refund Echeck payment' ],
154   );
155
156   foreach my $oldright (keys %migrate) {
157     my @old = qsearch('access_right', { 'righttype'=>'FS::access_group',
158                                         'rightname'=>$oldright,
159                                       }
160                      );
161
162     foreach my $old ( @old ) {
163
164       foreach my $newright ( @{ $migrate{$oldright} } ) {
165         my %hash = (
166           'righttype'   => 'FS::access_group',
167           'rightobjnum' => $old->rightobjnum,
168           'rightname'   => $newright,
169         );
170         next if qsearchs('access_right', \%hash);
171         my $access_right = new FS::access_right \%hash;
172         my $error = $access_right->insert;
173         die $error if $error;
174       }
175
176       #after the WEST stuff is sorted, etc.
177       #my $error = $old->delete;
178       #die $error if $error;
179
180     }
181
182   }
183
184   my @all_groups = qsearch('access_group', {});
185
186   my %onetime = (
187     'List customers'                      => 'List all customers',
188     'List packages'                       => 'Summarize packages',
189     'Post payment'                        => 'Backdate payment',
190     'Cancel customer package immediately' => 'Un-cancel customer package',
191     'Suspend customer package'            => 'Suspend customer',
192     'Unsuspend customer package'          => 'Unsuspend customer',
193
194     'List services'    => [ 'Services: Accounts',
195                             'Services: Domains',
196                             'Services: Certificates',
197                             'Services: Mail forwards',
198                             'Services: Virtual hosting services',
199                             'Services: Wireless broadband services',
200                             'Services: DSLs',
201                             'Services: Dish services',
202                             'Services: Hardware',
203                             'Services: Phone numbers',
204                             'Services: PBXs',
205                             'Services: Ports',
206                             'Services: Mailing lists',
207                             'Services: External services',
208                           ],
209     'List rating data' => [ 'Usage: RADIUS sessions',
210                             'Usage: Call Detail Records (CDRs)',
211                             'Usage: Unrateable CDRs',
212                           ],
213   );
214
215   foreach my $old_acl ( keys %onetime ) {
216
217     my @new_acl = ref($onetime{$old_acl})
218                     ? @{ $onetime{$old_acl} }
219                     :  ( $onetime{$old_acl} );
220
221     foreach my $new_acl ( @new_acl ) {
222
223       ( my $journal = 'ACL_'.lc($new_acl) ) =~ s/\W/_/g;
224       next if FS::upgrade_journal->is_done($journal);
225
226       # grant $new_acl to all groups who have $old_acl
227       for my $group (@all_groups) {
228         next unless $group->access_right($old_acl);
229         next if     $group->access_right($new_acl);
230         my $access_right = FS::access_right->new( {
231             'righttype'   => 'FS::access_group',
232             'rightobjnum' => $group->groupnum,
233             'rightname'   => $new_acl,
234         } );
235         my $error = $access_right->insert;
236         die $error if $error;
237       }
238     
239       FS::upgrade_journal->set_done($journal);
240
241     }
242
243   }
244
245   ### ACL_download_report_data
246   if ( !FS::upgrade_journal->is_done('ACL_download_report_data') ) {
247
248     # grant to everyone
249     for my $group (@all_groups) {
250       my $access_right = FS::access_right->new( {
251           'righttype'   => 'FS::access_group',
252           'rightobjnum' => $group->groupnum,
253           'rightname'   => 'Download report data',
254       } );
255       my $error = $access_right->insert;
256       die $error if $error;
257     }
258
259     FS::upgrade_journal->set_done('ACL_download_report_data');
260   }
261
262   '';
263
264 }
265
266 =back
267
268 =head1 BUGS
269
270 =head1 SEE ALSO
271
272 L<FS::Record>, schema.html from the base documentation.
273
274 =cut
275
276 1;
277