Last active
August 29, 2015 14:13
-
-
Save dbcooper/3a33066bd3d564b9dd6f to your computer and use it in GitHub Desktop.
My solution to exercise 1-3 of http://marpa-guide.github.io/chapter3.html
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/perl | |
| # Part of my solution to exercise parts 1, 2, and 3 of http://marpa-guide.github.io/chapter3.html | |
| use strict; | |
| use warnings; | |
| use Authorization; | |
| use Data::Dumper; | |
| # Allow/deny source data | |
| my $input = <<'EXERCISE'; | |
| admins = admin root dbcooper | |
| Deny baduser cracker | |
| Allow @admins | |
| Allow jessica murphy | |
| Deny everybody | |
| EXERCISE | |
| my $auth = Authorization->new( source => $input ); | |
| # Test access permission for multiple users, die if unexpected results | |
| my %expected = ( | |
| admin => 1, | |
| root => 1, | |
| dbcooper => 1, | |
| baduser => 0, | |
| cracker => 0, | |
| jessica => 1, | |
| murphy => 1, | |
| all => 0, | |
| everybody => 0, | |
| guest => 0, | |
| ); | |
| for my $u (sort keys %expected) { | |
| my $authorized = $auth->CanAccess($u); | |
| if ($expected{$u} xor $authorized) { | |
| warn "?unexpected result for user $u, expected $expected{$u}, got $authorized\n"; | |
| } | |
| else { | |
| my $verb = ($authorized) ? 'allowed' : 'denied'; | |
| printf "%-10s $verb\n", $u; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment