Created
August 30, 2010 12:15
-
-
Save andreypopp/557334 to your computer and use it in GitHub Desktop.
This file contains 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
%%% @doc Basic utilities for working with ACLs over integer-based principals. | |
%%% | |
%%% @type acl() = [acl_entry()] | |
%%% @type acl_entry() = | |
%%% {allow, principal_qualifier()} | |
%%% | {deny, principal_qualifier()} | |
%%% @type principal_qualifier() = | |
%%% int() | |
%%% | any | |
%%% | {range, int(), int()} | |
-module(dummyacl). | |
-export([is_allowed/2]). | |
%% @doc Check principal against given acl. | |
%% If provided ACL is empty -- this function will return false. | |
%% @spec is_allowed(Principal, ACL) -> boolean() | |
%% Principal = int() | |
%% ACL = acl() | |
is_allowed(_Principal, [{Granted, any} | _]) -> Granted == allow; | |
is_allowed(Principal, [{Granted, Principal} | _]) -> Granted == allow; | |
is_allowed(Principal, [{Granted, {range, Min, Max}} | _]) | |
when Principal >= Min andalso Principal =< Max -> Granted == allow; | |
is_allowed(Principal, [_ | ACL]) -> is_allowed(Principal, ACL); | |
is_allowed(_Principal, []) -> false. |
This file contains 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
-module(dummyacl_tests). | |
-include_lib("eunit/include/eunit.hrl"). | |
is_allowed_empty_acl_test_() -> [ | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(1, [])) | |
]. | |
is_allowed_deny_any_test_() -> [ | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(1, [{deny, any}])) | |
]. | |
is_allowed_allow_any_test_() -> [ | |
?_assertEqual( | |
true, | |
dummyacl:is_allowed(1, [{allow, any}])) | |
]. | |
is_allowed_allow_one_test_() -> | |
ACL = [{allow, 1}, {deny, any}], | |
[ | |
?_assertEqual( | |
true, | |
dummyacl:is_allowed(1, ACL)), | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(2, ACL)) | |
]. | |
is_allowed_deny_one_test_() -> | |
ACL = [{deny, 2}, {allow, any}], | |
[ | |
?_assertEqual( | |
true, | |
dummyacl:is_allowed(1, ACL)), | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(2, ACL)) | |
]. | |
is_allowed_allow_range_test_() -> | |
ACL = [{allow, {range, 1, 10}}, {deny, any}], | |
[ | |
?_assertEqual( | |
true, | |
dummyacl:is_allowed(1, ACL)), | |
?_assertEqual( | |
true, | |
dummyacl:is_allowed(5, ACL)), | |
?_assertEqual( | |
true, | |
dummyacl:is_allowed(10, ACL)), | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(20, ACL)), | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(-20, ACL)) | |
]. | |
is_allowed_deny_range_test_() -> | |
ACL = [{deny, {range, 1, 10}}, {allow, any}], | |
[ | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(1, ACL)), | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(5, ACL)), | |
?_assertEqual( | |
false, | |
dummyacl:is_allowed(10, ACL)), | |
?_assertEqual( | |
true, | |
dummyacl:is_allowed(20, ACL)), | |
?_assertEqual( | |
true, | |
dummyacl:is_allowed(-20, ACL)) | |
]. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment