feat: add get_childrole user

This commit is contained in:
jhonliu 2022-05-09 15:18:41 +08:00
parent 13d4148880
commit b829eedfed
6 changed files with 46 additions and 18 deletions

View File

@ -51,7 +51,7 @@ Dlink协议是针对物联网开发领域设计的一种数据交换规范
## payload设计
### 属性上报 ($dg/user/{deviceId}/properties/report)
### 属性上报 ($dg/thing/{productId}/{deviceAddr}/properties/report)
+ dlink json格式
```json
{

View File

@ -25,6 +25,7 @@
properties_report(ProductId, DevAddr, Payload) when is_map(Payload) ->
io:format("~s ~p ProductId ~p, DevAddr ~p, Payload: ~p ~n", [?FILE, ?LINE, ProductId, DevAddr, Payload]),
dgiot_task:save_td(ProductId, DevAddr, Payload, #{});
properties_report(ProductId, DevAddr, Payload) ->
@ -34,7 +35,7 @@ properties_report(ProductId, DevAddr, Payload) ->
(_) ->
pass
end, dgiot_bridge:get_proctol_channel(ProductId)),
%% io:format("~s ~p ProductId ~p, DevAddr ~p, Payload: ~p ~n", [?FILE, ?LINE, ProductId, DevAddr, Payload]),
io:format("~s ~p ProductId ~p, DevAddr ~p, Payload: ~p ~n", [?FILE, ?LINE, ProductId, DevAddr, Payload]),
ok.
login(_A, _B, _C) ->

View File

@ -549,6 +549,17 @@
"type": "number",
"default": 1,
"description": "limit, 显示多少条记录显示100条记录 eg:100"
},
{
"name": "include",
"in": "query",
"type": "boolean",
"default": false,
"enum": [
false,
true
],
"description": "include 是否包含子部门用户"
}
],
"responses": {

View File

@ -33,8 +33,9 @@
put_User_Role/3,
put_roleuser/2,
post_roleuser/2,
get_roleuser/2,
del_roleuser/2
get_roleuser/3,
del_roleuser/2,
get_UserIds/1
]).
-export([create_user/2, delete_user/2, put_user/2, disableusere/3, check_roles/1]).
-export([login_by_account/2, login_by_token/2, login_by_mail_phone/1, do_login/1]).
@ -81,6 +82,14 @@ role_ets(RoleId) ->
_ -> pass
end.
get_UserIds(RoleId) ->
case dgiot_data:get(?ROLE_USER_ETS, RoleId) of
not_find ->
[];
UserIds ->
UserIds
end.
save_RoleIds(UserId, RoleId) ->
case dgiot_data:get(?USER_ROLE_ETS, UserId) of
not_find ->
@ -261,25 +270,29 @@ refresh_session(Token) ->
}).
get_roleuser(Filter, SessionToken) ->
get_roleuser(Filter, IncludeChild, SessionToken) ->
case dgiot_parse:query_object(<<"_Role">>, Filter,
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
{ok, #{<<"results">> := Roles}} ->
Users =
lists:foldl(fun(#{<<"objectId">> := RoleId} = Role, Acc) ->
ChildRoleIds =
case IncludeChild of
true ->
dgiot_role:get_childrole(RoleId);
false ->
[RoleId]
end,
UserIds =
lists:foldl(fun(ChildRoleId, Acc2) ->
Acc2 ++ dgiot_parse_auth:get_UserIds(ChildRoleId)
end, [], ChildRoleIds),
UsersQuery =
#{<<"where">> => #{<<"$relatedTo">> => #{
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => RoleId
},
<<"key">> => <<"users">>}
}
#{<<"where">> => #{<<"objectId">> => #{<<"$in">> => UserIds}},
<<"keys">> => []
},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results}} ->
Acc ++ lists:foldl(fun(X, Acc2) ->
Acc2 ++ [X#{<<"role">> => maps:with([<<"org_type">>, <<"tag">>, <<"depname">>], Role)}]
end, [], Results);

View File

@ -65,7 +65,8 @@ get_childrole(Role) ->
{error, not_find} ->
[Role];
Values ->
childrole(Values, dgiot_utils:unique_1(Values ++ [Role]))
FlatValues = dgiot_utils:unique_1(lists:flatten(Values)),
dgiot_utils:unique_1(lists:flatten(childrole(FlatValues, FlatValues ++ [Role])))
end.
childrole([], Acc) ->
@ -75,7 +76,8 @@ childrole([Role | Roles], Acc) ->
{error, not_find} ->
childrole(Roles, Acc);
Values ->
childrole(dgiot_utils:unique_1(Roles ++ Values), Acc ++ Values)
FlatValues = dgiot_utils:unique_1(lists:flatten(Values)),
childrole(dgiot_utils:unique_1(Roles ++ FlatValues), Acc ++ FlatValues)
end.
load_roles() ->

View File

@ -204,8 +204,9 @@ do_request(post_logout, #{<<"sessionToken">> := SessionToken}, _Context, _Req)
%% RoleUser : :json文件导库
%% OperationId:get_roleuser
%% :GET /iotapi/roleuser
do_request(get_roleuser, #{<<"where">> := Where} = Filter, #{<<"sessionToken">> := SessionToken} = _Context, _Req0) ->
dgiot_parse_auth:get_roleuser(Filter#{<<"where">> => jsx:decode(Where, [return_maps])}, SessionToken);
do_request(get_roleuser, #{<<"where">> := Where} = Filter, #{<<"sessionToken">> := SessionToken} = Context, _Req0) ->
IncludeChild = maps:get(<<"include">>, Context, false),
dgiot_parse_auth:get_roleuser(Filter#{<<"where">> => jsx:decode(Where, [return_maps])}, IncludeChild, SessionToken);
%% Role模版 : :json文件导库
%% OperationId:put_roleuser