1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
4 use OpenSRF::Utils::Cache;
5 use OpenSRF::EX qw(:try);
7 use OpenSRF::Utils::Logger;
8 my $logger = "OpenSRF::Utils::Logger";
11 my $cache_client = "OpenSRF::Utils::Cache";
13 # ---------------------------------------------------------------------------
14 # Pile of utilty methods used accross applications.
15 # ---------------------------------------------------------------------------
18 # ---------------------------------------------------------------------------
19 # on sucess, returns the created session, on failure throws ERROR exception
20 # ---------------------------------------------------------------------------
21 sub start_db_session {
24 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
25 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
27 my $trans_resp = $trans_req->recv();
28 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
29 if( ! $trans_resp->content() ) {
31 ("Unable to Begin Transaction with database" );
38 # returns undef if user has all of the perms provided
39 # returns the first failed perm on failure
40 sub check_user_perms {
41 my($self, $user_id, $org_id, @perm_types ) = @_;
43 $logger->debug("Checking perms with user : $user_id , org: $org_id, @perm_types");
45 throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
46 unless( defined($user_id) and defined($org_id) and @perm_types);
48 my $session = OpenSRF::AppSession->create("open-ils.storage");
49 for my $type (@perm_types) {
50 my $req = $session->request(
51 "open-ils.storage.permission.user_has_perm",
52 $user_id, $type, $org_id );
53 my $resp = $req->gather(1);
55 $session->disconnect();
60 $session->disconnect();
64 # checks the list of user perms. The first one that fails returns a new
65 # OpenILS::Perm object of that type. Returns undef if all perms are allowed
67 my( $self, $user_id, $org_id, @perm_types ) = @_;
68 my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
69 return OpenILS::Event->new('PERM_FAILURE', ilsperm => $t, ilspermloc => $org_id ) if $t;
75 # ---------------------------------------------------------------------------
76 # commits and destroys the session
77 # ---------------------------------------------------------------------------
78 sub commit_db_session {
79 my( $self, $session ) = @_;
81 my $req = $session->request( "open-ils.storage.transaction.commit" );
82 my $resp = $req->recv();
85 throw OpenSRF::EX::ERROR ("Unable to commit db session");
88 if(UNIVERSAL::isa($resp,"Error")) {
89 throw $resp ($resp->stringify);
93 throw OpenSRF::EX::ERROR ("Unable to commit db session");
97 $session->disconnect();
101 sub rollback_db_session {
102 my( $self, $session ) = @_;
104 my $req = $session->request("open-ils.storage.transaction.rollback");
105 my $resp = $req->recv();
106 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
109 $session->disconnect();
113 # ---------------------------------------------------------------------------
114 # Checks to see if a user is logged in. Returns the user record on success,
115 # throws an exception on error.
116 # ---------------------------------------------------------------------------
119 sub check_user_session {
121 my( $self, $user_session ) = @_;
123 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
124 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
125 my $response = $request->recv();
128 throw OpenSRF::EX::User
129 ("Error communication with storage server");
132 if(ref($response) and $response->isa("OpenSRF::EX")) {
133 throw $response ($response->stringify);
137 my $content = $response->content;
138 if( ref($content) eq 'HASH' ) {
139 if(defined($content->{ilsevent}) and $content->{ilsevent} ne '0' ) {
140 throw OpenSRF::EX::ERROR
141 ("Session [$user_session] cannot be authenticated" );
147 throw OpenSRF::EX::ERROR
148 ("Session [$user_session] cannot be authenticated" );
151 $session->disconnect();
157 # generic simple request returning a scalar value
159 my($self, $service, $method, @params) = @_;
160 return $self->simple_scalar_request($service, $method, @params);
164 sub simple_scalar_request {
165 my($self, $service, $method, @params) = @_;
167 my $session = OpenSRF::AppSession->create( $service );
168 my $request = $session->request( $method, @params );
169 my $response = $request->recv(30);
171 $request->wait_complete;
173 if(!$request->complete) {
174 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" .
175 "\n did not complete successfully");
179 warn "No response from $service for method $method with params @params";
182 if(UNIVERSAL::isa($response,"Error")) {
183 throw $response ("Call to $service for method $method with params @params" .
184 "\n failed with exception: " . $response->stringify );
190 $session->disconnect();
194 if($response) { $value = $response->content; }
195 else { $value = undef; }
206 my $org_typelist = undef;
207 my $org_typelist_hash = {};
212 if($tree) { return $tree; }
214 # see if it's in the cache
215 $tree = $cache_client->new()->get_cache('_orgtree');
216 if($tree) { return $tree; }
219 warn "Retrieving Org Tree\n";
220 $orglist = $self->simple_scalar_request(
222 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
225 if( ! $org_typelist ) {
226 warn "Retrieving org types\n";
227 $org_typelist = $self->simple_scalar_request(
229 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
230 $self->build_org_type($org_typelist);
233 $tree = $self->build_org_tree($orglist,1);
234 $cache_client->new()->put_cache('_orgtree', $tree);
239 my $slimtree = undef;
240 sub get_slim_org_tree {
243 if($slimtree) { return $slimtree; }
245 # see if it's in the cache
246 $slimtree = $cache_client->new()->get_cache('slimorgtree');
247 if($slimtree) { return $slimtree; }
250 warn "Retrieving Org Tree\n";
251 $orglist = $self->simple_scalar_request(
253 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
256 $slimtree = $self->build_org_tree($orglist);
257 $cache_client->new->put_cache('slimorgtree', $slimtree);
264 my($self, $org_typelist) = @_;
265 for my $type (@$org_typelist) {
266 $org_typelist_hash->{$type->id()} = $type;
274 my( $self, $orglist, $add_types ) = @_;
276 return $orglist unless (
277 ref($orglist) and @$orglist > 1 );
280 $a->ou_type <=> $b->ou_type ||
281 $a->name cmp $b->name } @$orglist;
283 for my $org (@list) {
287 if(!ref($org->ou_type()) and $add_types) {
288 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
291 next unless (defined($org->parent_ou));
293 my ($parent) = grep { $_->id == $org->parent_ou } @list;
295 $parent->children([]) unless defined($parent->children);
296 push( @{$parent->children}, $org );
306 return $self->simple_scalar_request(
308 'open-ils.storage.direct.actor.user.retrieve', $id );
312 my( $self, $session ) = @_;
313 my $user; my $evt; my $e;
316 $user = $self->check_user_session($session);
317 } catch Error with { $e = 1; };
319 if( $e or !$user ) { $evt = OpenILS::Event->new('NO_SESSION'); }
320 return ( $user, $evt );
325 my( $self, $staffobj, $userid, @perms ) = @_;
328 $logger->debug("checkrequestor(): staff => " . $staffobj->id . ", user => $userid");
330 if( $userid ne $staffobj->id ) {
331 if( ! ($user = $self->fetch_user($userid)) ) {
332 $evt = OpenILS::Event->new('USER_NOT_FOUND');
333 return (undef, $evt);
335 $evt = $self->check_perms( $staffobj->id, $user->home_ou, @perms );
338 return ($user, $evt);
341 sub checkses_requestor {
342 my( $self, $authtoken, $targetid, @perms ) = @_;
343 my( $requestor, $target, $evt );
345 ($requestor, $evt) = $self->checkses($authtoken);
346 return (undef, undef, $evt) if $evt;
348 ($target, $evt) = $self->checkrequestor( $requestor, $targetid, @perms );
349 return( $requestor, $target, $evt);