1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
6 use Digest::MD5 qw(md5_hex);
8 use OpenSRF::EX qw(:try);
12 use OpenILS::Application::AppUtils;
13 use OpenILS::Utils::Fieldmapper;
14 use OpenILS::Application::Search::Actor;
15 use OpenILS::Utils::ModsParser;
17 use OpenSRF::Utils::Cache;
19 my $apputils = "OpenILS::Application::AppUtils";
21 sub _d { warn "Patron:\n" . Dumper(shift()); }
26 my $set_user_settings;
29 __PACKAGE__->register_method(
30 method => "set_user_settings",
31 api_name => "open-ils.actor.patron.settings.update",
33 sub set_user_settings {
34 my( $self, $client, $user_session, $uid, $settings ) = @_;
36 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
38 if (ref($uid) eq 'HASH') {
42 $uid ||= $user_obj->id;
44 if( $user_obj->id != $uid ) {
45 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
46 return OpenILS::Perm->new("UPDATE_USER");
50 # $set_user_settings ||=
51 # $self->method_lookup('open-ils.storage.direct.actor.user_setting.batch.merge');
53 # return $set_user_settings->run(map { [{ usr => $uid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
55 return $apputils->simple_scalar_request(
57 'open-ils.storage.direct.actor.user_setting.batch.merge',
58 map { [{ usr => $uid, name => $_}, {value => $$settings{$_}}] } keys %$settings );
65 __PACKAGE__->register_method(
66 method => "set_ou_settings",
67 api_name => "open-ils.actor.org_unit.settings.update",
70 my( $self, $client, $user_session, $ouid, $settings ) = @_;
72 throw OpenSRF::EX::InvalidArg ("OrgUnit ID and Settings hash required for setting OrgUnit settings") unless ($ouid && $settings);
74 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
76 if($apputils->check_user_perms($user_obj->id, $ouid, "UPDATE_ORG_UNIT")) {
77 return OpenILS::Perm->new("UPDATE_ORG_UNIT");
82 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.merge');
84 return $set_ou_settings->run(map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings);
90 my $fetch_user_settings;
91 my $fetch_ou_settings;
93 __PACKAGE__->register_method(
94 method => "user_settings",
95 api_name => "open-ils.actor.patron.settings.retrieve",
98 my( $self, $client, $user_session, $uid ) = @_;
100 my $user_obj = $apputils->check_user_session( $user_session ); #throws EX on error
102 $uid ||= $user_obj->id;
104 if( $user_obj->id != $uid ) {
105 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
106 return OpenILS::Perm->new("VIEW_USER");
110 warn "fetching user settings for $uid...\n";
111 my $s = $apputils->simple_scalar_request(
113 'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
115 return { map { ($_->name,$_->value) } @$s };
120 __PACKAGE__->register_method(
121 method => "ou_settings",
122 api_name => "open-ils.actor.org_unit.settings.retrieve",
125 my( $self, $client, $ouid ) = @_;
127 throw OpenSRF::EX::InvalidArg ("OrgUnit ID required for lookup of OrgUnit settings") unless ($ouid);
129 $fetch_ou_settings ||=
130 $self->method_lookup('open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic');
132 my ($s) = $fetch_ou_settings->run($ouid);
134 return { map { ($_->name,$_->value) } @$s };
139 __PACKAGE__->register_method(
140 method => "update_patron",
141 api_name => "open-ils.actor.patron.update",);
144 my( $self, $client, $user_session, $patron ) = @_;
146 my $session = $apputils->start_db_session();
149 warn $user_session . " " . $patron . "\n";
153 OpenILS::Application::AppUtils->check_user_session(
154 $user_session ); #throws EX on error
156 # XXX does this user have permission to add/create users. Granularity?
157 # $new_patron is the patron in progress. $patron is the original patron
158 # passed in with the method. new_patron will change as the components
159 # of patron are added/updated.
163 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
164 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
165 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
167 # create/update the patron first so we can use his id
168 if($patron->isnew()) {
170 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
172 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
173 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
174 $client->respond_complete($new_patron->ex);
178 } else { $new_patron = $patron; }
180 $new_patron = _add_update_addresses($session, $patron, $new_patron, $user_obj);
182 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
183 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
184 $client->respond_complete($new_patron->ex);
188 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
190 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
191 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
192 $client->respond_complete($new_patron->ex);
196 $new_patron = _add_survey_responses($session, $patron, $new_patron, $user_obj);
197 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
198 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
199 $client->respond_complete($new_patron->ex);
204 # re-update the patron if anything has happened to him during this process
205 if($new_patron->ischanged()) {
206 $new_patron = _update_patron($session, $new_patron, $user_obj);
208 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
209 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
210 $client->respond_complete($new_patron->ex);
215 $apputils->commit_db_session($session);
217 $session = OpenSRF::AppSession->create("open-ils.storage");
218 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
219 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
220 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
221 $client->respond_complete($new_patron->ex);
226 warn "Patron Update/Create complete\n";
227 return flesh_user($new_patron->id());
233 __PACKAGE__->register_method(
234 method => "user_retrieve_fleshed_by_id",
235 api_name => "open-ils.actor.user.fleshed.retrieve",);
237 sub user_retrieve_fleshed_by_id {
238 my( $self, $client, $user_session, $user_id ) = @_;
239 my $user_obj = $apputils->check_user_session( $user_session );
241 if(!defined($user_id)) { $user_id = $user_obj->id; }
243 if( $user_obj->id ne $user_id ) {
244 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER")) {
245 return OpenILS::Perm->new("VIEW_USER");
249 return flesh_user($user_id);
260 $session = OpenSRF::AppSession->create("open-ils.storage");
264 # grab the user with the given id
265 my $ureq = $session->request(
266 "open-ils.storage.direct.actor.user.retrieve", $id);
267 my $user = $ureq->gather(1);
269 if(!$user) { return undef; }
272 my $cards_req = $session->request(
273 "open-ils.storage.direct.actor.card.search.usr.atomic",
275 $user->cards( $cards_req->gather(1) );
277 for my $c(@{$user->cards}) {
278 if($c->id == $user->card || $c->id eq $user->card ) {
279 warn "Setting my card to " . $c->id . "\n";
284 my $add_req = $session->request(
285 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
287 $user->addresses( $add_req->gather(1) );
289 for my $c(@{$user->addresses}) {
290 if($c->id == $user->billing_address || $c->id eq $user->billing_address ) {
291 warn "Setting my address to " . $c->id . "\n";
292 $user->billing_address($c);
296 for my $c(@{$user->addresses}) {
297 if($c->id == $user->mailing_address || $c->id eq $user->mailing_address ) {
298 warn "Setting my address to " . $c->id . "\n";
299 $user->mailing_address($c);
303 my $stat_req = $session->request(
304 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
306 $user->stat_cat_entries($stat_req->gather(1));
308 if($kill) { $session->disconnect(); }
309 $user->clear_passwd();
317 # clone and clear stuff that would break the database
321 my $new_patron = $patron->clone;
323 # Using the Fieldmapper clone method
324 #my $new_patron = Fieldmapper::actor::user->new();
326 #my $fmap = $Fieldmapper::fieldmap;
327 #no strict; # shallow clone, may be useful in the fieldmapper
329 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
330 # $new_patron->$field( $patron->$field() );
335 $new_patron->clear_billing_address();
336 $new_patron->clear_mailing_address();
337 $new_patron->clear_addresses();
338 $new_patron->clear_card();
339 $new_patron->clear_cards();
340 $new_patron->clear_id();
341 $new_patron->clear_isnew();
342 $new_patron->clear_ischanged();
343 $new_patron->clear_isdeleted();
344 $new_patron->clear_stat_cat_entries();
353 my $user_obj = shift;
356 if($apputils->check_user_perms(
357 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
358 return OpenILS::Perm->new("CREATE_USER");
361 warn "Creating new patron\n";
364 my $req = $session->request(
365 "open-ils.storage.direct.actor.user.create",$patron);
366 my $id = $req->gather(1);
368 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
371 # retrieve the patron from the db to collect defaults
372 my $ureq = $session->request(
373 "open-ils.storage.direct.actor.user.retrieve",
376 warn "Created new patron with id $id\n";
378 return $ureq->gather(1);
383 my( $session, $patron, $user_obj) = @_;
386 if($apputils->check_user_perms(
387 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
388 return OpenILS::Perm->new("UPDATE_USER");
391 warn "updating patron " . Dumper($patron) . "\n";
393 my $req = $session->request(
394 "open-ils.storage.direct.actor.user.update",$patron );
395 my $status = $req->gather(1);
396 if(!defined($status)) {
397 throw OpenSRF::EX::ERROR
398 ("Unknown error updating patron");
404 sub _add_update_addresses {
407 my $new_patron = shift;
409 my $current_id; # id of the address before creation
411 for my $address (@{$patron->addresses()}) {
413 $address->usr($new_patron->id());
415 if(ref($address) and $address->isnew()) {
416 warn "Adding new address at street " . $address->street1() . "\n";
418 $current_id = $address->id();
419 $address = _add_address($session,$address);
421 if( $patron->billing_address() and
422 $patron->billing_address() == $current_id ) {
423 $new_patron->billing_address($address->id());
424 $new_patron->ischanged(1);
427 if( $patron->mailing_address() and
428 $patron->mailing_address() == $current_id ) {
429 $new_patron->mailing_address($address->id());
430 $new_patron->ischanged(1);
433 } elsif( ref($address) and $address->ischanged() ) {
434 warn "Updating address at street " . $address->street1();
435 $address->usr($new_patron->id());
436 _update_address($session,$address);
438 } elsif( ref($address) and $address->isdeleted() ) {
439 warn "Deleting address at street " . $address->street1();
441 if( $address->id() == $new_patron->mailing_address() ) {
442 $new_patron->clear_mailing_address();
443 _update_patron($session, $new_patron);
446 if( $address->id() == $new_patron->billing_address() ) {
447 $new_patron->clear_billing_address();
448 _update_patron($session, $new_patron);
451 _delete_address($session,$address);
459 # adds an address to the db and returns the address with new id
461 my($session, $address) = @_;
462 $address->clear_id();
465 warn "Adding Address:\n";
466 warn Dumper($address);
468 # put the address into the database
469 my $req = $session->request(
470 "open-ils.storage.direct.actor.user_address.create",
474 my $id = $req->gather(1);
476 throw OpenSRF::EX::ERROR
477 ("Unable to create new user address");
480 warn "Created address with id $id\n";
482 # update all the necessary id's
488 sub _update_address {
489 my( $session, $address ) = @_;
490 my $req = $session->request(
491 "open-ils.storage.direct.actor.user_address.update",
493 my $status = $req->gather(1);
494 if(!defined($status)) {
495 throw OpenSRF::EX::ERROR
496 ("Unknown error updating address");
503 sub _add_update_cards {
507 my $new_patron = shift;
509 my $virtual_id; #id of the card before creation
510 for my $card (@{$patron->cards()}) {
512 $card->usr($new_patron->id());
514 if(ref($card) and $card->isnew()) {
516 $virtual_id = $card->id();
517 $card = _add_card($session,$card);
518 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
522 #if(ref($patron->card)) { $patron->card($patron->card->id); }
523 if($patron->card() == $virtual_id) {
524 $new_patron->card($card->id());
525 $new_patron->ischanged(1);
528 } elsif( ref($card) and $card->ischanged() ) {
529 $card->usr($new_patron->id());
530 _update_card($session, $card);
537 # adds an card to the db and returns the card with new id
539 my( $session, $card ) = @_;
542 warn "Adding card with barcode " . $card->barcode() . "\n";
543 my $req = $session->request(
544 "open-ils.storage.direct.actor.card.create",
547 my $id = $req->gather(1);
549 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
553 warn "Created patron card with id $id\n";
559 my( $session, $card ) = @_;
562 my $req = $session->request(
563 "open-ils.storage.direct.actor.card.update",
565 my $status = $req->gather(1);
566 if(!defined($status)) {
567 throw OpenSRF::EX::ERROR
568 ("Unknown error updating card");
576 sub _delete_address {
577 my( $session, $address ) = @_;
579 warn "Deleting address " . $address->street1() . "\n";
581 my $req = $session->request(
582 "open-ils.storage.direct.actor.user_address.delete",
584 my $status = $req->gather(1);
585 if(!defined($status)) {
586 throw OpenSRF::EX::ERROR
587 ("Unknown error updating address");
589 warn "Delete address status is $status\n";
594 sub _add_survey_responses {
595 my ($session, $patron, $new_patron) = @_;
597 warn "updating responses for user " . $new_patron->id . "\n";
599 my $responses = $patron->survey_responses;
603 for my $resp( @$responses ) {
604 $resp->usr($new_patron->id);
607 my $status = $apputils->simple_scalar_request(
609 "open-ils.circ.survey.submit.user_id",
618 sub _create_stat_maps {
620 my($session, $user_session, $patron, $new_patron) = @_;
622 my $maps = $patron->stat_cat_entries();
624 for my $map (@$maps) {
626 next unless($map->isnew() || $map->ischanged());
628 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
630 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
634 $map->target_usr($new_patron->id);
636 warn "Updating stat entry with method $method and session $user_session and map $map\n";
638 my $req = $session->request($method, $map);
639 my $status = $req->gather(1);
644 throw OpenSRF::EX::ERROR
645 ("Error updating stat map with method $method");
655 __PACKAGE__->register_method(
656 method => "search_username",
657 api_name => "open-ils.actor.user.search.username",
660 sub search_username {
661 my($self, $client, $username) = @_;
662 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
664 "open-ils.storage.direct.actor.user.search.usrname.atomic",
672 __PACKAGE__->register_method(
673 method => "user_retrieve_by_barcode",
674 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
676 sub user_retrieve_by_barcode {
677 my($self, $client, $user_session, $barcode) = @_;
678 warn "Searching for user with barcode $barcode\n";
679 my $user_obj = $apputils->check_user_session( $user_session );
681 my $session = OpenSRF::AppSession->create("open-ils.storage");
683 # find the card with the given barcode
684 my $creq = $session->request(
685 "open-ils.storage.direct.actor.card.search.barcode.atomic",
687 my $card = $creq->gather(1);
689 if(!$card || !$card->[0]) {
690 $session->disconnect();
695 my $user = flesh_user($card->usr(), $session);
696 $session->disconnect();
703 __PACKAGE__->register_method(
704 method => "get_user_by_id",
705 api_name => "open-ils.actor.user.retrieve",);
708 my ($self, $client, $user_session, $id) = @_;
710 my $user_obj = $apputils->check_user_session( $user_session );
712 return $apputils->simple_scalar_request(
714 "open-ils.storage.direct.actor.user.retrieve",
720 __PACKAGE__->register_method(
721 method => "get_org_types",
722 api_name => "open-ils.actor.org_types.retrieve",);
726 my($self, $client) = @_;
728 return $org_types if $org_types;
730 $apputils->simple_scalar_request(
732 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
737 __PACKAGE__->register_method(
738 method => "get_user_profiles",
739 api_name => "open-ils.actor.user.profiles.retrieve",
743 sub get_user_profiles {
744 return $user_profiles if $user_profiles;
746 return $user_profiles =
747 $apputils->simple_scalar_request(
749 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
754 __PACKAGE__->register_method(
755 method => "get_user_ident_types",
756 api_name => "open-ils.actor.user.ident_types.retrieve",
759 sub get_user_ident_types {
760 return $ident_types if $ident_types;
761 return $ident_types =
762 $apputils->simple_scalar_request(
764 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
770 __PACKAGE__->register_method(
771 method => "get_org_unit",
772 api_name => "open-ils.actor.org_unit.retrieve",
777 my( $self, $client, $user_session, $org_id ) = @_;
779 if(defined($user_session) && !defined($org_id)) {
781 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
782 if(!defined($org_id)) {
783 $org_id = $user_obj->home_ou;
788 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
790 "open-ils.storage.direct.actor.org_unit.retrieve",
799 __PACKAGE__->register_method(
800 method => "get_org_tree",
801 api_name => "open-ils.actor.org_tree.retrieve",
803 note => "Returns the entire org tree structure",
807 my( $self, $client) = @_;
810 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
812 # see if it's in the cache
813 warn "Getting ORG Tree\n";
814 my $tree = $cache_client->get_cache('orgtree');
816 warn "Found orgtree in cache. returning...\n";
820 my $orglist = $apputils->simple_scalar_request(
822 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
825 warn "found org list\n";
828 $tree = $self->build_org_tree($orglist);
829 $cache_client->put_cache('orgtree', $tree);
835 # turns an org list into an org tree
838 my( $self, $orglist) = @_;
840 return $orglist unless (
841 ref($orglist) and @$orglist > 1 );
844 $a->ou_type <=> $b->ou_type ||
845 $a->name cmp $b->name } @$orglist;
847 for my $org (@list) {
849 next unless ($org and defined($org->parent_ou));
850 my ($parent) = grep { $_->id == $org->parent_ou } @list;
853 $parent->children([]) unless defined($parent->children);
854 push( @{$parent->children}, $org );
862 __PACKAGE__->register_method(
863 method => "get_org_descendants",
864 api_name => "open-ils.actor.org_tree.descendants.retrieve"
867 # depth is optional. org_unit is the id
868 sub get_org_descendants {
869 my( $self, $client, $org_unit, $depth ) = @_;
870 my $orglist = $apputils->simple_scalar_request(
872 "open-ils.storage.actor.org_unit.descendants.atomic",
874 return $self->build_org_tree($orglist);
878 __PACKAGE__->register_method(
879 method => "get_org_ancestors",
880 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
883 # depth is optional. org_unit is the id
884 sub get_org_ancestors {
885 my( $self, $client, $org_unit, $depth ) = @_;
886 my $orglist = $apputils->simple_scalar_request(
888 "open-ils.storage.actor.org_unit.ancestors.atomic",
890 return $self->build_org_tree($orglist);
894 __PACKAGE__->register_method(
895 method => "get_standings",
896 api_name => "open-ils.actor.standings.retrieve"
901 return $user_standings if $user_standings;
902 return $user_standings =
903 $apputils->simple_scalar_request(
905 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
910 __PACKAGE__->register_method(
911 method => "get_my_org_path",
912 api_name => "open-ils.actor.org_unit.full_path.retrieve"
915 sub get_my_org_path {
916 my( $self, $client, $user_session, $org_id ) = @_;
917 my $user_obj = $apputils->check_user_session($user_session);
918 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
920 return $apputils->simple_scalar_request(
922 "open-ils.storage.actor.org_unit.full_path.atomic",
927 __PACKAGE__->register_method(
928 method => "patron_adv_search",
929 api_name => "open-ils.actor.patron.search.advanced" );
931 sub patron_adv_search {
932 my( $self, $client, $staff_login, $search_hash ) = @_;
935 warn "patron adv with $staff_login and search " .
936 Dumper($search_hash) . "\n";
938 my $session = OpenSRF::AppSession->create("open-ils.storage");
939 my $req = $session->request(
940 "open-ils.storage.actor.user.crazy_search", $search_hash);
942 my $ans = $req->gather(1);
944 my %hash = map { ($_ =>1) } @$ans;
945 $ans = [ keys %hash ];
947 warn "Returning @$ans\n";
949 $session->disconnect();
956 sub _verify_password {
957 my($user_session, $password) = @_;
958 my $user_obj = $apputils->check_user_session($user_session);
960 #grab the user with password
961 $user_obj = $apputils->simple_scalar_request(
963 "open-ils.storage.direct.actor.user.retrieve",
966 if($user_obj->passwd eq $password) {
974 __PACKAGE__->register_method(
975 method => "update_password",
976 api_name => "open-ils.actor.user.password.update");
978 __PACKAGE__->register_method(
979 method => "update_password",
980 api_name => "open-ils.actor.user.username.update");
982 __PACKAGE__->register_method(
983 method => "update_password",
984 api_name => "open-ils.actor.user.email.update");
986 sub update_password {
987 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
989 warn "Updating user with method " .$self->api_name . "\n";
990 my $user_obj = $apputils->check_user_session($user_session);
992 if($self->api_name =~ /password/) {
994 #make sure they know the current password
995 if(!_verify_password($user_session, md5_hex($current_password))) {
996 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
999 $user_obj->passwd($new_value);
1001 elsif($self->api_name =~ /username/) {
1002 $user_obj->usrname($new_value);
1005 elsif($self->api_name =~ /email/) {
1006 warn "Updating email to $new_value\n";
1007 $user_obj->email($new_value);
1010 my $session = $apputils->start_db_session();
1011 $user_obj = _update_patron($session, $user_obj, $user_obj);
1012 $apputils->commit_db_session($session);
1014 if($user_obj) { return 1; }
1019 __PACKAGE__->register_method(
1020 method => "check_user_perms",
1021 api_name => "open-ils.actor.user.perm.check",
1022 notes => <<" NOTES");
1023 Takes a login session, user id, an org id, and an array of perm type strings. For each
1024 perm type, if the user does *not* have the given permission it is added
1025 to a list which is returned from the method. If all permissions
1026 are allowed, an empty list is returned
1027 if the logged in user does not match 'user_id', then the logged in user must
1028 have VIEW_PERMISSION priveleges.
1031 sub check_user_perms {
1032 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1033 my $user_obj = $apputils->check_user_session($login_session);
1035 if($user_obj->id ne $user_id) {
1036 if($apputils->check_user_perms($user_obj->id, $org_id, "VIEW_PERMISSION")) {
1037 return OpenILS::Perm->new("VIEW_PERMISSION");
1042 for my $perm (@$perm_types) {
1043 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
1044 push @not_allowed, $perm;
1048 return \@not_allowed
1053 __PACKAGE__->register_method(
1054 method => "user_fines_summary",
1055 api_name => "open-ils.actor.user.fines.summary",
1056 notes => <<" NOTES");
1057 Returns a short summary of the users total open fines, excluding voided fines
1058 Params are login_session, user_id
1059 Returns a 'mus' object.
1062 sub user_fines_summary {
1063 my( $self, $client, $login_session, $user_id ) = @_;
1065 my $user_obj = $apputils->check_user_session($login_session);
1066 if($user_obj->id ne $user_id) {
1067 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1068 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1072 return $apputils->simple_scalar_request(
1074 "open-ils.storage.direct.money.user_summary.search.usr",
1082 __PACKAGE__->register_method(
1083 method => "user_transactions",
1084 api_name => "open-ils.actor.user.transactions",
1085 notes => <<" NOTES");
1086 Returns a list of open user transactions (mbts objects);
1087 Params are login_session, user_id
1088 Optional third parameter is the transactions type. defaults to all
1091 __PACKAGE__->register_method(
1092 method => "user_transactions",
1093 api_name => "open-ils.actor.user.transactions.have_charge",
1094 notes => <<" NOTES");
1095 Returns a list of all open user transactions (mbts objects) that have an initial charge
1096 Params are login_session, user_id
1097 Optional third parameter is the transactions type. defaults to all
1100 __PACKAGE__->register_method(
1101 method => "user_transactions",
1102 api_name => "open-ils.actor.user.transactions.have_balance",
1103 notes => <<" NOTES");
1104 Returns a list of all open user transactions (mbts objects) that have a balance
1105 Params are login_session, user_id
1106 Optional third parameter is the transactions type. defaults to all
1109 __PACKAGE__->register_method(
1110 method => "user_transactions",
1111 api_name => "open-ils.actor.user.transactions.fleshed",
1112 notes => <<" NOTES");
1113 Returns an object/hash of transaction, circ, title where transaction = an open
1114 user transactions (mbts objects), circ is the attached circluation, and title
1115 is the title the circ points to
1116 Params are login_session, user_id
1117 Optional third parameter is the transactions type. defaults to all
1120 __PACKAGE__->register_method(
1121 method => "user_transactions",
1122 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1123 notes => <<" NOTES");
1124 Returns an object/hash of transaction, circ, title where transaction = an open
1125 user transactions that has an initial charge (mbts objects), circ is the
1126 attached circluation, and title is the title the circ points to
1127 Params are login_session, user_id
1128 Optional third parameter is the transactions type. defaults to all
1131 __PACKAGE__->register_method(
1132 method => "user_transactions",
1133 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1134 notes => <<" NOTES");
1135 Returns an object/hash of transaction, circ, title where transaction = an open
1136 user transaction that has a balance (mbts objects), circ is the attached
1137 circluation, and title is the title the circ points to
1138 Params are login_session, user_id
1139 Optional third parameter is the transaction type. defaults to all
1144 sub user_transactions {
1145 my( $self, $client, $login_session, $user_id, $type ) = @_;
1147 my $user_obj = $apputils->check_user_session($login_session);
1148 if($user_obj->id ne $user_id) {
1149 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_TRANSACTIONS")) {
1150 return OpenILS::Perm->new("VIEW_USER_TRANSACTIONS");
1154 my $api = $self->api_name();
1157 if(defined($type)) { @xact = (xact_type => $type);
1158 } else { @xact = (); }
1160 if($api =~ /have_charge/) {
1162 $trans = $apputils->simple_scalar_request(
1164 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1165 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1167 } elsif($api =~ /have_balance/) {
1169 $trans = $apputils->simple_scalar_request(
1171 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1172 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1176 $trans = $apputils->simple_scalar_request(
1178 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1179 { usr => $user_id, @xact });
1182 if($api !~ /fleshed/) { return $trans; }
1187 for my $t (@$trans) {
1191 my $circ = $apputils->simple_scalar_request(
1193 "open-ils.storage.direct.action.circulation.retrieve",
1198 my $title = $apputils->simple_scalar_request(
1200 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1201 $circ->target_copy );
1205 my $u = OpenILS::Utils::ModsParser->new();
1206 $u->start_mods_batch($title->marc());
1207 my $mods = $u->finish_mods_batch();
1209 push @resp, {transaction => $t, circ => $circ, record => $mods };
1219 __PACKAGE__->register_method(
1220 method => "retrieve_groups",
1221 api_name => "open-ils.actor.groups.retrieve",
1222 notes => <<" NOTES");
1223 Returns a list of user groups
1225 sub retrieve_groups {
1226 my( $self, $client ) = @_;
1227 return $apputils->simple_scalar_request(
1229 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1283 some old methods that may be good to keep around for now
1286 my( $session, $card ) = @_;
1288 warn "Deleting card with barcode " . $card->barcode() . "\n";
1289 my $req = $session->request(
1290 "open-ils.storage.direct.actor.card.delete",
1292 my $status = $req->gather(1);
1293 if(!defined($status)) {
1294 throw OpenSRF::EX::ERROR
1295 ("Unknown error updating card");
1301 # deletes the patron and any attached addresses and cards
1302 __PACKAGE__->register_method(
1303 method => "delete_patron",
1304 api_name => "open-ils.actor.patron.delete",
1309 my( $self, $client, $patron ) = @_;
1310 my $session = $apputils->start_db_session();
1315 $patron->clear_mailing_address();
1316 $patron->clear_billing_address();
1317 $patron->ischanged(1);
1319 _update_patron($session, $patron);
1320 _delete_address($session,$_) for (@{$patron->addresses()});
1321 _delete_card($session,$_) for (@{$patron->cards()});
1322 _delete_patron($session,$patron);
1323 $apputils->commit_db_session($session);
1325 } catch Error with {
1327 $err = "-*- Failure deleting user: $e";
1328 $apputils->rollback_db_session($session);
1332 if($err) { throw OpenSRF::EX::ERROR ($err); }
1333 warn "Patron Delete complete\n";
1337 sub _delete_patron {
1338 my( $session, $patron ) = @_;
1340 warn "Deleting patron " . $patron->usrname() . "\n";
1342 my $req = $session->request(
1343 "open-ils.storage.direct.actor.user.delete",
1345 my $status = $req->gather(1);
1346 if(!defined($status)) {
1347 throw OpenSRF::EX::ERROR
1348 ("Unknown error updating patron");