Skip to content

Instantly share code, notes, and snippets.

@andreypopp
Created August 30, 2010 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andreypopp/557334 to your computer and use it in GitHub Desktop.
Save andreypopp/557334 to your computer and use it in GitHub Desktop.
%%% @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.
-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