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 $logger->debug("Setting user settings: $user_session, $uid, " . Dumper($settings));
47 my( $staff, $user, $evt ) =
48 $apputils->checkses_requestor( $user_session, $uid, 'UPDATE_USER' );
53 [{ usr => $user->id, name => $_}, {value => $$settings{$_}}] } keys %$settings;
55 $logger->activity("User " . $staff->id . " updating user $uid settings with: " . Dumper($params));
57 return $apputils->simplereq(
59 'open-ils.storage.direct.actor.user_setting.batch.merge', $params );
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 my( $staff, $evt ) = $apputils->checkses( $user_session );
74 $evt = $apputils->check_perms( $staff->id, $ouid, 'UPDATE_ORG_UNIT' );
79 map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings;
81 $logger->activity("Updating org unit [$ouid] settings with: " . Dumper($params));
83 return $apputils->simplereq(
85 'open-ils.storage.direct.actor.org_unit_setting.merge', @$params );
89 my $fetch_user_settings;
90 my $fetch_ou_settings;
92 __PACKAGE__->register_method(
93 method => "user_settings",
94 api_name => "open-ils.actor.patron.settings.retrieve",
97 my( $self, $client, $user_session, $uid ) = @_;
99 my( $staff, $user, $evt ) =
100 $apputils->checkses_requestor( $user_session, $uid, 'VIEW_USER' );
103 $logger->debug("User " . $staff->id . " fetching user $uid\n");
104 my $s = $apputils->simplereq(
106 'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
108 return { map { ($_->name,$_->value) } @$s };
113 __PACKAGE__->register_method(
114 method => "ou_settings",
115 api_name => "open-ils.actor.org_unit.settings.retrieve",
118 my( $self, $client, $ouid ) = @_;
120 my $s = $apputils->simplereq(
122 'open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic', $ouid);
124 return { map { ($_->name,$_->value) } @$s };
129 __PACKAGE__->register_method(
130 method => "update_patron",
131 api_name => "open-ils.actor.patron.update",);
134 my( $self, $client, $user_session, $patron ) = @_;
136 my $session = $apputils->start_db_session();
139 warn $user_session . " " . $patron . "\n";
143 OpenILS::Application::AppUtils->check_user_session(
144 $user_session ); #throws EX on error
146 # XXX does this user have permission to add/create users. Granularity?
147 # $new_patron is the patron in progress. $patron is the original patron
148 # passed in with the method. new_patron will change as the components
149 # of patron are added/updated.
153 if(ref($patron->card)) { $patron->card( $patron->card->id ); }
154 if(ref($patron->billing_address)) { $patron->billing_address( $patron->billing_address->id ); }
155 if(ref($patron->mailing_address)) { $patron->mailing_address( $patron->mailing_address->id ); }
157 # create/update the patron first so we can use his id
158 if($patron->isnew()) {
160 $new_patron = _add_patron($session, _clone_patron($patron), $user_obj);
162 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
163 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
164 $client->respond_complete($new_patron->ex);
168 } else { $new_patron = $patron; }
170 $new_patron = _add_update_addresses($session, $patron, $new_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 $new_patron = _add_update_cards($session, $patron, $new_patron, $user_obj);
180 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
181 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
182 $client->respond_complete($new_patron->ex);
186 $new_patron = _add_survey_responses($session, $patron, $new_patron, $user_obj);
187 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
188 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
189 $client->respond_complete($new_patron->ex);
194 # re-update the patron if anything has happened to him during this process
195 if($new_patron->ischanged()) {
196 $new_patron = _update_patron($session, $new_patron, $user_obj);
198 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
199 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
200 $client->respond_complete($new_patron->ex);
205 $apputils->commit_db_session($session);
207 $session = OpenSRF::AppSession->create("open-ils.storage");
208 $new_patron = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
209 if(UNIVERSAL::isa($new_patron, "OpenILS::EX") ||
210 UNIVERSAL::isa($new_patron, "OpenILS::Perm")) {
211 $client->respond_complete($new_patron->ex);
216 warn "Patron Update/Create complete\n";
217 return flesh_user($new_patron->id());
223 __PACKAGE__->register_method(
224 method => "user_retrieve_fleshed_by_id",
225 api_name => "open-ils.actor.user.fleshed.retrieve",);
227 sub user_retrieve_fleshed_by_id {
228 my( $self, $client, $user_session, $user_id ) = @_;
230 my( $requestor, $target, $evt ) = $apputils->
231 checkses_requestor( $user_session, $user_id, 'VIEW_USER' );
234 return flesh_user($user_id);
245 $session = OpenSRF::AppSession->create("open-ils.storage");
249 # grab the user with the given id
250 my $ureq = $session->request(
251 "open-ils.storage.direct.actor.user.retrieve", $id);
252 my $user = $ureq->gather(1);
254 if(!$user) { return undef; }
257 my $cards_req = $session->request(
258 "open-ils.storage.direct.actor.card.search.usr.atomic",
260 $user->cards( $cards_req->gather(1) );
262 for my $c(@{$user->cards}) {
263 if($c->id == $user->card || $c->id eq $user->card ) {
264 warn "Setting my card to " . $c->id . "\n";
269 my $add_req = $session->request(
270 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
272 $user->addresses( $add_req->gather(1) );
274 for my $c(@{$user->addresses}) {
275 if($c->id eq $user->billing_address ) { $user->billing_address($c); }
276 if($c->id eq $user->mailing_address ) { $user->mailing_address($c); }
279 my $stat_req = $session->request(
280 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
282 $user->stat_cat_entries($stat_req->gather(1));
284 if($kill) { $session->disconnect(); }
285 $user->clear_passwd();
291 # clone and clear stuff that would break the database
295 my $new_patron = $patron->clone;
297 # Using the Fieldmapper clone method
298 #my $new_patron = Fieldmapper::actor::user->new();
300 #my $fmap = $Fieldmapper::fieldmap;
301 #no strict; # shallow clone, may be useful in the fieldmapper
303 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
304 # $new_patron->$field( $patron->$field() );
309 $new_patron->clear_billing_address();
310 $new_patron->clear_mailing_address();
311 $new_patron->clear_addresses();
312 $new_patron->clear_card();
313 $new_patron->clear_cards();
314 $new_patron->clear_id();
315 $new_patron->clear_isnew();
316 $new_patron->clear_ischanged();
317 $new_patron->clear_isdeleted();
318 $new_patron->clear_stat_cat_entries();
327 my $user_obj = shift;
330 if($apputils->check_user_perms(
331 $user_obj->id, $user_obj->home_ou, "CREATE_USER")) {
332 return OpenILS::Perm->new("CREATE_USER");
335 warn "Creating new patron\n";
338 my $req = $session->request(
339 "open-ils.storage.direct.actor.user.create",$patron);
340 my $id = $req->gather(1);
342 return OpenILS::EX->new("DUPLICATE_USER_USERNAME");
345 # retrieve the patron from the db to collect defaults
346 my $ureq = $session->request(
347 "open-ils.storage.direct.actor.user.retrieve",
350 warn "Created new patron with id $id\n";
352 return $ureq->gather(1);
357 my( $session, $patron, $user_obj) = @_;
360 if($patron->id ne $user_obj->id) {
361 if($apputils->check_user_perms(
362 $user_obj->id, $user_obj->home_ou, "UPDATE_USER")) {
363 return OpenILS::Perm->new("UPDATE_USER");
367 warn "updating patron " . Dumper($patron) . "\n";
369 my $req = $session->request(
370 "open-ils.storage.direct.actor.user.update",$patron );
371 my $status = $req->gather(1);
372 if(!defined($status)) {
373 throw OpenSRF::EX::ERROR
374 ("Unknown error updating patron");
380 sub _add_update_addresses {
383 my $new_patron = shift;
385 my $current_id; # id of the address before creation
387 for my $address (@{$patron->addresses()}) {
389 $address->usr($new_patron->id());
391 if(ref($address) and $address->isnew()) {
392 warn "Adding new address at street " . $address->street1() . "\n";
394 $current_id = $address->id();
395 $address = _add_address($session,$address);
397 if( $patron->billing_address() and
398 $patron->billing_address() == $current_id ) {
399 $new_patron->billing_address($address->id());
400 $new_patron->ischanged(1);
403 if( $patron->mailing_address() and
404 $patron->mailing_address() == $current_id ) {
405 $new_patron->mailing_address($address->id());
406 $new_patron->ischanged(1);
409 } elsif( ref($address) and $address->ischanged() ) {
410 warn "Updating address at street " . $address->street1();
411 $address->usr($new_patron->id());
412 _update_address($session,$address);
414 } elsif( ref($address) and $address->isdeleted() ) {
415 warn "Deleting address at street " . $address->street1();
417 if( $address->id() == $new_patron->mailing_address() ) {
418 $new_patron->clear_mailing_address();
419 _update_patron($session, $new_patron);
422 if( $address->id() == $new_patron->billing_address() ) {
423 $new_patron->clear_billing_address();
424 _update_patron($session, $new_patron);
427 _delete_address($session,$address);
435 # adds an address to the db and returns the address with new id
437 my($session, $address) = @_;
438 $address->clear_id();
441 warn "Adding Address:\n";
442 warn Dumper($address);
444 # put the address into the database
445 my $req = $session->request(
446 "open-ils.storage.direct.actor.user_address.create",
450 my $id = $req->gather(1);
452 throw OpenSRF::EX::ERROR
453 ("Unable to create new user address");
456 warn "Created address with id $id\n";
458 # update all the necessary id's
464 sub _update_address {
465 my( $session, $address ) = @_;
466 my $req = $session->request(
467 "open-ils.storage.direct.actor.user_address.update",
469 my $status = $req->gather(1);
470 if(!defined($status)) {
471 throw OpenSRF::EX::ERROR
472 ("Unknown error updating address");
479 sub _add_update_cards {
483 my $new_patron = shift;
485 my $virtual_id; #id of the card before creation
486 for my $card (@{$patron->cards()}) {
488 $card->usr($new_patron->id());
490 if(ref($card) and $card->isnew()) {
492 $virtual_id = $card->id();
493 $card = _add_card($session,$card);
494 if(UNIVERSAL::isa($card,"OpenILS::EX")) {
498 #if(ref($patron->card)) { $patron->card($patron->card->id); }
499 if($patron->card() == $virtual_id) {
500 $new_patron->card($card->id());
501 $new_patron->ischanged(1);
504 } elsif( ref($card) and $card->ischanged() ) {
505 $card->usr($new_patron->id());
506 _update_card($session, $card);
513 # adds an card to the db and returns the card with new id
515 my( $session, $card ) = @_;
518 warn "Adding card with barcode " . $card->barcode() . "\n";
519 my $req = $session->request(
520 "open-ils.storage.direct.actor.card.create",
523 my $id = $req->gather(1);
525 return OpenILS::EX->new("DUPLICATE_INVALID_USER_BARCODE");
529 warn "Created patron card with id $id\n";
535 my( $session, $card ) = @_;
538 my $req = $session->request(
539 "open-ils.storage.direct.actor.card.update",
541 my $status = $req->gather(1);
542 if(!defined($status)) {
543 throw OpenSRF::EX::ERROR
544 ("Unknown error updating card");
552 sub _delete_address {
553 my( $session, $address ) = @_;
555 warn "Deleting address " . $address->street1() . "\n";
557 my $req = $session->request(
558 "open-ils.storage.direct.actor.user_address.delete",
560 my $status = $req->gather(1);
561 if(!defined($status)) {
562 throw OpenSRF::EX::ERROR
563 ("Unknown error updating address");
565 warn "Delete address status is $status\n";
570 sub _add_survey_responses {
571 my ($session, $patron, $new_patron) = @_;
573 warn "updating responses for user " . $new_patron->id . "\n";
575 my $responses = $patron->survey_responses;
579 for my $resp( @$responses ) {
580 $resp->usr($new_patron->id);
583 my $status = $apputils->simple_scalar_request(
585 "open-ils.circ.survey.submit.user_id",
594 sub _create_stat_maps {
596 my($session, $user_session, $patron, $new_patron) = @_;
598 my $maps = $patron->stat_cat_entries();
600 for my $map (@$maps) {
602 next unless($map->isnew() || $map->ischanged());
604 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
606 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
610 $map->target_usr($new_patron->id);
612 warn "Updating stat entry with method $method and session $user_session and map $map\n";
614 my $req = $session->request($method, $map);
615 my $status = $req->gather(1);
620 throw OpenSRF::EX::ERROR
621 ("Error updating stat map with method $method");
631 __PACKAGE__->register_method(
632 method => "search_username",
633 api_name => "open-ils.actor.user.search.username",
636 sub search_username {
637 my($self, $client, $username) = @_;
638 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
640 "open-ils.storage.direct.actor.user.search.usrname.atomic",
648 __PACKAGE__->register_method(
649 method => "user_retrieve_by_barcode",
650 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
652 sub user_retrieve_by_barcode {
653 my($self, $client, $user_session, $barcode) = @_;
655 $logger->debug("Searching for user with barcode $barcode");
656 #my $user_obj = $apputils->check_user_session( $user_session );
657 my ($user_obj, $evt) = $apputils->check_ses($user_session);
660 my $session = OpenSRF::AppSession->create("open-ils.storage");
662 # find the card with the given barcode
663 my $creq = $session->request(
664 "open-ils.storage.direct.actor.card.search.barcode.atomic",
666 my $card = $creq->gather(1);
668 if(!$card || !$card->[0]) {
669 $session->disconnect();
670 return OpenILS::Event->new( 'USER_NOT_FOUND' );
674 my $user = flesh_user($card->usr(), $session);
675 $session->disconnect();
676 if(!$user) { return OpenILS::Event->new( 'USER_NOT_FOUND' ); }
683 __PACKAGE__->register_method(
684 method => "get_user_by_id",
685 api_name => "open-ils.actor.user.retrieve",);
688 my ($self, $client, $user_session, $id) = @_;
690 my $user_obj = $apputils->check_user_session( $user_session );
692 return $apputils->simple_scalar_request(
694 "open-ils.storage.direct.actor.user.retrieve",
700 __PACKAGE__->register_method(
701 method => "get_org_types",
702 api_name => "open-ils.actor.org_types.retrieve",);
706 my($self, $client) = @_;
708 return $org_types if $org_types;
710 $apputils->simple_scalar_request(
712 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
717 __PACKAGE__->register_method(
718 method => "get_user_profiles",
719 api_name => "open-ils.actor.user.profiles.retrieve",
723 sub get_user_profiles {
724 return $user_profiles if $user_profiles;
726 return $user_profiles =
727 $apputils->simple_scalar_request(
729 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
734 __PACKAGE__->register_method(
735 method => "get_user_ident_types",
736 api_name => "open-ils.actor.user.ident_types.retrieve",
739 sub get_user_ident_types {
740 return $ident_types if $ident_types;
741 return $ident_types =
742 $apputils->simple_scalar_request(
744 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
750 __PACKAGE__->register_method(
751 method => "get_org_unit",
752 api_name => "open-ils.actor.org_unit.retrieve",
757 my( $self, $client, $user_session, $org_id ) = @_;
759 if(defined($user_session) && !defined($org_id)) {
761 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
762 if(!defined($org_id)) {
763 $org_id = $user_obj->home_ou;
768 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
770 "open-ils.storage.direct.actor.org_unit.retrieve",
779 __PACKAGE__->register_method(
780 method => "get_org_tree",
781 api_name => "open-ils.actor.org_tree.retrieve",
783 note => "Returns the entire org tree structure",
787 my( $self, $client) = @_;
790 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
792 # see if it's in the cache
793 warn "Getting ORG Tree\n";
794 my $tree = $cache_client->get_cache('orgtree');
796 warn "Found orgtree in cache. returning...\n";
800 my $orglist = $apputils->simple_scalar_request(
802 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
805 warn "found org list\n";
808 $tree = $self->build_org_tree($orglist);
809 $cache_client->put_cache('orgtree', $tree);
815 # turns an org list into an org tree
818 my( $self, $orglist) = @_;
820 return $orglist unless (
821 ref($orglist) and @$orglist > 1 );
824 $a->ou_type <=> $b->ou_type ||
825 $a->name cmp $b->name } @$orglist;
827 for my $org (@list) {
829 next unless ($org and defined($org->parent_ou));
830 my ($parent) = grep { $_->id == $org->parent_ou } @list;
833 $parent->children([]) unless defined($parent->children);
834 push( @{$parent->children}, $org );
842 __PACKAGE__->register_method(
843 method => "get_org_descendants",
844 api_name => "open-ils.actor.org_tree.descendants.retrieve"
847 # depth is optional. org_unit is the id
848 sub get_org_descendants {
849 my( $self, $client, $org_unit, $depth ) = @_;
850 my $orglist = $apputils->simple_scalar_request(
852 "open-ils.storage.actor.org_unit.descendants.atomic",
854 return $self->build_org_tree($orglist);
858 __PACKAGE__->register_method(
859 method => "get_org_ancestors",
860 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
863 # depth is optional. org_unit is the id
864 sub get_org_ancestors {
865 my( $self, $client, $org_unit, $depth ) = @_;
866 my $orglist = $apputils->simple_scalar_request(
868 "open-ils.storage.actor.org_unit.ancestors.atomic",
870 return $self->build_org_tree($orglist);
874 __PACKAGE__->register_method(
875 method => "get_standings",
876 api_name => "open-ils.actor.standings.retrieve"
881 return $user_standings if $user_standings;
882 return $user_standings =
883 $apputils->simple_scalar_request(
885 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
890 __PACKAGE__->register_method(
891 method => "get_my_org_path",
892 api_name => "open-ils.actor.org_unit.full_path.retrieve"
895 sub get_my_org_path {
896 my( $self, $client, $user_session, $org_id ) = @_;
897 my $user_obj = $apputils->check_user_session($user_session);
898 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
900 return $apputils->simple_scalar_request(
902 "open-ils.storage.actor.org_unit.full_path.atomic",
907 __PACKAGE__->register_method(
908 method => "patron_adv_search",
909 api_name => "open-ils.actor.patron.search.advanced" );
911 sub patron_adv_search {
912 my( $self, $client, $staff_login, $search_hash ) = @_;
915 warn "patron adv with $staff_login and search " .
916 Dumper($search_hash) . "\n";
918 my $session = OpenSRF::AppSession->create("open-ils.storage");
919 my $req = $session->request(
920 "open-ils.storage.actor.user.crazy_search", $search_hash);
922 my $ans = $req->gather(1);
924 my %hash = map { ($_ =>1) } @$ans;
925 $ans = [ keys %hash ];
927 warn "Returning @$ans\n";
929 $session->disconnect();
936 sub _verify_password {
937 my($user_session, $password) = @_;
938 my $user_obj = $apputils->check_user_session($user_session);
940 #grab the user with password
941 $user_obj = $apputils->simple_scalar_request(
943 "open-ils.storage.direct.actor.user.retrieve",
946 if($user_obj->passwd eq $password) {
954 __PACKAGE__->register_method(
955 method => "update_password",
956 api_name => "open-ils.actor.user.password.update");
958 __PACKAGE__->register_method(
959 method => "update_password",
960 api_name => "open-ils.actor.user.username.update");
962 __PACKAGE__->register_method(
963 method => "update_password",
964 api_name => "open-ils.actor.user.email.update");
966 sub update_password {
967 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
969 warn "Updating user with method " .$self->api_name . "\n";
970 my $user_obj = $apputils->check_user_session($user_session);
972 if($self->api_name =~ /password/) {
974 #make sure they know the current password
975 if(!_verify_password($user_session, md5_hex($current_password))) {
976 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
979 $user_obj->passwd($new_value);
981 elsif($self->api_name =~ /username/) {
982 my $users = search_username(undef, undef, $new_value);
983 if( $users and $users->[0] ) {
984 return OpenILS::Event->new('USERNAME_EXISTS');
986 $user_obj->usrname($new_value);
989 elsif($self->api_name =~ /email/) {
990 warn "Updating email to $new_value\n";
991 $user_obj->email($new_value);
994 my $session = $apputils->start_db_session();
995 $user_obj = _update_patron($session, $user_obj, $user_obj);
996 $apputils->commit_db_session($session);
998 if($user_obj) { return 1; }
1003 __PACKAGE__->register_method(
1004 method => "check_user_perms",
1005 api_name => "open-ils.actor.user.perm.check",
1006 notes => <<" NOTES");
1007 Takes a login session, user id, an org id, and an array of perm type strings. For each
1008 perm type, if the user does *not* have the given permission it is added
1009 to a list which is returned from the method. If all permissions
1010 are allowed, an empty list is returned
1011 if the logged in user does not match 'user_id', then the logged in user must
1012 have VIEW_PERMISSION priveleges.
1015 sub check_user_perms {
1016 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1018 my( $staff, $evt ) = $apputils->checkses($login_session);
1019 return $evt if $evt;
1021 if($staff->id ne $user_id) {
1022 if( my $evt = $apputils->check_perms(
1023 $staff->id, $org_id, 'VIEW_PERMISSION') ) {
1029 for my $perm (@$perm_types) {
1030 if($apputils->check_user_perms($user_id, $org_id, $perm)) {
1031 push @not_allowed, $perm;
1035 return \@not_allowed
1038 __PACKAGE__->register_method(
1039 method => "check_user_perms2",
1040 api_name => "open-ils.actor.user.perm.check.multi_org",
1042 Checks the permissions on a list of perms and orgs for a user
1043 @param authtoken The login session key
1044 @param user_id The id of the user to check
1045 @param orgs The array of org ids
1046 @param perms The array of permission names
1047 @return An array of [ orgId, permissionName ] arrays that FAILED the check
1048 if the logged in user does not match 'user_id', then the logged in user must
1049 have VIEW_PERMISSION priveleges.
1052 sub check_user_perms2 {
1053 my( $self, $client, $authtoken, $user_id, $orgs, $perms ) = @_;
1055 my( $staff, $evt ) = $apputils->checkses($authtoken);
1056 return $evt if $evt;
1059 for my $org (@$orgs) {
1061 if($staff->id ne $user_id) {
1062 if( $evt = $apputils->check_perms(
1063 $staff->id, $org, 'VIEW_PERMISSION') ) {
1068 for my $perm (@$perms) {
1069 if($apputils->check_perms($user_id, $org, $perm)) {
1070 push @not_allowed, [ $org, $perm ];
1075 return \@not_allowed
1080 __PACKAGE__->register_method(
1081 method => "user_fines_summary",
1082 api_name => "open-ils.actor.user.fines.summary",
1083 notes => <<" NOTES");
1084 Returns a short summary of the users total open fines, excluding voided fines
1085 Params are login_session, user_id
1086 Returns a 'mous' object.
1089 sub user_fines_summary {
1090 my( $self, $client, $login_session, $user_id ) = @_;
1092 my $user_obj = $apputils->check_user_session($login_session);
1093 if($user_obj->id ne $user_id) {
1094 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1095 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1099 return $apputils->simple_scalar_request(
1101 "open-ils.storage.direct.money.open_user_summary.search.usr",
1109 __PACKAGE__->register_method(
1110 method => "user_transactions",
1111 api_name => "open-ils.actor.user.transactions",
1112 notes => <<" NOTES");
1113 Returns a list of open user transactions (mbts objects);
1114 Params are login_session, user_id
1115 Optional third parameter is the transactions type. defaults to all
1118 __PACKAGE__->register_method(
1119 method => "user_transactions",
1120 api_name => "open-ils.actor.user.transactions.have_charge",
1121 notes => <<" NOTES");
1122 Returns a list of all open user transactions (mbts objects) that have an initial charge
1123 Params are login_session, user_id
1124 Optional third parameter is the transactions type. defaults to all
1127 __PACKAGE__->register_method(
1128 method => "user_transactions",
1129 api_name => "open-ils.actor.user.transactions.have_balance",
1130 notes => <<" NOTES");
1131 Returns a list of all open user transactions (mbts objects) that have a balance
1132 Params are login_session, user_id
1133 Optional third parameter is the transactions type. defaults to all
1136 __PACKAGE__->register_method(
1137 method => "user_transactions",
1138 api_name => "open-ils.actor.user.transactions.fleshed",
1139 notes => <<" NOTES");
1140 Returns an object/hash of transaction, circ, title where transaction = an open
1141 user transactions (mbts objects), circ is the attached circluation, and title
1142 is the title the circ points to
1143 Params are login_session, user_id
1144 Optional third parameter is the transactions type. defaults to all
1147 __PACKAGE__->register_method(
1148 method => "user_transactions",
1149 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1150 notes => <<" NOTES");
1151 Returns an object/hash of transaction, circ, title where transaction = an open
1152 user transactions that has an initial charge (mbts objects), circ is the
1153 attached circluation, and title is the title the circ points to
1154 Params are login_session, user_id
1155 Optional third parameter is the transactions type. defaults to all
1158 __PACKAGE__->register_method(
1159 method => "user_transactions",
1160 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1161 notes => <<" NOTES");
1162 Returns an object/hash of transaction, circ, title where transaction = an open
1163 user transaction that has a balance (mbts objects), circ is the attached
1164 circluation, and title is the title the circ points to
1165 Params are login_session, user_id
1166 Optional third parameter is the transaction type. defaults to all
1171 sub user_transactions {
1172 my( $self, $client, $login_session, $user_id, $type ) = @_;
1174 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1175 $login_session, $user_id, 'VIEW_USER_TRANSACTIONS' );
1176 return $evt if $evt;
1178 my $api = $self->api_name();
1182 if(defined($type)) { @xact = (xact_type => $type);
1184 } else { @xact = (); }
1186 if($api =~ /have_charge/) {
1188 $trans = $apputils->simple_scalar_request(
1190 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1191 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1193 } elsif($api =~ /have_balance/) {
1195 $trans = $apputils->simple_scalar_request(
1197 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1198 { usr => $user_id, balance_owed => { ">" => 0 }, @xact });
1202 $trans = $apputils->simple_scalar_request(
1204 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1205 { usr => $user_id, @xact });
1208 if($api !~ /fleshed/) { return $trans; }
1213 for my $t (@$trans) {
1217 my $circ = $apputils->simple_scalar_request(
1219 "open-ils.storage.direct.action.circulation.retrieve",
1224 my $title = $apputils->simple_scalar_request(
1226 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1227 $circ->target_copy );
1231 my $u = OpenILS::Utils::ModsParser->new();
1232 $u->start_mods_batch($title->marc());
1233 my $mods = $u->finish_mods_batch();
1235 push @resp, {transaction => $t, circ => $circ, record => $mods };
1245 __PACKAGE__->register_method(
1246 method => "retrieve_groups",
1247 api_name => "open-ils.actor.groups.retrieve",
1248 notes => <<" NOTES");
1249 Returns a list of user groupss
1251 sub retrieve_groups {
1252 my( $self, $client ) = @_;
1253 return $apputils->simple_scalar_request(
1255 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1258 __PACKAGE__->register_method(
1259 method => "retrieve_groups_tree",
1260 api_name => "open-ils.actor.groups.tree.retrieve",
1261 notes => <<" NOTES");
1262 Returns a list of user groups
1264 sub retrieve_groups_tree {
1265 my( $self, $client ) = @_;
1266 my $groups = $apputils->simple_scalar_request(
1268 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1269 return $self->build_group_tree($groups);
1273 # turns an org list into an org tree
1274 sub build_group_tree {
1276 my( $self, $grplist) = @_;
1278 return $grplist unless (
1279 ref($grplist) and @$grplist > 1 );
1281 my @list = sort { $a->name cmp $b->name } @$grplist;
1284 for my $grp (@list) {
1286 if ($grp and !defined($grp->parent)) {
1290 my ($parent) = grep { $_->id == $grp->parent} @list;
1292 $parent->children([]) unless defined($parent->children);
1293 push( @{$parent->children}, $grp );
1301 __PACKAGE__->register_method(
1302 method => "add_user_to_groups",
1303 api_name => "open-ils.actor.user.set_groups",
1304 notes => <<" NOTES");
1305 Adds a user to one or more permission groups
1308 sub add_user_to_groups {
1309 my( $self, $client, $authtoken, $userid, $groups ) = @_;
1311 my( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1312 $authtoken, $userid, 'CREATE_USER_GROUP_LINK' );
1313 return $evt if $evt;
1315 ( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1316 $authtoken, $userid, 'REMOVE_USER_GROUP_LINK' );
1317 return $evt if $evt;
1319 $apputils->simplereq(
1321 'open-ils.storage.direct.permission.usr_grp_map.mass_delete', { usr => $userid } );
1323 for my $group (@$groups) {
1324 my $link = Fieldmapper::permission::usr_grp_map->new;
1326 $link->usr($userid);
1328 my $id = $apputils->simplereq(
1330 'open-ils.storage.direct.permission.usr_grp_map.create', $link );
1336 __PACKAGE__->register_method(
1337 method => "get_user_perm_groups",
1338 api_name => "open-ils.actor.user.get_groups",
1339 notes => <<" NOTES");
1340 Retrieve a user's permission groups.
1344 sub get_user_perm_groups {
1345 my( $self, $client, $authtoken, $userid ) = @_;
1347 my( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1348 $authtoken, $userid, 'VIEW_PERM_GROUPS' );
1349 return $evt if $evt;
1351 return $apputils->simplereq(
1353 'open-ils.storage.direct.permission.usr_grp_map.search.usr.atomic', $userid );