1 package OpenILS::Application::PermaCrud;
2 use OpenILS::Application;
3 use base qw/OpenILS::Application/;
5 use Unicode::Normalize;
6 use OpenSRF::EX qw/:try/;
8 use OpenSRF::AppSession;
9 use OpenSRF::Utils::SettingsClient;
10 use OpenSRF::Utils::Logger qw/:level/;
12 use OpenILS::Utils::Fieldmapper;
13 use OpenSRF::Utils::JSON;
15 use OpenILS::Utils::CStoreEditor qw/:funcs/;
18 use XML::LibXML::XPathContext;
21 our %namespace_map = (
22 oils_persist=> {ns => 'http://open-ils.org/spec/opensrf/IDL/persistence/v1'},
23 oils_obj => {ns => 'http://open-ils.org/spec/opensrf/IDL/objects/v1'},
24 idl => {ns => 'http://opensrf.org/spec/IDL/base/v1'},
25 reporter => {ns => 'http://open-ils.org/spec/opensrf/IDL/reporter/v1'},
29 my $log = 'OpenSRF::Utils::Logger';
31 my $parser = XML::LibXML->new();
32 my $xslt = XML::LibXSLT->new();
34 my $xpc = XML::LibXML::XPathContext->new();
35 $xpc->registerNs($_, $namespace_map{$_}{ns}) for ( keys %namespace_map );
41 my $conf = OpenSRF::Utils::SettingsClient->new;
42 my $idl_file = $conf->config_value( 'IDL' );
44 $idl = $parser->parse_file( $idl_file );
46 $log->debug( 'IDL XML file loaded' );
50 sub CRUD_action_object_permcheck {
56 my $e = new_editor(authtoken => $auth, xact => 1);
57 return $e->event unless $e->checkauth;
59 unless ($obj->json_hint eq $self->{class_hint}) {
60 throw OpenSRF::DomainObject::oilsException->new(
62 status => "Class missmatch: $self->{class_hint} method called with " . $obj->json_hint,
66 my $action = $self->api_name =~ s/^open-ils\.admin\.([^\.])\..+$/$1/o;
67 my $o_type = $obj->cdbi =~ s/::/./go;
69 my ($class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
70 my ($action_node) = $xpc->findnodes( "perm:permacrud/perm:actions/perm:$action", $class_node );
71 my $all_perms = $xpc->getAttribute( 'all_perms', $action_node );
73 my $perm_field_value = $aciton_node->getAttribute('permission');
75 if (defined($perm_field_value)) {
76 my @perms = split '|', $aciton_node->getAttribute('permission');
79 if ($aciton_node->getAttribute('global_required')) {
80 push @context_ous, $e->search_actor_org_unit( { parent_ou => undef } )->[0]->id;
83 my $context_field_value = $aciton_node->getAttribute('context_field');
85 if (defined($context_field_value)) {
86 push @context_ous, $obj->$_ for ( split '|', $context_field_value );
88 for my $context_node ( $xpc->findnodes( "perm:context", $action_node ) ) {
89 my $context_field = $context_node->getAttribute('field');
90 my $link_field = $context_node->getAttribute('link');
94 my ($link_node) = $xpc->findnodes( "idl:links/idl:link[\@field='$link_field']", $class_node );
95 my $link_class_hint = $link_node->getAttribute('class');
96 my $remote_field = $link_node->getAttribute('key');
98 my ($remote_class_node) = $xpc->findnodes( "//idl:class[\@id='$self->{class_hint}']", $idl->documentElement );
99 my $search_method = 'search_' . $xpc->findvalue( '@oils_obj:fieldmapper', $remote_class_node );
100 $search_method =~ s/::/_/go;
102 for my $remote_object ( @{$e->$search_method( { $key => $obj->$link_field } )} ) {
103 push @context_ous, $remote_object->$context_field;
106 push @context_ous, $obj->$_ for ( split '|', $context_field );
113 for my $perm (@perms) {
115 for my $c_ou (@context_ous) {
116 if ($e->allowed($perm => $c_ou => $obj)) {
122 $pok++ if ($e->allowed($perm => undef => $obj));
127 if ((lc($all_perms) eq 'true' && @perms != $pok) or !$pok) {
128 throw OpenSRF::DomainObject::oilsException->new(
130 status => "Perm failure -- action: $action, object type: $self->{json_hint}",
136 for my $class_node ( $xpc->findnodes( "perm:permacrud/perm:actions/perm:$action", $class_node ) ) {
137 my $hint = $class_node->getAttribute('id');
139 for my $action_node ( $xpc->findnodes( "perm:permacrud/perm:actions/perm:*", $class_node ) ) {
140 my $method = $action_node->localname =~ s/^.+:(.+)$/$1/o;
142 __PACKAGE__->register_method(
143 method => 'CRUD_action_object_permcheck',
144 api_name => 'open-ils.permacrud.' . $method . '.' . $hint,