fix(ldap): integrate parser and fix lexer errors

This commit is contained in:
firest 2023-08-01 11:40:37 +08:00
parent fa6343cc80
commit 8c9b136d15
4 changed files with 33 additions and 8 deletions

2
apps/emqx_ldap/.gitignore vendored Normal file
View File

@ -0,0 +1,2 @@
src/emqx_ldap_filter_lexer.erl
src/emqx_ldap_filter_parser.erl

View File

@ -167,14 +167,22 @@ on_query(
[] ->
do_ldap_query(InstId, [{base, Base} | SearchOptions], State);
_ ->
FilterBin = emqx_placeholder:proc_tmpl(FilterTks, Data),
%% TODO
Filter = FilterBin,
do_ldap_query(
InstId,
[{base, Base}, {filter, Filter} | SearchOptions],
State
)
FilterBin = emqx_placeholder:proc_tmpl(FilterTks, Data, #{return => rawlist}),
case emqx_ldap_filter_parser:scan_and_parse(FilterBin) of
{ok, Filter} ->
do_ldap_query(
InstId,
[{base, Base}, {filter, Filter} | SearchOptions],
State
);
{error, Reason} = Error ->
?SLOG(error, #{
msg => "filter_parse_failed",
filter => FilterBin,
reason => Reason
}),
Error
end
end.
do_ldap_query(

View File

@ -1,5 +1,6 @@
Definitions.
Control = [()&|!=~><:*]
NonControl = [^()&|!=~><:*]
String = {NonControl}*
White = [\s\t\n\r]+
@ -20,5 +21,7 @@ Rules.
dn : {token, {dn, TokenLine}}.
{White} : skip_token.
{String} : {token, {string, TokenLine, TokenChars}}.
%% Leex will hang if a composite operation is missing a character
{Control} : {error, lists:flatten(io_lib:format("Unexpected Tokens:~ts", [TokenChars]))}.
Erlang code.

View File

@ -94,6 +94,8 @@ matchingrule ->
colon value: {matchingRule, '$2'}.
Erlang code.
-export([scan_and_parse/1]).
-ignore_xref({return_error, 2}).
'and'(Value) ->
eldap:'and'(Value).
@ -131,3 +133,13 @@ flatten(List) -> lists:flatten(List).
get_value({_Token, _Line, Value}) ->
Value.
scan_and_parse(Bin) when is_binary(Bin) ->
scan_and_parse(erlang:binary_to_list(Bin));
scan_and_parse(String) ->
case emqx_ldap_filter_lexer:string(String) of
{ok, Tokens, _} ->
parse(Tokens);
{error, Reason, _} ->
{error, Reason}
end.