]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/AppUtils.pm
just keeping up to date
[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
43
44 # ---------------------------------------------------------------------------
45 # Checks to see if a user is logged in.  Returns the user record on success,
46 # throws an exception on error.
47 # ---------------------------------------------------------------------------
48 sub check_user_session {
49
50         my( $self, $user_session ) = @_;
51         my $session = OpenSRF::AppSession->create( "open-ils.auth" );
52         my $request = $session->request("open-ils.auth.session.retrieve", $user_session );
53         my $response = $request->recv();
54         if($response) {
55                 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
56         }
57         if($response->isa("OpenSRF::EX")) {
58                 throw $response ($response->stringify);
59         }
60
61         my $user = $response->content;
62         if(!$user ) {
63                 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
64         }
65
66         $session->disconnect();
67         $session->kill_me();
68
69         return $user;
70
71
72 =head blah
73         my $method = $self->method_lookup("open-ils.auth.session.retrieve");
74         if(!$method) {
75                 throw OpenSRF::EX::PANIC ("Can't locate method 'open-ils.auth.session.retrieve'" );
76         }
77
78         my ($user) = $method->run( $user_session );
79         if(!$user ) {
80                 throw OpenSRF::EX::ERROR ("Session [$user_session] cannot be authenticated" );
81         }
82         return $user;
83 =cut
84         
85 }
86
87
88
89 1;