]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
moving forward, added wormizing
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / AppUtils.pm
1 package OpenILS::Application::AppUtils;
2 use strict; use warnings;
3 use base qw/OpenSRF::Application/;
4
5
6 # ---------------------------------------------------------------------------
7 # Pile of utilty methods used accross applications.
8 # ---------------------------------------------------------------------------
9
10
11 # ---------------------------------------------------------------------------
12 # on sucess, returns the created session, on failure throws ERROR exception
13 # ---------------------------------------------------------------------------
14 sub start_db_session {
15         my $self = shift;
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" );
22         }
23         $trans_req->finish();
24         return $session;
25 }
26
27 # ---------------------------------------------------------------------------
28 # commits and destroys the session
29 # ---------------------------------------------------------------------------
30 sub commit_db_session {
31         my( $self, $session ) = @_;
32
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; }
36
37         $session->finish();
38         $session->disconnect();
39         $session->kill_me();
40 }
41
42 sub rollback_db_session {
43         my( $self, $session ) = @_;
44
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; }
48
49         $session->finish();
50         $session->disconnect();
51         $session->kill_me();
52 }
53
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 {
59
60         my( $self, $user_session ) = @_;
61
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();
65
66         if(!$response) {
67                 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
68         }
69
70         if($response->isa("OpenSRF::EX")) {
71                 throw $response ($response->stringify);
72         }
73
74         my $user = $response->content;
75         if(!$user) {
76                 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
77         }
78
79         $session->disconnect();
80         $session->kill_me();
81
82         return $user;
83
84         
85 }
86
87
88
89 1;