This commit is contained in:
Ery Lee 2015-04-20 04:29:56 +08:00
parent 02614e8ae4
commit 50e033c71d
1 changed files with 6 additions and 6 deletions

View File

@ -37,7 +37,7 @@
%% ACL callbacks %% ACL callbacks
-export([init/1, check_acl/2, reload_acl/1, description/0]). -export([init/1, check_acl/2, reload_acl/1, description/0]).
-define(ACL_RULE_TABLE, mqtt_acl_rule). -define(ACL_RULE_TAB, mqtt_acl_rule).
-record(state, {acl_file, nomatch = allow}). -record(state, {acl_file, nomatch = allow}).
@ -48,7 +48,7 @@
%% @doc Read all rules. %% @doc Read all rules.
-spec all_rules() -> list(emqttd_access_rule:rule()). -spec all_rules() -> list(emqttd_access_rule:rule()).
all_rules() -> all_rules() ->
case ets:lookup(?ACL_RULE_TABLE, all_rules) of case ets:lookup(?ACL_RULE_TAB, all_rules) of
[] -> []; [] -> [];
[{_, Rules}] -> Rules [{_, Rules}] -> Rules
end. end.
@ -60,7 +60,7 @@ all_rules() ->
%% @doc init internal ACL. %% @doc init internal ACL.
-spec init(AclOpts :: list()) -> {ok, State :: any()}. -spec init(AclOpts :: list()) -> {ok, State :: any()}.
init(AclOpts) -> init(AclOpts) ->
ets:new(?ACL_RULE_TABLE, [set, public, named_table, {read_concurrency, true}]), ets:new(?ACL_RULE_TAB, [set, public, named_table, {read_concurrency, true}]),
AclFile = proplists:get_value(file, AclOpts), AclFile = proplists:get_value(file, AclOpts),
Default = proplists:get_value(nomatch, AclOpts, allow), Default = proplists:get_value(nomatch, AclOpts, allow),
State = #state{acl_file = AclFile, nomatch = Default}, State = #state{acl_file = AclFile, nomatch = Default},
@ -71,10 +71,10 @@ load_rules(#state{acl_file = AclFile}) ->
{ok, Terms} = file:consult(AclFile), {ok, Terms} = file:consult(AclFile),
Rules = [emqttd_access_rule:compile(Term) || Term <- Terms], Rules = [emqttd_access_rule:compile(Term) || Term <- Terms],
lists:foreach(fun(PubSub) -> lists:foreach(fun(PubSub) ->
ets:insert(?ACL_RULE_TABLE, {PubSub, ets:insert(?ACL_RULE_TAB, {PubSub,
lists:filter(fun(Rule) -> filter(PubSub, Rule) end, Rules)}) lists:filter(fun(Rule) -> filter(PubSub, Rule) end, Rules)})
end, [publish, subscribe]), end, [publish, subscribe]),
ets:insert(?ACL_RULE_TABLE, {all_rules, Terms}). ets:insert(?ACL_RULE_TAB, {all_rules, Terms}).
filter(_PubSub, {allow, all}) -> filter(_PubSub, {allow, all}) ->
true; true;
@ -103,7 +103,7 @@ check_acl({Client, PubSub, Topic}, #state{nomatch = Default}) ->
end. end.
lookup(PubSub) -> lookup(PubSub) ->
case ets:lookup(?ACL_RULE_TABLE, PubSub) of case ets:lookup(?ACL_RULE_TAB, PubSub) of
[] -> []; [] -> [];
[{PubSub, Rules}] -> Rules [{PubSub, Rules}] -> Rules
end. end.