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