1 package FS::part_export::aradial;
3 use base qw( FS::part_export );
9 use Date::Format 'time2str';
11 use vars qw( %options %info $me $DEBUG );
14 $me = '[FS::part_export::aradial]';
17 tie %options, 'Tie::IxHash',
18 'port' => { label => 'HTTP port', default => 8000 },
19 'login' => { label => 'Admin username' },
20 'pass' => { label => 'Admin password' },
21 'realm' => { label => 'Admin authentication realm' },
22 'group' => { label => 'Group name' },
27 'desc' => 'Export accounts to Aradial RADIUS HTTP interface',
28 'options' => \%options,
31 <p>This export maintains user accounts on an Aradial Technologies access
32 control server, via the HTTP interface. The export hostname and the
33 <i>HTTP port</i> option determine the location of the server.</p>
34 <p><i>Admin username, password, authentication realm</i> are the settings
35 for the HTTP interface, set in the "Admin Web Interface Security" options
36 for your Aradial server.</p>
37 <p><i>Group name</i> is the user group to assign to new users, and must
38 already exist on the Aradial server. Currently this export will assign
39 all users to a single group; if you want multiple groups for different
40 service types, create another export instance.</p>
45 my ($self, $svc) = @_;
46 my $result = $self->request_user_edit(
48 $self->svc_acct_params($svc),
50 if ($svc->cust_svc->cust_pkg->susp > 0 ) {
51 $result ||= $self->export_suspend($svc);
57 my ($self, $new, $old) = @_;
58 if ($new->username ne $old->username) {
59 return $old->export_delete || $new->export_insert;
62 $UserLockout = 1 if $new->cust_svc->cust_pkg->susp > 0;
63 $self->request_user_edit(
66 $self->svc_acct_params($new),
67 UserLockout => $UserLockout,
72 my ($self, $svc) = @_;
73 $self->request_user_edit(
75 'UserID' => $svc->username,
80 sub export_unsuspend {
81 my ($self, $svc) = @_;
82 $self->request_user_edit(
84 'UserID' => $svc->username,
90 my ($self, $svc) = @_;
91 $self->request_user_edit(
93 ('$Delete$' . $svc->username) => 1,
97 # Send a request to the 'UserEdit' interface, and process the response into
98 # an error string (empty on success, per Freeside convention).
100 sub request_user_edit {
101 my ($self, @params) = @_;
102 my $result = eval { $self->request( Page => 'UserEdit', @params ) };
103 return $result unless ref($result);
104 my $status = $result->findvalue('Result/Status/@value'); # XPath
105 if ($status eq 'Success') {
108 my $error = $result->findvalue('Result/Reason/@value')
110 return "updating Aradial user database: $error";
114 # Send a request to any interface, parse the response (from XML), and
115 # return it (as an XML::LibXML::Document). Returns a string if there's an
121 my $path = '/Admin'; # I think this is always right
122 my $url = URI->new('http://' . $self->host . $path);
123 warn "$me request: \n".Dumper(\@params)."\n\n" if $DEBUG >= 2;
124 my $response = $self->ua->post($url, \@params);
125 if ( $response->is_success ) {
126 my $content = $response->decoded_content;
127 warn "$me response: \n$content\n\n" if $DEBUG >= 2;
128 return $self->parser->parse_string($content);
129 # the formats of these are _variable_.
130 # Some of them have a <Result><Status value="Success"><Entity ... >
131 # kind of structure, but not all. They do all seem to be XML, though.
133 return "API request error: ".$response->status_line;
137 sub svc_acct_params {
140 my $pkg = $svc->cust_svc->cust_pkg;
141 my $cust = $pkg->cust_main;
142 my $location = $pkg->cust_location;
143 # should we use the package contact's name/phone here?
145 my $setup_date = time2str('D%Y-%m-%d',
146 ($pkg->setup || $pkg->start_date || time)
148 my $expire_date = $pkg->expire ? time2str('D%Y-%m-%d', $pkg->expire) : '';
151 'db_Users.UserID' => $svc->username,
152 $self->password_params($svc),
153 'db_$N$Users.Status' => 0, # we suspend using UserLockout
154 'db_$D$Users.StartDate' => $setup_date,
155 'db_$D$Users.UserExpiryDate' => $expire_date,
156 'db_$RS$Users.GroupName' => $self->option('group'),
157 'db_$I$Users.UserIP' => $svc->slipip,
158 'db_UserDetails.FirstName' => $cust->first,
159 'db_UserDetails.LastName' => $cust->last,
160 'db_UserDetails.Company' => $cust->company,
161 'db_UserDetails.Email' => $cust->invoicing_list_emailonly_scalar,
162 'db_UserDetails.Address1' => $location->address1,
163 'db_UserDetails.Address2' => $location->address2,
164 'db_UserDetails.City' => $location->city,
165 'db_%GS%UserDetails.State' => $location->state,
166 'db_%GS%UserDetails.Country' => $location->country,
167 'db_UserDetails.Zip' => $location->zip,
168 'db_UserDetails.PhoneHome' => ($cust->daytime || $cust->night || $cust->mobile),
169 'db_UserDetails.PhoneFax' => $cust->fax,
173 sub password_params {
174 my ($self, $svc) = @_;
176 my $password_encryption = 0;
177 my $password = $svc->_password;
178 if ($svc->_password_encoding eq 'crypt') {
179 if ($svc->_password_encryption eq 'des') {
180 $password_encryption = 2;
181 } elsif ( $svc->_password_encryption eq 'md5') {
182 $password_encryption = 5;
184 } elsif ( $svc->_password_encoding eq 'ldap' ) {
185 $svc->_password =~ /^\{([\w-]+)\}(.*)$/;
188 $password_encryption = 7;
189 } elsif ($1 eq 'SHA' or $1 eq 'SHA-1') {
190 $password_encryption = 1;
193 ( Password => $password,
194 PasswordEncryptionType => $password_encryption
198 # return the XML parser
201 $self->{_parser} ||= XML::LibXML->new;
204 # return hostname:port
207 $self->machine . ':' . $self->option('port');
210 # return the LWP::UserAgent object
213 $self->{_ua} ||= do {
214 my $ua = LWP::UserAgent->new;
217 $self->option('realm'),
218 $self->option('login'),
219 $self->option('pass')