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 warn "Checking perm with user : $user_id , org: $org_id, @perm_types\n";
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 # ---------------------------------------------------------------------------
118 my( $self, $session ) = @_;
122 $user = $self->check_user_session($session);
124 $evt = OpenILS::Event->new('NO_SESSION');
127 return ( $user, $evt );
130 sub check_user_session {
132 my( $self, $user_session ) = @_;
134 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
135 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
136 my $response = $request->recv();
139 throw OpenSRF::EX::User ("Session [$user_session] cannot be authenticated" );
142 if($response->isa("OpenSRF::EX")) {
143 throw $response ($response->stringify);
146 my $user = $response->content;
148 throw OpenSRF::EX::ERROR ("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 );
313 # handy tool to handle the ever-recurring situation of someone requesting
314 # something on someone else's behalf (think staff member creating something for a user)
315 # returns ($requestor, $targetuser_id, $failed_perm, $exception)
316 # $failed_perm is undef if perms are OK
317 # exception is OK if there was no exception
318 # $targetuser == $staffuser->id when $targetuser is undefined.
319 sub handle_requestor {
320 my( $self, $authtoken, $targetuser, @permissions ) = @_;
322 my $requestor = $self->check_user_session($authtoken);
323 $targetuser = $requestor->id unless defined $targetuser;
324 my $userobj = $requestor;
325 $userobj = $self->fetch_user($targetuser)
326 unless $targetuser eq $requestor->id;
328 if(!$userobj) {} # XXX Friendly exception
332 #everyone is allowed to view their own data
333 if( $targetuser ne $requestor->id ) {
334 $perm = $self->check_perms(
335 $requestor->id, $userobj->home_ou, @permissions );
338 return ($requestor, $targetuser, $perm);