1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
7 use Digest::MD5 qw(md5_hex);
9 use OpenSRF::EX qw(:try);
13 use OpenILS::Application::AppUtils;
14 use OpenILS::Utils::Fieldmapper;
15 use OpenILS::Application::Search::Actor;
16 use OpenILS::Utils::ModsParser;
17 use OpenSRF::Utils::Logger;
18 my $logger = "OpenSRF::Utils::Logger";
20 use OpenSRF::Utils::Cache;
23 use OpenILS::Application::Actor::Container;
25 OpenILS::Application::Actor::Container->initialize();
28 my $apputils = "OpenILS::Application::AppUtils";
30 sub _d { warn "Patron:\n" . Dumper(shift()); }
35 my $set_user_settings;
38 __PACKAGE__->register_method(
39 method => "set_user_settings",
40 api_name => "open-ils.actor.patron.settings.update",
42 sub set_user_settings {
43 my( $self, $client, $user_session, $uid, $settings ) = @_;
45 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
47 if (ref($uid) eq 'HASH') {
51 $uid ||= $user_obj->id;
53 if( $user_obj->id != $uid ) {
54 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
55 return OpenILS::Perm->new("UPDATE_USER");
59 # $set_user_settings ||=
60 # $self->method_lookup('open-ils.storage.direct.actor.user_setting.batch.merge');
62 # return $set_user_settings->run(map { [{ usr => $uid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
64 return $apputils->simple_scalar_request(
66 'open-ils.storage.direct.actor.user_setting.batch.merge',
67 map { [{ usr => $uid, name => $_}, {value => $$settings{$_}}] } keys %$settings );
74 __PACKAGE__->register_method(
75 method => "set_ou_settings",
76 api_name => "open-ils.actor.org_unit.settings.update",
79 my( $self, $client, $user_session, $ouid, $settings ) = @_;
81 throw OpenSRF::EX::InvalidArg ("OrgUnit ID and Settings hash required for setting OrgUnit settings") unless ($ouid && $settings);
83 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
85 if($apputils->check_user_perms($user_obj->id, $ouid, "UPDATE_ORG_UNIT")) {
86 return OpenILS::Perm->new("UPDATE_ORG_UNIT");
91 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.merge');
93 return $set_ou_settings->run(map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
99 my $fetch_user_settings;
100 my $fetch_ou_settings;
102 __PACKAGE__->register_method(
103 method => "user_settings",
104 api_name => "open-ils.actor.patron.settings.retrieve",
107 my( $self, $client, $user_session, $uid ) = @_;
109 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
111 $uid ||= $user_obj->id;
113 if( $user_obj->id != $uid ) {
114 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
115 return OpenILS::Perm->new("VIEW_USER");
119 warn "fetching user settings for $uid...\n";
120 my $s = $apputils->simple_scalar_request(
122 'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
124 return { map { ($_->name,$_->value) } @$s };
129 __PACKAGE__->register_method(
130 method => "ou_settings",
131 api_name => "open-ils.actor.org_unit.settings.retrieve",
134 my( $self, $client, $ouid ) = @_;
136 throw OpenSRF::EX::InvalidArg ("OrgUnit ID required for lookup of OrgUnit settings") unless ($ouid);
138 $fetch_ou_settings ||=
139 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic');
141 my ($s) = $fetch_ou_settings->run($ouid);
143 return { map { ($_->name,$_->value) } @$s };
148 __PACKAGE__->register_method(
149 method => "update_patron",
150 api_name => "open-ils.actor.patron.update",);
153 my( $self, $client, $user_session, $patron ) = @_;
155 my $session = $apputils->start_db_session();
158 warn $user_session . " " . $patron . "\n";
162 OpenILS::Application::AppUtils->check_user_session(
163 $user_session ); #throws EX on error
165 # XXX does this user have permission to add/create users. Granularity?
166 # $new_patron is the patron in progress. $patron is the original patron
167 # passed in with the method. new_patron will change as the components
168 # of patron are added/updated.
172 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
173 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
174 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
176 # create/update the patron first so we can use his id
177 if($patron->isnew()) {
179 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
181 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
182 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
183 $client->respond_complete($new_patron->ex);
187 } else { $new_patron = $patron; }
189 $new_patron = _add_update_addresses($session, $patron, $new_patron, $user_obj);
191 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
192 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
193 $client->respond_complete($new_patron->ex);
197 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
199 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
200 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
201 $client->respond_complete($new_patron->ex);
205 $new_patron = _add_survey_responses($session, $patron, $new_patron, $user_obj);
206 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
207 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
208 $client->respond_complete($new_patron->ex);
213 # re-update the patron if anything has happened to him during this process
214 if($new_patron->ischanged()) {
215 $new_patron = _update_patron($session, $new_patron, $user_obj);
217 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
218 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
219 $client->respond_complete($new_patron->ex);
224 $apputils->commit_db_session($session);
226 $session = OpenSRF::AppSession->create("open-ils.storage");
227 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
228 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
229 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
230 $client->respond_complete($new_patron->ex);
235 warn "Patron Update/Create complete\n";
236 return flesh_user($new_patron->id());
242 __PACKAGE__->register_method(
243 method => "user_retrieve_fleshed_by_id",
244 api_name => "open-ils.actor.user.fleshed.retrieve",);
246 sub user_retrieve_fleshed_by_id {
247 my( $self, $client, $user_session, $user_id ) = @_;
248 my $user_obj = $apputils->check_user_session( $user_session );
250 if(!defined($user_id)) { $user_id = $user_obj->id; }
252 if( $user_obj->id ne $user_id ) {
253 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
254 return OpenILS::Perm->new("VIEW_USER");
258 return flesh_user($user_id);
269 $session = OpenSRF::AppSession->create("open-ils.storage");
273 # grab the user with the given id
274 my $ureq = $session->request(
275 "open-ils.storage.direct.actor.user.retrieve", $id);
276 my $user = $ureq->gather(1);
278 if(!$user) { return undef; }
281 my $cards_req = $session->request(
282 "open-ils.storage.direct.actor.card.search.usr.atomic",
284 $user->cards( $cards_req->gather(1) );
286 for my $c(@{$user->cards}) {
287 if($c->id == $user->card || $c->id eq $user->card ) {
288 warn "Setting my card to " . $c->id . "\n";
293 my $add_req = $session->request(
294 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
296 $user->addresses( $add_req->gather(1) );
298 for my $c(@{$user->addresses}) {
299 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
300 warn "Setting my address to " . $c->id . "\n";
301 $user->billing_address($c);
305 for my $c(@{$user->addresses}) {
306 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
307 warn "Setting my address to " . $c->id . "\n";
308 $user->mailing_address($c);
312 my $stat_req = $session->request(
313 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
315 $user->stat_cat_entries($stat_req->gather(1));
317 if($kill) { $session->disconnect(); }
318 $user->clear_passwd();
326 # clone and clear stuff that would break the database
330 my $new_patron = $patron->clone;
332 # Using the Fieldmapper clone method
333 #my $new_patron = Fieldmapper::actor::user->new();
335 #my $fmap = $Fieldmapper::fieldmap;
336 #no strict; # shallow clone, may be useful in the fieldmapper
338 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
339 # $new_patron->$field( $patron->$field() );
344 $new_patron->clear_billing_address();
345 $new_patron->clear_mailing_address();
346 $new_patron->clear_addresses();
347 $new_patron->clear_card();
348 $new_patron->clear_cards();
349 $new_patron->clear_id();
350 $new_patron->clear_isnew();
351 $new_patron->clear_ischanged();
352 $new_patron->clear_isdeleted();
353 $new_patron->clear_stat_cat_entries();
362 my $user_obj = shift;
365 if($apputils->check_user_perms(
366 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
367 return OpenILS::Perm->new("CREATE_USER");
370 warn "Creating new patron\n";
373 my $req = $session->request(
374 "open-ils.storage.direct.actor.user.create",$patron);
375 my $id = $req->gather(1);
377 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
380 # retrieve the patron from the db to collect defaults
381 my $ureq = $session->request(
382 "open-ils.storage.direct.actor.user.retrieve",
385 warn "Created new patron with id $id\n";
387 return $ureq->gather(1);
392 my( $session, $patron, $user_obj) = @_;
395 if($patron->id ne $user_obj->id) {
396 if($apputils->check_user_perms(
397 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
398 return OpenILS::Perm->new("UPDATE_USER");
402 warn "updating patron " . Dumper($patron) . "\n";
404 my $req = $session->request(
405 "open-ils.storage.direct.actor.user.update",$patron );
406 my $status = $req->gather(1);
407 if(!defined($status)) {
408 throw OpenSRF::EX::ERROR
409 ("Unknown error updating patron");
415 sub _add_update_addresses {
418 my $new_patron = shift;
420 my $current_id; # id of the address before creation
422 for my $address (@{$patron->addresses()}) {
424 $address->usr($new_patron->id());
426 if(ref($address) and $address->isnew()) {
427 warn "Adding new address at street " . $address->street1() . "\n";
429 $current_id = $address->id();
430 $address = _add_address($session,$address);
432 if( $patron->billing_address() and
433 $patron->billing_address() == $current_id ) {
434 $new_patron->billing_address($address->id());
435 $new_patron->ischanged(1);
438 if( $patron->mailing_address() and
439 $patron->mailing_address() == $current_id ) {
440 $new_patron->mailing_address($address->id());
441 $new_patron->ischanged(1);
444 } elsif( ref($address) and $address->ischanged() ) {
445 warn "Updating address at street " . $address->street1();
446 $address->usr($new_patron->id());
447 _update_address($session,$address);
449 } elsif( ref($address) and $address->isdeleted() ) {
450 warn "Deleting address at street " . $address->street1();
452 if( $address->id() == $new_patron->mailing_address() ) {
453 $new_patron->clear_mailing_address();
454 _update_patron($session, $new_patron);
457 if( $address->id() == $new_patron->billing_address() ) {
458 $new_patron->clear_billing_address();
459 _update_patron($session, $new_patron);
462 _delete_address($session,$address);
470 # adds an address to the db and returns the address with new id
472 my($session, $address) = @_;
473 $address->clear_id();
476 warn "Adding Address:\n";
477 warn Dumper($address);
479 # put the address into the database
480 my $req = $session->request(
481 "open-ils.storage.direct.actor.user_address.create",
485 my $id = $req->gather(1);
487 throw OpenSRF::EX::ERROR
488 ("Unable to create new user address");
491 warn "Created address with id $id\n";
493 # update all the necessary id's
499 sub _update_address {
500 my( $session, $address ) = @_;
501 my $req = $session->request(
502 "open-ils.storage.direct.actor.user_address.update",
504 my $status = $req->gather(1);
505 if(!defined($status)) {
506 throw OpenSRF::EX::ERROR
507 ("Unknown error updating address");
514 sub _add_update_cards {
518 my $new_patron = shift;
520 my $virtual_id; #id of the card before creation
521 for my $card (@{$patron->cards()}) {
523 $card->usr($new_patron->id());
525 if(ref($card) and $card->isnew()) {
527 $virtual_id = $card->id();
528 $card = _add_card($session,$card);
529 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
533 #if(ref($patron->card)) { $patron->card($patron->card->id); }
534 if($patron->card() == $virtual_id) {
535 $new_patron->card($card->id());
536 $new_patron->ischanged(1);
539 } elsif( ref($card) and $card->ischanged() ) {
540 $card->usr($new_patron->id());
541 _update_card($session, $card);
548 # adds an card to the db and returns the card with new id
550 my( $session, $card ) = @_;
553 warn "Adding card with barcode " . $card->barcode() . "\n";
554 my $req = $session->request(
555 "open-ils.storage.direct.actor.card.create",
558 my $id = $req->gather(1);
560 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
564 warn "Created patron card with id $id\n";
570 my( $session, $card ) = @_;
573 my $req = $session->request(
574 "open-ils.storage.direct.actor.card.update",
576 my $status = $req->gather(1);
577 if(!defined($status)) {
578 throw OpenSRF::EX::ERROR
579 ("Unknown error updating card");
587 sub _delete_address {
588 my( $session, $address ) = @_;
590 warn "Deleting address " . $address->street1() . "\n";
592 my $req = $session->request(
593 "open-ils.storage.direct.actor.user_address.delete",
595 my $status = $req->gather(1);
596 if(!defined($status)) {
597 throw OpenSRF::EX::ERROR
598 ("Unknown error updating address");
600 warn "Delete address status is $status\n";
605 sub _add_survey_responses {
606 my ($session, $patron, $new_patron) = @_;
608 warn "updating responses for user " . $new_patron->id . "\n";
610 my $responses = $patron->survey_responses;
614 for my $resp( @$responses ) {
615 $resp->usr($new_patron->id);
618 my $status = $apputils->simple_scalar_request(
620 "open-ils.circ.survey.submit.user_id",
629 sub _create_stat_maps {
631 my($session, $user_session, $patron, $new_patron) = @_;
633 my $maps = $patron->stat_cat_entries();
635 for my $map (@$maps) {
637 next unless($map->isnew() || $map->ischanged());
639 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
641 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
645 $map->target_usr($new_patron->id);
647 warn "Updating stat entry with method $method and session $user_session and map $map\n";
649 my $req = $session->request($method, $map);
650 my $status = $req->gather(1);
655 throw OpenSRF::EX::ERROR
656 ("Error updating stat map with method $method");
666 __PACKAGE__->register_method(
667 method => "search_username",
668 api_name => "open-ils.actor.user.search.username",
671 sub search_username {
672 my($self, $client, $username) = @_;
673 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
675 "open-ils.storage.direct.actor.user.search.usrname.atomic",
683 __PACKAGE__->register_method(
684 method => "user_retrieve_by_barcode",
685 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
687 sub user_retrieve_by_barcode {
688 my($self, $client, $user_session, $barcode) = @_;
690 $logger->debug("Searching for user with barcode $barcode");
691 #my $user_obj = $apputils->check_user_session( $user_session );
692 my ($user_obj, $evt) = $apputils->check_ses($user_session);
695 my $session = OpenSRF::AppSession->create("open-ils.storage");
697 # find the card with the given barcode
698 my $creq = $session->request(
699 "open-ils.storage.direct.actor.card.search.barcode.atomic",
701 my $card = $creq->gather(1);
703 if(!$card || !$card->[0]) {
704 $session->disconnect();
709 my $user = flesh_user($card->usr(), $session);
710 $session->disconnect();
717 __PACKAGE__->register_method(
718 method => "get_user_by_id",
719 api_name => "open-ils.actor.user.retrieve",);
722 my ($self, $client, $user_session, $id) = @_;
724 my $user_obj = $apputils->check_user_session( $user_session );
726 return $apputils->simple_scalar_request(
728 "open-ils.storage.direct.actor.user.retrieve",
734 __PACKAGE__->register_method(
735 method => "get_org_types",
736 api_name => "open-ils.actor.org_types.retrieve",);
740 my($self, $client) = @_;
742 return $org_types if $org_types;
744 $apputils->simple_scalar_request(
746 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
751 __PACKAGE__->register_method(
752 method => "get_user_profiles",
753 api_name => "open-ils.actor.user.profiles.retrieve",
757 sub get_user_profiles {
758 return $user_profiles if $user_profiles;
760 return $user_profiles =
761 $apputils->simple_scalar_request(
763 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
768 __PACKAGE__->register_method(
769 method => "get_user_ident_types",
770 api_name => "open-ils.actor.user.ident_types.retrieve",
773 sub get_user_ident_types {
774 return $ident_types if $ident_types;
775 return $ident_types =
776 $apputils->simple_scalar_request(
778 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
784 __PACKAGE__->register_method(
785 method => "get_org_unit",
786 api_name => "open-ils.actor.org_unit.retrieve",
791 my( $self, $client, $user_session, $org_id ) = @_;
793 if(defined($user_session) && !defined($org_id)) {
795 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
796 if(!defined($org_id)) {
797 $org_id = $user_obj->home_ou;
802 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
804 "open-ils.storage.direct.actor.org_unit.retrieve",
813 __PACKAGE__->register_method(
814 method => "get_org_tree",
815 api_name => "open-ils.actor.org_tree.retrieve",
817 note => "Returns the entire org tree structure",
821 my( $self, $client) = @_;
824 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
826 # see if it's in the cache
827 warn "Getting ORG Tree\n";
828 my $tree = $cache_client->get_cache('orgtree');
830 warn "Found orgtree in cache. returning...\n";
834 my $orglist = $apputils->simple_scalar_request(
836 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
839 warn "found org list\n";
842 $tree = $self->build_org_tree($orglist);
843 $cache_client->put_cache('orgtree', $tree);
849 # turns an org list into an org tree
852 my( $self, $orglist) = @_;
854 return $orglist unless (
855 ref($orglist) and @$orglist > 1 );
858 $a->ou_type <=> $b->ou_type ||
859 $a->name cmp $b->name } @$orglist;
861 for my $org (@list) {
863 next unless ($org and defined($org->parent_ou));
864 my ($parent) = grep { $_->id == $org->parent_ou } @list;
867 $parent->children([]) unless defined($parent->children);
868 push( @{$parent->children}, $org );
876 __PACKAGE__->register_method(
877 method => "get_org_descendants",
878 api_name => "open-ils.actor.org_tree.descendants.retrieve"
881 # depth is optional. org_unit is the id
882 sub get_org_descendants {
883 my( $self, $client, $org_unit, $depth ) = @_;
884 my $orglist = $apputils->simple_scalar_request(
886 "open-ils.storage.actor.org_unit.descendants.atomic",
888 return $self->build_org_tree($orglist);
892 __PACKAGE__->register_method(
893 method => "get_org_ancestors",
894 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
897 # depth is optional. org_unit is the id
898 sub get_org_ancestors {
899 my( $self, $client, $org_unit, $depth ) = @_;
900 my $orglist = $apputils->simple_scalar_request(
902 "open-ils.storage.actor.org_unit.ancestors.atomic",
904 return $self->build_org_tree($orglist);
908 __PACKAGE__->register_method(
909 method => "get_standings",
910 api_name => "open-ils.actor.standings.retrieve"
915 return $user_standings if $user_standings;
916 return $user_standings =
917 $apputils->simple_scalar_request(
919 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
924 __PACKAGE__->register_method(
925 method => "get_my_org_path",
926 api_name => "open-ils.actor.org_unit.full_path.retrieve"
929 sub get_my_org_path {
930 my( $self, $client, $user_session, $org_id ) = @_;
931 my $user_obj = $apputils->check_user_session($user_session);
932 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
934 return $apputils->simple_scalar_request(
936 "open-ils.storage.actor.org_unit.full_path.atomic",
941 __PACKAGE__->register_method(
942 method => "patron_adv_search",
943 api_name => "open-ils.actor.patron.search.advanced" );
945 sub patron_adv_search {
946 my( $self, $client, $staff_login, $search_hash ) = @_;
949 warn "patron adv with $staff_login and search " .
950 Dumper($search_hash) . "\n";
952 my $session = OpenSRF::AppSession->create("open-ils.storage");
953 my $req = $session->request(
954 "open-ils.storage.actor.user.crazy_search", $search_hash);
956 my $ans = $req->gather(1);
958 my %hash = map { ($_ =>1) } @$ans;
959 $ans = [ keys %hash ];
961 warn "Returning @$ans\n";
963 $session->disconnect();
970 sub _verify_password {
971 my($user_session, $password) = @_;
972 my $user_obj = $apputils->check_user_session($user_session);
974 #grab the user with password
975 $user_obj = $apputils->simple_scalar_request(
977 "open-ils.storage.direct.actor.user.retrieve",
980 if($user_obj->passwd eq $password) {
988 __PACKAGE__->register_method(
989 method => "update_password",
990 api_name => "open-ils.actor.user.password.update");
992 __PACKAGE__->register_method(
993 method => "update_password",
994 api_name => "open-ils.actor.user.username.update");
996 __PACKAGE__->register_method(
997 method => "update_password",
998 api_name => "open-ils.actor.user.email.update");
1000 sub update_password {
1001 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
1003 warn "Updating user with method " .$self->api_name . "\n";
1004 my $user_obj = $apputils->check_user_session($user_session);
1006 if($self->api_name =~ /password/) {
1008 #make sure they know the current password
1009 if(!_verify_password($user_session, md5_hex($current_password))) {
1010 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
1013 $user_obj->passwd($new_value);
1015 elsif($self->api_name =~ /username/) {
1016 $user_obj->usrname($new_value);
1019 elsif($self->api_name =~ /email/) {
1020 warn "Updating email to $new_value\n";
1021 $user_obj->email($new_value);
1024 my $session = $apputils->start_db_session();
1025 $user_obj = _update_patron($session, $user_obj, $user_obj);
1026 $apputils->commit_db_session($session);
1028 if($user_obj) { return 1; }
1033 __PACKAGE__->register_method(
1034 method => "check_user_perms",
1035 api_name => "open-ils.actor.user.perm.check",
1036 notes => <<" NOTES");
1037 Takes a login session, user id, an org id, and an array of perm type strings. For each
1038 perm type, if the user does *not* have the given permission it is added
1039 to a list which is returned from the method. If all permissions
1040 are allowed, an empty list is returned
1041 if the logged in user does not match 'user_id', then the logged in user must
1042 have VIEW_PERMISSION priveleges.
1045 sub check_user_perms {
1046 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1047 my $user_obj = $apputils->check_user_session($login_session);
1049 if($user_obj->id ne $user_id) {
1050 if($apputils->check_user_perms($user_obj->id, $org_id, "VIEW_PERMISSION")) {
1051 return OpenILS::Perm->new("VIEW_PERMISSION");
1056 for my $perm (@$perm_types) {
1057 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
1058 push @not_allowed, $perm;
1062 return \@not_allowed
1067 __PACKAGE__->register_method(
1068 method => "user_fines_summary",
1069 api_name => "open-ils.actor.user.fines.summary",
1070 notes => <<" NOTES");
1071 Returns a short summary of the users total open fines, excluding voided fines
1072 Params are login_session, user_id
1073 Returns a 'mous' object.
1076 sub user_fines_summary {
1077 my( $self, $client, $login_session, $user_id ) = @_;
1079 my $user_obj = $apputils->check_user_session($login_session);
1080 if($user_obj->id ne $user_id) {
1081 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1082 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1086 return $apputils->simple_scalar_request(
1088 "open-ils.storage.direct.money.open_user_summary.search.usr",
1096 __PACKAGE__->register_method(
1097 method => "user_transactions",
1098 api_name => "open-ils.actor.user.transactions",
1099 notes => <<" NOTES");
1100 Returns a list of open user transactions (mbts objects);
1101 Params are login_session, user_id
1102 Optional third parameter is the transactions type. defaults to all
1105 __PACKAGE__->register_method(
1106 method => "user_transactions",
1107 api_name => "open-ils.actor.user.transactions.have_charge",
1108 notes => <<" NOTES");
1109 Returns a list of all open user transactions (mbts objects) that have an initial charge
1110 Params are login_session, user_id
1111 Optional third parameter is the transactions type. defaults to all
1114 __PACKAGE__->register_method(
1115 method => "user_transactions",
1116 api_name => "open-ils.actor.user.transactions.have_balance",
1117 notes => <<" NOTES");
1118 Returns a list of all open user transactions (mbts objects) that have a balance
1119 Params are login_session, user_id
1120 Optional third parameter is the transactions type. defaults to all
1123 __PACKAGE__->register_method(
1124 method => "user_transactions",
1125 api_name => "open-ils.actor.user.transactions.fleshed",
1126 notes => <<" NOTES");
1127 Returns an object/hash of transaction, circ, title where transaction = an open
1128 user transactions (mbts objects), circ is the attached circluation, and title
1129 is the title the circ points to
1130 Params are login_session, user_id
1131 Optional third parameter is the transactions type. defaults to all
1134 __PACKAGE__->register_method(
1135 method => "user_transactions",
1136 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1137 notes => <<" NOTES");
1138 Returns an object/hash of transaction, circ, title where transaction = an open
1139 user transactions that has an initial charge (mbts objects), circ is the
1140 attached circluation, and title is the title the circ points to
1141 Params are login_session, user_id
1142 Optional third parameter is the transactions type. defaults to all
1145 __PACKAGE__->register_method(
1146 method => "user_transactions",
1147 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1148 notes => <<" NOTES");
1149 Returns an object/hash of transaction, circ, title where transaction = an open
1150 user transaction that has a balance (mbts objects), circ is the attached
1151 circluation, and title is the title the circ points to
1152 Params are login_session, user_id
1153 Optional third parameter is the transaction type. defaults to all
1158 sub user_transactions {
1159 my( $self, $client, $login_session, $user_id, $type ) = @_;
1161 my $user_obj = $apputils->check_user_session($login_session);
1162 if($user_obj->id ne $user_id) {
1163 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_TRANSACTIONS")) {
1164 return OpenILS::Perm->new("VIEW_USER_TRANSACTIONS");
1168 my $api = $self->api_name();
1171 if(defined($type)) { @xact = (xact_type => $type);
1172 } else { @xact = (); }
1174 if($api =~ /have_charge/) {
1176 $trans = $apputils->simple_scalar_request(
1178 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1179 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1181 } elsif($api =~ /have_balance/) {
1183 $trans = $apputils->simple_scalar_request(
1185 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1186 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1190 $trans = $apputils->simple_scalar_request(
1192 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1193 { usr => $user_id, @xact });
1196 if($api !~ /fleshed/) { return $trans; }
1201 for my $t (@$trans) {
1205 my $circ = $apputils->simple_scalar_request(
1207 "open-ils.storage.direct.action.circulation.retrieve",
1212 my $title = $apputils->simple_scalar_request(
1214 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1215 $circ->target_copy );
1219 my $u = OpenILS::Utils::ModsParser->new();
1220 $u->start_mods_batch($title->marc());
1221 my $mods = $u->finish_mods_batch();
1223 push @resp, {transaction => $t, circ => $circ, record => $mods };
1233 __PACKAGE__->register_method(
1234 method => "retrieve_groups",
1235 api_name => "open-ils.actor.groups.retrieve",
1236 notes => <<" NOTES");
1237 Returns a list of user groups
1239 sub retrieve_groups {
1240 my( $self, $client ) = @_;
1241 return $apputils->simple_scalar_request(
1243 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1297 some old methods that may be good to keep around for now
1300 my( $session, $card ) = @_;
1302 warn "Deleting card with barcode " . $card->barcode() . "\n";
1303 my $req = $session->request(
1304 "open-ils.storage.direct.actor.card.delete",
1306 my $status = $req->gather(1);
1307 if(!defined($status)) {
1308 throw OpenSRF::EX::ERROR
1309 ("Unknown error updating card");
1315 # deletes the patron and any attached addresses and cards
1316 __PACKAGE__->register_method(
1317 method => "delete_patron",
1318 api_name => "open-ils.actor.patron.delete",
1323 my( $self, $client, $patron ) = @_;
1324 my $session = $apputils->start_db_session();
1329 $patron->clear_mailing_address();
1330 $patron->clear_billing_address();
1331 $patron->ischanged(1);
1333 _update_patron($session, $patron);
1334 _delete_address($session,$_) for (@{$patron->addresses()});
1335 _delete_card($session,$_) for (@{$patron->cards()});
1336 _delete_patron($session,$patron);
1337 $apputils->commit_db_session($session);
1339 } catch Error with {
1341 $err = "-*- Failure deleting user: $e";
1342 $apputils->rollback_db_session($session);
1346 if($err) { throw OpenSRF::EX::ERROR ($err); }
1347 warn "Patron Delete complete\n";
1351 sub _delete_patron {
1352 my( $session, $patron ) = @_;
1354 warn "Deleting patron " . $patron->usrname() . "\n";
1356 my $req = $session->request(
1357 "open-ils.storage.direct.actor.user.delete",
1359 my $status = $req->gather(1);
1360 if(!defined($status)) {
1361 throw OpenSRF::EX::ERROR
1362 ("Unknown error updating patron");