]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/support-scripts/oils_header.pl
LP2042879 Shelving Location Groups Admin accessibility
[Evergreen.git] / Open-ILS / src / support-scripts / oils_header.pl
1 #!/usr/bin/perl
2
3 #----------------------------------------------------------------
4 # Generic header for tesing OpenSRF methods
5 #----------------------------------------------------------------
6
7 use strict;
8 use warnings;
9 use OpenSRF::Utils::JSON;
10 use Data::Dumper;
11 use OpenSRF::System;
12 use OpenSRF::AppSession;
13 use OpenSRF::EX qw(:try);
14 use Time::HiRes qw/time/;
15 use Digest::MD5 qw(md5_hex);
16 use OpenILS::Utils::Fieldmapper;
17 use OpenILS::Application::AppUtils;
18 use OpenSRF::Utils::SettingsClient;
19 use OpenSRF::Utils::Logger qw/:logger/;
20 use UNIVERSAL::require;
21
22
23 # Some useful objects
24 our $cache    = "OpenSRF::Utils::Cache";
25 our $apputils = "OpenILS::Application::AppUtils";
26 our $memcache;
27 our $user;
28 our $authtoken;
29 our $authtime;
30
31 # Some constants for our services
32 our $AUTH     = 'open-ils.auth';
33 our $STORAGE  = 'open-ils.storage';
34 our $SEARCH   = 'open-ils.search';
35 our $CIRC     = 'open-ils.circ';
36 our $CAT      = 'open-ils.cat';
37 our $MATH     = 'opensrf.math';
38 our $SETTINGS = 'opensrf.settings';
39 our $ACTOR    = 'open-ils.actor';
40
41 sub AUTH     { return $AUTH;     }
42 sub STORAGE  { return $STORAGE;  }
43 sub SEARCH   { return $SEARCH;   }
44 sub CIRC     { return $CIRC;     }
45 sub CAT      { return $CAT;      }
46 sub MATH     { return $MATH;     }
47 sub SETTINGS { return $SETTINGS; }
48 sub ACTOR    { return $ACTOR;    }
49
50
51 #----------------------------------------------------------------
52 # Exit a script
53 #----------------------------------------------------------------
54 sub err { 
55         my ($pkg, $file, $line, $sub)  = _caller(); 
56         no warnings;
57         die "Script halted with error ".
58                 "($pkg : $file : $line : $sub):\n" . shift() . "\n"; 
59 }
60
61 #----------------------------------------------------------------
62 # Print with newline
63 #----------------------------------------------------------------
64 sub printl { print "@_\n"; }
65
66 #----------------------------------------------------------------
67 # Print with Data::Dumper
68 #----------------------------------------------------------------
69 sub debug { printl(Dumper(@_)); }
70
71
72 #----------------------------------------------------------------
73 # This is not the function you're looking for
74 #----------------------------------------------------------------
75 sub _caller {
76         my ($pkg, $file, $line, $sub)  = caller(2);
77         if(!$line) {
78                 ($pkg, $file, $line)  = caller(1);
79                 $sub = "";
80         }
81         return ($pkg, $file, $line, $sub);
82 }
83
84
85 #----------------------------------------------------------------
86 # Connect to the servers
87 #----------------------------------------------------------------
88 sub osrf_connect {
89         my $config = shift;
90         err("Bootstrap config required") unless $config;
91         OpenSRF::System->bootstrap_client( config_file => $config );
92         Fieldmapper->import(IDL =>
93                 OpenSRF::Utils::SettingsClient->new->config_value("IDL"));
94         reset_cstore();
95 }
96
97 sub reset_cstore {
98         my ($key) = grep { $_ =~ /OpenILS.*CStoreEditor/o } keys %INC;
99         return unless $key;
100         delete $INC{$key};
101         my $h = $SIG{__WARN__};
102         $SIG{__WARN__} = sub {};
103         require OpenILS::Utils::CStoreEditor;
104         $SIG{__WARN__} = $h;
105 }
106
107
108 #----------------------------------------------------------------
109 # Get a handle for the memcache object
110 #----------------------------------------------------------------
111 sub osrf_cache { 
112         $cache->use;
113         $memcache = $cache->new('global') unless $memcache;
114         return $memcache;
115 }
116
117 #----------------------------------------------------------------
118 # Is the given object an OILS event?
119 #----------------------------------------------------------------
120 sub oils_is_event {
121         my $e = shift;
122         if( $e and ref($e) eq 'HASH' ) {
123                 return 1 if defined($e->{ilsevent});
124         }
125         return 0;       
126 }
127
128 sub oils_event_equals {
129         my( $e, $name ) = @_;
130         return 1 if (oils_is_event($e) and ($e->{textcode} eq $name));
131         return 0;
132 }
133
134 #----------------------------------------------------------------
135 # If the given object is an event, this prints the event info 
136 # and exits the script
137 #----------------------------------------------------------------
138 sub oils_event_die {
139         my $evt = shift;
140         my ($pkg, $file, $line, $sub)  = _caller();
141         if(oils_is_event($evt)) {
142                 if($evt->{ilsevent}) {
143                         printl("\nReceived Event($pkg : $file : $line : $sub): \n" . Dumper($evt));
144                         exit 1;
145                 }
146         }
147 }
148
149
150 #----------------------------------------------------------------
151 # Login to the auth server and set the global $authtoken var
152 #----------------------------------------------------------------
153 sub oils_login {
154         my( $username, $password, $type, $workstation ) = @_;
155
156         $type |= "staff";
157
158         my $nonce = rand($$);
159         my $seed = $apputils->simplereq( $AUTH, 
160                 'open-ils.auth.authenticate.init', $username, $nonce);
161         err("No auth seed") unless $seed;
162
163     my $opts = {
164         username => $username,
165         password => md5_hex($seed . md5_hex($password)),
166         type => $type,
167         nonce => $nonce
168     };
169
170         if(defined($workstation)) {
171                 $opts->{workstation} = $workstation;
172         }
173
174         my $response = $apputils->simplereq( $AUTH, 
175                 'open-ils.auth.authenticate.complete', $opts);
176
177         err("No auth response returned on login") unless $response;
178
179         oils_event_die($response);
180
181         $authtime  = $response->{payload}->{authtime};
182         $authtoken = $response->{payload}->{authtoken};
183         return $authtoken;
184 }
185
186
187 #----------------------------------------------------------------
188 # Destroys the login session on the server
189 #----------------------------------------------------------------
190 sub oils_logout {
191         $apputils->simplereq(
192                 'open-ils.auth',
193                 'open-ils.auth.session.delete', (@_ ? shift : $authtoken) );
194 }
195
196 #----------------------------------------------------------------
197 # Fetches the user object and sets the global $user var
198 #----------------------------------------------------------------
199 sub oils_fetch_session {
200         my $ses = shift;
201         my $resp = $apputils->simplereq( $AUTH, 
202                 'open-ils.auth.session.retrieve', $ses, 'staff' );
203         oils_event_die($resp);
204         return $user = $resp;
205 }
206
207 #----------------------------------------------------------------
208 # var $response = simplereq( $service, $method, @params );
209 #----------------------------------------------------------------
210 sub simplereq    { return $apputils->simplereq(@_); }
211 sub osrf_request { return $apputils->simplereq(@_); }
212
213 1;