3 package OpenILS::Application::PermaCrud;
4 use OpenILS::Application;
5 use base qw/OpenILS::Application/;
7 use Unicode::Normalize;
8 use OpenSRF::EX qw/:try/;
10 use OpenSRF::AppSession;
11 use OpenSRF::Utils::SettingsClient;
12 use OpenSRF::Utils::Logger qw/:level/;
14 use OpenILS::Utils::Fieldmapper;
15 use OpenSRF::Utils::JSON;
17 use OpenILS::Utils::CStoreEditor qw/:funcs/;
20 use XML::LibXML::XPathContext;
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'},
32 my $log = 'OpenSRF::Utils::Logger';
34 my $parser = XML::LibXML->new();
35 my $xslt = XML::LibXSLT->new();
37 my $xpc = XML::LibXML::XPathContext->new();
38 $xpc->registerNs($_, $namespace_map{$_}{ns}) for ( keys %namespace_map );
44 my $conf = OpenSRF::Utils::SettingsClient->new;
45 my $idl_file = $conf->config_value( 'IDL' );
47 $idl = $parser->parse_file( $idl_file );
49 $log->debug( 'IDL XML file loaded' );
56 sub CRUD_action_object_permcheck {
62 my $e = new_editor(authtoken => $auth, xact => 1);
63 return $e->event unless $e->checkauth;
65 if (ref($obj) && $obj->json_hint ne $self->{class_hint}) {
66 throw OpenSRF::DomainObject::oilsException->new(
68 status => "Class missmatch: $self->{class_hint} method called with " . $obj->json_hint,
74 ($class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
77 $log->error("Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]");
78 throw OpenSRF::DomainObject::oilsException->new(
80 status => "Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]"
85 $log->error("Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]");
86 throw OpenSRF::DomainObject::oilsException->new(
88 status => "Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]"
94 ($action_node) = $xpc->findnodes( "perm:permacrud/perm:actions/perm:$self->{action}", $class_node );
97 $log->error("Error finding action node: $error [perm:permacrud/perm:actions/perm:$self->{action}]");
98 throw OpenSRF::DomainObject::oilsException->new(
100 status => "Error finding action node: $error [perm:permacrud/perm:actions/perm:$self->{action}]"
105 $log->error("Error finding action node: $error [perm:permacrud/perm:actions/perm:$self->{action}]");
106 throw OpenSRF::DomainObject::oilsException->new(
108 status => "Error finding action node: $error [perm:permacrud/perm:actions/perm:$self->{action}]"
112 my $all_perms = $action_node->getAttribute( 'all_perms' );
114 my $fm_class = $xpc->findvalue( '@oils_obj:fieldmapper', $class_node );
116 my $retrieve_method = 'retrieve_' . $fm_class;
117 $retrieve_method =~ s/::/_/go;
118 $obj = $e->$retrieve_method( $obj );
121 (my $o_type = $fm_class) =~ s/::/./go;
122 my $id_field = $obj->Identity;
124 my $perm_field_value = $action_node->getAttribute('permission');
126 if ($perm_field_value) {
127 my @perms = split '\|', $action_node->getAttribute('permission');
130 if ($action_node->getAttribute('global_required')) {
131 push @context_ous, $e->search_actor_org_unit( { parent_ou => undef } )->[0]->id;
134 my $context_field_value = $action_node->getAttribute('context_field');
136 if ($context_field_value) {
137 push @context_ous, $obj->$_ for ( split '\|', $context_field_value );
139 for my $context_node ( $xpc->findnodes( "perm:context", $action_node ) ) {
140 my $context_field = $context_node->getAttribute('field');
141 my $link_field = $context_node->getAttribute('link');
145 my ($link_node) = $xpc->findnodes( "idl:links/idl:link[\@field='$link_field']", $class_node );
146 my $link_class_hint = $link_node->getAttribute('class');
147 my $remote_field = $link_node->getAttribute('key');
149 my ($remote_class_node) = $xpc->findnodes( "//idl:class[\@id='$link_class_hint']", $idl->documentElement );
150 my $search_method = 'search_' . $xpc->findvalue( '@oils_obj:fieldmapper', $remote_class_node );
151 $search_method =~ s/::/_/go;
153 for my $remote_object ( @{$e->$search_method( { $remote_field => $obj->$link_field } )} ) {
154 push @context_ous, $remote_object->$context_field;
157 push @context_ous, $obj->$_ for ( split '\|', $context_field );
164 for my $perm (@perms) {
166 for my $c_ou (@context_ous) {
167 if ($e->allowed($perm => $c_ou => $obj)) {
173 $pok++ if ($e->allowed($perm => undef => $obj));
177 if ((lc($all_perms) eq 'true' && @perms != $pok) or !$pok) {
178 throw OpenSRF::DomainObject::oilsException->new(
180 status => "Perm failure -- action: $self->{action}, object type: $self->{json_hint}",
185 return $obj if ($self->{action} eq 'retrieve');
187 my $val = $e->session->request("open-ils.cstore.direct.$o_type.$self->{action}" => $obj )->gather(1);
193 sub search_permacrud {
199 my $e = new_editor(authtoken => $auth, xact => 1);
200 return $e->event unless $e->checkauth;
204 ($class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
207 $log->error("Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]");
208 throw OpenSRF::DomainObject::oilsException->new(
210 status => "Error finding class node: $error [//idl:class[\@id='$self->{class_hint}']]"
214 my $search_method = 'search_' . $xpc->findvalue( '@oils_obj:fieldmapper', $class_node );
215 $search_method =~ s/::/_/go;
217 $log->debug("Calling CStoreEditor search method: $search_method");
219 my $obj_list = $e->$search_method( \@args );
221 my $retriever = $self->method_lookup( $self->{retriever} );
222 for my $o ( @$obj_list ) {
223 my ($o) = $retriever->run( $auth, $o );
224 $client->respond( $o ) if ($o);
230 sub generate_methods {
232 for my $class_node ( $xpc->findnodes( '//idl:class[perm:permacrud]', $idl->documentElement ) ) {
233 my $hint = $class_node->getAttribute('id');
234 $log->debug("permacrud class_node $hint");
236 for my $action_node ( $xpc->findnodes( "perm:permacrud/perm:actions/perm:*", $class_node ) ) {
237 (my $method = $action_node->localname) =~ s/^.+:(.+)$/$1/o;
238 $log->internal("permacrud method = $method");
240 __PACKAGE__->register_method(
241 method => 'CRUD_action_object_permcheck',
242 api_name => 'open-ils.permacrud.' . $method . '.' . $hint,
247 if ($method eq 'retrieve') {
248 __PACKAGE__->register_method(
249 method => 'search_permacrud',
250 api_name => 'open-ils.permacrud.search.' . $hint,
252 retriever => 'open-ils.permacrud.retrieve.' . $hint,
260 $log->error("error generating permacrud methods: $e");