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);
9 my $cache_client = "OpenSRF::Utils::Cache";
11 # ---------------------------------------------------------------------------
12 # Pile of utilty methods used accross applications.
13 # ---------------------------------------------------------------------------
16 # ---------------------------------------------------------------------------
17 # on sucess, returns the created session, on failure throws ERROR exception
18 # ---------------------------------------------------------------------------
19 sub start_db_session {
22 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
23 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
25 my $trans_resp = $trans_req->recv();
26 if(ref($trans_resp) and UNIVERSAL::isa($trans_resp,"Error")) { throw $trans_resp; }
27 if( ! $trans_resp->content() ) {
29 ("Unable to Begin Transaction with database" );
36 # returns undef if user has all of the perms provided
37 # returns the first failed perm on failure
38 sub check_user_perms {
39 my($self, $user_id, $org_id, @perm_types ) = @_;
41 warn "Checking perm with user : $user_id , org: $org_id, @perm_types\n";
43 throw OpenSRF::EX::ERROR ("Invalid call to check_user_perms()")
44 unless( defined($user_id) and defined($org_id) and @perm_types);
46 my $session = OpenSRF::AppSession->create("open-ils.storage");
47 for my $type (@perm_types) {
48 my $req = $session->request(
49 "open-ils.storage.permission.user_has_perm",
50 $user_id, $type, $org_id );
51 my $resp = $req->gather(1);
53 $session->disconnect();
58 $session->disconnect();
62 # checks the list of user perms. The first one that fails returns a new
63 # OpenILS::Perm object of that type. Returns undef if all perms are allowed
65 my( $self, $user_id, $org_id, @perm_types ) = @_;
66 my $t = $self->check_user_perms( $user_id, $org_id, @perm_types );
67 return OpenILS::Event->new('PERM_FAILURE', perm => $t, permloc => $org_id ) if $t;
73 # ---------------------------------------------------------------------------
74 # commits and destroys the session
75 # ---------------------------------------------------------------------------
76 sub commit_db_session {
77 my( $self, $session ) = @_;
79 my $req = $session->request( "open-ils.storage.transaction.commit" );
80 my $resp = $req->recv();
83 throw OpenSRF::EX::ERROR ("Unable to commit db session");
86 if(UNIVERSAL::isa($resp,"Error")) {
87 throw $resp ($resp->stringify);
91 throw OpenSRF::EX::ERROR ("Unable to commit db session");
95 $session->disconnect();
99 sub rollback_db_session {
100 my( $self, $session ) = @_;
102 my $req = $session->request("open-ils.storage.transaction.rollback");
103 my $resp = $req->recv();
104 if(UNIVERSAL::isa($resp,"Error")) { throw $resp; }
107 $session->disconnect();
111 # ---------------------------------------------------------------------------
112 # Checks to see if a user is logged in. Returns the user record on success,
113 # throws an exception on error.
114 # ---------------------------------------------------------------------------
115 sub check_user_session {
117 my( $self, $user_session ) = @_;
119 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
120 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
121 my $response = $request->recv();
124 throw OpenSRF::EX::User ("Session [$user_session] cannot be authenticated" );
127 if($response->isa("OpenSRF::EX")) {
128 throw $response ($response->stringify);
131 my $user = $response->content;
133 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
136 $session->disconnect();
144 # generic simple request returning a scalar value
146 my($self, $service, $method, @params) = @_;
147 return $self->simple_scalar_request($service, $method, @params);
151 sub simple_scalar_request {
152 my($self, $service, $method, @params) = @_;
154 my $session = OpenSRF::AppSession->create( $service );
155 my $request = $session->request( $method, @params );
156 my $response = $request->recv(30);
158 $request->wait_complete;
160 if(!$request->complete) {
161 throw OpenSRF::EX::ERROR ("Call to $service for method $method with params @params" .
162 "\n did not complete successfully");
166 warn "No response from $service for method $method with params @params";
169 if(UNIVERSAL::isa($response,"Error")) {
170 throw $response ("Call to $service for method $method with params @params" .
171 "\n failed with exception: " . $response->stringify );
177 $session->disconnect();
181 if($response) { $value = $response->content; }
182 else { $value = undef; }
193 my $org_typelist = undef;
194 my $org_typelist_hash = {};
199 if($tree) { return $tree; }
201 # see if it's in the cache
202 $tree = $cache_client->new()->get_cache('_orgtree');
203 if($tree) { return $tree; }
206 warn "Retrieving Org Tree\n";
207 $orglist = $self->simple_scalar_request(
209 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
212 if( ! $org_typelist ) {
213 warn "Retrieving org types\n";
214 $org_typelist = $self->simple_scalar_request(
216 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
217 $self->build_org_type($org_typelist);
220 $tree = $self->build_org_tree($orglist,1);
221 $cache_client->new()->put_cache('_orgtree', $tree);
226 my $slimtree = undef;
227 sub get_slim_org_tree {
230 if($slimtree) { return $slimtree; }
232 # see if it's in the cache
233 $slimtree = $cache_client->new()->get_cache('slimorgtree');
234 if($slimtree) { return $slimtree; }
237 warn "Retrieving Org Tree\n";
238 $orglist = $self->simple_scalar_request(
240 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
243 $slimtree = $self->build_org_tree($orglist);
244 $cache_client->new->put_cache('slimorgtree', $slimtree);
251 my($self, $org_typelist) = @_;
252 for my $type (@$org_typelist) {
253 $org_typelist_hash->{$type->id()} = $type;
261 my( $self, $orglist, $add_types ) = @_;
263 return $orglist unless (
264 ref($orglist) and @$orglist > 1 );
267 $a->ou_type <=> $b->ou_type ||
268 $a->name cmp $b->name } @$orglist;
270 for my $org (@list) {
274 if(!ref($org->ou_type()) and $add_types) {
275 $org->ou_type( $org_typelist_hash->{$org->ou_type()});
278 next unless (defined($org->parent_ou));
280 my ($parent) = grep { $_->id == $org->parent_ou } @list;
282 $parent->children([]) unless defined($parent->children);
283 push( @{$parent->children}, $org );
293 return $self->simple_scalar_request(
295 'open-ils.storage.direct.actor.user.retrieve', $id );
300 # handy tool to handle the ever-recurring situation of someone requesting
301 # something on someone else's behalf (think staff member creating something for a user)
302 # returns ($requestor, $targetuser_id, $failed_perm, $exception)
303 # $failed_perm is undef if perms are OK
304 # exception is OK if there was no exception
305 # $targetuser == $staffuser->id when $targetuser is undefined.
306 sub handle_requestor {
307 my( $self, $authtoken, $targetuser, @permissions ) = @_;
309 my $requestor = $self->check_user_session($authtoken);
310 $targetuser = $requestor->id unless defined $targetuser;
311 my $userobj = $requestor;
312 $userobj = $self->fetch_user($targetuser)
313 unless $targetuser eq $requestor->id;
315 if(!$userobj) {} # XXX Friendly exception
319 #everyone is allowed to view their own data
320 if( $targetuser ne $requestor->id ) {
321 $perm = $self->check_perms(
322 $requestor->id, $userobj->home_ou, @permissions );
325 return ($requestor, $targetuser, $perm);