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;
32 OpenILS::Application::Actor::Container->initialize();
35 my $apputils = "OpenILS::Application::AppUtils";
38 sub _d { warn "Patron:\n" . Dumper(shift()); }
43 my $set_user_settings;
46 __PACKAGE__->register_method(
47 method => "set_user_settings",
48 api_name => "open-ils.actor.patron.settings.update",
50 sub set_user_settings {
51 my( $self, $client, $user_session, $uid, $settings ) = @_;
53 $logger->debug("Setting user settings: $user_session, $uid, " . Dumper($settings));
55 my( $staff, $user, $evt ) =
56 $apputils->checkses_requestor( $user_session, $uid, 'UPDATE_USER' );
61 # [{ usr => $user->id, name => $_}, {value => $$settings{$_}}] } keys %$settings;
64 [{ usr => $user->id, name => $_}, {value => $$settings{$_}}] } keys %$settings;
66 $logger->activity("User " . $staff->id . " updating user $uid settings with: " . Dumper(\@params));
68 return $apputils->simplereq(
70 'open-ils.storage.direct.actor.user_setting.batch.merge', @params );
76 __PACKAGE__->register_method(
77 method => "set_ou_settings",
78 api_name => "open-ils.actor.org_unit.settings.update",
81 my( $self, $client, $user_session, $ouid, $settings ) = @_;
83 my( $staff, $evt ) = $apputils->checkses( $user_session );
85 $evt = $apputils->check_perms( $staff->id, $ouid, 'UPDATE_ORG_UNIT' );
90 map { [{ org_unit => $ouid, name => $_}, {value => $$settings{$_}}] } keys %$settings;
92 $logger->activity("Updating org unit [$ouid] settings with: " . Dumper($params));
94 return $apputils->simplereq(
96 'open-ils.storage.direct.actor.org_unit_setting.merge', @$params );
100 my $fetch_user_settings;
101 my $fetch_ou_settings;
103 __PACKAGE__->register_method(
104 method => "user_settings",
105 api_name => "open-ils.actor.patron.settings.retrieve",
108 my( $self, $client, $user_session, $uid ) = @_;
110 my( $staff, $user, $evt ) =
111 $apputils->checkses_requestor( $user_session, $uid, 'VIEW_USER' );
114 $logger->debug("User " . $staff->id . " fetching user $uid\n");
115 my $s = $apputils->simplereq(
117 'open-ils.storage.direct.actor.user_setting.search.usr.atomic',$uid );
119 return { map { ($_->name,$_->value) } @$s };
124 __PACKAGE__->register_method(
125 method => "ou_settings",
126 api_name => "open-ils.actor.org_unit.settings.retrieve",
129 my( $self, $client, $ouid ) = @_;
131 $logger->info("Fetching org unit settings for org $ouid");
133 my $s = $apputils->simplereq(
135 'open-ils.storage.direct.actor.org_unit_setting.search.org_unit.atomic', $ouid);
137 return { map { ($_->name,$_->value) } @$s };
140 __PACKAGE__->register_method (
141 method => "ou_setting_delete",
142 api_name => 'open-ils.actor.org_setting.delete',
144 Deletes a specific org unit setting for a specific location
145 @param authtoken The login session key
146 @param orgid The org unit whose setting we're changing
147 @param setting The name of the setting to delete
148 @return True value on success.
152 sub ou_setting_delete {
153 my( $self, $conn, $authtoken, $orgid, $setting ) = @_;
154 my( $reqr, $evt) = $U->checkses($authtoken);
156 $evt = $U->check_perms($reqr->id, $orgid, 'UPDATE_ORG_SETTING');
159 my $id = $U->storagereq(
160 'open-ils.storage.id_list.actor.org_unit_setting.search_where',
161 { name => $setting, org_unit => $orgid } );
163 $logger->debug("Retrieved setting $id in org unit setting delete");
165 my $s = $U->storagereq(
166 'open-ils.storage.direct.actor.org_unit_setting.delete', $id );
168 $logger->activity("User ".$reqr->id." deleted org unit setting $id") if $s;
174 __PACKAGE__->register_method(
175 method => "update_patron",
176 api_name => "open-ils.actor.patron.update",);
179 my( $self, $client, $user_session, $patron ) = @_;
181 my $session = $apputils->start_db_session();
184 $logger->info("Creating new patron...") if $patron->isnew;
185 $logger->info("Updating Patron: " . $patron->id) unless $patron->isnew;
187 my( $user_obj, $evt ) = $U->checkses($user_session);
190 # XXX does this user have permission to add/create users. Granularity?
191 # $new_patron is the patron in progress. $patron is the original patron
192 # passed in with the method. new_patron will change as the components
193 # of patron are added/updated.
197 # unflesh the real items on the patron
198 $patron->card( $patron->card->id ) if(ref($patron->card));
199 $patron->billing_address( $patron->billing_address->id )
200 if(ref($patron->billing_address));
201 $patron->mailing_address( $patron->mailing_address->id )
202 if(ref($patron->mailing_address));
204 # create/update the patron first so we can use his id
205 if($patron->isnew()) {
206 ( $new_patron, $evt ) = _add_patron($session, _clone_patron($patron), $user_obj);
208 } else { $new_patron = $patron; }
210 ( $new_patron, $evt ) = _add_update_addresses($session, $patron, $new_patron, $user_obj);
213 ( $new_patron, $evt ) = _add_update_cards($session, $patron, $new_patron, $user_obj);
216 ( $new_patron, $evt ) = _add_survey_responses($session, $patron, $new_patron, $user_obj);
219 # re-update the patron if anything has happened to him during this process
220 if($new_patron->ischanged()) {
221 ( $new_patron, $evt ) = _update_patron($session, $new_patron, $user_obj);
225 #$session = OpenSRF::AppSession->create("open-ils.storage"); # why did i put this here?
227 ($new_patron, $evt) = _create_stat_maps($session, $user_session, $patron, $new_patron, $user_obj);
230 ($new_patron, $evt) = _create_perm_maps($session, $user_session, $patron, $new_patron, $user_obj);
233 ($new_patron, $evt) = _create_standing_penalties($session, $user_session, $patron, $new_patron, $user_obj);
236 $logger->activity("user ".$user_obj->id." updating/creating user ".$new_patron->id);
237 $apputils->commit_db_session($session);
239 #warn "Patron Update/Create complete\n";
240 return flesh_user($new_patron->id());
246 __PACKAGE__->register_method(
247 method => "user_retrieve_fleshed_by_id",
248 api_name => "open-ils.actor.user.fleshed.retrieve",);
250 sub user_retrieve_fleshed_by_id {
251 my( $self, $client, $user_session, $user_id ) = @_;
253 my( $requestor, $target, $evt ) = $apputils->
254 checkses_requestor( $user_session, $user_id, 'VIEW_USER' );
257 return flesh_user($user_id);
261 # fleshes: card, cards, address, addresses, stat_cat_entries, standing_penalties
269 $session = OpenSRF::AppSession->create("open-ils.storage");
273 # grab the user with the given id
274 my $ureq = $session->request(
275 "open-ils.storage.direct.actor.user.retrieve", $id);
276 my $user = $ureq->gather(1);
278 if(!$user) { return undef; }
281 my $cards_req = $session->request(
282 "open-ils.storage.direct.actor.card.search.usr.atomic",
284 $user->cards( $cards_req->gather(1) );
286 for my $c(@{$user->cards}) {
287 if($c->id == $user->card || $c->id eq $user->card ) {
288 #warn "Setting my card to " . $c->id . "\n";
293 my $add_req = $session->request(
294 "open-ils.storage.direct.actor.user_address.search.usr.atomic",
296 $user->addresses( $add_req->gather(1) );
298 for my $c(@{$user->addresses}) {
299 if($c->id eq $user->billing_address ) { $user->billing_address($c); }
300 if($c->id eq $user->mailing_address ) { $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 my $standing_penalties_req = $session->request(
309 "open-ils.storage.direct.actor.user_standing_penalty.search.usr.atomic",
311 $user->standing_penalties($standing_penalties_req->gather(1));
313 if($kill) { $session->disconnect(); }
314 $user->clear_passwd();
320 # clone and clear stuff that would break the database
324 my $new_patron = $patron->clone;
326 # Using the Fieldmapper clone method
327 #my $new_patron = Fieldmapper::actor::user->new();
329 #my $fmap = $Fieldmapper::fieldmap;
330 #no strict; # shallow clone, may be useful in the fieldmapper
332 # (keys %{$fmap->{"Fieldmapper::actor::user"}->{'fields'}}) {
333 # $new_patron->$field( $patron->$field() );
338 $new_patron->clear_billing_address();
339 $new_patron->clear_mailing_address();
340 $new_patron->clear_addresses();
341 $new_patron->clear_card();
342 $new_patron->clear_cards();
343 $new_patron->clear_id();
344 $new_patron->clear_isnew();
345 $new_patron->clear_ischanged();
346 $new_patron->clear_isdeleted();
347 $new_patron->clear_stat_cat_entries();
348 $new_patron->clear_permissions();
349 $new_patron->clear_standing_penalties();
359 my $user_obj = shift;
361 my $evt = $U->check_perms($user_obj->id, $patron->home_ou, 'CREATE_USER');
362 return (undef, $evt) if $evt;
364 $logger->info("Creating new user in the DB with username: ".$patron->usrname());
366 my $id = $session->request(
367 "open-ils.storage.direct.actor.user.create", $patron)->gather(1);
368 return (undef, $U->DB_UPDATE_FAILED($patron)) unless $id;
370 $logger->info("Successfully created new user [$id] in DB");
372 return ( $session->request(
373 "open-ils.storage.direct.actor.user.retrieve", $id)->gather(1), undef );
378 my( $session, $patron, $user_obj) = @_;
380 $logger->info("Updating patron ".$patron->id." in DB");
381 my $evt = $U->check_perms($user_obj->id, $patron->home_ou, 'UPDATE_USER');
382 return (undef, $evt) if $evt;
384 $patron->clear_passwd unless $patron->passwd;
386 if(!$patron->ident_type) {
387 $patron->clear_ident_type;
388 $patron->clear_ident_value;
391 if(!$patron->ident_type2) {
392 $patron->clear_ident_type2;
393 $patron->clear_ident_value2;
396 my $stat = $session->request(
397 "open-ils.storage.direct.actor.user.update",$patron )->gather(1);
398 return (undef, $U->DB_UPDATE_FAILED($patron)) unless defined($stat);
404 sub _add_update_addresses {
408 my $new_patron = shift;
412 my $current_id; # id of the address before creation
414 for my $address (@{$patron->addresses()}) {
416 $address->usr($new_patron->id());
418 if(ref($address) and $address->isnew()) {
420 $current_id = $address->id();
421 ($address, $evt) = _add_address($session,$address);
422 return (undef, $evt) if $evt;
424 if( $patron->billing_address() and
425 $patron->billing_address() == $current_id ) {
426 $new_patron->billing_address($address->id());
427 $new_patron->ischanged(1);
430 if( $patron->mailing_address() and
431 $patron->mailing_address() == $current_id ) {
432 $new_patron->mailing_address($address->id());
433 $new_patron->ischanged(1);
436 } elsif( ref($address) and $address->ischanged() ) {
438 $address->usr($new_patron->id());
439 ($address, $evt) = _update_address($session, $address);
440 return (undef, $evt) if $evt;
442 } elsif( ref($address) and $address->isdeleted() ) {
444 if( $address->id() == $new_patron->mailing_address() ) {
445 $new_patron->clear_mailing_address();
446 ($new_patron, $evt) = _update_patron($session, $new_patron);
447 return (undef, $evt) if $evt;
450 if( $address->id() == $new_patron->billing_address() ) {
451 $new_patron->clear_billing_address();
452 ($new_patron, $evt) = _update_patron($session, $new_patron);
453 return (undef, $evt) if $evt;
456 $evt = _delete_address($session, $address);
457 return (undef, $evt) if $evt;
461 return ( $new_patron, undef );
465 # adds an address to the db and returns the address with new id
467 my($session, $address) = @_;
468 $address->clear_id();
470 $logger->info("Creating new address at street ".$address->street1);
472 # put the address into the database
473 my $id = $session->request(
474 "open-ils.storage.direct.actor.user_address.create", $address )->gather(1);
475 return (undef, $U->DB_UPDATE_FAILED($address)) unless $id;
478 return ($address, undef);
482 sub _update_address {
483 my( $session, $address ) = @_;
485 $logger->info("Updating address ".$address->id." in the DB");
487 my $stat = $session->request(
488 "open-ils.storage.direct.actor.user_address.update", $address )->gather(1);
490 return (undef, $U->DB_UPDATE_FAILED($address)) unless defined($stat);
491 return ($address, undef);
496 sub _add_update_cards {
500 my $new_patron = shift;
504 my $virtual_id; #id of the card before creation
505 for my $card (@{$patron->cards()}) {
507 $card->usr($new_patron->id());
509 if(ref($card) and $card->isnew()) {
511 $virtual_id = $card->id();
512 ( $card, $evt ) = _add_card($session,$card);
513 return (undef, $evt) if $evt;
515 #if(ref($patron->card)) { $patron->card($patron->card->id); }
516 if($patron->card() == $virtual_id) {
517 $new_patron->card($card->id());
518 $new_patron->ischanged(1);
521 } elsif( ref($card) and $card->ischanged() ) {
522 $card->usr($new_patron->id());
523 $evt = _update_card($session, $card);
524 return (undef, $evt) if $evt;
528 return ( $new_patron, undef );
532 # adds an card to the db and returns the card with new id
534 my( $session, $card ) = @_;
537 $logger->info("Adding new patron card ".$card->barcode);
539 my $id = $session->request(
540 "open-ils.storage.direct.actor.card.create", $card )->gather(1);
541 return (undef, $U->DB_UPDATE_FAILED($card)) unless $id;
542 $logger->info("Successfully created patron card $id");
545 return ( $card, undef );
549 # returns event on error. returns undef otherwise
551 my( $session, $card ) = @_;
552 $logger->info("Updating patron card ".$card->id);
554 my $stat = $session->request(
555 "open-ils.storage.direct.actor.card.update", $card )->gather(1);
556 return $U->DB_UPDATE_FAILED($card) unless defined($stat);
563 # returns event on error. returns undef otherwise
564 sub _delete_address {
565 my( $session, $address ) = @_;
567 $logger->info("Deleting address ".$address->id." from DB");
569 my $stat = $session->request(
570 "open-ils.storage.direct.actor.user_address.delete", $address )->gather(1);
572 return $U->DB_UPDATE_FAILED($address) unless defined($stat);
578 sub _add_survey_responses {
579 my ($session, $patron, $new_patron) = @_;
581 $logger->info( "Updating survey responses for patron ".$new_patron->id );
583 my $responses = $patron->survey_responses;
587 $_->usr($new_patron->id) for (@$responses);
589 my $evt = $U->simplereq( "open-ils.circ",
590 "open-ils.circ.survey.submit.user_id", $responses );
592 return (undef, $evt) if defined($U->event_code($evt));
596 return ( $new_patron, undef );
600 sub _create_stat_maps {
602 my($session, $user_session, $patron, $new_patron) = @_;
604 my $maps = $patron->stat_cat_entries();
606 for my $map (@$maps) {
608 my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
610 if ($map->isdeleted()) {
611 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.delete";
613 } elsif ($map->isnew()) {
614 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
619 $map->target_usr($new_patron->id);
622 $logger->info("Updating stat entry with method $method and map $map");
624 my $stat = $session->request($method, $map)->gather(1);
625 return (undef, $U->DB_UPDATE_FAILED($map)) unless defined($stat);
629 return ($new_patron, undef);
632 sub _create_perm_maps {
634 my($session, $user_session, $patron, $new_patron) = @_;
636 my $maps = $patron->permissions;
638 for my $map (@$maps) {
640 my $method = "open-ils.storage.direct.permission.usr_perm_map.update";
641 if ($map->isdeleted()) {
642 $method = "open-ils.storage.direct.permission.usr_perm_map.delete";
643 } elsif ($map->isnew()) {
644 $method = "open-ils.storage.direct.permission.usr_perm_map.create";
649 $map->usr($new_patron->id);
651 #warn( "Updating permissions with method $method and session $user_session and map $map" );
652 $logger->info( "Updating permissions with method $method and map $map" );
654 my $stat = $session->request($method, $map)->gather(1);
655 return (undef, $U->DB_UPDATE_FAILED($map)) unless defined($stat);
659 return ($new_patron, undef);
663 sub _create_standing_penalties {
665 my($session, $user_session, $patron, $new_patron) = @_;
667 my $maps = $patron->standing_penalties;
670 for my $map (@$maps) {
672 if ($map->isdeleted()) {
673 $method = "open-ils.storage.direct.actor.user_standing_penalty.delete";
674 } elsif ($map->isnew()) {
675 $method = "open-ils.storage.direct.actor.user_standing_penalty.create";
681 $map->usr($new_patron->id);
683 $logger->debug( "Updating standing penalty with method $method and session $user_session and map $map" );
685 my $stat = $session->request($method, $map)->gather(1);
686 return (undef, $U->DB_UPDATE_FAILED($map)) unless $stat;
689 return ($new_patron, undef);
694 __PACKAGE__->register_method(
695 method => "search_username",
696 api_name => "open-ils.actor.user.search.username",
699 sub search_username {
700 my($self, $client, $username) = @_;
701 my $users = OpenILS::Application::AppUtils->simple_scalar_request(
703 "open-ils.storage.direct.actor.user.search.usrname.atomic",
711 __PACKAGE__->register_method(
712 method => "user_retrieve_by_barcode",
713 api_name => "open-ils.actor.user.fleshed.retrieve_by_barcode",);
715 sub user_retrieve_by_barcode {
716 my($self, $client, $user_session, $barcode) = @_;
718 $logger->debug("Searching for user with barcode $barcode");
719 my ($user_obj, $evt) = $apputils->checkses($user_session);
723 my $session = OpenSRF::AppSession->create("open-ils.storage");
725 # find the card with the given barcode
726 my $creq = $session->request(
727 "open-ils.storage.direct.actor.card.search.barcode.atomic",
729 my $card = $creq->gather(1);
731 if(!$card || !$card->[0]) {
732 $session->disconnect();
733 return OpenILS::Event->new( 'ACTOR_USER_NOT_FOUND' );
737 my $user = flesh_user($card->usr(), $session);
739 $evt = $U->check_perms($user_obj->id, $user->home_ou, 'VIEW_USER');
742 $session->disconnect();
743 if(!$user) { return OpenILS::Event->new( 'ACTOR_USER_NOT_FOUND' ); }
750 __PACKAGE__->register_method(
751 method => "get_user_by_id",
752 api_name => "open-ils.actor.user.retrieve",);
755 my ($self, $client, $user_session, $id) = @_;
757 my $user_obj = $apputils->check_user_session( $user_session );
759 return $apputils->simple_scalar_request(
761 "open-ils.storage.direct.actor.user.retrieve",
767 __PACKAGE__->register_method(
768 method => "get_org_types",
769 api_name => "open-ils.actor.org_types.retrieve",);
773 my($self, $client) = @_;
775 return $org_types if $org_types;
777 $apputils->simple_scalar_request(
779 "open-ils.storage.direct.actor.org_unit_type.retrieve.all.atomic" );
784 __PACKAGE__->register_method(
785 method => "get_user_profiles",
786 api_name => "open-ils.actor.user.profiles.retrieve",
790 sub get_user_profiles {
791 return $user_profiles if $user_profiles;
793 return $user_profiles =
794 $apputils->simple_scalar_request(
796 "open-ils.storage.direct.actor.profile.retrieve.all.atomic");
801 __PACKAGE__->register_method(
802 method => "get_user_ident_types",
803 api_name => "open-ils.actor.user.ident_types.retrieve",
806 sub get_user_ident_types {
807 return $ident_types if $ident_types;
808 return $ident_types =
809 $apputils->simple_scalar_request(
811 "open-ils.storage.direct.config.identification_type.retrieve.all.atomic" );
817 __PACKAGE__->register_method(
818 method => "get_org_unit",
819 api_name => "open-ils.actor.org_unit.retrieve",
824 my( $self, $client, $user_session, $org_id ) = @_;
826 if(defined($user_session) && !defined($org_id)) {
828 OpenILS::Application::AppUtils->check_user_session( $user_session ); #throws EX on error
829 if(!defined($org_id)) {
830 $org_id = $user_obj->home_ou;
835 my $home_ou = OpenILS::Application::AppUtils->simple_scalar_request(
837 "open-ils.storage.direct.actor.org_unit.retrieve",
843 __PACKAGE__->register_method(
844 method => "search_org_unit",
845 api_name => "open-ils.actor.org_unit_list.search",
848 sub search_org_unit {
850 my( $self, $client, $field, $value ) = @_;
852 my $list = OpenILS::Application::AppUtils->simple_scalar_request(
854 "open-ils.storage.direct.actor.org_unit.search.$field.atomic",
863 __PACKAGE__->register_method(
864 method => "get_org_tree",
865 api_name => "open-ils.actor.org_tree.retrieve",
867 note => "Returns the entire org tree structure",
871 my( $self, $client) = @_;
874 $cache_client = OpenSRF::Utils::Cache->new("global", 0);
876 # see if it's in the cache
877 #warn "Getting ORG Tree\n";
878 my $tree = $cache_client->get_cache('orgtree');
880 #warn "Found orgtree in cache. returning...\n";
884 my $orglist = $apputils->simple_scalar_request(
886 "open-ils.storage.direct.actor.org_unit.retrieve.all.atomic" );
889 #warn "found org list\n";
892 $tree = $self->build_org_tree($orglist);
893 $cache_client->put_cache('orgtree', $tree);
899 # turns an org list into an org tree
902 my( $self, $orglist) = @_;
904 return $orglist unless (
905 ref($orglist) and @$orglist > 1 );
908 $a->ou_type <=> $b->ou_type ||
909 $a->name cmp $b->name } @$orglist;
911 for my $org (@list) {
913 next unless ($org and defined($org->parent_ou));
914 my ($parent) = grep { $_->id == $org->parent_ou } @list;
917 $parent->children([]) unless defined($parent->children);
918 push( @{$parent->children}, $org );
926 __PACKAGE__->register_method(
927 method => "get_org_descendants",
928 api_name => "open-ils.actor.org_tree.descendants.retrieve"
931 # depth is optional. org_unit is the id
932 sub get_org_descendants {
933 my( $self, $client, $org_unit, $depth ) = @_;
934 my $orglist = $apputils->simple_scalar_request(
936 "open-ils.storage.actor.org_unit.descendants.atomic",
938 return $self->build_org_tree($orglist);
942 __PACKAGE__->register_method(
943 method => "get_org_ancestors",
944 api_name => "open-ils.actor.org_tree.ancestors.retrieve"
947 # depth is optional. org_unit is the id
948 sub get_org_ancestors {
949 my( $self, $client, $org_unit, $depth ) = @_;
950 my $orglist = $apputils->simple_scalar_request(
952 "open-ils.storage.actor.org_unit.ancestors.atomic",
954 return $self->build_org_tree($orglist);
958 __PACKAGE__->register_method(
959 method => "get_standings",
960 api_name => "open-ils.actor.standings.retrieve"
965 return $user_standings if $user_standings;
966 return $user_standings =
967 $apputils->simple_scalar_request(
969 "open-ils.storage.direct.config.standing.retrieve.all.atomic" );
974 __PACKAGE__->register_method(
975 method => "get_my_org_path",
976 api_name => "open-ils.actor.org_unit.full_path.retrieve"
979 sub get_my_org_path {
980 my( $self, $client, $user_session, $org_id ) = @_;
981 my $user_obj = $apputils->check_user_session($user_session);
982 if(!defined($org_id)) { $org_id = $user_obj->home_ou; }
984 return $apputils->simple_scalar_request(
986 "open-ils.storage.actor.org_unit.full_path.atomic",
991 __PACKAGE__->register_method(
992 method => "patron_adv_search",
993 api_name => "open-ils.actor.patron.search.advanced" );
994 sub patron_adv_search {
995 my( $self, $client, $auth, $search_hash, $search_limit, $search_sort ) = @_;
996 my $e = OpenILS::Utils::Editor->new(authtoken=>$auth);
997 return $e->event unless $e->checkauth;
998 return $e->event unless $e->allowed('VIEW_USER');
1000 "open-ils.storage.actor.user.crazy_search",
1001 $search_hash, $search_limit, $search_sort);
1006 sub _verify_password {
1007 my($user_session, $password) = @_;
1008 my $user_obj = $apputils->check_user_session($user_session);
1010 #grab the user with password
1011 $user_obj = $apputils->simple_scalar_request(
1013 "open-ils.storage.direct.actor.user.retrieve",
1016 if($user_obj->passwd eq $password) {
1024 __PACKAGE__->register_method(
1025 method => "update_password",
1026 api_name => "open-ils.actor.user.password.update");
1028 __PACKAGE__->register_method(
1029 method => "update_password",
1030 api_name => "open-ils.actor.user.username.update");
1032 __PACKAGE__->register_method(
1033 method => "update_password",
1034 api_name => "open-ils.actor.user.email.update");
1036 sub update_password {
1037 my( $self, $client, $user_session, $new_value, $current_password ) = @_;
1041 #warn "Updating user with method " .$self->api_name . "\n";
1042 my $user_obj = $apputils->check_user_session($user_session);
1044 if($self->api_name =~ /password/o) {
1046 #make sure they know the current password
1047 if(!_verify_password($user_session, md5_hex($current_password))) {
1048 return OpenILS::EX->new("USER_WRONG_PASSWORD")->ex;
1051 $user_obj->passwd($new_value);
1053 elsif($self->api_name =~ /username/o) {
1054 my $users = search_username(undef, undef, $new_value);
1055 if( $users and $users->[0] ) {
1056 return OpenILS::Event->new('USERNAME_EXISTS');
1058 $user_obj->usrname($new_value);
1061 elsif($self->api_name =~ /email/o) {
1062 #warn "Updating email to $new_value\n";
1063 $user_obj->email($new_value);
1066 my $session = $apputils->start_db_session();
1068 ( $user_obj, $evt ) = _update_patron($session, $user_obj, $user_obj);
1069 return $evt if $evt;
1071 $apputils->commit_db_session($session);
1073 if($user_obj) { return 1; }
1078 __PACKAGE__->register_method(
1079 method => "check_user_perms",
1080 api_name => "open-ils.actor.user.perm.check",
1081 notes => <<" NOTES");
1082 Takes a login session, user id, an org id, and an array of perm type strings. For each
1083 perm type, if the user does *not* have the given permission it is added
1084 to a list which is returned from the method. If all permissions
1085 are allowed, an empty list is returned
1086 if the logged in user does not match 'user_id', then the logged in user must
1087 have VIEW_PERMISSION priveleges.
1090 sub check_user_perms {
1091 my( $self, $client, $login_session, $user_id, $org_id, $perm_types ) = @_;
1093 my( $staff, $evt ) = $apputils->checkses($login_session);
1094 return $evt if $evt;
1096 if($staff->id ne $user_id) {
1097 if( my $evt = $apputils->check_perms(
1098 $staff->id, $org_id, 'VIEW_PERMISSION') ) {
1104 for my $perm (@$perm_types) {
1105 if($apputils->check_perms($user_id, $org_id, $perm)) {
1106 push @not_allowed, $perm;
1110 return \@not_allowed
1113 __PACKAGE__->register_method(
1114 method => "check_user_perms2",
1115 api_name => "open-ils.actor.user.perm.check.multi_org",
1117 Checks the permissions on a list of perms and orgs for a user
1118 @param authtoken The login session key
1119 @param user_id The id of the user to check
1120 @param orgs The array of org ids
1121 @param perms The array of permission names
1122 @return An array of [ orgId, permissionName ] arrays that FAILED the check
1123 if the logged in user does not match 'user_id', then the logged in user must
1124 have VIEW_PERMISSION priveleges.
1127 sub check_user_perms2 {
1128 my( $self, $client, $authtoken, $user_id, $orgs, $perms ) = @_;
1130 my( $staff, $target, $evt ) = $apputils->checkses_requestor(
1131 $authtoken, $user_id, 'VIEW_PERMISSION' );
1132 return $evt if $evt;
1135 for my $org (@$orgs) {
1136 for my $perm (@$perms) {
1137 if($apputils->check_perms($user_id, $org, $perm)) {
1138 push @not_allowed, [ $org, $perm ];
1143 return \@not_allowed
1147 __PACKAGE__->register_method(
1148 method => 'check_user_perms3',
1149 api_name => 'open-ils.actor.user.perm.highest_org',
1151 Returns the highest org unit id at which a user has a given permission
1152 If the requestor does not match the target user, the requestor must have
1153 'VIEW_PERMISSION' rights at the home org unit of the target user
1154 @param authtoken The login session key
1155 @param userid The id of the user in question
1156 @param perm The permission to check
1157 @return The org unit highest in the org tree within which the user has
1158 the requested permission
1161 sub check_user_perms3 {
1162 my( $self, $client, $authtoken, $userid, $perm ) = @_;
1164 my( $staff, $target, $org, $evt );
1166 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
1167 $authtoken, $userid, 'VIEW_PERMISSION' );
1168 return $evt if $evt;
1170 my $tree = $self->get_org_tree();
1171 return _find_highest_perm_org( $perm, $userid, $target->home_ou, $tree );
1175 sub _find_highest_perm_org {
1176 my ( $perm, $userid, $start_org, $org_tree ) = @_;
1177 my $org = $apputils->find_org($org_tree, $start_org );
1181 last if ($apputils->check_perms( $userid, $org->id, $perm )); # perm failed
1183 $org = $apputils->find_org( $org_tree, $org->parent_ou() );
1189 __PACKAGE__->register_method(
1190 method => 'check_user_perms4',
1191 api_name => 'open-ils.actor.user.perm.highest_org.batch',
1193 Returns the highest org unit id at which a user has a given permission
1194 If the requestor does not match the target user, the requestor must have
1195 'VIEW_PERMISSION' rights at the home org unit of the target user
1196 @param authtoken The login session key
1197 @param userid The id of the user in question
1198 @param perms An array of perm names to check
1199 @return An array of orgId's representing the org unit
1200 highest in the org tree within which the user has the requested permission
1201 The arrah of orgId's has matches the order of the perms array
1204 sub check_user_perms4 {
1205 my( $self, $client, $authtoken, $userid, $perms ) = @_;
1207 my( $staff, $target, $org, $evt );
1209 ( $staff, $target, $evt ) = $apputils->checkses_requestor(
1210 $authtoken, $userid, 'VIEW_PERMISSION' );
1211 return $evt if $evt;
1214 return [] unless ref($perms);
1215 my $tree = $self->get_org_tree();
1217 for my $p (@$perms) {
1218 push( @arr, _find_highest_perm_org( $p, $userid, $target->home_ou, $tree ) );
1226 __PACKAGE__->register_method(
1227 method => "user_fines_summary",
1228 api_name => "open-ils.actor.user.fines.summary",
1229 notes => <<" NOTES");
1230 Returns a short summary of the users total open fines, excluding voided fines
1231 Params are login_session, user_id
1232 Returns a 'mous' object.
1235 sub user_fines_summary {
1236 my( $self, $client, $login_session, $user_id ) = @_;
1238 my $user_obj = $apputils->check_user_session($login_session);
1239 if($user_obj->id ne $user_id) {
1240 if($apputils->check_user_perms($user_obj->id, $user_obj->home_ou, "VIEW_USER_FINES_SUMMARY")) {
1241 return OpenILS::Perm->new("VIEW_USER_FINES_SUMMARY");
1245 return $apputils->simple_scalar_request(
1247 "open-ils.storage.direct.money.open_user_summary.search.usr",
1255 __PACKAGE__->register_method(
1256 method => "user_transactions",
1257 api_name => "open-ils.actor.user.transactions",
1258 notes => <<" NOTES");
1259 Returns a list of open user transactions (mbts objects);
1260 Params are login_session, user_id
1261 Optional third parameter is the transactions type. defaults to all
1264 __PACKAGE__->register_method(
1265 method => "user_transactions",
1266 api_name => "open-ils.actor.user.transactions.have_charge",
1267 notes => <<" NOTES");
1268 Returns a list of all open user transactions (mbts objects) that have an initial charge
1269 Params are login_session, user_id
1270 Optional third parameter is the transactions type. defaults to all
1273 __PACKAGE__->register_method(
1274 method => "user_transactions",
1275 api_name => "open-ils.actor.user.transactions.have_balance",
1276 notes => <<" NOTES");
1277 Returns a list of all open user transactions (mbts objects) that have a balance
1278 Params are login_session, user_id
1279 Optional third parameter is the transactions type. defaults to all
1282 __PACKAGE__->register_method(
1283 method => "user_transactions",
1284 api_name => "open-ils.actor.user.transactions.fleshed",
1285 notes => <<" NOTES");
1286 Returns an object/hash of transaction, circ, title where transaction = an open
1287 user transactions (mbts objects), circ is the attached circluation, and title
1288 is the title the circ points to
1289 Params are login_session, user_id
1290 Optional third parameter is the transactions type. defaults to all
1293 __PACKAGE__->register_method(
1294 method => "user_transactions",
1295 api_name => "open-ils.actor.user.transactions.have_charge.fleshed",
1296 notes => <<" NOTES");
1297 Returns an object/hash of transaction, circ, title where transaction = an open
1298 user transactions that has an initial charge (mbts objects), circ is the
1299 attached circluation, and title is the title the circ points to
1300 Params are login_session, user_id
1301 Optional third parameter is the transactions type. defaults to all
1304 __PACKAGE__->register_method(
1305 method => "user_transactions",
1306 api_name => "open-ils.actor.user.transactions.have_balance.fleshed",
1307 notes => <<" NOTES");
1308 Returns an object/hash of transaction, circ, title where transaction = an open
1309 user transaction that has a balance (mbts objects), circ is the attached
1310 circluation, and title is the title the circ points to
1311 Params are login_session, user_id
1312 Optional third parameter is the transaction type. defaults to all
1315 __PACKAGE__->register_method(
1316 method => "user_transactions",
1317 api_name => "open-ils.actor.user.transactions.count",
1318 notes => <<" NOTES");
1319 Returns an object/hash of transaction, circ, title where transaction = an open
1320 user transactions (mbts objects), circ is the attached circluation, and title
1321 is the title the circ points to
1322 Params are login_session, user_id
1323 Optional third parameter is the transactions type. defaults to all
1326 __PACKAGE__->register_method(
1327 method => "user_transactions",
1328 api_name => "open-ils.actor.user.transactions.have_charge.count",
1329 notes => <<" NOTES");
1330 Returns an object/hash of transaction, circ, title where transaction = an open
1331 user transactions that has an initial charge (mbts objects), circ is the
1332 attached circluation, and title is the title the circ points to
1333 Params are login_session, user_id
1334 Optional third parameter is the transactions type. defaults to all
1337 __PACKAGE__->register_method(
1338 method => "user_transactions",
1339 api_name => "open-ils.actor.user.transactions.have_balance.count",
1340 notes => <<" NOTES");
1341 Returns an object/hash of transaction, circ, title where transaction = an open
1342 user transaction that has a balance (mbts objects), circ is the attached
1343 circluation, and title is the title the circ points to
1344 Params are login_session, user_id
1345 Optional third parameter is the transaction type. defaults to all
1348 __PACKAGE__->register_method(
1349 method => "user_transactions",
1350 api_name => "open-ils.actor.user.transactions.have_balance.total",
1351 notes => <<" NOTES");
1352 Returns an object/hash of transaction, circ, title where transaction = an open
1353 user transaction that has a balance (mbts objects), circ is the attached
1354 circluation, and title is the title the circ points to
1355 Params are login_session, user_id
1356 Optional third parameter is the transaction type. defaults to all
1361 sub user_transactions {
1362 my( $self, $client, $login_session, $user_id, $type ) = @_;
1364 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1365 $login_session, $user_id, 'VIEW_USER_TRANSACTIONS' );
1366 return $evt if $evt;
1368 my $api = $self->api_name();
1372 if(defined($type)) { @xact = (xact_type => $type);
1374 } else { @xact = (); }
1376 if($api =~ /have_charge/o) {
1378 $trans = $apputils->simple_scalar_request(
1380 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1381 { usr => $user_id, total_owed => { ">" => 0 }, @xact });
1383 } elsif($api =~ /have_balance/o) {
1385 $trans = $apputils->simple_scalar_request(
1387 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1388 { usr => $user_id, balance_owed => { "<>" => 0 }, @xact });
1392 $trans = $apputils->simple_scalar_request(
1394 "open-ils.storage.direct.money.open_billable_transaction_summary.search_where.atomic",
1395 { usr => $user_id, @xact });
1398 if($api =~ /total/o) {
1400 for my $t (@$trans) {
1401 $total += $t->balance_owed;
1404 $logger->debug("Total balance owed by user $user_id: $total");
1408 if($api =~ /count/o) { return scalar @$trans; }
1409 if($api !~ /fleshed/o) { return $trans; }
1412 for my $t (@$trans) {
1414 if( $t->xact_type ne 'circulation' ) {
1415 push @resp, {transaction => $t};
1419 my $circ = $apputils->simple_scalar_request(
1421 "open-ils.storage.direct.action.circulation.retrieve",
1426 my $title = $apputils->simple_scalar_request(
1428 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1429 $circ->target_copy );
1433 my $u = OpenILS::Utils::ModsParser->new();
1434 $u->start_mods_batch($title->marc());
1435 my $mods = $u->finish_mods_batch();
1437 push @resp, {transaction => $t, circ => $circ, record => $mods };
1445 __PACKAGE__->register_method(
1446 method => "user_transaction_retrieve",
1447 api_name => "open-ils.actor.user.transaction.fleshed.retrieve",
1449 notes => <<" NOTES");
1450 Returns a fleshedtransaction record
1452 __PACKAGE__->register_method(
1453 method => "user_transaction_retrieve",
1454 api_name => "open-ils.actor.user.transaction.retrieve",
1456 notes => <<" NOTES");
1457 Returns a transaction record
1459 sub user_transaction_retrieve {
1460 my( $self, $client, $login_session, $bill_id ) = @_;
1462 my $trans = $apputils->simple_scalar_request(
1464 "open-ils.storage.direct.money.billable_transaction_summary.retrieve",
1468 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1469 $login_session, $trans->usr, 'VIEW_USER_TRANSACTIONS' );
1470 return $evt if $evt;
1472 my $api = $self->api_name();
1473 if($api !~ /fleshed/o) { return $trans; }
1475 if( $trans->xact_type ne 'circulation' ) {
1476 $logger->debug("Returning non-circ transaction");
1477 return {transaction => $trans};
1480 my $circ = $apputils->simple_scalar_request(
1482 "open-ils.storage.direct.action.circulation.retrieve",
1485 return {transaction => $trans} unless $circ;
1486 $logger->debug("Found the circ transaction");
1488 my $title = $apputils->simple_scalar_request(
1490 "open-ils.storage.fleshed.biblio.record_entry.retrieve_by_copy",
1491 $circ->target_copy );
1493 return {transaction => $trans, circ => $circ } unless $title;
1494 $logger->debug("Found the circ title");
1498 my $u = OpenILS::Utils::ModsParser->new();
1499 $u->start_mods_batch($title->marc());
1500 $mods = $u->finish_mods_batch();
1502 if ($title->id == -1) {
1503 my $copy = $apputils->simple_scalar_request(
1505 "open-ils.storage.direct.asset.copy.retrieve",
1506 $circ->target_copy );
1508 $mods = new Fieldmapper::metabib::virtual_record;
1510 $mods->title($copy->dummy_title);
1511 $mods->author($copy->dummy_author);
1515 $logger->debug("MODSized the circ title");
1517 return {transaction => $trans, circ => $circ, record => $mods };
1521 __PACKAGE__->register_method(
1522 method => "hold_request_count",
1523 api_name => "open-ils.actor.user.hold_requests.count",
1525 notes => <<" NOTES");
1526 Returns hold ready/total counts
1528 sub hold_request_count {
1529 my( $self, $client, $login_session, $userid ) = @_;
1531 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1532 $login_session, $userid, 'VIEW_HOLD' );
1533 return $evt if $evt;
1536 my $holds = $apputils->simple_scalar_request(
1538 "open-ils.storage.direct.action.hold_request.search_where.atomic",
1540 fulfillment_time => {"=" => undef } }
1544 for my $h (@$holds) {
1545 next unless $h->capture_time;
1547 my $copy = $apputils->simple_scalar_request(
1549 "open-ils.storage.direct.asset.copy.retrieve",
1553 if ($copy->status == 8) {
1558 return { total => scalar(@$holds), ready => scalar(@ready) };
1562 __PACKAGE__->register_method(
1563 method => "checkedout_count",
1564 api_name => "open-ils.actor.user.checked_out.count",
1566 notes => <<" NOTES");
1567 Returns a transaction record
1569 sub checkedout_count {
1570 my( $self, $client, $login_session, $userid ) = @_;
1572 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1573 $login_session, $userid, 'VIEW_CIRCULATIONS' );
1574 return $evt if $evt;
1577 my $circs = $apputils->simple_scalar_request(
1579 "open-ils.storage.direct.action.circulation.search_where.atomic",
1581 checkin_time => {"=" => undef } }
1584 my $parser = DateTime::Format::ISO8601->new;
1587 for my $c (@$circs) {
1588 my $due_dt = $parser->parse_datetime( clense_ISO8601( $c->due_date ) );
1589 my $due = $due_dt->epoch;
1591 if ($due < DateTime->today->epoch) {
1596 return { total => scalar(@$circs), overdue => scalar(@overdue) };
1599 __PACKAGE__->register_method(
1600 method => "user_transaction_history",
1601 api_name => "open-ils.actor.user.transactions.history",
1603 notes => <<" NOTES");
1604 Returns a list of billable transaction ids for a user, optionally by type
1606 __PACKAGE__->register_method(
1607 method => "user_transaction_history",
1608 api_name => "open-ils.actor.user.transactions.history.have_charge",
1610 notes => <<" NOTES");
1611 Returns a list of billable transaction ids for a user that have an initial charge, optionally by type
1613 sub user_transaction_history {
1614 my( $self, $client, $login_session, $user_id, $type ) = @_;
1616 my( $user_obj, $target, $evt ) = $apputils->checkses_requestor(
1617 $login_session, $user_id, 'VIEW_USER_TRANSACTIONS' );
1618 return $evt if $evt;
1620 my $api = $self->api_name();
1624 @xact = (xact_type => $type) if(defined($type));
1625 @charge = (total_owed => { ">" => 0}) if($api =~ /have_charge/);
1627 my $trans = $apputils->simple_scalar_request(
1629 "open-ils.storage.direct.money.billable_transaction_summary.search_where.atomic",
1630 { usr => $user_id, @xact, @charge }, { order_by => 'xact_start DESC' });
1632 return [ map { $_->id } @$trans ];
1636 __PACKAGE__->register_method(
1637 method => "user_perms",
1638 api_name => "open-ils.actor.permissions.user_perms.retrieve",
1640 notes => <<" NOTES");
1641 Returns a list of permissions
1644 my( $self, $client, $authtoken, $user ) = @_;
1646 my( $staff, $evt ) = $apputils->checkses($authtoken);
1647 return $evt if $evt;
1649 $user ||= $staff->id;
1651 if( $user != $staff->id and $evt = $apputils->check_perms( $staff->id, $staff->home_ou, 'VIEW_PERMISSION') ) {
1655 return $apputils->simple_scalar_request(
1657 "open-ils.storage.permission.user_perms.atomic",
1661 __PACKAGE__->register_method(
1662 method => "retrieve_perms",
1663 api_name => "open-ils.actor.permissions.retrieve",
1664 notes => <<" NOTES");
1665 Returns a list of permissions
1667 sub retrieve_perms {
1668 my( $self, $client ) = @_;
1669 return $apputils->simple_scalar_request(
1671 "open-ils.storage.direct.permission.perm_list.retrieve.all.atomic");
1674 __PACKAGE__->register_method(
1675 method => "retrieve_groups",
1676 api_name => "open-ils.actor.groups.retrieve",
1677 notes => <<" NOTES");
1678 Returns a list of user groupss
1680 sub retrieve_groups {
1681 my( $self, $client ) = @_;
1682 return $apputils->simple_scalar_request(
1684 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1687 __PACKAGE__->register_method(
1688 method => "retrieve_org_address",
1689 api_name => "open-ils.actor.org_unit.address.retrieve",
1690 notes => <<' NOTES');
1691 Returns an org_unit address by ID
1692 @param An org_address ID
1694 sub retrieve_org_address {
1695 my( $self, $client, $id ) = @_;
1696 return $apputils->simple_scalar_request(
1698 "open-ils.storage.direct.actor.org_address.retrieve",
1703 __PACKAGE__->register_method(
1704 method => "retrieve_groups_tree",
1705 api_name => "open-ils.actor.groups.tree.retrieve",
1706 notes => <<" NOTES");
1707 Returns a list of user groups
1709 sub retrieve_groups_tree {
1710 my( $self, $client ) = @_;
1711 my $groups = $apputils->simple_scalar_request(
1713 "open-ils.storage.direct.permission.grp_tree.retrieve.all.atomic");
1714 return $self->build_group_tree($groups);
1718 # turns an org list into an org tree
1719 sub build_group_tree {
1721 my( $self, $grplist) = @_;
1723 return $grplist unless (
1724 ref($grplist) and @$grplist > 1 );
1726 my @list = sort { $a->name cmp $b->name } @$grplist;
1729 for my $grp (@list) {
1731 if ($grp and !defined($grp->parent)) {
1735 my ($parent) = grep { $_->id == $grp->parent} @list;
1737 $parent->children([]) unless defined($parent->children);
1738 push( @{$parent->children}, $grp );
1746 __PACKAGE__->register_method(
1747 method => "add_user_to_groups",
1748 api_name => "open-ils.actor.user.set_groups",
1749 notes => <<" NOTES");
1750 Adds a user to one or more permission groups
1753 sub add_user_to_groups {
1754 my( $self, $client, $authtoken, $userid, $groups ) = @_;
1756 my( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1757 $authtoken, $userid, 'CREATE_USER_GROUP_LINK' );
1758 return $evt if $evt;
1760 ( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1761 $authtoken, $userid, 'REMOVE_USER_GROUP_LINK' );
1762 return $evt if $evt;
1764 $apputils->simplereq(
1766 'open-ils.storage.direct.permission.usr_grp_map.mass_delete', { usr => $userid } );
1768 for my $group (@$groups) {
1769 my $link = Fieldmapper::permission::usr_grp_map->new;
1771 $link->usr($userid);
1773 my $id = $apputils->simplereq(
1775 'open-ils.storage.direct.permission.usr_grp_map.create', $link );
1781 __PACKAGE__->register_method(
1782 method => "get_user_perm_groups",
1783 api_name => "open-ils.actor.user.get_groups",
1784 notes => <<" NOTES");
1785 Retrieve a user's permission groups.
1789 sub get_user_perm_groups {
1790 my( $self, $client, $authtoken, $userid ) = @_;
1792 my( $requestor, $target, $evt ) = $apputils->checkses_requestor(
1793 $authtoken, $userid, 'VIEW_PERM_GROUPS' );
1794 return $evt if $evt;
1796 return $apputils->simplereq(
1798 'open-ils.storage.direct.permission.usr_grp_map.search.usr.atomic', $userid );
1803 __PACKAGE__->register_method (
1804 method => 'register_workstation',
1805 api_name => 'open-ils.actor.workstation.register',
1807 Registers a new workstion in the system
1808 @param authtoken The login session key
1809 @param name The name of the workstation id
1810 @param owner The org unit that owns this workstation
1811 @return The workstation id on success, WORKSTATION_NAME_EXISTS
1812 if the name is already in use.
1815 sub register_workstation {
1816 my( $self, $connection, $authtoken, $name, $owner ) = @_;
1817 my( $requestor, $evt ) = $U->checkses($authtoken);
1818 return $evt if $evt;
1819 $evt = $U->check_perms($requestor->id, $owner, 'REGISTER_WORKSTATION');
1820 return $evt if $evt;
1822 my $ws = $U->storagereq(
1823 'open-ils.storage.direct.actor.workstation.search.name', $name );
1824 return OpenILS::Event->new('WORKSTATION_NAME_EXISTS') if $ws;
1826 $ws = Fieldmapper::actor::workstation->new;
1827 $ws->owning_lib($owner);
1830 my $id = $U->storagereq(
1831 'open-ils.storage.direct.actor.workstation.create', $ws );
1832 return $U->DB_UPDATE_FAILED($ws) unless $id;
1839 __PACKAGE__->register_method (
1840 method => 'fetch_patron_note',
1841 api_name => 'open-ils.actor.note.retrieve.all',
1843 Returns a list of notes for a given user
1844 Requestor must have VIEW_USER permission if pub==false and
1845 @param authtoken The login session key
1846 @param args Hash of params including
1847 patronid : the patron's id
1848 pub : true if retrieving only public notes
1852 sub fetch_patron_note {
1853 my( $self, $conn, $authtoken, $args ) = @_;
1854 my $patronid = $$args{patronid};
1856 my($reqr, $evt) = $U->checkses($authtoken);
1859 ($patron, $evt) = $U->fetch_user($patronid);
1860 return $evt if $evt;
1863 if( $patronid ne $reqr->id ) {
1864 $evt = $U->check_perms($reqr->id, $patron->home_ou, 'VIEW_USER');
1865 return $evt if $evt;
1867 return $U->storagereq(
1868 'open-ils.storage.direct.actor.usr_note.search_where.atomic',
1869 { usr => $patronid, pub => 't' } );
1872 $evt = $U->check_perms($reqr->id, $patron->home_ou, 'VIEW_USER');
1873 return $evt if $evt;
1875 return $U->storagereq(
1876 'open-ils.storage.direct.actor.usr_note.search.usr.atomic', $patronid );
1879 __PACKAGE__->register_method (
1880 method => 'create_user_note',
1881 api_name => 'open-ils.actor.note.create',
1883 Creates a new note for the given user
1884 @param authtoken The login session key
1885 @param note The note object
1888 sub create_user_note {
1889 my( $self, $conn, $authtoken, $note ) = @_;
1890 my( $reqr, $patron, $evt ) =
1891 $U->checkses_requestor($authtoken, $note->usr, 'UPDATE_USER');
1892 return $evt if $evt;
1893 $logger->activity("user ".$reqr->id." creating note for user ".$note->usr);
1895 $note->pub('f') unless $note->pub;
1896 $note->creator($reqr->id);
1897 my $id = $U->storagereq(
1898 'open-ils.storage.direct.actor.usr_note.create', $note );
1899 return $U->DB_UPDATE_FAILED($note) unless $id;
1904 __PACKAGE__->register_method (
1905 method => 'delete_user_note',
1906 api_name => 'open-ils.actor.note.delete',
1908 Deletes a note for the given user
1909 @param authtoken The login session key
1910 @param noteid The note id
1913 sub delete_user_note {
1914 my( $self, $conn, $authtoken, $noteid ) = @_;
1916 my $note = $U->storagereq(
1917 'open-ils.storage.direct.actor.usr_note.retrieve', $noteid);
1918 return OpenILS::Event->new('USER_NOTE_NOT_FOUND') unless $note;
1920 my( $reqr, $patron, $evt ) =
1921 $U->checkses_requestor($authtoken, $note->usr, 'UPDATE_USER');
1922 return $evt if $evt;
1923 $logger->activity("user ".$reqr->id." deleting note [$noteid] for user ".$note->usr);
1925 my $stat = $U->storagereq(
1926 'open-ils.storage.direct.actor.usr_note.delete', $noteid );
1927 return $U->DB_UPDATE_FAILED($note) unless defined $stat;
1933 __PACKAGE__->register_method (
1934 method => 'create_closed_date',
1935 api_name => 'open-ils.actor.org_unit.closed_date.create',
1937 Creates a new closing entry for the given org_unit
1938 @param authtoken The login session key
1939 @param note The closed_date object
1942 sub create_closed_date {
1943 my( $self, $conn, $authtoken, $cd ) = @_;
1945 my( $user, $evt ) = $U->checkses($authtoken);
1946 return $evt if $evt;
1948 $evt = $U->check_perms($user->id, $cd->org_unit, 'CREATE_CLOSEING');
1949 return $evt if $evt;
1951 $logger->activity("user ".$user->id." creating library closing for ".$cd->org_unit);
1953 my $id = $U->storagereq(
1954 'open-ils.storage.direct.actor.org_unit.closed_date.create', $cd );
1955 return $U->DB_UPDATE_FAILED($cd) unless $id;
1960 __PACKAGE__->register_method (
1961 method => 'delete_closed_date',
1962 api_name => 'open-ils.actor.org_unit.closed_date.delete',
1964 Deletes a closing entry for the given org_unit
1965 @param authtoken The login session key
1966 @param noteid The close_date id
1969 sub delete_closed_date {
1970 my( $self, $conn, $authtoken, $cd ) = @_;
1972 my( $user, $evt ) = $U->checkses($authtoken);
1973 return $evt if $evt;
1976 ($cd_obj, $evt) = fetch_closed_date($cd);
1977 return $evt if $evt;
1979 $evt = $U->check_perms($user->id, $cd->org_unit, 'DELETE_CLOSEING');
1980 return $evt if $evt;
1982 $logger->activity("user ".$user->id." deleting library closing for ".$cd->org_unit);
1984 my $stat = $U->storagereq(
1985 'open-ils.storage.direct.actor.org_unit.closed_date.delete', $cd );
1986 return $U->DB_UPDATE_FAILED($cd) unless $stat;