1 package OpenILS::Application::Actor;
2 use base qw/OpenSRF::Application/;
3 use strict; use warnings;
5 $Data::Dumper::Indent = 0;
8 use Digest::MD5 qw(md5_hex);
10 use OpenSRF::EX qw(:try);
14 use OpenILS::Application::AppUtils;
16 use OpenILS::Utils::Fieldmapper;
17 use OpenILS::Application::Search::Actor;
18 use OpenILS::Utils::ModsParser;
19 use OpenSRF::Utils::Logger qw/$logger/;
20 use OpenSRF::Utils qw/:datetime/;
22 use OpenSRF::Utils::Cache;
25 use DateTime::Format::ISO8601;
27 use OpenILS::Application::Actor::Container;
29 use OpenILS::Utils::Editor;
31 use OpenILS::Application::Actor::UserGroups;
33 OpenILS::Application::Actor::Container->initialize();
34 OpenILS::Application::Actor::UserGroups->initialize();
37 my $apputils = "OpenILS::Application::AppUtils";
40 sub _d { warn "Patron:\n" . Dumper(shift()); }
45 my $set_user_settings;
48 __PACKAGE__->register_method(
49 method => "set_user_settings",
50 api_name => "open-ils.actor.patron.settings.update",
52 sub set_user_settings {
53 my( $self, $client, $user_session, $uid, $settings ) = @_;
55 $logger->debug("Setting user settings: $user_session, $uid, " . Dumper($settings));
57 my( $staff, $user, $evt ) =
58 $apputils->checkses_requestor( $user_session, $uid, 'UPDATE_USER' );
63 # [{ usr => $user->id, name => $_}, {value => $$settings{$_}}] } keys %$settings;
66 [{ usr => $user->id, name => $_}, {value => $$settings{$_}}] } keys %$settings;
68 $logger->activity("User " . $staff->id . " updating user $uid settings with: " . Dumper(\@params));
70 return $apputils->simplereq(
72 'open-ils.storage.direct.actor.user_setting.batch.merge', @params );
78 __PACKAGE__->register_method(
79 method => "set_ou_settings",
80 api_name => "open-ils.actor.org_unit.settings.update",
83 my( $self, $client, $user_session, $ouid, $settings ) = @_;
85 my( $staff, $evt ) = $apputils->checkses( $user_session );
87 $evt = $apputils->check_perms( $staff->id, $ouid, 'UPDATE_ORG_UNIT' );
92 map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings;
94 $logger->activity("Updating org unit [$ouid] settings with: " . Dumper($params));
96 return $apputils->simplereq(
98 'open-ils.storage.direct.actor.org_unit_setting.merge', @$params );
102 my $fetch_user_settings;
103 my $fetch_ou_settings;
105 __PACKAGE__->register_method(
106 method => "user_settings",
107 api_name => "open-ils.actor.patron.settings.retrieve",
110 my( $self, $client, $user_session, $uid ) = @_;
112 my( $staff, $user, $evt ) =
113 $apputils->checkses_requestor( $user_session, $uid, 'VIEW_USER' );
116 $logger->debug("User " . $staff->id . " fetching user $uid\n");
117 my $s = $apputils->simplereq(
119 'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
121 return { map { ($_->name,$_->value) } @$s };
126 __PACKAGE__->register_method(
127 method => "ou_settings",
128 api_name => "open-ils.actor.org_unit.settings.retrieve",
131 my( $self, $client, $ouid ) = @_;
133 $logger->info("Fetching org unit settings for org $ouid");
135 my $s = $apputils->simplereq(
137 'open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic', $ouid);
139 return { map { ($_->name,$_->value) } @$s };
142 __PACKAGE__->register_method (
143 method => "ou_setting_delete",
144 api_name => 'open-ils.actor.org_setting.delete',
146 Deletes a specific org unit setting for a specific location
147 @param authtoken The login session key
148 @param orgid The org unit whose setting we're changing
149 @param setting The name of the setting to delete
150 @return True value on success.
154 sub ou_setting_delete {
155 my( $self, $conn, $authtoken, $orgid, $setting ) = @_;
156 my( $reqr, $evt) = $U->checkses($authtoken);
158 $evt = $U->check_perms($reqr->id, $orgid, 'UPDATE_ORG_SETTING');
161 my $id = $U->storagereq(
162 'open-ils.storage.id_list.actor.org_unit_setting.search_where',
163 { name => $setting, org_unit => $orgid } );
165 $logger->debug("Retrieved setting $id in org unit setting delete");
167 my $s = $U->storagereq(
168 'open-ils.storage.direct.actor.org_unit_setting.delete', $id );
170 $logger->activity("User ".$reqr->id." deleted org unit setting $id") if $s;
176 __PACKAGE__->register_method(
177 method => "update_patron",
178 api_name => "open-ils.actor.patron.update",);
181 my( $self, $client, $user_session, $patron ) = @_;
183 my $session = $apputils->start_db_session();
186 $logger->info("Creating new patron...") if $patron->isnew;
187 $logger->info("Updating Patron: " . $patron->id) unless $patron->isnew;
189 my( $user_obj, $evt ) = $U->checkses($user_session);
192 # XXX does this user have permission to add/create users. Granularity?
193 # $new_patron is the patron in progress. $patron is the original patron
194 # passed in with the method. new_patron will change as the components
195 # of patron are added/updated.
199 # unflesh the real items on the patron
200 $patron->card( $patron->card->id ) if(ref($patron->card));
201 $patron->billing_address( $patron->billing_address->id )
202 if(ref($patron->billing_address));
203 $patron->mailing_address( $patron->mailing_address->id )
204 if(ref($patron->mailing_address));
206 # create/update the patron first so we can use his id
207 if($patron->isnew()) {
208 ( $new_patron, $evt ) = _add_patron($session, _clone_patron($patron), $user_obj);
210 } else { $new_patron = $patron; }
212 ( $new_patron, $evt ) = _add_update_addresses($session, $patron, $new_patron, $user_obj);
215 ( $new_patron, $evt ) = _add_update_cards($session, $patron, $new_patron, $user_obj);
218 ( $new_patron, $evt ) = _add_survey_responses($session, $patron, $new_patron, $user_obj);
221 # re-update the patron if anything has happened to him during this process
222 if($new_patron->ischanged()) {
223 ( $new_patron, $evt ) = _update_patron($session, $new_patron, $user_obj);
227 #$session = OpenSRF::AppSession->create("open-ils.storage"); # why did i put this here?
229 ($new_patron, $evt) = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
232 ($new_patron, $evt) = _create_perm_maps($session, $user_session, $patron, $new_patron, $user_obj);
235 ($new_patron, $evt) = _create_standing_penalties($session, $user_session, $patron, $new_patron, $user_obj);
238 $logger->activity("user ".$user_obj->id." updating/creating user ".$new_patron->id);
239 $apputils->commit_db_session($session);
241 #warn "Patron Update/Create complete\n";
242 return flesh_user($new_patron->id());
248 __PACKAGE__->register_method(
249 method => "user_retrieve_fleshed_by_id",
250 api_name => "open-ils.actor.user.fleshed.retrieve",);
252 sub user_retrieve_fleshed_by_id {
253 my( $self, $client, $user_session, $user_id ) = @_;
255 my( $requestor, $target, $evt ) = $apputils->
256 checkses_requestor( $user_session, $user_id, 'VIEW_USER' );
259 return flesh_user($user_id);
263 # fleshes: card, cards, address, addresses, stat_cat_entries, standing_penalties
271 $session = OpenSRF::AppSession->create("open-ils.storage");
275 # grab the user with the given id
276 my $ureq = $session->request(
277 "open-ils.storage.direct.actor.user.retrieve", $id);
278 my $user = $ureq->gather(1);
280 if(!$user) { return undef; }
283 my $cards_req = $session->request(
284 "open-ils.storage.direct.actor.card.search.usr.atomic",
286 $user->cards( $cards_req->gather(1) );
288 for my $c(@{$user->cards}) {
289 if($c->id == $user->card || $c->id eq $user->card ) {
290 #warn "Setting my card to " . $c->id . "\n";
295 my $add_req = $session->request(
296 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
298 $user->addresses( $add_req->gather(1) );
300 if( @{$user->addresses} ) {
301 if( ! grep { $_->id eq $user->billing_address } @{$user->addresses} ) {
302 my $ba = $session->request(
303 'open-ils.storage.direct.actor.user_address.retrieve',
304 $user->billing_address)->gather(1);
305 push( @{$user->addresses}, $ba );
308 if( ! grep { $_->id eq $user->mailing_address } @{$user->addresses} ) {
309 my $ba = $session->request(
310 'open-ils.storage.direct.actor.user_address.retrieve',
311 $user->mailing_address)->gather(1);
312 push( @{$user->addresses}, $ba );
317 for my $c(@{$user->addresses}) {
318 if($c->id eq $user->billing_address ) { $user->billing_address($c); }
319 if($c->id eq $user->mailing_address ) { $user->mailing_address($c); }
322 my $stat_req = $session->request(
323 "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic",
325 $user->stat_cat_entries($stat_req->gather(1));
327 my $standing_penalties_req = $session->request(
328 "open-ils.storage.direct.actor.user_standing_penalty.search.usr.atomic",
330 $user->standing_penalties($standing_penalties_req->gather(1));
332 if($kill) { $session->disconnect(); }
333 $user->clear_passwd();
339 # clone and clear stuff that would break the database
343 my $new_patron = $patron->clone;
345 # Using the Fieldmapper clone method
346 #my $new_patron = Fieldmapper::actor::user->new();
348 #my $fmap = $Fieldmapper::fieldmap;
349 #no strict; # shallow clone, may be useful in the fieldmapper
351 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
352 # $new_patron->$field( $patron->$field() );
357 $new_patron->clear_billing_address();
358 $new_patron->clear_mailing_address();
359 $new_patron->clear_addresses();
360 $new_patron->clear_card();
361 $new_patron->clear_cards();
362 $new_patron->clear_id();
363 $new_patron->clear_isnew();
364 $new_patron->clear_ischanged();
365 $new_patron->clear_isdeleted();
366 $new_patron->clear_stat_cat_entries();
367 $new_patron->clear_permissions();
368 $new_patron->clear_standing_penalties();
378 my $user_obj = shift;
380 my $evt = $U->check_perms($user_obj->id, $patron->home_ou, 'CREATE_USER');
381 return (undef, $evt) if $evt;
383 $logger->info("Creating new user in the DB with username: ".$patron->usrname());
385 my $id = $session->request(
386 "open-ils.storage.direct.actor.user.create", $patron)->gather(1);
387 return (undef, $U->DB_UPDATE_FAILED($patron)) unless $id;
389 $logger->info("Successfully created new user [$id] in DB");
391 return ( $session->request(
392 "open-ils.storage.direct.actor.user.retrieve", $id)->gather(1), undef );
397 my( $session, $patron, $user_obj, $noperm) = @_;
399 $logger->info("Updating patron ".$patron->id." in DB");
402 my $evt = $U->check_perms($user_obj->id, $patron->home_ou, 'UPDATE_USER');
403 return (undef, $evt) if $evt;
406 # update the password by itself to avoid the password protection magic
407 if( $patron->passwd ) {
408 my $s = $session->request(
409 'open-ils.storage.direct.actor.user.remote_update',
410 {id => $patron->id}, {passwd => $patron->passwd})->gather(1);
411 return (undef, $U->DB_UPDATE_FAILED($patron)) unless defined($s);
412 $patron->clear_passwd;
415 if(!$patron->ident_type) {
416 $patron->clear_ident_type;
417 $patron->clear_ident_value;
420 if(!$patron->ident_type2) {
421 $patron->clear_ident_type2;
422 $patron->clear_ident_value2;
425 my $stat = $session->request(
426 "open-ils.storage.direct.actor.user.update",$patron )->gather(1);
427 return (undef, $U->DB_UPDATE_FAILED($patron)) unless defined($stat);
433 sub _add_update_addresses {
437 my $new_patron = shift;
441 my $current_id; # id of the address before creation
443 for my $address (@{$patron->addresses()}) {
445 $address->usr($new_patron->id());
447 if(ref($address) and $address->isnew()) {
449 $current_id = $address->id();
450 ($address, $evt) = _add_address($session,$address);
451 return (undef, $evt) if $evt;
453 if( $patron->billing_address() and
454 $patron->billing_address() == $current_id ) {
455 $new_patron->billing_address($address->id());
456 $new_patron->ischanged(1);
459 if( $patron->mailing_address() and
460 $patron->mailing_address() == $current_id ) {
461 $new_patron->mailing_address($address->id());
462 $new_patron->ischanged(1);
465 } elsif( ref($address) and $address->ischanged() ) {
467 $address->usr($new_patron->id());
468 ($address, $evt) = _update_address($session, $address);
469 return (undef, $evt) if $evt;
471 } elsif( ref($address) and $address->isdeleted() ) {
473 if( $address->id() == $new_patron->mailing_address() ) {
474 $new_patron->clear_mailing_address();
475 ($new_patron, $evt) = _update_patron($session, $new_patron);
476 return (undef, $evt) if $evt;
479 if( $address->id() == $new_patron->billing_address() ) {
480 $new_patron->clear_billing_address();
481 ($new_patron, $evt) = _update_patron($session, $new_patron);
482 return (undef, $evt) if $evt;
485 $evt = _delete_address($session, $address);
486 return (undef, $evt) if $evt;
490 return ( $new_patron, undef );
494 # adds an address to the db and returns the address with new id
496 my($session, $address) = @_;
497 $address->clear_id();
499 $logger->info("Creating new address at street ".$address->street1);
501 # put the address into the database
502 my $id = $session->request(
503 "open-ils.storage.direct.actor.user_address.create", $address )->gather(1);
504 return (undef, $U->DB_UPDATE_FAILED($address)) unless $id;
507 return ($address, undef);
511 sub _update_address {
512 my( $session, $address ) = @_;
514 $logger->info("Updating address ".$address->id." in the DB");
516 my $stat = $session->request(
517 "open-ils.storage.direct.actor.user_address.update", $address )->gather(1);
519 return (undef, $U->DB_UPDATE_FAILED($address)) unless defined($stat);
520 return ($address, undef);
525 sub _add_update_cards {
529 my $new_patron = shift;
533 my $virtual_id; #id of the card before creation
534 for my $card (@{$patron->cards()}) {
536 $card->usr($new_patron->id());
538 if(ref($card) and $card->isnew()) {
540 $virtual_id = $card->id();
541 ( $card, $evt ) = _add_card($session,$card);
542 return (undef, $evt) if $evt;
544 #if(ref($patron->card)) { $patron->card($patron->card->id); }
545 if($patron->card() == $virtual_id) {
546 $new_patron->card($card->id());
547 $new_patron->ischanged(1);
550 } elsif( ref($card) and $card->ischanged() ) {
551 $card->usr($new_patron->id());
552 $evt = _update_card($session, $card);
553 return (undef, $evt) if $evt;
557 return ( $new_patron, undef );
561 # adds an card to the db and returns the card with new id
563 my( $session, $card ) = @_;
566 $logger->info("Adding new patron card ".$card->barcode);
568 my $id = $session->request(
569 "open-ils.storage.direct.actor.card.create", $card )->gather(1);
570 return (undef, $U->DB_UPDATE_FAILED($card)) unless $id;
571 $logger->info("Successfully created patron card $id");
574 return ( $card, undef );
578 # returns event on error. returns undef otherwise
580 my( $session, $card ) = @_;
581 $logger->info("Updating patron card ".$card->id);
583 my $stat = $session->request(
584 "open-ils.storage.direct.actor.card.update", $card )->gather(1);
585 return $U->DB_UPDATE_FAILED($card) unless defined($stat);
592 # returns event on error. returns undef otherwise
593 sub _delete_address {
594 my( $session, $address ) = @_;
596 $logger->info("Deleting address ".$address->id." from DB");
598 my $stat = $session->request(
599 "open-ils.storage.direct.actor.user_address.delete", $address )->gather(1);
601 return $U->DB_UPDATE_FAILED($address) unless defined($stat);
607 sub _add_survey_responses {
608 my ($session, $patron, $new_patron) = @_;
610 $logger->info( "Updating survey responses for patron ".$new_patron->id );
612 my $responses = $patron->survey_responses;
616 $_->usr($new_patron->id) for (@$responses);
618 my $evt = $U->simplereq( "open-ils.circ",
619 "open-ils.circ.survey.submit.user_id", $responses );
621 return (undef, $evt) if defined($U->event_code($evt));
625 return ( $new_patron, undef );
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 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
639 if ($map->isdeleted()) {
640 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.delete";
642 } elsif ($map->isnew()) {
643 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
648 $map->target_usr($new_patron->id);
651 $logger->info("Updating stat entry with method $method and map $map");
653 my $stat = $session->request($method, $map)->gather(1);
654 return (undef, $U->DB_UPDATE_FAILED($map)) unless defined($stat);
658 return ($new_patron, undef);
661 sub _create_perm_maps {
663 my($session, $user_session, $patron, $new_patron) = @_;
665 my $maps = $patron->permissions;
667 for my $map (@$maps) {
669 my $method = "open-ils.storage.direct.permission.usr_perm_map.update";
670 if ($map->isdeleted()) {
671 $method = "open-ils.storage.direct.permission.usr_perm_map.delete";
672 } elsif ($map->isnew()) {
673 $method = "open-ils.storage.direct.permission.usr_perm_map.create";
678 $map->usr($new_patron->id);
680 #warn( "Updating permissions with method $method and session $user_session and map $map" );
681 $logger->info( "Updating permissions with method $method and map $map" );
683 my $stat = $session->request($method, $map)->gather(1);
684 return (undef, $U->DB_UPDATE_FAILED($map)) unless defined($stat);
688 return ($new_patron, undef);
692 sub _create_standing_penalties {
694 my($session, $user_session, $patron, $new_patron) = @_;
696 my $maps = $patron->standing_penalties;
699 for my $map (@$maps) {
701 if ($map->isdeleted()) {
702 $method = "open-ils.storage.direct.actor.user_standing_penalty.delete";
703 } elsif ($map->isnew()) {
704 $method = "open-ils.storage.direct.actor.user_standing_penalty.create";
710 $map->usr($new_patron->id);
712 $logger->debug( "Updating standing penalty with method $method and session $user_session and map $map" );
714 my $stat = $session->request($method, $map)->gather(1);
715 return (undef, $U->DB_UPDATE_FAILED($map)) unless $stat;
718 return ($new_patron, undef);
723 __PACKAGE__->register_method(
724 method => "search_username",
725 api_name => "open-ils.actor.user.search.username",
728 sub search_username {
729 my($self, $client, $username) = @_;
730 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
732 "open-ils.storage.direct.actor.user.search.usrname.atomic",
740 __PACKAGE__->register_method(
741 method => "user_retrieve_by_barcode",
742 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
744 sub user_retrieve_by_barcode {
745 my($self, $client, $user_session, $barcode) = @_;
747 $logger->debug("Searching for user with barcode $barcode");
748 my ($user_obj, $evt) = $apputils->checkses($user_session);
752 my $session = OpenSRF::AppSession->create("open-ils.storage");
754 # find the card with the given barcode
755 my $creq = $session->request(
756 "open-ils.storage.direct.actor.card.search.barcode.atomic",
758 my $card = $creq->gather(1);
760 if(!$card || !$card->[0]) {
761 $session->disconnect();
762 return OpenILS::Event->new( 'ACTOR_USER_NOT_FOUND' );
766 my $user = flesh_user($card->usr(), $session);
768 $evt = $U->check_perms($user_obj->id, $user->home_ou, 'VIEW_USER');
771 $session->disconnect();
772 if(!$user) { return OpenILS::Event->new( 'ACTOR_USER_NOT_FOUND' ); }
779 __PACKAGE__->register_method(
780 method => "get_user_by_id",
781 api_name => "open-ils.actor.user.retrieve",);
784 my ($self, $client, $user_session, $id) = @_;
786 my $user_obj = $apputils->check_user_session( $user_session );
788 return $apputils->simple_scalar_request(
790 "open-ils.storage.direct.actor.user.retrieve",
796 __PACKAGE__->register_method(
797 method => "get_org_types",
798 api_name => "open-ils.actor.org_types.retrieve",);
802 my($self, $client) = @_;
804 return $org_types if $org_types;
806 $apputils->simple_scalar_request(
808 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
813 __PACKAGE__->register_method(
814 method => "get_user_profiles",
815 api_name => "open-ils.actor.user.profiles.retrieve",
819 sub get_user_profiles {
820 return $user_profiles if $user_profiles;
822 return $user_profiles =
823 $apputils->simple_scalar_request(
825 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
830 __PACKAGE__->register_method(
831 method => "get_user_ident_types",
832 api_name => "open-ils.actor.user.ident_types.retrieve",
835 sub get_user_ident_types {
836 return $ident_types if $ident_types;
837 return $ident_types =
838 $apputils->simple_scalar_request(
840 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
846 __PACKAGE__->register_method(
847 method => "get_org_unit",
848 api_name => "open-ils.actor.org_unit.retrieve",
853 my( $self, $client, $user_session, $org_id ) = @_;
855 if(defined($user_session) && !defined($org_id)) {
857 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
858 if(!defined($org_id)) {
859 $org_id = $user_obj->home_ou;
864 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
866 "open-ils.storage.direct.actor.org_unit.retrieve",
872 __PACKAGE__->register_method(
873 method => "search_org_unit",
874 api_name => "open-ils.actor.org_unit_list.search",
877 sub search_org_unit {
879 my( $self, $client, $field, $value ) = @_;
881 my $list = OpenILS::Application::AppUtils->simple_scalar_request(
883 "open-ils.storage.direct.actor.org_unit.search.$field.atomic",
892 __PACKAGE__->register_method(
893 method => "get_org_tree",
894 api_name => "open-ils.actor.org_tree.retrieve",
896 note => "Returns the entire org tree structure",
900 my( $self, $client) = @_;
903 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
905 # see if it's in the cache
906 #warn "Getting ORG Tree\n";
907 my $tree = $cache_client->get_cache('orgtree');
909 #warn "Found orgtree in cache. returning...\n";
913 my $orglist = $apputils->simple_scalar_request(
915 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
918 #warn "found org list\n";
921 $tree = $self->build_org_tree($orglist);
922 $cache_client->put_cache('orgtree', $tree);
928 # turns an org list into an org tree
931 my( $self, $orglist) = @_;
933 return $orglist unless (
934 ref($orglist) and @$orglist > 1 );
937 $a->ou_type <=> $b->ou_type ||
938 $a->name cmp $b->name } @$orglist;
940 for my $org (@list) {
942 next unless ($org and defined($org->parent_ou));
943 my ($parent) = grep { $_->id == $org->parent_ou } @list;
946 $parent->children([]) unless defined($parent->children);
947 push( @{$parent->children}, $org );
955 __PACKAGE__->register_method(
956 method => "get_org_descendants",
957 api_name => "open-ils.actor.org_tree.descendants.retrieve"
960 # depth is optional. org_unit is the id
961 sub get_org_descendants {
962 my( $self, $client, $org_unit, $depth ) = @_;
963 my $orglist = $apputils->simple_scalar_request(
965 "open-ils.storage.actor.org_unit.descendants.atomic",
967 return $self->build_org_tree($orglist);
971 __PACKAGE__->register_method(
972 method => "get_org_ancestors",
973 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
976 # depth is optional. org_unit is the id
977 sub get_org_ancestors {
978 my( $self, $client, $org_unit, $depth ) = @_;
979 my $orglist = $apputils->simple_scalar_request(
981 "open-ils.storage.actor.org_unit.ancestors.atomic",
983 return $self->build_org_tree($orglist);
987 __PACKAGE__->register_method(
988 method => "get_standings",
989 api_name => "open-ils.actor.standings.retrieve"
994 return $user_standings if $user_standings;
995 return $user_standings =
996 $apputils->simple_scalar_request(
998 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
1003 __PACKAGE__->register_method(
1004 method => "get_my_org_path",
1005 api_name => "open-ils.actor.org_unit.full_path.retrieve"
1008 sub get_my_org_path {
1009 my( $self, $client, $user_session, $org_id ) = @_;
1010 my $user_obj = $apputils->check_user_session($user_session);
1011 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
1013 return $apputils->simple_scalar_request(
1015 "open-ils.storage.actor.org_unit.full_path.atomic",
1020 __PACKAGE__->register_method(
1021 method => "patron_adv_search",
1022 api_name => "open-ils.actor.patron.search.advanced" );
1023 sub patron_adv_search {
1024 my( $self, $client, $auth, $search_hash, $search_limit, $search_sort ) = @_;
1025 my $e = OpenILS::Utils::Editor->new(authtoken=>$auth);
1026 return $e->event unless $e->checkauth;
1027 return $e->event unless $e->allowed('VIEW_USER');
1029 "open-ils.storage.actor.user.crazy_search",
1030 $search_hash, $search_limit, $search_sort);
1035 sub _verify_password {
1036 my($user_session, $password) = @_;
1037 my $user_obj = $apputils->check_user_session($user_session);
1039 #grab the user with password
1040 $user_obj = $apputils->simple_scalar_request(
1042 "open-ils.storage.direct.actor.user.retrieve",
1045 if($user_obj->passwd eq $password) {
1053 __PACKAGE__->register_method(
1054 method => "update_password",
1055 api_name => "open-ils.actor.user.password.update");
1057 __PACKAGE__->register_method(
1058 method => "update_password",
1059 api_name => "open-ils.actor.user.username.update");
1061 __PACKAGE__->register_method(
1062 method => "update_password",
1063 api_name => "open-ils.actor.user.email.update");
1065 sub update_password {
1066 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
1070 my $user_obj = $apputils->check_user_session($user_session);
1072 if($self->api_name =~ /password/o) {
1074 #make sure they know the current password
1075 if(!_verify_password($user_session, md5_hex($current_password))) {
1076 return OpenILS::Event->new('INCORRECT_PASSWORD');
1079 $logger->debug("update_password setting new password $new_value");
1080 $user_obj->passwd($new_value);
1082 } elsif($self->api_name =~ /username/o) {
1083 my $users = search_username(undef, undef, $new_value);
1084 if( $users and $users->[0] ) {
1085 return OpenILS::Event->new('USERNAME_EXISTS');
1087 $user_obj->usrname($new_value);
1089 } elsif($self->api_name =~ /email/o) {
1090 #warn "Updating email to $new_value\n";
1091 $user_obj->email($new_value);
1094 my $session = $apputils->start_db_session();
1096 ( $user_obj, $evt ) = _update_patron($session, $user_obj, $user_obj, 1);
1097 return $evt if $evt;
1099 $apputils->commit_db_session($session);
1101 if($user_obj) { return 1; }
1106 __PACKAGE__->register_method(
1107 method => "check_user_perms",
1108 api_name => "open-ils.actor.user.perm.check",
1109 notes => <<" NOTES");
1110 Takes a login session, user id, an org id, and an array of perm type strings. For each
1111 perm type, if the user does *not* have the given permission it is added
1112 to a list which is returned from the method. If all permissions
1113 are allowed, an empty list is returned
1114 if the logged in user does not match 'user_id', then the logged in user must
1115 have VIEW_PERMISSION priveleges.
1118 sub check_user_perms {
1119 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1121 my( $staff, $evt ) = $apputils->checkses($login_session);
1122 return $evt if $evt;
1124 if($staff->id ne $user_id) {
1125 if( my $evt = $apputils->check_perms(
1126 $staff->id, $org_id, 'VIEW_PERMISSION') ) {
1132 for my $perm (@$perm_types) {
1133 if($apputils->check_perms($user_id, $org_id, $perm)) {
1134 push @not_allowed, $perm;
1138 return \@not_allowed
1141 __PACKAGE__->register_method(
1142 method => "check_user_perms2",
1143 api_name => "open-ils.actor.user.perm.check.multi_org",
1145 Checks the permissions on a list of perms and orgs for a user
1146 @param authtoken The login session key
1147 @param user_id The id of the user to check
1148 @param orgs The array of org ids
1149 @param perms The array of permission names
1150 @return An array of [ orgId, permissionName ] arrays that FAILED the check
1151 if the logged in user does not match 'user_id', then the logged in user must
1152 have VIEW_PERMISSION priveleges.
1155 sub check_user_perms2 {
1156 my( $self, $client, $authtoken, $user_id, $orgs, $perms ) = @_;
1158 my( $staff, $target, $evt ) = $apputils->checkses_requestor(
1159 $authtoken, $user_id, 'VIEW_PERMISSION' );
1160 return $evt if $evt;
1163 for my $org (@$orgs) {
1164 for my $perm (@$perms) {
1165 if($apputils->check_perms($user_id, $org, $perm)) {
1166 push @not_allowed, [ $org, $perm ];
1171 return \@not_allowed
1175 __PACKAGE__->register_method(
1176 method => 'check_user_perms3',
1177 api_name => 'open-ils.actor.user.perm.highest_org',
1179 Returns the highest org unit id at which a user has a given permission
1180 If the requestor does not match the target user, the requestor must have
1181 'VIEW_PERMISSION' rights at the home org unit of the target user
1182 @param authtoken The login session key
1183 @param userid The id of the user in question
1184 @param perm The permission to check
1185 @return The org unit highest in the org tree within which the user has
1186 the requested permission
1189 sub check_user_perms3 {
1190 my( $self, $client, $authtoken, $userid, $perm ) = @_;
1192 my( $staff, $target, $org, $evt );
1194 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
1195 $authtoken, $userid, 'VIEW_PERMISSION' );
1196 return $evt if $evt;
1198 my $tree = $self->get_org_tree();
1199 return _find_highest_perm_org( $perm, $userid, $target->home_ou, $tree );
1203 sub _find_highest_perm_org {
1204 my ( $perm, $userid, $start_org, $org_tree ) = @_;
1205 my $org = $apputils->find_org($org_tree, $start_org );
1209 last if ($apputils->check_perms( $userid, $org->id, $perm )); # perm failed
1211 $org = $apputils->find_org( $org_tree, $org->parent_ou() );
1217 __PACKAGE__->register_method(
1218 method => 'check_user_perms4',
1219 api_name => 'open-ils.actor.user.perm.highest_org.batch',
1221 Returns the highest org unit id at which a user has a given permission
1222 If the requestor does not match the target user, the requestor must have
1223 'VIEW_PERMISSION' rights at the home org unit of the target user
1224 @param authtoken The login session key
1225 @param userid The id of the user in question
1226 @param perms An array of perm names to check
1227 @return An array of orgId's representing the org unit
1228 highest in the org tree within which the user has the requested permission
1229 The arrah of orgId's has matches the order of the perms array
1232 sub check_user_perms4 {
1233 my( $self, $client, $authtoken, $userid, $perms ) = @_;
1235 my( $staff, $target, $org, $evt );
1237 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
1238 $authtoken, $userid, 'VIEW_PERMISSION' );
1239 return $evt if $evt;
1242 return [] unless ref($perms);
1243 my $tree = $self->get_org_tree();
1245 for my $p (@$perms) {
1246 push( @arr, _find_highest_perm_org( $p, $userid, $target->home_ou, $tree ) );
1254 __PACKAGE__->register_method(
1255 method => "user_fines_summary",
1256 api_name => "open-ils.actor.user.fines.summary",
1257 notes => <<" NOTES");
1258 Returns a short summary of the users total open fines, excluding voided fines
1259 Params are login_session, user_id
1260 Returns a 'mous' object.
1263 sub user_fines_summary {
1264 my( $self, $client, $login_session, $user_id ) = @_;
1266 my $user_obj = $apputils->check_user_session($login_session);
1267 if($user_obj->id ne $user_id) {
1268 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1269 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1273 return $apputils->simple_scalar_request(
1275 "open-ils.storage.direct.money.open_user_summary.search.usr",
1283 __PACKAGE__->register_method(
1284 method => "user_transactions",
1285 api_name => "open-ils.actor.user.transactions",
1286 notes => <<" NOTES");
1287 Returns a list of open user transactions (mbts objects);
1288 Params are login_session, user_id
1289 Optional third parameter is the transactions type. defaults to all
1292 __PACKAGE__->register_method(
1293 method => "user_transactions",
1294 api_name => "open-ils.actor.user.transactions.have_charge",
1295 notes => <<" NOTES");
1296 Returns a list of all open user transactions (mbts objects) that have an initial charge
1297 Params are login_session, user_id
1298 Optional third parameter is the transactions type. defaults to all
1301 __PACKAGE__->register_method(
1302 method => "user_transactions",
1303 api_name => "open-ils.actor.user.transactions.have_balance",
1304 notes => <<" NOTES");
1305 Returns a list of all open user transactions (mbts objects) that have a balance
1306 Params are login_session, user_id
1307 Optional third parameter is the transactions type. defaults to all
1310 __PACKAGE__->register_method(
1311 method => "user_transactions",
1312 api_name => "open-ils.actor.user.transactions.fleshed",
1313 notes => <<" NOTES");
1314 Returns an object/hash of transaction, circ, title where transaction = an open
1315 user transactions (mbts objects), circ is the attached circluation, and title
1316 is the title the circ points to
1317 Params are login_session, user_id
1318 Optional third parameter is the transactions type. defaults to all
1321 __PACKAGE__->register_method(
1322 method => "user_transactions",
1323 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1324 notes => <<" NOTES");
1325 Returns an object/hash of transaction, circ, title where transaction = an open
1326 user transactions that has an initial charge (mbts objects), circ is the
1327 attached circluation, and title is the title the circ points to
1328 Params are login_session, user_id
1329 Optional third parameter is the transactions type. defaults to all
1332 __PACKAGE__->register_method(
1333 method => "user_transactions",
1334 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1335 notes => <<" NOTES");
1336 Returns an object/hash of transaction, circ, title where transaction = an open
1337 user transaction that has a balance (mbts objects), circ is the attached
1338 circluation, and title is the title the circ points to
1339 Params are login_session, user_id
1340 Optional third parameter is the transaction type. defaults to all
1343 __PACKAGE__->register_method(
1344 method => "user_transactions",
1345 api_name => "open-ils.actor.user.transactions.count",
1346 notes => <<" NOTES");
1347 Returns an object/hash of transaction, circ, title where transaction = an open
1348 user transactions (mbts objects), circ is the attached circluation, and title
1349 is the title the circ points to
1350 Params are login_session, user_id
1351 Optional third parameter is the transactions type. defaults to all
1354 __PACKAGE__->register_method(
1355 method => "user_transactions",
1356 api_name => "open-ils.actor.user.transactions.have_charge.count",
1357 notes => <<" NOTES");
1358 Returns an object/hash of transaction, circ, title where transaction = an open
1359 user transactions that has an initial charge (mbts objects), circ is the
1360 attached circluation, and title is the title the circ points to
1361 Params are login_session, user_id
1362 Optional third parameter is the transactions type. defaults to all
1365 __PACKAGE__->register_method(
1366 method => "user_transactions",
1367 api_name => "open-ils.actor.user.transactions.have_balance.count",
1368 notes => <<" NOTES");
1369 Returns an object/hash of transaction, circ, title where transaction = an open
1370 user transaction that has a balance (mbts objects), circ is the attached
1371 circluation, and title is the title the circ points to
1372 Params are login_session, user_id
1373 Optional third parameter is the transaction type. defaults to all
1376 __PACKAGE__->register_method(
1377 method => "user_transactions",
1378 api_name => "open-ils.actor.user.transactions.have_balance.total",
1379 notes => <<" NOTES");
1380 Returns an object/hash of transaction, circ, title where transaction = an open
1381 user transaction that has a balance (mbts objects), circ is the attached
1382 circluation, and title is the title the circ points to
1383 Params are login_session, user_id
1384 Optional third parameter is the transaction type. defaults to all
1389 sub user_transactions {
1390 my( $self, $client, $login_session, $user_id, $type ) = @_;
1392 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1393 $login_session, $user_id, 'VIEW_USER_TRANSACTIONS' );
1394 return $evt if $evt;
1396 my $api = $self->api_name();
1400 if(defined($type)) { @xact = (xact_type => $type);
1402 } else { @xact = (); }
1404 if($api =~ /have_charge/o) {
1406 $trans = $apputils->simple_scalar_request(
1408 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1409 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1411 } elsif($api =~ /have_balance/o) {
1413 $trans = $apputils->simple_scalar_request(
1415 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1416 { usr => $user_id, balance_owed => { "<>" => 0 }, @xact });
1420 $trans = $apputils->simple_scalar_request(
1422 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1423 { usr => $user_id, @xact });
1426 if($api =~ /total/o) {
1428 for my $t (@$trans) {
1429 $total += $t->balance_owed;
1432 $logger->debug("Total balance owed by user $user_id: $total");
1436 if($api =~ /count/o) { return scalar @$trans; }
1437 if($api !~ /fleshed/o) { return $trans; }
1440 for my $t (@$trans) {
1442 if( $t->xact_type ne 'circulation' ) {
1443 push @resp, {transaction => $t};
1447 my $circ = $apputils->simple_scalar_request(
1449 "open-ils.storage.direct.action.circulation.retrieve",
1454 my $title = $apputils->simple_scalar_request(
1456 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1457 $circ->target_copy );
1461 my $u = OpenILS::Utils::ModsParser->new();
1462 $u->start_mods_batch($title->marc());
1463 my $mods = $u->finish_mods_batch();
1465 push @resp, {transaction => $t, circ => $circ, record => $mods };
1473 __PACKAGE__->register_method(
1474 method => "user_transaction_retrieve",
1475 api_name => "open-ils.actor.user.transaction.fleshed.retrieve",
1477 notes => <<" NOTES");
1478 Returns a fleshedtransaction record
1480 __PACKAGE__->register_method(
1481 method => "user_transaction_retrieve",
1482 api_name => "open-ils.actor.user.transaction.retrieve",
1484 notes => <<" NOTES");
1485 Returns a transaction record
1487 sub user_transaction_retrieve {
1488 my( $self, $client, $login_session, $bill_id ) = @_;
1490 my $trans = $apputils->simple_scalar_request(
1492 "open-ils.storage.direct.money.billable_transaction_summary.retrieve",
1496 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1497 $login_session, $trans->usr, 'VIEW_USER_TRANSACTIONS' );
1498 return $evt if $evt;
1500 my $api = $self->api_name();
1501 if($api !~ /fleshed/o) { return $trans; }
1503 if( $trans->xact_type ne 'circulation' ) {
1504 $logger->debug("Returning non-circ transaction");
1505 return {transaction => $trans};
1508 my $circ = $apputils->simple_scalar_request(
1510 "open-ils.storage.direct.action.circulation.retrieve",
1513 return {transaction => $trans} unless $circ;
1514 $logger->debug("Found the circ transaction");
1516 my $title = $apputils->simple_scalar_request(
1518 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1519 $circ->target_copy );
1521 return {transaction => $trans, circ => $circ } unless $title;
1522 $logger->debug("Found the circ title");
1526 my $u = OpenILS::Utils::ModsParser->new();
1527 $u->start_mods_batch($title->marc());
1528 $mods = $u->finish_mods_batch();
1530 if ($title->id == -1) {
1531 my $copy = $apputils->simple_scalar_request(
1533 "open-ils.storage.direct.asset.copy.retrieve",
1534 $circ->target_copy );
1536 $mods = new Fieldmapper::metabib::virtual_record;
1538 $mods->title($copy->dummy_title);
1539 $mods->author($copy->dummy_author);
1543 $logger->debug("MODSized the circ title");
1545 return {transaction => $trans, circ => $circ, record => $mods };
1549 __PACKAGE__->register_method(
1550 method => "hold_request_count",
1551 api_name => "open-ils.actor.user.hold_requests.count",
1553 notes => <<" NOTES");
1554 Returns hold ready/total counts
1556 sub hold_request_count {
1557 my( $self, $client, $login_session, $userid ) = @_;
1559 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1560 $login_session, $userid, 'VIEW_HOLD' );
1561 return $evt if $evt;
1564 my $holds = $apputils->simple_scalar_request(
1566 "open-ils.storage.direct.action.hold_request.search_where.atomic",
1568 fulfillment_time => {"=" => undef } }
1572 for my $h (@$holds) {
1573 next unless $h->capture_time;
1575 my $copy = $apputils->simple_scalar_request(
1577 "open-ils.storage.direct.asset.copy.retrieve",
1581 if ($copy->status == 8) {
1586 return { total => scalar(@$holds), ready => scalar(@ready) };
1590 __PACKAGE__->register_method(
1591 method => "checkedout_count",
1592 api_name => "open-ils.actor.user.checked_out.count",
1594 notes => <<" NOTES");
1595 Returns a transaction record
1597 sub checkedout_count {
1598 my( $self, $client, $login_session, $userid ) = @_;
1600 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1601 $login_session, $userid, 'VIEW_CIRCULATIONS' );
1602 return $evt if $evt;
1605 my $circs = $apputils->simple_scalar_request(
1607 "open-ils.storage.direct.action.circulation.search_where.atomic",
1609 checkin_time => {"=" => undef } }
1612 my $parser = DateTime::Format::ISO8601->new;
1615 for my $c (@$circs) {
1616 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
1617 my $due = $due_dt->epoch;
1619 if ($due < DateTime->today->epoch) {
1624 return { total => scalar(@$circs), overdue => scalar(@overdue) };
1627 __PACKAGE__->register_method(
1628 method => "user_transaction_history",
1629 api_name => "open-ils.actor.user.transactions.history",
1631 notes => <<" NOTES");
1632 Returns a list of billable transaction ids for a user, optionally by type
1634 __PACKAGE__->register_method(
1635 method => "user_transaction_history",
1636 api_name => "open-ils.actor.user.transactions.history.have_charge",
1638 notes => <<" NOTES");
1639 Returns a list of billable transaction ids for a user that have an initial charge, optionally by type
1641 sub user_transaction_history {
1642 my( $self, $client, $login_session, $user_id, $type ) = @_;
1644 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1645 $login_session, $user_id, 'VIEW_USER_TRANSACTIONS' );
1646 return $evt if $evt;
1648 my $api = $self->api_name();
1652 @xact = (xact_type => $type) if(defined($type));
1653 @charge = (total_owed => { ">" => 0}) if($api =~ /have_charge/);
1655 my $trans = $apputils->simple_scalar_request(
1657 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1658 { usr => $user_id, @xact, @charge }, { order_by => 'xact_start DESC' });
1660 return [ map { $_->id } @$trans ];
1664 __PACKAGE__->register_method(
1665 method => "user_perms",
1666 api_name => "open-ils.actor.permissions.user_perms.retrieve",
1668 notes => <<" NOTES");
1669 Returns a list of permissions
1672 my( $self, $client, $authtoken, $user ) = @_;
1674 my( $staff, $evt ) = $apputils->checkses($authtoken);
1675 return $evt if $evt;
1677 $user ||= $staff->id;
1679 if( $user != $staff->id and $evt = $apputils->check_perms( $staff->id, $staff->home_ou, 'VIEW_PERMISSION') ) {
1683 return $apputils->simple_scalar_request(
1685 "open-ils.storage.permission.user_perms.atomic",
1689 __PACKAGE__->register_method(
1690 method => "retrieve_perms",
1691 api_name => "open-ils.actor.permissions.retrieve",
1692 notes => <<" NOTES");
1693 Returns a list of permissions
1695 sub retrieve_perms {
1696 my( $self, $client ) = @_;
1697 return $apputils->simple_scalar_request(
1699 "open-ils.storage.direct.permission.perm_list.retrieve.all.atomic");
1702 __PACKAGE__->register_method(
1703 method => "retrieve_groups",
1704 api_name => "open-ils.actor.groups.retrieve",
1705 notes => <<" NOTES");
1706 Returns a list of user groupss
1708 sub retrieve_groups {
1709 my( $self, $client ) = @_;
1710 return $apputils->simple_scalar_request(
1712 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1715 __PACKAGE__->register_method(
1716 method => "retrieve_org_address",
1717 api_name => "open-ils.actor.org_unit.address.retrieve",
1718 notes => <<' NOTES');
1719 Returns an org_unit address by ID
1720 @param An org_address ID
1722 sub retrieve_org_address {
1723 my( $self, $client, $id ) = @_;
1724 return $apputils->simple_scalar_request(
1726 "open-ils.storage.direct.actor.org_address.retrieve",
1731 __PACKAGE__->register_method(
1732 method => "retrieve_groups_tree",
1733 api_name => "open-ils.actor.groups.tree.retrieve",
1734 notes => <<" NOTES");
1735 Returns a list of user groups
1737 sub retrieve_groups_tree {
1738 my( $self, $client ) = @_;
1739 my $groups = $apputils->simple_scalar_request(
1741 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1742 return $self->build_group_tree($groups);
1746 # turns an org list into an org tree
1747 sub build_group_tree {
1749 my( $self, $grplist) = @_;
1751 return $grplist unless (
1752 ref($grplist) and @$grplist > 1 );
1754 my @list = sort { $a->name cmp $b->name } @$grplist;
1757 for my $grp (@list) {
1759 if ($grp and !defined($grp->parent)) {
1763 my ($parent) = grep { $_->id == $grp->parent} @list;
1765 $parent->children([]) unless defined($parent->children);
1766 push( @{$parent->children}, $grp );
1774 __PACKAGE__->register_method(
1775 method => "add_user_to_groups",
1776 api_name => "open-ils.actor.user.set_groups",
1777 notes => <<" NOTES");
1778 Adds a user to one or more permission groups
1781 sub add_user_to_groups {
1782 my( $self, $client, $authtoken, $userid, $groups ) = @_;
1784 my( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1785 $authtoken, $userid, 'CREATE_USER_GROUP_LINK' );
1786 return $evt if $evt;
1788 ( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1789 $authtoken, $userid, 'REMOVE_USER_GROUP_LINK' );
1790 return $evt if $evt;
1792 $apputils->simplereq(
1794 'open-ils.storage.direct.permission.usr_grp_map.mass_delete', { usr => $userid } );
1796 for my $group (@$groups) {
1797 my $link = Fieldmapper::permission::usr_grp_map->new;
1799 $link->usr($userid);
1801 my $id = $apputils->simplereq(
1803 'open-ils.storage.direct.permission.usr_grp_map.create', $link );
1809 __PACKAGE__->register_method(
1810 method => "get_user_perm_groups",
1811 api_name => "open-ils.actor.user.get_groups",
1812 notes => <<" NOTES");
1813 Retrieve a user's permission groups.
1817 sub get_user_perm_groups {
1818 my( $self, $client, $authtoken, $userid ) = @_;
1820 my( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1821 $authtoken, $userid, 'VIEW_PERM_GROUPS' );
1822 return $evt if $evt;
1824 return $apputils->simplereq(
1826 'open-ils.storage.direct.permission.usr_grp_map.search.usr.atomic', $userid );
1831 __PACKAGE__->register_method (
1832 method => 'register_workstation',
1833 api_name => 'open-ils.actor.workstation.register',
1835 Registers a new workstion in the system
1836 @param authtoken The login session key
1837 @param name The name of the workstation id
1838 @param owner The org unit that owns this workstation
1839 @return The workstation id on success, WORKSTATION_NAME_EXISTS
1840 if the name is already in use.
1843 sub register_workstation {
1844 my( $self, $connection, $authtoken, $name, $owner ) = @_;
1845 my( $requestor, $evt ) = $U->checkses($authtoken);
1846 return $evt if $evt;
1847 $evt = $U->check_perms($requestor->id, $owner, 'REGISTER_WORKSTATION');
1848 return $evt if $evt;
1850 my $ws = $U->storagereq(
1851 'open-ils.storage.direct.actor.workstation.search.name', $name );
1852 return OpenILS::Event->new('WORKSTATION_NAME_EXISTS') if $ws;
1854 $ws = Fieldmapper::actor::workstation->new;
1855 $ws->owning_lib($owner);
1858 my $id = $U->storagereq(
1859 'open-ils.storage.direct.actor.workstation.create', $ws );
1860 return $U->DB_UPDATE_FAILED($ws) unless $id;
1867 __PACKAGE__->register_method (
1868 method => 'fetch_patron_note',
1869 api_name => 'open-ils.actor.note.retrieve.all',
1871 Returns a list of notes for a given user
1872 Requestor must have VIEW_USER permission if pub==false and
1873 @param authtoken The login session key
1874 @param args Hash of params including
1875 patronid : the patron's id
1876 pub : true if retrieving only public notes
1880 sub fetch_patron_note {
1881 my( $self, $conn, $authtoken, $args ) = @_;
1882 my $patronid = $$args{patronid};
1884 my($reqr, $evt) = $U->checkses($authtoken);
1887 ($patron, $evt) = $U->fetch_user($patronid);
1888 return $evt if $evt;
1891 if( $patronid ne $reqr->id ) {
1892 $evt = $U->check_perms($reqr->id, $patron->home_ou, 'VIEW_USER');
1893 return $evt if $evt;
1895 return $U->storagereq(
1896 'open-ils.storage.direct.actor.usr_note.search_where.atomic',
1897 { usr => $patronid, pub => 't' } );
1900 $evt = $U->check_perms($reqr->id, $patron->home_ou, 'VIEW_USER');
1901 return $evt if $evt;
1903 return $U->storagereq(
1904 'open-ils.storage.direct.actor.usr_note.search.usr.atomic', $patronid );
1907 __PACKAGE__->register_method (
1908 method => 'create_user_note',
1909 api_name => 'open-ils.actor.note.create',
1911 Creates a new note for the given user
1912 @param authtoken The login session key
1913 @param note The note object
1916 sub create_user_note {
1917 my( $self, $conn, $authtoken, $note ) = @_;
1918 my( $reqr, $patron, $evt ) =
1919 $U->checkses_requestor($authtoken, $note->usr, 'UPDATE_USER');
1920 return $evt if $evt;
1921 $logger->activity("user ".$reqr->id." creating note for user ".$note->usr);
1923 $note->pub('f') unless $note->pub;
1924 $note->creator($reqr->id);
1925 my $id = $U->storagereq(
1926 'open-ils.storage.direct.actor.usr_note.create', $note );
1927 return $U->DB_UPDATE_FAILED($note) unless $id;
1932 __PACKAGE__->register_method (
1933 method => 'delete_user_note',
1934 api_name => 'open-ils.actor.note.delete',
1936 Deletes a note for the given user
1937 @param authtoken The login session key
1938 @param noteid The note id
1941 sub delete_user_note {
1942 my( $self, $conn, $authtoken, $noteid ) = @_;
1944 my $note = $U->storagereq(
1945 'open-ils.storage.direct.actor.usr_note.retrieve', $noteid);
1946 return OpenILS::Event->new('USER_NOTE_NOT_FOUND') unless $note;
1948 my( $reqr, $patron, $evt ) =
1949 $U->checkses_requestor($authtoken, $note->usr, 'UPDATE_USER');
1950 return $evt if $evt;
1951 $logger->activity("user ".$reqr->id." deleting note [$noteid] for user ".$note->usr);
1953 my $stat = $U->storagereq(
1954 'open-ils.storage.direct.actor.usr_note.delete', $noteid );
1955 return $U->DB_UPDATE_FAILED($note) unless defined $stat;
1961 __PACKAGE__->register_method (
1962 method => 'create_closed_date',
1963 api_name => 'open-ils.actor.org_unit.closed_date.create',
1965 Creates a new closing entry for the given org_unit
1966 @param authtoken The login session key
1967 @param note The closed_date object
1970 sub create_closed_date {
1971 my( $self, $conn, $authtoken, $cd ) = @_;
1973 my( $user, $evt ) = $U->checkses($authtoken);
1974 return $evt if $evt;
1976 $evt = $U->check_perms($user->id, $cd->org_unit, 'CREATE_CLOSEING');
1977 return $evt if $evt;
1979 $logger->activity("user ".$user->id." creating library closing for ".$cd->org_unit);
1981 my $id = $U->storagereq(
1982 'open-ils.storage.direct.actor.org_unit.closed_date.create', $cd );
1983 return $U->DB_UPDATE_FAILED($cd) unless $id;
1988 __PACKAGE__->register_method (
1989 method => 'delete_closed_date',
1990 api_name => 'open-ils.actor.org_unit.closed_date.delete',
1992 Deletes a closing entry for the given org_unit
1993 @param authtoken The login session key
1994 @param noteid The close_date id
1997 sub delete_closed_date {
1998 my( $self, $conn, $authtoken, $cd ) = @_;
2000 my( $user, $evt ) = $U->checkses($authtoken);
2001 return $evt if $evt;
2004 ($cd_obj, $evt) = fetch_closed_date($cd);
2005 return $evt if $evt;
2007 $evt = $U->check_perms($user->id, $cd->org_unit, 'DELETE_CLOSEING');
2008 return $evt if $evt;
2010 $logger->activity("user ".$user->id." deleting library closing for ".$cd->org_unit);
2012 my $stat = $U->storagereq(
2013 'open-ils.storage.direct.actor.org_unit.closed_date.delete', $cd );
2014 return $U->DB_UPDATE_FAILED($cd) unless $stat;
2019 #__PACKAGE__->register_method(