1 package FS::radius_attr;
2 use base qw( FS::Record );
5 use vars qw( $noexport_hack );
6 use FS::Record qw( qsearch dbh );
12 FS::radius_attr - Object methods for radius_attr records
18 $record = new FS::radius_attr \%hash;
19 $record = new FS::radius_attr { 'column' => 'value' };
21 $error = $record->insert;
23 $error = $new_record->replace($old_record);
25 $error = $record->delete;
27 $error = $record->check;
31 An FS::radius_attr object represents a RADIUS group attribute.
32 FS::radius_attr inherits from FS::Record. The following fields are
37 =item attrnum - primary key
39 =item groupnum - L<FS::radius_group> to assign this attribute
41 =item attrname - Attribute name, as defined in the RADIUS server's dictionary
43 =item value - Attribute value
45 =item attrtype - 'C' (check) or 'R' (reply)
47 =item op - Operator (see L<http://wiki.freeradius.org/Operators>)
57 Creates a new record. To add the record to the database, see L<"insert">.
59 Note that this stores the hash reference, not a distinct copy of the hash it
60 points to. You can ask the object for a copy with the I<hash> method.
64 # the new method can be inherited from FS::Record, if a table method is defined
66 sub table { 'radius_attr'; }
70 Adds this record to the database. If there is an error, returns the error,
71 otherwise returns false. If any sqlradius-type exports exist and have the
72 C<export_attrs> option enabled, the new attribute will be exported to them.
78 my $error = $self->SUPER::insert;
79 return $error if $error;
80 return if $noexport_hack;
82 foreach ( qsearch('part_export', {}) ) {
83 next if !$_->option('export_attrs',1);
84 $error = $_->export_attr_insert($self);
85 return $error if $error;
94 Delete this record from the database. Like C<insert>, this will delete
95 the attribute from any attached RADIUS databases.
102 if ( !$noexport_hack ) {
103 foreach ( qsearch('part_export', {}) ) {
104 next if !$_->option('export_attrs',1);
105 $error = $_->export_attr_delete($self);
106 return $error if $error;
110 $self->SUPER::delete;
113 =item replace OLD_RECORD
115 Replaces the OLD_RECORD with this one in the database. If there is an error,
116 returns the error, otherwise returns false.
121 my ($self, $old) = @_;
122 $old ||= $self->replace_old;
123 return 'can\'t change radius_attr.groupnum'
124 if $self->groupnum != $old->groupnum;
126 unless grep { $self->$_ ne $old->$_ } qw(attrname value op attrtype);
128 # don't attempt export on an invalid record
129 my $error = $self->check;
130 return $error if $error;
133 $old->set('groupname', $old->radius_group->groupname);
134 if ( !$noexport_hack ) {
135 foreach ( qsearch('part_export', {}) ) {
136 next if !$_->option('export_attrs',1);
137 $error = $_->export_attr_replace($self, $old);
138 return $error if $error;
142 $self->SUPER::replace($old);
148 Checks all fields to make sure this is a valid record. If there is
149 an error, returns the error, otherwise returns false. Called by the insert
158 $self->ut_numbern('attrnum')
159 || $self->ut_foreign_key('groupnum', 'radius_group', 'groupnum')
160 || $self->ut_text('attrname')
161 || $self->ut_text('value')
162 || $self->ut_enum('attrtype', [ 'C', 'R' ])
164 return $error if $error;
166 my @ops = $self->ops($self->get('attrtype'));
167 $self->set('op' => $ops[0]) if !$self->get('op');
168 $error ||= $self->ut_enum('op', \@ops);
170 return $error if $error;
177 Returns the L<FS::radius_group> object to which this attribute applies.
187 Returns a list of all legal values of the "op" field. ATTRTYPE must be C for
188 check or R for reply.
193 C => [ '=', '==', ':=', '+=', '!=', '>', '>=', '<', '<=', '=~', '!~', '=*', '!*' ],
194 R => [ '=', ':=', '+=' ],
199 my $attrtype = shift;
200 return @{ $ops{$attrtype} };
207 sub _upgrade_schema {
208 my ($class, %opts) = @_;
211 DELETE FROM radius_attr WHERE NOT EXISTS
212 ( SELECT 1 FROM radius_group WHERE radius_group.groupnum = radius_attr.groupnum )
215 my $sth = dbh->prepare($sql) or die dbh->errstr;
216 $sth->execute or die $sth->errstr;
224 L<FS::Record>, schema.html from the base documentation.