delete RADIUS groups, #16491
[freeside.git] / FS / FS / radius_group.pm
1 package FS::radius_group;
2
3 use strict;
4 use base qw( FS::o2m_Common FS::Record );
5 use FS::Record qw( qsearch qsearchs dbh );
6 use FS::radius_attr;
7
8 =head1 NAME
9
10 FS::radius_group - Object methods for radius_group records
11
12 =head1 SYNOPSIS
13
14   use FS::radius_group;
15
16   $record = new FS::radius_group \%hash;
17   $record = new FS::radius_group { 'column' => 'value' };
18
19   $error = $record->insert;
20
21   $error = $new_record->replace($old_record);
22
23   $error = $record->delete;
24
25   $error = $record->check;
26
27 =head1 DESCRIPTION
28
29 An FS::radius_group object represents a RADIUS group.  FS::radius_group inherits from
30 FS::Record.  The following fields are currently supported:
31
32 =over 4
33
34 =item groupnum
35
36 primary key
37
38 =item groupname
39
40 groupname
41
42 =item description
43
44 description
45
46 =item priority
47
48 priority - for export
49
50
51 =back
52
53 =head1 METHODS
54
55 =over 4
56
57 =item new HASHREF
58
59 Creates a new RADIUS group.  To add the RADIUS group to the database, see L<"insert">.
60
61 Note that this stores the hash reference, not a distinct copy of the hash it
62 points to.  You can ask the object for a copy with the I<hash> method.
63
64 =cut
65
66 # the new method can be inherited from FS::Record, if a table method is defined
67
68 sub table { 'radius_group'; }
69
70 =item insert
71
72 Adds this record to the database.  If there is an error, returns the error,
73 otherwise returns false.
74
75 =cut
76
77 # the insert method can be inherited from FS::Record
78
79 =item delete
80
81 Delete this record from the database.
82
83 =cut
84
85 sub delete {
86   # okay, I guess we support it now
87   my $self = shift;
88   local $SIG{HUP} = 'IGNORE';
89   local $SIG{INT} = 'IGNORE';
90   local $SIG{QUIT} = 'IGNORE';
91   local $SIG{TERM} = 'IGNORE';
92   local $SIG{TSTP} = 'IGNORE';
93   local $SIG{PIPE} = 'IGNORE';
94
95   my $oldAutoCommit = $FS::UID::AutoCommit;
96   local $FS::UID::AutoCommit = 0;
97   my $dbh = dbh;
98
99   my $groupnum = $self->groupnum;
100   my $error = $self->process_o2m(
101                 'table' => 'radius_usergroup',
102                 'num_col' => 'groupnum',
103                 'fields' => ['groupnum'], # just delete them
104                 'params' => {},
105               ) || $self->SUPER::delete(@_);
106
107   if ( $error ) {
108     $dbh->rollback if $oldAutoCommit;
109     return $error;
110   }
111
112   foreach my $part_svc_column (
113     qsearch('part_svc_column', { columnname => 'usergroup' }) 
114   ) {
115     my $new_values = join(',', 
116       grep { $_ != $groupnum } split(',', $part_svc_column->columnvalue)
117     );
118     next if $new_values eq $part_svc_column->columnvalue;
119     $part_svc_column->set(columnvalue => $new_values);
120     $error = $part_svc_column->replace;
121     if ( $error ) {
122       $dbh->rollback if $oldAutoCommit;
123       return $error;
124     }
125   }
126   dbh->commit;
127   '';
128 }
129
130 =item replace OLD_RECORD
131
132 Replaces the OLD_RECORD with this one in the database.  If there is an error,
133 returns the error, otherwise returns false.
134
135 =cut
136
137 # To keep these things from proliferating, we will follow the same 
138 # export/noexport switches that radius_attr uses.  If you _don't_ use
139 # Freeside to maintain your RADIUS group attributes, then it probably 
140 # shouldn't try to rename groups either.
141
142 sub replace {
143   my ($self, $old) = @_;
144   $old ||= $self->replace_old;
145
146   my $error = $self->check;
147   return $error if $error;
148
149   if ( !$FS::radius_attr::noexport_hack ) {
150     foreach ( qsearch('part_export', {}) ) {
151       next if !$_->option('export_attrs',1);
152       $error = $_->export_group_replace($self, $old);
153       return $error if $error;
154     }
155   }
156
157   $self->SUPER::replace($old);
158 }
159
160 =item check
161
162 Checks all fields to make sure this is a valid RADIUS group.  If there is
163 an error, returns the error, otherwise returns false.  Called by the insert
164 and replace methods.
165
166 =cut
167
168 # the check method should currently be supplied - FS::Record contains some
169 # data checking routines
170
171 sub check {
172   my $self = shift;
173
174   my $error = 
175     $self->ut_numbern('groupnum')
176     || $self->ut_text('groupname')
177     || $self->ut_textn('description')
178     || $self->ut_numbern('priority')
179   ;
180   return $error if $error;
181
182   $self->SUPER::check;
183 }
184
185 =item long_description
186
187 Returns a description for this group consisting of its description field, 
188 if any, and the RADIUS group name.
189
190 =cut
191
192 sub long_description {
193     my $self = shift;
194     $self->description ? $self->description . " (". $self->groupname . ")"
195                        : $self->groupname;
196 }
197
198 =item radius_attr
199
200 Returns all L<FS::radius_attr> objects (check and reply attributes) for 
201 this group.
202
203 =cut
204
205 sub radius_attr {
206   my $self = shift;
207   qsearch({
208       table   => 'radius_attr', 
209       hashref => {'groupnum' => $self->groupnum },
210       order_by  => 'ORDER BY attrtype, attrname',
211   })
212 }
213
214 =back
215
216 =head1 BUGS
217
218 This isn't export-specific (i.e. groups are globally unique, as opposed to being
219 unique per-export).
220
221 =head1 SEE ALSO
222
223 L<FS::radius_usergroup>, L<FS::Record>, schema.html from the base documentation.
224
225 =cut
226
227 1;
228