backup the schema for tables we don't need the data from. RT#85959
[freeside.git] / FS / FS / access_group.pm
1 package FS::access_group;
2 use base qw( FS::m2m_Common FS::m2name_Common FS::Record );
3
4 use strict;
5 use Carp qw( croak );
6 use FS::Record qw( qsearch qsearchs );
7 use FS::access_right;
8
9 =head1 NAME
10
11 FS::access_group - Object methods for access_group records
12
13 =head1 SYNOPSIS
14
15   use FS::access_group;
16
17   $record = new FS::access_group \%hash;
18   $record = new FS::access_group { 'column' => 'value' };
19
20   $error = $record->insert;
21
22   $error = $new_record->replace($old_record);
23
24   $error = $record->delete;
25
26   $error = $record->check;
27
28 =head1 DESCRIPTION
29
30 An FS::access_group object represents an access group.  FS::access_group inherits from
31 FS::Record.  The following fields are currently supported:
32
33 =over 4
34
35 =item groupnum - primary key
36
37 =item groupname - Access group name
38
39 =back
40
41 =head1 METHODS
42
43 =over 4
44
45 =item new HASHREF
46
47 Creates a new access group.  To add the access group to the database, see L<"insert">.
48
49 Note that this stores the hash reference, not a distinct copy of the hash it
50 points to.  You can ask the object for a copy with the I<hash> method.
51
52 =cut
53
54 # the new method can be inherited from FS::Record, if a table method is defined
55
56 sub table { 'access_group'; }
57
58 =item insert
59
60 Adds this record to the database.  If there is an error, returns the error,
61 otherwise returns false.
62
63 =cut
64
65 # the insert method can be inherited from FS::Record
66
67 =item delete
68
69 Delete this record from the database.
70
71 =cut
72
73 # the delete method can be inherited from FS::Record
74
75 =item replace OLD_RECORD
76
77 Replaces the OLD_RECORD with this one in the database.  If there is an error,
78 returns the error, otherwise returns false.
79
80 =cut
81
82 # the replace method can be inherited from FS::Record
83
84 =item check
85
86 Checks all fields to make sure this is a valid access group.  If there is
87 an error, returns the error, otherwise returns false.  Called by the insert
88 and replace methods.
89
90 =cut
91
92 # the check method should currently be supplied - FS::Record contains some
93 # data checking routines
94
95 sub check {
96   my $self = shift;
97
98   my $error = 
99     $self->ut_numbern('groupnum')
100     || $self->ut_text('groupname')
101   ;
102   return $error if $error;
103
104   $self->SUPER::check;
105 }
106
107 =item access_groupagent
108
109 Returns all associated FS::access_groupagent records.
110
111 =item access_rights
112
113 Returns all associated FS::access_right records.
114
115 =cut
116
117 sub access_rights {
118   my $self = shift;
119   qsearch('access_right', { 'righttype'   => 'FS::access_group',
120                             'rightobjnum' => $self->groupnum 
121                           }
122          );
123 }
124
125 =item access_right RIGHTNAME
126
127 Returns the specified FS::access_right record.  Can be used as a boolean, to
128 test if this group has the given RIGHTNAME.
129
130 =cut
131
132 sub access_right {
133   my( $self, $name ) = @_;
134   qsearchs('access_right', { 'righttype'   => 'FS::access_group',
135                              'rightobjnum' => $self->groupnum,
136                              'rightname'   => $name,
137                            }
138           );
139 }
140
141 =item grant_access_right RIGHTNAME
142
143 Grant the specified specified FS::access_right record to this group.
144 Return the FS::access_right record.
145
146 =cut
147
148 sub grant_access_right {
149   my ( $self, $rightname ) = @_;
150
151   croak "grant_access_right() requires \$rightname"
152     unless $rightname;
153
154   my $access_right = $self->access_right( $rightname );
155   return $access_right if $access_right;
156
157   $access_right = FS::access_right->new({
158     righttype   => 'FS::access_group',
159     rightobjnum => $self->groupnum,
160     rightname   => $rightname,
161   });
162   if ( my $error = $access_right->insert ) {
163     die "grant_access_right() error: $error";
164   }
165
166   $access_right;
167 }
168
169 =item revoke_access_right RIGHTNAME
170
171 Revoke the specified FS::access_right record from this group.
172
173 =cut
174
175 sub revoke_access_right {
176   my ( $self, $rightname ) = @_;
177
178   croak "revoke_access_right() requires \$rightname"
179     unless $rightname;
180
181   my $access_right = $self->access_right( $rightname )
182     or return;
183
184   if ( my $error = $access_right->delete ) {
185     die "revoke_access_right() error: $error";
186   }
187 }
188
189 =back
190
191 =head1 BUGS
192
193 =head1 SEE ALSO
194
195 L<FS::Record>, schema.html from the base documentation.
196
197 =cut
198
199 1;