1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
6 # ---------------------------------------------------------------------------
7 # Pile of utilty methods used accross applications.
8 # ---------------------------------------------------------------------------
11 # ---------------------------------------------------------------------------
12 # on sucess, returns the created session, on failure throws ERROR exception
13 # ---------------------------------------------------------------------------
14 sub start_db_session {
16 my $session = OpenSRF::AppSession->connect( "open-ils.storage" );
17 my $trans_req = $session->request( "open-ils.storage.transaction.begin" );
18 my $trans_resp = $trans_req->recv();
19 if(ref($trans_resp) and $trans_resp->isa("Error")) { throw $trans_resp; }
20 if( ! $trans_resp->content() ) {
21 throw OpenSRF::ERROR ("Unable to Begin Transaction with database" );
27 # ---------------------------------------------------------------------------
28 # commits and destroys the session
29 # ---------------------------------------------------------------------------
30 sub commit_db_session {
31 my( $self, $session ) = @_;
33 my $req = $session->request( "open-ils.storage.transaction.commit" );
34 my $resp = $req->recv();
35 if(ref($resp) and $resp->isa("Error")) { throw $resp; }
38 $session->disconnect();
42 sub rollback_db_session {
43 my( $self, $session ) = @_;
45 my $req = $session->request("open-ils.storage.transaction.rollback");
46 my $resp = $req->recv();
47 if(ref($resp) and $resp->isa("Error")) { throw $resp; }
50 $session->disconnect();
54 # ---------------------------------------------------------------------------
55 # Checks to see if a user is logged in. Returns the user record on success,
56 # throws an exception on error.
57 # ---------------------------------------------------------------------------
58 sub check_user_session {
60 my( $self, $user_session ) = @_;
62 my $session = OpenSRF::AppSession->create( "open-ils.auth" );
63 my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
64 my $response = $request->recv();
67 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
70 if($response->isa("OpenSRF::EX")) {
71 throw $response ($response->stringify);
74 my $user = $response->content;
76 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
79 $session->disconnect();