| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642 |
- package RideLogicACL;
- use strict;
- #use RideLogic;
- #use RideLogicAPIQueryWrapper;
- use POSIX;
- require Exporter;
- use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
- $VERSION = "0.01";
- @ISA = qw( Exporter );
- @EXPORT = qw( );
- @EXPORT_OK = qw( );
- %EXPORT_TAGS = ( DEFAULT => [ qw( ) ] );
- my $PACKAGE_NAME = "RideLogicACL";
- #####
- sub new {
- my $class_name = shift;
- my $rldbh = shift;
- my %acl;
- $acl{aro_ref} = {};
- $acl{aco_ref} = {};
- $acl{aro_aco_ref} = {};
- my $class = {};
- $class->{acl_ref} = \%acl;
- $class->{dbh_ref} = \$rldbh;
- bless($class, $class_name);
- $class->get_tree();
- return $class;
- }
- #####
- sub print_debug {
- my $self = shift;
- my $acl_href = $self->{acl_href};
- my $aros_href = $acl_href->{aros_href};
- my $acos_href = $acl_href->{acos_href};
- my $aros_acos_href = $acl_href->{aros_acos_href};
- print "aros:\n";
- foreach my $k (keys(%{$aros_href})) {
- print "$k " . $aros_href->{$k} . "\n";
- }
- print "\n";
- print "acos:\n";
- foreach my $k (keys(%{$acos_href})) {
- print "$k " . $acos_href->{$k} . "\n";
- }
- print "\n";
- print "aros_acos:\n";
- foreach my $k (keys(%{$aros_acos_href})) {
- print "$k " . $aros_acos_href->{$k} . "\n";
- }
- }
- #####
- sub print_formatted_acl_table {
- my $self = shift;
- my $type = shift;
- return undef if !($type =~ /^(aros|acos|aros_acos)$/);
- my $rldbh = ${$self->{dbh_ref}};
- my $query = $rldbh->prepare(" select * from rlapi_" . $type . " order by lft asc");
- $query->execute();
- my @s;
- while (my $row = $query->fetchrow_hashref) {
- my $id = $row->{id};
- my $lft = $row->{lft};
- my $rght = $row->{rght};
- my $alias = $row->{alias};
- if (scalar(@s)) {
- while ( (scalar(@s)>0) && ($s[scalar(@s)-1] < $rght) ) {
- pop @s;
- }
- }
- print " " x (scalar(@s)*2) . "($id)[$lft,$rght] $alias\n";
- push @s, $rght;
- }
- }
- #####
- sub print_acl_tree {
- my $self = shift;
- $self->print_formatted_acl_table("aros");
- print "\n\n";
- $self->print_formatted_acl_table("acos");
- print "\n\n";
- my $rldbh = ${$self->{dbh_ref}};
- my $query = $rldbh->prepare("select id, aros_id, acos_id from rlapi_aros_acos order by id asc");
- $query->execute();
- while (my $row = $query->fetchrow_arrayref) {
- my ($id, $aros_id, $acos_id) = ($row->[0], $row->[1], $row->[2]);
- print "[$id] ($aros_id, $acos_id)\n";
- }
- print "\n\n";
- }
- #####
- sub get_tree {
- my $self = shift;
- my $rldbh = ${$self->{dbh_ref}};
- $self->{acl_href} = {};
- my $acl_href = $self->{acl_href};
- $acl_href->{aros_href} = {};
- $acl_href->{acos_href} = {};
- $acl_href->{aros_acos_href} = {};
- my $aros_href = $acl_href->{aros_href};
- my $acos_href = $acl_href->{acos_href};
- my $aros_acos_href = $acl_href->{aros_acos_href};
- my $query = $rldbh->prepare("select id, lft, rght, alias from rlapi_aros order by lft asc");
- $query->execute();
- my @rght_stack;
- my @alias_stack;
- while (my $row = $query->fetchrow_hashref) {
- my $id = $row->{id};
- my $lft = $row->{lft};
- my $rght = $row->{rght};
- my $alias = $row->{alias};
- if (scalar(@rght_stack)) {
- while ( (scalar(@rght_stack)>0) && ($rght_stack[scalar(@rght_stack)-1] < $rght) ) {
- pop @rght_stack;
- pop @alias_stack;
- }
- }
- push @rght_stack, $rght;
- push @alias_stack, $alias;
- $aros_href->{ join('/', @alias_stack) } = $id;
- }
-
- my $query = $rldbh->prepare("select id, lft, rght, alias from rlapi_acos order by lft asc");
- $query->execute();
- @rght_stack = ();
- @alias_stack = ();
- while (my $row = $query->fetchrow_hashref) {
- my $id = $row->{id};
- my $lft = $row->{lft};
- my $rght = $row->{rght};
- my $alias = $row->{alias};
- if (scalar(@rght_stack)) {
- while ( (scalar(@rght_stack)>0) && ($rght_stack[scalar(@rght_stack)-1] < $rght) ) {
- pop @rght_stack;
- pop @alias_stack;
- }
- }
- push @rght_stack, $rght;
- push @alias_stack, $alias;
- $acos_href->{ join('/', @alias_stack) } = $id;
- }
- my $query = $rldbh->prepare("select aros_id, acos_id from rlapi_aros_acos ");
- $query->execute();
- while (my $row = $query->fetchrow_arrayref) {
- $aros_acos_href->{ $row->[0] . ":" . $row->[1] } = 1;
- }
- }
- #####
- sub rlapi_acl_get_effective_id {
- my $ao = shift;
- my $ao_href = shift;
- $ao =~ s/^\/*//;
- my @a = split(/\//, $ao);
- while (scalar(@a)>0) {
- my $s = join('/', @a);
- if (defined($ao_href->{$s})) {
- return $ao_href->{$s};
- }
- pop @a;
- }
- return undef;
- }
- #####
- sub get_aro_id {
- my $self = shift;
- my $aro = shift;
- my $acl_href = $self->{acl_href};
- return rlapi_acl_get_effective_id( $aro, $acl_href->{aros_href} );
- }
- #####
- sub get_real_aro_id {
- my $self = shift;
- my $aro = shift;
- $aro =~ s/^\///;
- my $acl_href = $self->{acl_href};
- my $href = $acl_href->{aros_href};
- return $href->{$aro};
- }
- #####
- sub get_aco_id {
- my $self = shift;
- my $aco = shift;
- my $acl_href = $self->{acl_href};
- return rlapi_acl_get_effective_id( $aco, $acl_href->{acos_href} );
- }
- #####
- sub get_real_aco_id {
- my $self = shift;
- my $aco = shift;
- $aco =~ s/^\///;
- my $acl_href = $self->{acl_href};
- my $href = $acl_href->{acos_href};
- return $href->{$aco};
- }
- #####
- # inefficient method. need to update this to have
- # a local version of the tree for efficient collection
- sub get_aro_aco_subtree_by_aro {
- my $self = shift;
- my $aro = shift;
- my $aco_root = shift;
- $aro =~ s/^\///;
- $aco_root =~ s/^\///;
- my $acl_href = $self->{acl_href};
- my $aros_href = $acl_href->{aros_href};
- my $acos_href = $acl_href->{acos_href};
- my $aros_acos_href = $acl_href->{aros_acos_href};
- my @res;
- my $aco_root_len = length($aco_root);
- my $aro_id = rlapi_acl_get_effective_id($aro, $aros_href);
- foreach my $k (keys(%$acos_href))
- {
- my $sub_aco = substr($k, 0, $aco_root_len);
- next if $sub_aco ne $aco_root;
- my $aco_id = rlapi_acl_get_effective_id($k, $acos_href);
- push @res, $k if $aros_acos_href->{ $aro_id . ":" . $aco_id };
- }
- return @res;
- }
- #####
- sub get_aro_aco_subtree_by_aco {
- my $self = shift;
- my $aro_root = shift;
- my $aco = shift;
- $aro_root =~ s/^\///;
- $aco =~ s/^\///;
- my $acl_href = $self->{acl_href};
- my $aros_href = $acl_href->{aros_href};
- my $acos_href = $acl_href->{acos_href};
- my $aros_acos_href = $acl_href->{aros_acos_href};
- my @res;
- my $aro_root_len = length($aro_root);
- my $aco_id = rlapi_acl_get_effective_id($aco, $acos_href);
- foreach my $k (keys(%$aros_href))
- {
- my $sub_aro = substr($k, 0, $aro_root_len);
- next if $sub_aro ne $aro_root;
- my $aro_id = rlapi_acl_get_effective_id($k, $aros_href);
- push @res, $k if $aros_acos_href->{ $aro_id . ":" . $aco_id };
- }
- return @res;
- }
- #####
- sub has_permission {
- my $self = shift;
- my $aro = shift;
- my $aco = shift;
- my $acl_href = $self->{acl_href};
- my $aros_href = $acl_href->{aros_href};
- my $acos_href = $acl_href->{acos_href};
- my $aros_acos_href = $acl_href->{aros_acos_href};
- my $aro_id = rlapi_acl_get_effective_id($aro, $aros_href);
- my $aco_id = rlapi_acl_get_effective_id($aco, $acos_href);
- return $aros_acos_href->{ $aro_id . ":" . $aco_id };
- }
- #####
- sub insert_aro {
- my $self = shift;
- my $aro = shift;
- $aro =~ s/^\/*//;
- my $rldbh = ${$self->{dbh_ref}};
- my $acl_href = $self->{acl_href};
- my $aros_href = $acl_href->{aros_href};
- return undef if ($aros_href->{$aro});
- my $foreign_key = undef;
- my @a = split(/\//, $aro);
- my $aro_alias = pop @a;
- if (scalar(@a)==0) {
- my $query = $rldbh->prepare("call rlapi_aros_insert( 1, ?, ?)");
- my $r = $query->execute($foreign_key, $aro_alias);
- my $tquery = $rldbh->prepare("select last_insert_id()");
- my $r = $tquery->execute();
- my $trow = $tquery->fetchrow_arrayref;
- my $id = $trow->[0];
- $self->get_tree();
- return $id;
- }
- my $parent_aro = join('/', @a);
- return undef if (!$aros_href->{$parent_aro});
- my $parent_id = $aros_href->{$parent_aro};
- my $query = $rldbh->prepare("call rlapi_aros_insert_under( ?, ?, ?)");
- my $r = $query->execute($parent_id, $foreign_key, $aro_alias);
- #my $id = $rldbh->last_insert_id();
- my $tquery = $rldbh->prepare("select last_insert_id()");
- $tquery->execute();
- my $trow = $tquery->fetchrow_arrayref;
- my $id = $trow->[0];
- $self->get_tree();
- return $id;
-
- }
- #####
- sub insert_aco {
- my $self = shift;
- my $aco = shift;
- $aco =~ s/^\/*//;
- my $rldbh = ${$self->{dbh_ref}};
- my $acl_href = $self->{acl_href};
- my $acos_href = $acl_href->{acos_href};
- return undef if ($acos_href->{$aco});
- my $foreign_key = undef;
- my @a = split(/\//, $aco);
- my $aco_alias = pop @a;
- if (scalar(@a)==0) {
- my $query = $rldbh->prepare("call rlapi_acos_insert( 1, ?, ?)");
- $query->execute($foreign_key, $aco_alias);
- #my $id = $rldbh->last_insert_id(undef, undef, undef, undef);
- my $tquery = $rldbh->prepare("select last_insert_id()");
- $tquery->execute();
- my $trow = $tquery->fetchrow_arrayref;
- my $id = $trow->[0];
- $self->get_tree();
- return $id;
- }
- my $parent_aco = join('/', @a);
- return undef if (!$acos_href->{$parent_aco});
- my $parent_id = $acos_href->{$parent_aco};
- my $query = $rldbh->prepare("call rlapi_acos_insert_under( ?, ?, ?)");
- $query->execute($parent_id, $foreign_key, $aco_alias);
- #my $id = $rldbh->last_insert_id();
- my $tquery = $rldbh->prepare("select last_insert_id()");
- $tquery->execute();
- my $trow = $tquery->fetchrow_arrayref;
- my $id = $trow->[0];
- $self->get_tree();
- return $id;
-
- }
- #####
- sub insert_aros_acos {
- my $self = shift;
- my $aro = shift;
- my $aco = shift;
- $aro =~ s/^\///;
- $aco =~ s/^\///;
- my $rldbh = ${$self->{dbh_ref}};
- my $acl_href = $self->{acl_href};
- my $aros_href = $acl_href->{aros_href};
- my $acos_href = $acl_href->{acos_href};
- my $aros_acos_href = $acl_href->{aros_acos_href};
- my $aro_id = $aros_href->{$aro};
- my $aco_id = $acos_href->{$aco};
- return undef if !$aro_id or !$aco_id;
- my $query = $rldbh->prepare("select count(id) from rlapi_aros_acos where aros_id = ? and acos_id = ?");
- $query->execute($aro_id, $aco_id);
- return undef if $query->fetchrow_arrayref->[0];
- my $query = $rldbh->prepare("insert into rlapi_aros_acos (aros_id, acos_id) values (?, ?)");
- $query->execute($aro_id, $aco_id);
- my $tquery = $rldbh->prepare("select last_insert_id()");
- $tquery->execute();
- my $trow = $tquery->fetchrow_arrayref;
- my $id = $trow->[0];
- $self->get_tree();
- return $id;
- }
- #####
- sub remove_aro_id {
- my $self = shift;
- my $aro_id = shift;
- my $rldbh = ${$self->{dbh_ref}};
- my $query = $rldbh->prepare("call rlapi_aros_delete(?)");
- $query->execute($aro_id);
- $self->get_tree();
- return 1;
- }
- #####
- sub remove_aco_id {
- my $self = shift;
- my $aco_id = shift;
- my $rldbh = ${$self->{dbh_ref}};
- my $query = $rldbh->prepare("call rlapi_acos_delete(?)");
- $query->execute($aco_id);
- $self->get_tree();
- return 1;
- }
- #####
- sub remove_aro_aco_id {
- my $self = shift;
- my $aro_id = shift,
- my $aco_id = shift;
- my $rldbh = ${$self->{dbh_ref}};
- my $query = $rldbh->prepare("delete from rlapi_aros_acos where aros_id = ? and acos_id = ?");
- $query->execute($aro_id, $aco_id);
- $self->get_tree();
- return 1;
- }
- #####
- sub remove_aro {
- my $self = shift;
- my $aro = shift;
- $aro =~ s/\///;
- my $rldbh = ${$self->{dbh_ref}};
- my $acl_href = $self->{acl_href};
- my $aros_href = $acl_href->{aros_href};
- return undef if !$aros_href->{$aro};
- my $query = $rldbh->prepare("call rlapi_aros_delete(?)");
- $query->execute($aros_href->{$aro});
- $self->get_tree();
- return 1;
- }
- #####
- sub remove_aco {
- my $self = shift;
- my $aco = shift;
- $aco =~ s/\///;
- my $rldbh = ${$self->{dbh_ref}};
- my $acl_href = $self->{acl_href};
- my $acos_href = $acl_href->{acos_href};
- return undef if !$acos_href->{$aco};
- my $query = $rldbh->prepare("call rlapi_acos_delete(?)");
- $query->execute($acos_href->{$aco});
- $self->get_tree();
- return 1;
- }
- #####
- sub remove_aros_acos {
- my $self = shift;
- my $aro = shift;
- my $aco = shift;
- $aro =~ s/^\///;
- $aco =~ s/^\///;
- my $rldbh = ${$self->{dbh_ref}};
- my $acl_href = $self->{acl_href};
- my $aros_href = $acl_href->{aros_href};
- my $acos_href = $acl_href->{acos_href};
- my $aros_acos_href = $acl_href->{aros_acos_href};
- my $aro_id = $aros_href->{$aro};
- my $aco_id = $acos_href->{$aco};
- return undef if !$aro_id or !$aco_id;
- my $query = $rldbh->prepare("delete from rlapi_aros_acos where aros_id = ? and acos_id = ?");
- $query->execute($aro_id, $aco_id);
- return 1;
- }
- return 1;
|