RideLogicACL.pm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642
  1. package RideLogicACL;
  2. use strict;
  3. #use RideLogic;
  4. #use RideLogicAPIQueryWrapper;
  5. use POSIX;
  6. require Exporter;
  7. use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  8. $VERSION = "0.01";
  9. @ISA = qw( Exporter );
  10. @EXPORT = qw( );
  11. @EXPORT_OK = qw( );
  12. %EXPORT_TAGS = ( DEFAULT => [ qw( ) ] );
  13. my $PACKAGE_NAME = "RideLogicACL";
  14. #####
  15. sub new {
  16. my $class_name = shift;
  17. my $rldbh = shift;
  18. my %acl;
  19. $acl{aro_ref} = {};
  20. $acl{aco_ref} = {};
  21. $acl{aro_aco_ref} = {};
  22. my $class = {};
  23. $class->{acl_ref} = \%acl;
  24. $class->{dbh_ref} = \$rldbh;
  25. bless($class, $class_name);
  26. $class->get_tree();
  27. return $class;
  28. }
  29. #####
  30. sub print_debug {
  31. my $self = shift;
  32. my $acl_href = $self->{acl_href};
  33. my $aros_href = $acl_href->{aros_href};
  34. my $acos_href = $acl_href->{acos_href};
  35. my $aros_acos_href = $acl_href->{aros_acos_href};
  36. print "aros:\n";
  37. foreach my $k (keys(%{$aros_href})) {
  38. print "$k " . $aros_href->{$k} . "\n";
  39. }
  40. print "\n";
  41. print "acos:\n";
  42. foreach my $k (keys(%{$acos_href})) {
  43. print "$k " . $acos_href->{$k} . "\n";
  44. }
  45. print "\n";
  46. print "aros_acos:\n";
  47. foreach my $k (keys(%{$aros_acos_href})) {
  48. print "$k " . $aros_acos_href->{$k} . "\n";
  49. }
  50. }
  51. #####
  52. sub print_formatted_acl_table {
  53. my $self = shift;
  54. my $type = shift;
  55. return undef if !($type =~ /^(aros|acos|aros_acos)$/);
  56. my $rldbh = ${$self->{dbh_ref}};
  57. my $query = $rldbh->prepare(" select * from rlapi_" . $type . " order by lft asc");
  58. $query->execute();
  59. my @s;
  60. while (my $row = $query->fetchrow_hashref) {
  61. my $id = $row->{id};
  62. my $lft = $row->{lft};
  63. my $rght = $row->{rght};
  64. my $alias = $row->{alias};
  65. if (scalar(@s)) {
  66. while ( (scalar(@s)>0) && ($s[scalar(@s)-1] < $rght) ) {
  67. pop @s;
  68. }
  69. }
  70. print " " x (scalar(@s)*2) . "($id)[$lft,$rght] $alias\n";
  71. push @s, $rght;
  72. }
  73. }
  74. #####
  75. sub print_acl_tree {
  76. my $self = shift;
  77. $self->print_formatted_acl_table("aros");
  78. print "\n\n";
  79. $self->print_formatted_acl_table("acos");
  80. print "\n\n";
  81. my $rldbh = ${$self->{dbh_ref}};
  82. my $query = $rldbh->prepare("select id, aros_id, acos_id from rlapi_aros_acos order by id asc");
  83. $query->execute();
  84. while (my $row = $query->fetchrow_arrayref) {
  85. my ($id, $aros_id, $acos_id) = ($row->[0], $row->[1], $row->[2]);
  86. print "[$id] ($aros_id, $acos_id)\n";
  87. }
  88. print "\n\n";
  89. }
  90. #####
  91. sub get_tree {
  92. my $self = shift;
  93. my $rldbh = ${$self->{dbh_ref}};
  94. $self->{acl_href} = {};
  95. my $acl_href = $self->{acl_href};
  96. $acl_href->{aros_href} = {};
  97. $acl_href->{acos_href} = {};
  98. $acl_href->{aros_acos_href} = {};
  99. my $aros_href = $acl_href->{aros_href};
  100. my $acos_href = $acl_href->{acos_href};
  101. my $aros_acos_href = $acl_href->{aros_acos_href};
  102. my $query = $rldbh->prepare("select id, lft, rght, alias from rlapi_aros order by lft asc");
  103. $query->execute();
  104. my @rght_stack;
  105. my @alias_stack;
  106. while (my $row = $query->fetchrow_hashref) {
  107. my $id = $row->{id};
  108. my $lft = $row->{lft};
  109. my $rght = $row->{rght};
  110. my $alias = $row->{alias};
  111. if (scalar(@rght_stack)) {
  112. while ( (scalar(@rght_stack)>0) && ($rght_stack[scalar(@rght_stack)-1] < $rght) ) {
  113. pop @rght_stack;
  114. pop @alias_stack;
  115. }
  116. }
  117. push @rght_stack, $rght;
  118. push @alias_stack, $alias;
  119. $aros_href->{ join('/', @alias_stack) } = $id;
  120. }
  121. my $query = $rldbh->prepare("select id, lft, rght, alias from rlapi_acos order by lft asc");
  122. $query->execute();
  123. @rght_stack = ();
  124. @alias_stack = ();
  125. while (my $row = $query->fetchrow_hashref) {
  126. my $id = $row->{id};
  127. my $lft = $row->{lft};
  128. my $rght = $row->{rght};
  129. my $alias = $row->{alias};
  130. if (scalar(@rght_stack)) {
  131. while ( (scalar(@rght_stack)>0) && ($rght_stack[scalar(@rght_stack)-1] < $rght) ) {
  132. pop @rght_stack;
  133. pop @alias_stack;
  134. }
  135. }
  136. push @rght_stack, $rght;
  137. push @alias_stack, $alias;
  138. $acos_href->{ join('/', @alias_stack) } = $id;
  139. }
  140. my $query = $rldbh->prepare("select aros_id, acos_id from rlapi_aros_acos ");
  141. $query->execute();
  142. while (my $row = $query->fetchrow_arrayref) {
  143. $aros_acos_href->{ $row->[0] . ":" . $row->[1] } = 1;
  144. }
  145. }
  146. #####
  147. sub rlapi_acl_get_effective_id {
  148. my $ao = shift;
  149. my $ao_href = shift;
  150. $ao =~ s/^\/*//;
  151. my @a = split(/\//, $ao);
  152. while (scalar(@a)>0) {
  153. my $s = join('/', @a);
  154. if (defined($ao_href->{$s})) {
  155. return $ao_href->{$s};
  156. }
  157. pop @a;
  158. }
  159. return undef;
  160. }
  161. #####
  162. sub get_aro_id {
  163. my $self = shift;
  164. my $aro = shift;
  165. my $acl_href = $self->{acl_href};
  166. return rlapi_acl_get_effective_id( $aro, $acl_href->{aros_href} );
  167. }
  168. #####
  169. sub get_real_aro_id {
  170. my $self = shift;
  171. my $aro = shift;
  172. $aro =~ s/^\///;
  173. my $acl_href = $self->{acl_href};
  174. my $href = $acl_href->{aros_href};
  175. return $href->{$aro};
  176. }
  177. #####
  178. sub get_aco_id {
  179. my $self = shift;
  180. my $aco = shift;
  181. my $acl_href = $self->{acl_href};
  182. return rlapi_acl_get_effective_id( $aco, $acl_href->{acos_href} );
  183. }
  184. #####
  185. sub get_real_aco_id {
  186. my $self = shift;
  187. my $aco = shift;
  188. $aco =~ s/^\///;
  189. my $acl_href = $self->{acl_href};
  190. my $href = $acl_href->{acos_href};
  191. return $href->{$aco};
  192. }
  193. #####
  194. # inefficient method. need to update this to have
  195. # a local version of the tree for efficient collection
  196. sub get_aro_aco_subtree_by_aro {
  197. my $self = shift;
  198. my $aro = shift;
  199. my $aco_root = shift;
  200. $aro =~ s/^\///;
  201. $aco_root =~ s/^\///;
  202. my $acl_href = $self->{acl_href};
  203. my $aros_href = $acl_href->{aros_href};
  204. my $acos_href = $acl_href->{acos_href};
  205. my $aros_acos_href = $acl_href->{aros_acos_href};
  206. my @res;
  207. my $aco_root_len = length($aco_root);
  208. my $aro_id = rlapi_acl_get_effective_id($aro, $aros_href);
  209. foreach my $k (keys(%$acos_href))
  210. {
  211. my $sub_aco = substr($k, 0, $aco_root_len);
  212. next if $sub_aco ne $aco_root;
  213. my $aco_id = rlapi_acl_get_effective_id($k, $acos_href);
  214. push @res, $k if $aros_acos_href->{ $aro_id . ":" . $aco_id };
  215. }
  216. return @res;
  217. }
  218. #####
  219. sub get_aro_aco_subtree_by_aco {
  220. my $self = shift;
  221. my $aro_root = shift;
  222. my $aco = shift;
  223. $aro_root =~ s/^\///;
  224. $aco =~ s/^\///;
  225. my $acl_href = $self->{acl_href};
  226. my $aros_href = $acl_href->{aros_href};
  227. my $acos_href = $acl_href->{acos_href};
  228. my $aros_acos_href = $acl_href->{aros_acos_href};
  229. my @res;
  230. my $aro_root_len = length($aro_root);
  231. my $aco_id = rlapi_acl_get_effective_id($aco, $acos_href);
  232. foreach my $k (keys(%$aros_href))
  233. {
  234. my $sub_aro = substr($k, 0, $aro_root_len);
  235. next if $sub_aro ne $aro_root;
  236. my $aro_id = rlapi_acl_get_effective_id($k, $aros_href);
  237. push @res, $k if $aros_acos_href->{ $aro_id . ":" . $aco_id };
  238. }
  239. return @res;
  240. }
  241. #####
  242. sub has_permission {
  243. my $self = shift;
  244. my $aro = shift;
  245. my $aco = shift;
  246. my $acl_href = $self->{acl_href};
  247. my $aros_href = $acl_href->{aros_href};
  248. my $acos_href = $acl_href->{acos_href};
  249. my $aros_acos_href = $acl_href->{aros_acos_href};
  250. my $aro_id = rlapi_acl_get_effective_id($aro, $aros_href);
  251. my $aco_id = rlapi_acl_get_effective_id($aco, $acos_href);
  252. return $aros_acos_href->{ $aro_id . ":" . $aco_id };
  253. }
  254. #####
  255. sub insert_aro {
  256. my $self = shift;
  257. my $aro = shift;
  258. $aro =~ s/^\/*//;
  259. my $rldbh = ${$self->{dbh_ref}};
  260. my $acl_href = $self->{acl_href};
  261. my $aros_href = $acl_href->{aros_href};
  262. return undef if ($aros_href->{$aro});
  263. my $foreign_key = undef;
  264. my @a = split(/\//, $aro);
  265. my $aro_alias = pop @a;
  266. if (scalar(@a)==0) {
  267. my $query = $rldbh->prepare("call rlapi_aros_insert( 1, ?, ?)");
  268. my $r = $query->execute($foreign_key, $aro_alias);
  269. my $tquery = $rldbh->prepare("select last_insert_id()");
  270. my $r = $tquery->execute();
  271. my $trow = $tquery->fetchrow_arrayref;
  272. my $id = $trow->[0];
  273. $self->get_tree();
  274. return $id;
  275. }
  276. my $parent_aro = join('/', @a);
  277. return undef if (!$aros_href->{$parent_aro});
  278. my $parent_id = $aros_href->{$parent_aro};
  279. my $query = $rldbh->prepare("call rlapi_aros_insert_under( ?, ?, ?)");
  280. my $r = $query->execute($parent_id, $foreign_key, $aro_alias);
  281. #my $id = $rldbh->last_insert_id();
  282. my $tquery = $rldbh->prepare("select last_insert_id()");
  283. $tquery->execute();
  284. my $trow = $tquery->fetchrow_arrayref;
  285. my $id = $trow->[0];
  286. $self->get_tree();
  287. return $id;
  288. }
  289. #####
  290. sub insert_aco {
  291. my $self = shift;
  292. my $aco = shift;
  293. $aco =~ s/^\/*//;
  294. my $rldbh = ${$self->{dbh_ref}};
  295. my $acl_href = $self->{acl_href};
  296. my $acos_href = $acl_href->{acos_href};
  297. return undef if ($acos_href->{$aco});
  298. my $foreign_key = undef;
  299. my @a = split(/\//, $aco);
  300. my $aco_alias = pop @a;
  301. if (scalar(@a)==0) {
  302. my $query = $rldbh->prepare("call rlapi_acos_insert( 1, ?, ?)");
  303. $query->execute($foreign_key, $aco_alias);
  304. #my $id = $rldbh->last_insert_id(undef, undef, undef, undef);
  305. my $tquery = $rldbh->prepare("select last_insert_id()");
  306. $tquery->execute();
  307. my $trow = $tquery->fetchrow_arrayref;
  308. my $id = $trow->[0];
  309. $self->get_tree();
  310. return $id;
  311. }
  312. my $parent_aco = join('/', @a);
  313. return undef if (!$acos_href->{$parent_aco});
  314. my $parent_id = $acos_href->{$parent_aco};
  315. my $query = $rldbh->prepare("call rlapi_acos_insert_under( ?, ?, ?)");
  316. $query->execute($parent_id, $foreign_key, $aco_alias);
  317. #my $id = $rldbh->last_insert_id();
  318. my $tquery = $rldbh->prepare("select last_insert_id()");
  319. $tquery->execute();
  320. my $trow = $tquery->fetchrow_arrayref;
  321. my $id = $trow->[0];
  322. $self->get_tree();
  323. return $id;
  324. }
  325. #####
  326. sub insert_aros_acos {
  327. my $self = shift;
  328. my $aro = shift;
  329. my $aco = shift;
  330. $aro =~ s/^\///;
  331. $aco =~ s/^\///;
  332. my $rldbh = ${$self->{dbh_ref}};
  333. my $acl_href = $self->{acl_href};
  334. my $aros_href = $acl_href->{aros_href};
  335. my $acos_href = $acl_href->{acos_href};
  336. my $aros_acos_href = $acl_href->{aros_acos_href};
  337. my $aro_id = $aros_href->{$aro};
  338. my $aco_id = $acos_href->{$aco};
  339. return undef if !$aro_id or !$aco_id;
  340. my $query = $rldbh->prepare("select count(id) from rlapi_aros_acos where aros_id = ? and acos_id = ?");
  341. $query->execute($aro_id, $aco_id);
  342. return undef if $query->fetchrow_arrayref->[0];
  343. my $query = $rldbh->prepare("insert into rlapi_aros_acos (aros_id, acos_id) values (?, ?)");
  344. $query->execute($aro_id, $aco_id);
  345. my $tquery = $rldbh->prepare("select last_insert_id()");
  346. $tquery->execute();
  347. my $trow = $tquery->fetchrow_arrayref;
  348. my $id = $trow->[0];
  349. $self->get_tree();
  350. return $id;
  351. }
  352. #####
  353. sub remove_aro_id {
  354. my $self = shift;
  355. my $aro_id = shift;
  356. my $rldbh = ${$self->{dbh_ref}};
  357. my $query = $rldbh->prepare("call rlapi_aros_delete(?)");
  358. $query->execute($aro_id);
  359. $self->get_tree();
  360. return 1;
  361. }
  362. #####
  363. sub remove_aco_id {
  364. my $self = shift;
  365. my $aco_id = shift;
  366. my $rldbh = ${$self->{dbh_ref}};
  367. my $query = $rldbh->prepare("call rlapi_acos_delete(?)");
  368. $query->execute($aco_id);
  369. $self->get_tree();
  370. return 1;
  371. }
  372. #####
  373. sub remove_aro_aco_id {
  374. my $self = shift;
  375. my $aro_id = shift,
  376. my $aco_id = shift;
  377. my $rldbh = ${$self->{dbh_ref}};
  378. my $query = $rldbh->prepare("delete from rlapi_aros_acos where aros_id = ? and acos_id = ?");
  379. $query->execute($aro_id, $aco_id);
  380. $self->get_tree();
  381. return 1;
  382. }
  383. #####
  384. sub remove_aro {
  385. my $self = shift;
  386. my $aro = shift;
  387. $aro =~ s/\///;
  388. my $rldbh = ${$self->{dbh_ref}};
  389. my $acl_href = $self->{acl_href};
  390. my $aros_href = $acl_href->{aros_href};
  391. return undef if !$aros_href->{$aro};
  392. my $query = $rldbh->prepare("call rlapi_aros_delete(?)");
  393. $query->execute($aros_href->{$aro});
  394. $self->get_tree();
  395. return 1;
  396. }
  397. #####
  398. sub remove_aco {
  399. my $self = shift;
  400. my $aco = shift;
  401. $aco =~ s/\///;
  402. my $rldbh = ${$self->{dbh_ref}};
  403. my $acl_href = $self->{acl_href};
  404. my $acos_href = $acl_href->{acos_href};
  405. return undef if !$acos_href->{$aco};
  406. my $query = $rldbh->prepare("call rlapi_acos_delete(?)");
  407. $query->execute($acos_href->{$aco});
  408. $self->get_tree();
  409. return 1;
  410. }
  411. #####
  412. sub remove_aros_acos {
  413. my $self = shift;
  414. my $aro = shift;
  415. my $aco = shift;
  416. $aro =~ s/^\///;
  417. $aco =~ s/^\///;
  418. my $rldbh = ${$self->{dbh_ref}};
  419. my $acl_href = $self->{acl_href};
  420. my $aros_href = $acl_href->{aros_href};
  421. my $acos_href = $acl_href->{acos_href};
  422. my $aros_acos_href = $acl_href->{aros_acos_href};
  423. my $aro_id = $aros_href->{$aro};
  424. my $aco_id = $acos_href->{$aco};
  425. return undef if !$aro_id or !$aco_id;
  426. my $query = $rldbh->prepare("delete from rlapi_aros_acos where aros_id = ? and acos_id = ?");
  427. $query->execute($aro_id, $aco_id);
  428. return 1;
  429. }
  430. return 1;