]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/Circ/StatCat.pm
added permission checks to stat_cat methods
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / Circ / StatCat.pm
1 # ---------------------------------------------------------------
2 # Copyright (C) 2005  Georgia Public Library Service 
3 # Bill Erickson <highfalutin@gmail.com>
4
5 # This program is free software; you can redistribute it and/or
6 # modify it under the terms of the GNU General Public License
7 # as published by the Free Software Foundation; either version 2
8 # of the License, or (at your option) any later version.
9
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 # GNU General Public License for more details.
14 # ---------------------------------------------------------------
15
16 package OpenILS::Application::Circ::StatCat;
17 use base qw/OpenSRF::Application/;
18 use strict; use warnings;
19
20 use OpenSRF::Utils::Logger qw($logger);
21 use OpenSRF::EX qw/:try/;
22 use OpenILS::Application::AppUtils;
23 my $apputils = "OpenILS::Application::AppUtils";
24
25
26
27 __PACKAGE__->register_method(
28         method  => "retrieve_stat_cats",
29         api_name        => "open-ils.circ.stat_cat.actor.retrieve.all");
30
31 __PACKAGE__->register_method(
32         method  => "retrieve_stat_cats",
33         api_name        => "open-ils.circ.stat_cat.asset.retrieve.all");
34
35 # retrieves all of the stat cats for a given org unit
36 # if no orgid, user_session->home_ou is used
37
38 sub retrieve_stat_cats {
39         my( $self, $client, $user_session, $orgid ) = @_;
40
41         my $method = "open-ils.storage.ranged.fleshed.actor.stat_cat.all.atomic"; 
42         if( $self->api_name =~ /asset/ ) {
43                 $method = "open-ils.storage.ranged.fleshed.asset.stat_cat.all.atomic"; 
44         }
45
46         my $user_obj = $apputils->check_user_session($user_session); 
47         if(!$orgid) { $orgid = $user_obj->home_ou; }
48         my $cats = $apputils->simple_scalar_request(
49                                 "open-ils.storage", $method, $orgid );
50
51         return [ sort { $a->name cmp $b->name } @$cats ];
52 }
53
54
55 __PACKAGE__->register_method(
56         method  => "retrieve_ranged_intersect_stat_cats",
57         api_name        => "open-ils.circ.stat_cat.asset.multirange.intersect.retrieve");
58
59 sub retrieve_ranged_intersect_stat_cats {
60         my( $self, $client, $user_session, $orglist ) = @_;
61
62         my $user_obj = $apputils->check_user_session($user_session); 
63         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
64
65         # uniquify, yay!
66         my %hash = map { ($_ => 1) } @$orglist;
67         $orglist = [ keys %hash ];
68
69         warn "range: @$orglist\n";
70
71         my      $method = "open-ils.storage.multiranged.intersect.fleshed.asset.stat_cat.all.atomic";
72         return $apputils->simple_scalar_request(
73                                 "open-ils.storage", $method, $orglist );
74 }
75
76
77 __PACKAGE__->register_method(
78         method  => "retrieve_ranged_union_stat_cats",
79         api_name        => "open-ils.circ.stat_cat.asset.multirange.union.retrieve");
80
81 sub retrieve_ranged_union_stat_cats {
82         my( $self, $client, $user_session, $orglist ) = @_;
83
84         my      $method = "open-ils.storage.multiranged.union.fleshed.asset.stat_cat.all.atomic";
85         use Data::Dumper;
86         warn "Retrieving stat_cats with method $method and orgs " . Dumper($orglist) . "\n";
87
88         my $user_obj = $apputils->check_user_session($user_session); 
89         if(!$orglist) { $orglist = [ $user_obj->home_ou ]; }
90
91         # uniquify, yay!
92         my %hash = map { ($_ => 1) } @$orglist;
93         $orglist = [ keys %hash ];
94
95         warn "range: @$orglist\n";
96
97         return $apputils->simple_scalar_request(
98                                 "open-ils.storage", $method, $orglist );
99 }
100
101
102
103 __PACKAGE__->register_method(
104         method  => "stat_cat_create",
105         api_name        => "open-ils.circ.stat_cat.asset.create");
106
107 __PACKAGE__->register_method(
108         method  => "stat_cat_create",
109         api_name        => "open-ils.circ.stat_cat.actor.create");
110
111 sub stat_cat_create {
112         my( $self, $client, $user_session, $stat_cat ) = @_;
113
114         my $method = "open-ils.storage.direct.actor.stat_cat.create";
115         my $entry_create = "open-ils.storage.direct.actor.stat_cat_entry.create";
116         my $perm = 'CREATE_PATRON_STAT_CAT';
117         my $eperm = 'CREATE_PATRON_STAT_CAT_ENTRY';
118
119         if($self->api_name =~ /asset/) {
120                 $method = "open-ils.storage.direct.asset.stat_cat.create";
121                 $entry_create = "open-ils.storage.direct.asset.stat_cat_entry.create";
122                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY';
123         }
124
125         #my $user_obj = $apputils->check_user_session($user_session); 
126         #my $orgid = $user_obj->home_ou();
127         my( $user_obj, $evt ) = $apputils->checkses($user_session);
128         return $evt if $evt;
129         $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $perm);
130         return $evt if $evt;
131
132         if($stat_cat->entries) {
133                 $evt = $apputils->check_perms($user_obj->id, $stat_cat->owner, $eperm);
134                 return $evt if $evt;
135         }
136
137
138         my $session = $apputils->start_db_session();
139         my $newid = _create_stat_cat($session, $stat_cat, $method);
140
141         if( ref($stat_cat->entries) ) {
142                 for my $entry (@{$stat_cat->entries}) {
143                         $entry->stat_cat($newid);
144                         _create_stat_entry($session, $entry, $entry_create);
145                 }
146         }
147
148         $apputils->commit_db_session($session);
149
150         $logger->debug("Stat cat creation successful with id $newid");
151
152         my $orgid = $user_obj->home_ou;
153         if( $self->api_name =~ /asset/ ) {
154                 return _flesh_asset_cat($newid, $orgid);
155         } else {
156                 return _flesh_user_cat($newid, $orgid);
157         }
158 }
159
160
161 sub _flesh_user_cat {
162         my $id = shift;
163         my $orgid = shift;
164
165         my $session = OpenSRF::AppSession->create("open-ils.storage");
166         my $cat = $session->request(
167                 "open-ils.storage.direct.actor.stat_cat.retrieve",
168                 $id )->gather(1);
169
170         $cat->entries( 
171                 $session->request(
172                         "open-ils.storage.ranged.actor.stat_cat_entry.search.stat_cat.atomic",
173                         $orgid, $id )->gather(1) );
174
175         return $cat;
176 }
177
178
179 sub _flesh_asset_cat {
180         my $id = shift;
181         my $orgid = shift;
182
183         my $session = OpenSRF::AppSession->create("open-ils.storage");
184         my $cat = $session->request(
185                 "open-ils.storage.direct.asset.stat_cat.retrieve",
186                 $id )->gather(1);
187
188         $cat->entries( 
189                 $session->request(
190                         "open-ils.storage.ranged.asset.stat_cat_entry.search.stat_cat.atomic",
191                         $orgid,  $id )->gather(1) );
192
193         return $cat;
194
195 }
196
197
198 sub _create_stat_cat {
199         my( $session, $stat_cat, $method) = @_;
200         warn "Creating new stat cat with name " . $stat_cat->name . "\n";
201         $stat_cat->clear_id();
202         my $req = $session->request( $method, $stat_cat );
203         my $id = $req->gather(1);
204         if(!$id) {
205                 throw OpenSRF::EX::ERROR 
206                 ("Error creating new statistical category"); }
207
208         warn "Stat cat create returned id $id\n";
209         return $id;
210 }
211
212
213 sub _create_stat_entry {
214         my( $session, $stat_entry, $method) = @_;
215
216         warn "Creating new stat entry with value " . $stat_entry->value . "\n";
217         $stat_entry->clear_id();
218
219         my $req = $session->request($method, $stat_entry);
220         my $id = $req->gather(1);
221
222         warn "Stat entry " . Dumper($stat_entry) . "\n";        
223         
224         if(!$id) {
225                 throw OpenSRF::EX::ERROR 
226                 ("Error creating new stat cat entry"); }
227
228         warn "Stat cat entry create returned id $id\n";
229         return $id;
230 }
231
232
233 __PACKAGE__->register_method(
234         method  => "update_stat_entry",
235         api_name        => "open-ils.circ.stat_cat.actor.entry.update");
236
237 __PACKAGE__->register_method(
238         method  => "update_stat_entry",
239         api_name        => "open-ils.circ.stat_cat.asset.entry.update");
240
241 sub update_stat_entry {
242         my( $self, $client, $user_session, $entry ) = @_;
243
244
245         my $method = "open-ils.storage.direct.actor.stat_cat_entry.update";
246         my $perm = 'UPDATE_PATRON_STAT_CAT_ENTRY';
247         if($self->api_name =~ /asset/) {
248                 $method = "open-ils.storage.direct.asset.stat_cat_entry.update";
249                 $perm = 'UPDATE_COPY_STAT_CAT_ENTRY';
250         }
251
252         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
253         return $evt if $evt;
254         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
255         return $evt if $evt;
256
257         my $session = $apputils->start_db_session();
258         my $req = $session->request($method, $entry); 
259         my $status = $req->gather(1);
260         $apputils->commit_db_session($session);
261         warn "stat cat entry with value " . $entry->value . " updated with status $status\n";
262         return 1;
263 }
264
265
266 __PACKAGE__->register_method(
267         method  => "update_stat",
268         api_name        => "open-ils.circ.stat_cat.actor.update");
269
270 __PACKAGE__->register_method(
271         method  => "update_stat",
272         api_name        => "open-ils.circ.stat_cat.asset.update");
273
274 sub update_stat {
275         my( $self, $client, $user_session, $cat ) = @_;
276
277         my $method = "open-ils.storage.direct.actor.stat_cat.update";
278         my $perm = 'UPDATE_PATRON_STAT_CAT';
279         if($self->api_name =~ /asset/) {
280                 $method = "open-ils.storage.direct.asset.stat_cat.update";
281                 $perm = 'UPDATE_COPY_STAT_CAT';
282         }
283
284         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
285         return $evt if $evt;
286         $evt = $apputils->check_perms( $user_obj->id, $cat->owner, $perm );
287         return $evt if $evt;
288
289         my $session = $apputils->start_db_session();
290         my $req = $session->request($method, $cat); 
291         my $status = $req->gather(1);
292         $apputils->commit_db_session($session);
293         warn "stat cat with id " . $cat->id . " updated with status $status\n";
294         return 1;
295 }
296
297
298 __PACKAGE__->register_method(
299         method  => "create_stat_entry",
300         api_name        => "open-ils.circ.stat_cat.actor.entry.create");
301
302 __PACKAGE__->register_method(
303         method  => "create_stat_entry",
304         api_name        => "open-ils.circ.stat_cat.asset.entry.create");
305
306 sub create_stat_entry {
307         my( $self, $client, $user_session, $entry ) = @_;
308
309         my $method = "open-ils.storage.direct.actor.stat_cat_entry.create";
310         my $perm = 'CREATE_PATRON_STAT_CAT_ENTRY';
311         if($self->api_name =~ /asset/) {
312                 $method = "open-ils.storage.direct.asset.stat_cat_entry.create";
313                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY';
314         }
315
316         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
317         return $evt if $evt;
318         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
319         return $evt if $evt;
320
321         $entry->clear_id();
322         my $session = $apputils->start_db_session();
323         my $req = $session->request($method, $entry); 
324         my $status = $req->gather(1);
325         $apputils->commit_db_session($session);
326
327         warn "stat cat entry with id " . $status . " updated with status $status\n";
328         return $status;
329 }
330
331
332
333 __PACKAGE__->register_method(
334         method  => "create_stat_map",
335         api_name        => "open-ils.circ.stat_cat.actor.user_map.create");
336
337 __PACKAGE__->register_method(
338         method  => "create_stat_map",
339         api_name        => "open-ils.circ.stat_cat.asset.copy_map.create");
340
341 sub create_stat_map {
342         my( $self, $client, $user_session, $map ) = @_;
343
344
345         my ( $evt, $copy, $volume, $patron, $user_obj );
346
347         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.create";
348         my $ret = "open-ils.storage.direct.actor.stat_cat_entry_user_map.retrieve";
349         my $perm = 'CREATE_PATRON_STAT_CAT_ENTRY_MAP';
350         my $perm_org;
351
352         if($self->api_name =~ /asset/) {
353                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.create";
354                 $ret = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.retrieve";
355                 $perm = 'CREATE_COPY_STAT_CAT_ENTRY_MAP';
356                 ( $copy, $evt ) = $apputils->fetch_copy($map->owning_copy);
357                 return $evt if $evt;
358                 ( $volume, $evt ) = $apputils->fetch_callnumber($copy->call_number);
359                 return $evt if $evt;
360                 $perm_org = $volume->owning_lib;
361
362         } else {
363                 ($patron, $evt) = $apputils->fetch_user($map->target_usr);
364                 return $evt if $evt;
365                 $perm_org = $patron->home_ou;
366         }
367
368         ( $user_obj, $evt )  = $apputils->checkses($user_session); 
369         return $evt if $evt;
370         $evt = $apputils->check_perms( $user_obj->id, $perm_org, $perm );
371         return $evt if $evt;
372
373         $logger->debug( $user_obj->id . " creating new stat cat map" );
374
375         $map->clear_id();
376
377         my $session = $apputils->start_db_session();
378         my $req = $session->request($method, $map); 
379         my $newid = $req->gather(1);
380         warn "Created new stat cat map with id $newid\n";
381         $apputils->commit_db_session($session);
382
383         return $apputils->simple_scalar_request( "open-ils.storage", $ret, $newid );
384
385 }
386
387
388 __PACKAGE__->register_method(
389         method  => "update_stat_map",
390         api_name        => "open-ils.circ.stat_cat.actor.user_map.update");
391
392 __PACKAGE__->register_method(
393         method  => "update_stat_map",
394         api_name        => "open-ils.circ.stat_cat.asset.copy_map.update");
395
396 sub update_stat_map {
397         my( $self, $client, $user_session, $map ) = @_;
398
399         my ( $evt, $copy, $volume, $patron, $user_obj );
400
401         my $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.update";
402         my $perm = 'UPDATE_PATRON_STAT_ENTRY_MAP';
403         my $perm_org;
404
405         if($self->api_name =~ /asset/) {
406                 $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.update";
407                 $perm = 'UPDATE_COPY_STAT_ENTRY_MAP';
408                 ( $copy, $evt ) = $apputils->fetch_copy($map->owning_copy);
409                 return $evt if $evt;
410                 ( $volume, $evt ) = $apputils->fetch_callnumber($copy->call_number);
411                 return $evt if $evt;
412                 $perm_org = $volume->owning_lib;
413
414         } else {
415                 ($patron, $evt) = $apputils->fetch_user($map->target_usr);
416                 return $evt if $evt;
417                 $perm_org = $patron->home_ou;
418         }
419
420
421         ( $user_obj, $evt )  = $apputils->checkses($user_session); 
422         return $evt if $evt;
423         $evt = $apputils->check_perms( $user_obj->id, $perm_org, $perm );
424         return $evt if $evt;
425
426
427         my $session = $apputils->start_db_session();
428         my $req = $session->request($method, $map); 
429         my $newid = $req->gather(1);
430         warn "Updated new stat cat map with id $newid\n";
431         $apputils->commit_db_session($session);
432
433         return $newid;
434 }
435
436
437
438 __PACKAGE__->register_method(
439         method  => "retrieve_maps",
440         api_name        => "open-ils.circ.stat_cat.actor.user_map.retrieve");
441
442 __PACKAGE__->register_method(
443         method  => "retrieve_maps",
444         api_name        => "open-ils.circ.stat_cat.asset.copy_map.retrieve");
445
446 sub retrieve_maps {
447         my( $self, $client, $user_session, $target ) = @_;
448
449
450         my( $user_obj, $evt ) = $apputils->checkses($user_session); 
451         return $evt if $evt;
452
453         my      $method = "open-ils.storage.direct.asset.stat_cat_entry_copy_map.search.owning_copy.atomic";
454         if($self->api_name =~ /actor/ ) {
455                 if(!$target) { $target = $user_obj->id; }
456                 $method = "open-ils.storage.direct.actor.stat_cat_entry_user_map.search.target_usr.atomic";
457         }
458
459         return $apputils->simple_scalar_request("open-ils.storage", $method, $target);
460 }
461
462
463
464
465 __PACKAGE__->register_method(
466         method  => "delete_stats",
467         api_name        => "open-ils.circ.stat_cat.actor.delete");
468
469 __PACKAGE__->register_method(
470         method  => "delete_stats",
471         api_name        => "open-ils.circ.stat_cat.asset.delete");
472
473 sub delete_stats {
474         my( $self, $client, $user_session, $target ) = @_;
475         
476         my $cat;
477
478         my $type = "actor";
479         my $perm = 'DELETE_PATRON_STAT_CAT';
480         if($self->api_name =~ /asset/) { 
481                 $type = "asset"; 
482                 $perm = 'DELETE_COPY_STAT_CAT';
483         }
484
485         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
486         return $evt if $evt;
487
488         ( $cat, $evt ) = $apputils->fetch_stat_cat( $type, $target );
489         return $evt if $evt;
490
491         $evt = $apputils->check_perms( $user_obj->id, $cat->owner, $perm );
492         return $evt if $evt;
493
494         my $session = OpenSRF::AppSession->create("open-ils.storage");
495         return _delete_stats($session, $target, $type);
496 }
497
498 sub _delete_stats {
499         my( $session, $stat, $type) = @_;
500
501         my      $method = "open-ils.storage.direct.asset.stat_cat.delete";
502         if($type =~ /actor/ ) {
503                 $method = "open-ils.storage.direct.actor.stat_cat.delete";
504         }
505         return $session->request($method, $stat)->gather(1);
506 }
507
508
509
510 __PACKAGE__->register_method(
511         method  => "delete_entry",
512         api_name        => "open-ils.circ.stat_cat.actor.entry.delete");
513
514 __PACKAGE__->register_method(
515         method  => "delete_entry",
516         api_name        => "open-ils.circ.stat_cat.asset.entry.delete");
517
518 sub delete_entry {
519         my( $self, $client, $user_session, $target ) = @_;
520
521         my $type = "actor";
522         my $perm = 'DELETE_PATRON_STAT_CAT_ENTRY';
523         if($self->api_name =~ /asset/) { 
524                 $type = "asset"; 
525                 $perm = 'DELETE_COPY_STAT_CAT_ENTRY';
526         }
527
528         my $entry;
529         my( $user_obj, $evt )  = $apputils->checkses($user_session); 
530         return $evt if $evt;
531
532         ( $entry, $evt ) = $apputils->fetch_stat_cat_entry( $type, $target );
533         return $evt if $evt;
534
535         $evt = $apputils->check_perms( $user_obj->id, $entry->owner, $perm );
536         return $evt if $evt;
537
538         my $session = OpenSRF::AppSession->create("open-ils.storage");
539         return _delete_entry($session, $target, $type);
540 }
541
542 sub _delete_entry {
543         my( $session, $stat_entry, $type) = @_;
544
545         my      $method = "open-ils.storage.direct.asset.stat_cat_entry.delete";
546         if($type =~ /actor/ ) {
547                 $method = "open-ils.storage.direct.actor.stat_cat_entry.delete";
548         }
549
550         return $session->request($method, $stat_entry)->gather(1);
551 }
552
553
554
555 1;