Merge pull request #1746 from emqtt/emqx30-feng

Use map to replace #state{} record
This commit is contained in:
Feng Lee 2018-08-27 10:35:43 +08:00 committed by GitHub
commit 6d2b31911a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 23 deletions

View File

@ -20,13 +20,11 @@
-export([all_rules/0]). -export([all_rules/0]).
%% ACL callbacks %% ACL mod 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_TAB, emqx_acl_rule). -define(ACL_RULE_TAB, emqx_acl_rule).
-record(state, {acl_file}).
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% API %% API
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
@ -43,21 +41,20 @@ all_rules() ->
%% ACL callbacks %% ACL callbacks
%%------------------------------------------------------------------------------ %%------------------------------------------------------------------------------
%% @doc Init internal ACL -spec(init([File :: string()]) -> {ok, #{}}).
-spec(init([File :: string()]) -> {ok, State :: term()}).
init([File]) -> init([File]) ->
_ = emqx_tables:new(?ACL_RULE_TAB, [set, public, {read_concurrency, true}]), _ = emqx_tables:new(?ACL_RULE_TAB, [set, public, {read_concurrency, true}]),
{ok, load_rules_from_file(#state{acl_file = File})}. true = load_rules_from_file(File),
{ok, #{acl_file => File}}.
load_rules_from_file(State = #state{acl_file = AclFile}) -> load_rules_from_file(AclFile) ->
{ok, Terms} = file:consult(AclFile), {ok, Terms} = file:consult(AclFile),
Rules = [emqx_access_rule:compile(Term) || Term <- Terms], Rules = [emqx_access_rule:compile(Term) || Term <- Terms],
lists:foreach(fun(PubSub) -> lists:foreach(fun(PubSub) ->
ets:insert(?ACL_RULE_TAB, {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_TAB, {all_rules, Terms}), ets:insert(?ACL_RULE_TAB, {all_rules, Terms}).
State.
filter(_PubSub, {allow, all}) -> filter(_PubSub, {allow, all}) ->
true; true;
@ -73,11 +70,11 @@ filter(_PubSub, {_AllowDeny, _Who, _, _Topics}) ->
false. false.
%% @doc Check ACL %% @doc Check ACL
-spec(check_acl({credentials(), pubsub(), topic()}, #state{}) -spec(check_acl({credentials(), pubsub(), topic()}, #{})
-> allow | deny | ignore). -> allow | deny | ignore).
check_acl(_Who, #state{acl_file = undefined}) -> check_acl(_Who, #{acl_file := undefined}) ->
allow; allow;
check_acl({Credentials, PubSub, Topic}, #state{}) -> check_acl({Credentials, PubSub, Topic}, #{}) ->
case match(Credentials, Topic, lookup(PubSub)) of case match(Credentials, Topic, lookup(PubSub)) of
{matched, allow} -> allow; {matched, allow} -> allow;
{matched, deny} -> deny; {matched, deny} -> deny;
@ -94,22 +91,24 @@ match(_Credentials, _Topic, []) ->
nomatch; nomatch;
match(Credentials, Topic, [Rule|Rules]) -> match(Credentials, Topic, [Rule|Rules]) ->
case emqx_access_rule:match(Credentials, Topic, Rule) of case emqx_access_rule:match(Credentials, Topic, Rule) of
nomatch -> match(Credentials, Topic, Rules); nomatch ->
{matched, AllowDeny} -> {matched, AllowDeny} match(Credentials, Topic, Rules);
{matched, AllowDeny} ->
{matched, AllowDeny}
end. end.
-spec(reload_acl(#state{}) -> ok | {error, term()}). -spec(reload_acl(#{}) -> ok | {error, term()}).
reload_acl(#state{acl_file = undefined}) -> reload_acl(#{acl_file := undefined}) ->
ok; ok;
reload_acl(State) -> reload_acl(#{acl_file := AclFile}) ->
case catch load_rules_from_file(State) of case catch load_rules_from_file(AclFile) of
true -> emqx_logger:error("Reload acl_file ~s successfully", [AclFile]),
{'EXIT', Error} -> {error, Error}; ok;
#state{config=File} -> {'EXIT', Error} ->
io:format("reload acl_internal successfully: ~p~n", [File]), {error, Error}
ok
end. end.
-spec(description() -> string()). -spec(description() -> string()).
description() -> description() ->
"Internal ACL with etc/acl.conf". "Internal ACL with etc/acl.conf".

View File

@ -18,6 +18,7 @@
-export_type([zone/0, client_id/0, username/0, password/0, peername/0, -export_type([zone/0, client_id/0, username/0, password/0, peername/0,
protocol/0, credentials/0]). protocol/0, credentials/0]).
-export_type([payload/0]).
%%-export_type([payload/0, message/0, delivery/0]). %%-export_type([payload/0, message/0, delivery/0]).
-type(zone() :: atom()). -type(zone() :: atom()).