]> git.evergreen-ils.org Git - Evergreen.git/blob - Open-ILS/src/perlmods/OpenILS/Application/PermaCrud.pm
883d9fd930d5578064709348e85c190682235c92
[Evergreen.git] / Open-ILS / src / perlmods / OpenILS / Application / PermaCrud.pm
1 # vim:et:ts=4:sw=4:
2
3 package OpenILS::Application::PermaCrud;
4 use OpenILS::Application;
5 use base qw/OpenILS::Application/;
6
7 use Unicode::Normalize;
8 use OpenSRF::EX qw/:try/;
9
10 use OpenSRF::AppSession;
11 use OpenSRF::Utils::SettingsClient;
12 use OpenSRF::Utils::Logger qw/:level/;
13
14 use OpenILS::Utils::Fieldmapper;
15 use OpenSRF::Utils::JSON;
16
17 use OpenILS::Utils::CStoreEditor qw/:funcs/;
18
19 use XML::LibXML;
20 use XML::LibXML::XPathContext;
21 use XML::LibXSLT;
22
23 our %namespace_map = (
24     oils_persist=> {ns => 'http://open-ils.org/spec/opensrf/IDL/persistence/v1'},
25     oils_obj    => {ns => 'http://open-ils.org/spec/opensrf/IDL/objects/v1'},
26     idl         => {ns => 'http://opensrf.org/spec/IDL/base/v1'},
27     reporter    => {ns => 'http://open-ils.org/spec/opensrf/IDL/reporter/v1'},
28     perm        => {ns => 'http://open-ils.org/spec/opensrf/IDL/permacrud/v1'},
29 );
30
31
32 my $log = 'OpenSRF::Utils::Logger';
33
34 my $parser = XML::LibXML->new();
35 my $xslt = XML::LibXSLT->new();
36
37 my $xpc = XML::LibXML::XPathContext->new();
38 $xpc->registerNs($_, $namespace_map{$_}{ns}) for ( keys %namespace_map );
39
40 my $idl;
41
42 sub initialize {
43
44     my $conf = OpenSRF::Utils::SettingsClient->new;
45     my $idl_file = $conf->config_value( 'IDL' );
46
47     $idl = $parser->parse_file( $idl_file );
48
49     $log->debug( 'IDL XML file loaded' );
50
51     generate_methods();
52
53 }
54 sub child_init {}
55
56 sub CRUD_action_object_permcheck {
57     my $self = shift;
58     my $client = shift;
59     my $auth = shift;
60     my $obj = shift;
61
62     my $e = new_editor(authtoken => $auth, xact => 1);
63     return $e->event unless $e->checkauth;
64
65     if (ref($obj) && $obj->json_hint ne $self->{class_hint}) {
66         throw OpenSRF::DomainObject::oilsException->new(
67             statusCode => 500,
68             status => "Class missmatch: $self->{class_hint} method called with " . $obj->json_hint,
69         );
70     }
71
72     my ($class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
73     my ($action_node) = $xpc->findnodes( "perm:permacrud/perm:actions/perm:$action", $class_node );
74     my $all_perms = $xpc->getAttribute( 'all_perms', $action_node );
75
76     if (!ref($obj)) {
77         my $retrieve_method = 'retrieve_' . $xpc->findvalue( '@oils_obj:fieldmapper', $class_node );
78         $retrieve_method =~ s/::/_/go;
79         $obj = $e->retrieve_method( $obj )->gather(1);
80     }
81
82     my $action = $self->api_name =~ s/^open-ils\.admin\.([^\.])\..+$/$1/o;
83     my $o_type = $obj->cdbi =~ s/::/./go;
84     my $id_field = $obj->Identity;
85
86     my $perm_field_value = $aciton_node->getAttribute('permission');
87
88     if (defined($perm_field_value)) {
89         my @perms = split '|', $aciton_node->getAttribute('permission');
90
91         my @context_ous;
92         if ($aciton_node->getAttribute('global_required')) {
93             push @context_ous, $e->search_actor_org_unit( { parent_ou => undef } )->[0]->id;
94
95         } else {
96             my $context_field_value = $aciton_node->getAttribute('context_field');
97
98             if (defined($context_field_value)) {
99                 push @context_ous, $obj->$_ for ( split '|', $context_field_value );
100             } else {  
101                 for my $context_node ( $xpc->findnodes( "perm:context", $action_node ) ) {
102                     my $context_field = $context_node->getAttribute('field');
103                     my $link_field = $context_node->getAttribute('link');
104
105                     if ($link_field) {
106
107                         my ($link_node) = $xpc->findnodes( "idl:links/idl:link[\@field='$link_field']", $class_node );
108                         my $link_class_hint = $link_node->getAttribute('class');
109                         my $remote_field = $link_node->getAttribute('key');
110
111                         my ($remote_class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
112                         my $search_method = 'search_' . $xpc->findvalue( '@oils_obj:fieldmapper', $remote_class_node );
113                         $search_method =~ s/::/_/go;
114
115                         for my $remote_object ( @{$e->$search_method( { $key => $obj->$link_field } )} ) {
116                             push @context_ous, $remote_object->$context_field;
117                         }
118                     } else {
119                         push @context_ous, $obj->$_ for ( split '|', $context_field );
120                     }
121                 }
122             }
123         }
124
125         my $pok = 0;
126         for my $perm (@perms) {
127             if (@context_ous) {
128                 for my $c_ou (@context_ous) {
129                     if ($e->allowed($perm => $c_ou => $obj)) {
130                         $pok++;
131                         last;
132                     }
133                 }
134             } else {
135                 $pok++ if ($e->allowed($perm => undef => $obj));
136             }
137         }
138
139         if ((lc($all_perms) eq 'true' && @perms != $pok) or !$pok) {
140             throw OpenSRF::DomainObject::oilsException->new(
141                 statusCode => 500,
142                 status => "Perm failure -- action: $action, object type: $self->{json_hint}",
143             );
144         }
145     }
146
147     return $obj if ($action eq 'retrieve');
148
149     return $e->session->request("open-ils.cstore.direct.$o_type.$action" => $obj )->gather(1);
150 }
151
152 sub search_permacrud {
153     my $self = shift;
154     my $client = shift;
155     my $auth = shift;
156     my @args = @_;
157
158     my $e = new_editor(authtoken => $auth);
159     return $e->event unless $e->checkauth;
160  
161     my ($class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
162     my $search_method = 'search_' . $xpc->findvalue( '@oils_obj:fieldmapper', $remote_class_node );
163     $search_method =~ s/::/_/go;
164
165     my $retriever = $self->method_lookup( $self->{retriever} );
166     my $obj_list = $e->$search_method( @args );
167
168     for my $o ( @$obj_list ) {
169         my ($o) = $retriever->run( $o );
170         $client->respond( $o ) if ($o);
171     }
172
173     return undef;
174 }
175
176 sub generate_methods {
177     try {
178         for my $class_node ( $xpc->findnodes( '//idl:class[perm:permacrud]', $idl->documentElement ) ) {
179             my $hint = $class_node->getAttribute('id');
180             $log->debug("permacrud class_node $hint");
181         
182             for my $action_node ( $xpc->findnodes( "perm:permacrud/perm:actions/perm:*", $class_node ) ) {
183                 (my $method = $action_node->localname) =~ s/^.+:(.+)$/$1/o;
184                 $log->internal("permacrud method = $method");
185         
186                 __PACKAGE__->register_method(
187                     method          => 'CRUD_action_object_permcheck',
188                     api_name        => 'open-ils.permacrud.' . $method . '.' . $hint,
189                     authoritative   => 1,
190                     class_hint      => $hint,
191                 );
192         
193                 if ($method eq 'retrieve') {
194                     __PACKAGE__->register_method(
195                         method      => 'search_permcheck',
196                         api_name    => 'open-ils.permacrud.search.' . $hint,
197                         class_hint  => $hint,
198                         retriever   => 'open-ils.permacrud.retrieve.' . $hint,
199                         stream      => 1
200                     );
201                 }
202             }
203         }
204     } catch Error with {
205         my $e = shift;
206         $log->error("error generating permacrud methods: $e");
207     };
208 }
209
210
211 1;
212