3 package OpenILS::Application::PermaCrud;
4 use OpenILS::Application;
5 use base qw/OpenILS::Application/;
6 use strict; use warnings;
8 use Unicode::Normalize;
9 use OpenSRF::EX qw/:try/;
11 use OpenSRF::AppSession;
12 use OpenSRF::Utils::SettingsClient;
13 use OpenSRF::Utils::Logger qw/:level/;
15 use OpenILS::Utils::Fieldmapper;
16 use OpenSRF::Utils::JSON;
18 use OpenILS::Utils::CStoreEditor qw/:funcs/;
21 use XML::LibXML::XPathContext;
25 our %namespace_map = (
26 oils_persist=> {ns => 'http://open-ils.org/spec/opensrf/IDL/persistence/v1'},
27 oils_obj => {ns => 'http://open-ils.org/spec/opensrf/IDL/objects/v1'},
28 idl => {ns => 'http://opensrf.org/spec/IDL/base/v1'},
29 reporter => {ns => 'http://open-ils.org/spec/opensrf/IDL/reporter/v1'},
30 perm => {ns => 'http://open-ils.org/spec/opensrf/IDL/permacrud/v1'},
34 my $log = 'OpenSRF::Utils::Logger';
36 my $parser = XML::LibXML->new();
37 my $xslt = XML::LibXSLT->new();
39 my $xpc = XML::LibXML::XPathContext->new();
40 $xpc->registerNs($_, $namespace_map{$_}{ns}) for ( keys %namespace_map );
46 my $conf = OpenSRF::Utils::SettingsClient->new;
47 my $idl_file = $conf->config_value( 'IDL' );
49 $idl = $parser->parse_file( $idl_file );
51 $log->debug( 'IDL XML file loaded' );
58 sub CRUD_action_object_permcheck {
64 my $e = shift || new_editor(authtoken => $auth, xact => 1);
65 return $e->event unless $e->checkauth;
67 if (ref($obj) && $obj->json_hint ne $self->{class_hint}) {
68 throw OpenSRF::DomainObject::oilsException->new(
70 status => "Class missmatch: $self->{class_hint} method called with " . $obj->json_hint,
77 ($class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
80 $log->error("Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]");
81 throw OpenSRF::DomainObject::oilsException->new(
83 status => "Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]"
88 $log->error("Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]");
89 throw OpenSRF::DomainObject::oilsException->new(
91 status => "Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]"
97 ($action_node) = $xpc->findnodes( "perm:permacrud/perm:actions/perm:$self->{action}", $class_node );
100 $log->error("Error finding action node: $error [perm:permacrud/perm:actions/perm:$self->{action}]");
101 throw OpenSRF::DomainObject::oilsException->new(
103 status => "Error finding action node: $error [perm:permacrud/perm:actions/perm:$self->{action}]"
108 $log->error("Error finding action node: $error [perm:permacrud/perm:actions/perm:$self->{action}]");
109 throw OpenSRF::DomainObject::oilsException->new(
111 status => "Error finding action node: $error [perm:permacrud/perm:actions/perm:$self->{action}]"
115 my $all_perms = $action_node->getAttribute( 'all_perms' );
117 my $fm_class = $xpc->findvalue( '@oils_obj:fieldmapper', $class_node );
119 my $retrieve_method = 'retrieve_' . $fm_class;
120 $retrieve_method =~ s/::/_/go;
121 $obj = $e->$retrieve_method( $obj ) or return $e->die_event;
124 (my $o_type = $fm_class) =~ s/::/./go;
126 my $perm_field_value = $action_node->getAttribute('permission');
128 if ($perm_field_value) {
129 my @perms = split ' ', $perm_field_value;
132 if ($action_node->getAttribute('global_required')) {
133 push @context_ous, $e->search_actor_org_unit( { parent_ou => undef } )->[0]->id;
136 my $context_field_value = $action_node->getAttribute('context_field');
138 if ($context_field_value) {
139 push @context_ous, $obj->$_ for ( split ' ', $context_field_value );
141 for my $context_node ( $xpc->findnodes( "perm:context", $action_node ) ) {
142 my $context_field = $context_node->getAttribute('field');
143 my $link_field = $context_node->getAttribute('link');
147 my ($link_node) = $xpc->findnodes( "idl:links/idl:link[\@field='$link_field']", $class_node );
148 my $link_class_hint = $link_node->getAttribute('class');
149 my $remote_field = $link_node->getAttribute('key');
151 my ($remote_class_node) = $xpc->findnodes( "//idl:class[\@id='$link_class_hint']", $idl->documentElement );
152 my $search_method = 'search_' . $xpc->findvalue( '@oils_obj:fieldmapper', $remote_class_node );
153 $search_method =~ s/::/_/go;
155 for my $remote_object ( @{$e->$search_method( { $remote_field => $obj->$link_field } )} ) {
156 push @context_ous, $remote_object->$context_field;
159 push @context_ous, $obj->$_ for ( split ' ', $context_field );
166 for my $perm (@perms) {
168 for my $c_ou (@context_ous) {
169 if ($e->allowed($perm => $c_ou => $obj)) {
175 $pok++ if ($e->allowed($perm => undef => $obj));
179 if ((lc($all_perms) eq 'true' && @perms != $pok) or !$pok) {
180 return OpenILS::Event->new('PERM_FAILURE',
181 ilsperm => "", # XXX add logic to report which perm failed
183 payload => "Perm failure -- action: $self->{action}, object type: $self->{json_hint}",
188 if ($self->{action} eq 'retrieve') {
193 $o_type =~ s/\./_/og;
194 my $method = $self->{action} . "_$o_type";
195 my $val = $e->$method($obj) or return $e->die_event;
201 sub search_permacrud {
208 delete $args[1]{flesh};
209 delete $args[1]{flesh_fields};
212 my $e = new_editor(authtoken => $auth, xact => 1);
213 return $e->event unless $e->checkauth;
217 ($class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
220 $log->error("Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]");
221 throw OpenSRF::DomainObject::oilsException->new(
223 status => "Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]"
227 my $search_method = 'search_' . $xpc->findvalue( '@oils_obj:fieldmapper', $class_node );
228 $search_method =~ s/::/_/go;
230 $log->debug("Calling CStoreEditor search method: $search_method");
232 my $obj_list = $e->$search_method( \@args );
234 my $retriever = $self->method_lookup( $self->{retriever} );
235 for my $o ( @$obj_list ) {
237 ($o) = $retriever->run( $auth, $o, $e );
238 $client->respond( $o ) if ($o);
245 sub generate_methods {
247 for my $class_node ( $xpc->findnodes( '//idl:class[perm:permacrud]', $idl->documentElement ) ) {
248 my $hint = $class_node->getAttribute('id');
249 $log->debug("permacrud class_node $hint");
251 for my $action_node ( $xpc->findnodes( "perm:permacrud/perm:actions/perm:*", $class_node ) ) {
252 (my $method = $action_node->localname) =~ s/^.+:(.+)$/$1/o;
253 $log->internal("permacrud method = $method");
255 __PACKAGE__->register_method(
256 method => 'CRUD_action_object_permcheck',
257 api_name => 'open-ils.permacrud.' . $method . '.' . $hint,
262 if ($method eq 'retrieve') {
263 __PACKAGE__->register_method(
264 method => 'search_permacrud',
265 api_name => 'open-ils.permacrud.search.' . $hint,
267 retriever => 'open-ils.permacrud.retrieve.' . $hint,
275 $log->error("error generating permacrud methods: $e");