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,
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 = $action_node->getAttribute( 'all_perms' );
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);
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;
86 my $perm_field_value = $aciton_node->getAttribute('permission');
88 if (defined($perm_field_value)) {
89 my @perms = split '|', $aciton_node->getAttribute('permission');
92 if ($aciton_node->getAttribute('global_required')) {
93 push @context_ous, $e->search_actor_org_unit( { parent_ou => undef } )->[0]->id;
96 my $context_field_value = $aciton_node->getAttribute('context_field');
98 if (defined($context_field_value)) {
99 push @context_ous, $obj->$_ for ( split '|', $context_field_value );
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');
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');
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;
115 for my $remote_object ( @{$e->$search_method( { $key => $obj->$link_field } )} ) {
116 push @context_ous, $remote_object->$context_field;
119 push @context_ous, $obj->$_ for ( split '|', $context_field );
126 for my $perm (@perms) {
128 for my $c_ou (@context_ous) {
129 if ($e->allowed($perm => $c_ou => $obj)) {
135 $pok++ if ($e->allowed($perm => undef => $obj));
139 if ((lc($all_perms) eq 'true' && @perms != $pok) or !$pok) {
140 throw OpenSRF::DomainObject::oilsException->new(
142 status => "Perm failure -- action: $action, object type: $self->{json_hint}",
147 return $obj if ($action eq 'retrieve');
149 return $e->session->request("open-ils.cstore.direct.$o_type.$action" => $obj )->gather(1);
152 sub search_permacrud {
158 my $e = new_editor(authtoken => $auth);
159 return $e->event unless $e->checkauth;
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;
165 my $retriever = $self->method_lookup( $self->{retriever} );
166 my $obj_list = $e->$search_method( @args );
168 for my $o ( @$obj_list ) {
169 my ($o) = $retriever->run( $o );
170 $client->respond( $o ) if ($o);
176 sub generate_methods {
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");
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");
186 __PACKAGE__->register_method(
187 method => 'CRUD_action_object_permcheck',
188 api_name => 'open-ils.permacrud.' . $method . '.' . $hint,
193 if ($method eq 'retrieve') {
194 __PACKAGE__->register_method(
195 method => 'search_permcheck',
196 api_name => 'open-ils.permacrud.search.' . $hint,
198 retriever => 'open-ils.permacrud.retrieve.' . $hint,
206 $log->error("error generating permacrud methods: $e");