Skip to content

Commit

Permalink
feat: parse_auth
Browse files Browse the repository at this point in the history
  • Loading branch information
dawnwinterLiu committed Apr 14, 2023
1 parent de71892 commit 0dbc2cc
Show file tree
Hide file tree
Showing 12 changed files with 193 additions and 113 deletions.
19 changes: 13 additions & 6 deletions apps/dgiot_device/src/utils/dgiot_device_cache.erl
Original file line number Diff line number Diff line change
Expand Up @@ -397,14 +397,21 @@ notification(DeviceId, Status, Longitude, Latitude, IsEnable, Now) ->
end,
Address =
case dgiot_data:get(?DGIOT_LOCATION_ADDRESS, DeviceId) of
not_find ->
get_address(DeviceId, Longitude, Latitude);
Addr ->
Addr
Addr when size(Addr) > 0 ->
Addr;
_ ->
get_address(DeviceId, Longitude, Latitude)
end,
PubData =
case size(Address) of
0 ->
#{};
_ ->
#{<<"address">> => Address}
end,
dgiot_mqtt:publish(DeviceId, Topic, jsx:encode(#{
dgiot_mqtt:publish(DeviceId, Topic, jsx:encode(PubData#{
DeviceId => #{
<<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Now, <<"address">> => Address,
<<"status">> => NewStatus, <<"isEnable">> => IsEnable, <<"lastOnlineTime">> => Now,
<<"location">> => #{<<"longitude">> => Longitude, <<"latitude">> => Latitude}
}}));
_ ->
Expand Down
25 changes: 25 additions & 0 deletions apps/dgiot_http/priv/cert/apiclient_key.pem
Original file line number Diff line number Diff line change
@@ -1,3 +1,28 @@
-----BEGIN PRIVATE KEY-----
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
MIIEvAIBADANBgkqhkiG9w0BAQEFAASCBKYwggSiAgEAAoIBAQCgjpVEZJTdmbzm
rid/yxITsCE4NB3NNxX7p1ob2Sd7s+O5cni7kl5scmkfTtHAb4hNEnHKZrrrVnQT
x8GSGyotuQa/O13Ek6f9uk1tGPdSPi2c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
G9x4hRJwG6VLRq+aPuBpz+ZZAxV9ztQy/1akB0p+Lyja/gGX0HaEncAv6+P3uDaM
yDwCKtqLRfnWujwaREd+fSmnskGELn83GEfUDYECgYBzN07EV6DAF51luwfo/83w
0/bPO2L32ezmS9f6yLkRdMeFJOGkgy0dRewpk9M5kQTkFpdz/RlkQ1tPJxf2IApC
AADFM+GKYm3EwuBoXFIwNQq25852Pi7cF6Z/dTP2nMvomAat35P4OPf6UT5Rq2xP
91h+FCct7r8wCvGvEoy6Gw==
-----END PRIVATE KEY-----
8 changes: 7 additions & 1 deletion apps/dgiot_http/src/baidu/dgiot_gps.erl
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,13 @@ get_baidu_gps(LonDeg, LatDeg, Lonoffset, Latoffset) ->
%%http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-geocoding-abroad
%<<"http://api.map.baidu.com/reverse_geocoding/v3/?ak=0twrgSghSF1Q6zUvvYdUu2KFhFiGdbm5&output=json&coordtype=wgs84ll&location=25.368078,111.064845">>.
get_baidu_addr(LonDeg, LatDeg) ->
AppKey = dgiot_utils:to_binary(application:get_env(dgiot_http, baidumap_appkey, <<"">>)),
AppKey =
case dgiot_data:get(dgiot_configuration, baidu_sak) of
not_find ->
dgiot_utils:to_binary(application:get_env(dgiot_http, baidumap_appkey, <<"">>));
Ak ->
Ak
end,
get_baidu_addr(AppKey, "wgs84ll", LonDeg, LatDeg).

get_baidu_addr(AK, Coordtype, Lng, Lat) ->
Expand Down
29 changes: 9 additions & 20 deletions apps/dgiot_http/src/umeng/dgiot_notification.erl
Original file line number Diff line number Diff line change
Expand Up @@ -35,9 +35,6 @@ init_ets() ->
dgiot_data:init(?CONFIGURATION),
dgiot_data:init(?NOTIFICATION).




% 验证类消息
send_verification_code(NationCode, Key) ->
case catch dgiot_cache:get(<<Key/binary, "wait">>) of
Expand Down Expand Up @@ -134,7 +131,7 @@ send_sms(NationCode, Mobile, TplId, Params, AppId, AppKey, Sign, Ext) ->
case jsx:decode(ResBody, [{labels, binary}, return_maps]) of
#{<<"result">> := 0, <<"errmsg">> := <<"OK">>} = Result ->
{ok, Result#{<<"code">> => 200}};
#{<<"errmsg">> := ErrMsg, <<"result">> := Code} = ErrResult->
#{<<"errmsg">> := ErrMsg, <<"result">> := Code} = ErrResult ->
{ok, ErrResult#{<<"code">> => Code, <<"error">> => ErrMsg}}
end;
{Err, Reason} when Err == error; Err == 'EXIT' ->
Expand Down Expand Up @@ -403,22 +400,14 @@ save_configuration() ->
DictId = dgiot_parse_id:get_dictid(<<"dgiotconfiguration">>, <<"configuration">>, <<"configuration">>, <<"dgiotconfiguration">>),
case dgiot_parse:get_object(<<"Dict">>, DictId) of
{ok, #{<<"data">> := Data}} ->
Sms = maps:get(<<"sms">>, Data, #{}),
Sms_appid = maps:get(<<"appid">>, Sms, <<"">>),
Sms_appkey = maps:get(<<"appkey">>, Sms, <<"">>),
Sms_sign = maps:get(<<"sign">>, Sms, <<"">>),
Verify_Code_Tplid = maps:get(<<"verify_code_tplid">>, Sms, <<"1715928">>),
dgiot_data:insert(?CONFIGURATION, sms_appid, Sms_appid),
dgiot_data:insert(?CONFIGURATION, sms_appkey, Sms_appkey),
dgiot_data:insert(?CONFIGURATION, sms_sign, Sms_sign),
dgiot_data:insert(?CONFIGURATION, sms_verify_code_tplid, Verify_Code_Tplid),
Mail = maps:get(<<"mail">>, Data, #{}),
Mail_username = maps:get(<<"username">>, Mail, <<"">>),
Mail_password = maps:get(<<"password">>, Mail, <<"">>),
Mail_smtp = maps:get(<<"smtp">>, Mail, <<"">>),
dgiot_data:insert(?CONFIGURATION, mail_username, Mail_username),
dgiot_data:insert(?CONFIGURATION, mail_password, Mail_password),
dgiot_data:insert(?CONFIGURATION, mail_smtp, Mail_smtp);
maps:fold(fun
(Key, Value, _) when is_map(Value) ->
maps:fold(fun(Key1, Value1, _) ->
dgiot_data:insert(?CONFIGURATION, dgiot_utils:to_atom(<<Key/binary, "_", Key1/binary>>), Value1)
end, #{}, Value);
(_, _, _) ->
pass
end, #{}, Data);
_ ->
pass
end.
107 changes: 72 additions & 35 deletions apps/dgiot_parse/src/dgiot_parse_auth.erl
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
get_children/1,
getuser/1
]).
-export([create_user/2, delete_user/2, put_user/2, disableusere/3, check_roles/1]).
-export([create_user/2, delete_user/2, put_user/2, get_user/2, disableusere/3, check_roles/1]).
-export([login_by_account/2, login_by_token/2, login_by_mail_phone/1, do_login/1]).
-export([create_user_for_app/1, get_token/1, set_cookies/3, add_acl/5]).
-export([get_usersession/1, put_usersession/2, del_usersession/1]).
Expand Down Expand Up @@ -343,7 +343,7 @@ refresh_session(Token) ->


get_roleuser(Filter, SessionToken) ->
_IncludeChild = maps:get(<<"include">>, Filter, false),
IncludeChild = maps:get(<<"include">>, Filter, false),
%% io:format("~s ~p Filter ~p IncludeChild ~p ~n", [?FILE, ?LINE, Filter, IncludeChild]),
case dgiot_parse:query_object(<<"_Role">>, maps:without([<<"include">>, <<"userfilter">>], Filter),
[{"X-Parse-Session-Token", SessionToken}], [{from, rest}]) of
Expand All @@ -359,13 +359,25 @@ get_roleuser(Filter, SessionToken) ->
_ ->
maps:without([<<"phone">>], UserWhere)
end,
UsersQuery =
UserFilter#{<<"where">> => NewUserWhere#{<<"$relatedTo">> => #{
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => RoleId}, <<"key">> => <<"users">>},
<<"tag">> => #{<<"$exists">> => true}}},
ChildRoleIds =
case IncludeChild of
true ->
dgiot_role:get_childrole(RoleId);
false ->
[RoleId]
end,
UsersQuery = UserFilter#{
<<"where">> => NewUserWhere#{
<<"tag">> => #{<<"$exists">> => true},
<<"$relatedTo">> => #{
<<"key">> => <<"users">>,
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => #{<<"$in">> => ChildRoleIds}
}
}
}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results} = UserResults} ->
NewResults =
Expand Down Expand Up @@ -863,29 +875,54 @@ create_user(#{<<"username">> := UserName, <<"department">> := RoleId} = Body, Se
%%[{"X-Parse-Session-Token", Session}], [{from, rest}]
delete_user(#{<<"username">> := UserName, <<"department">> := RoleId}, _SessionToken) ->
ChildRoleIds = dgiot_role:get_childrole(RoleId),
UserIds =
lists:foldl(fun(ChildRoleId, Acc) ->
Acc ++ dgiot_parse_auth:get_UserIds(ChildRoleId)
end, [], ChildRoleIds),
UsersQuery = #{<<"where">> => #{<<"objectId">> => #{<<"$in">> => UserIds}}},
UsersQuery = #{
<<"where">> => #{
<<"username">> => UserName,
<<"$relatedTo">> => #{
<<"key">> => <<"users">>,
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => #{<<"$in">> => ChildRoleIds}}
}}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results}} ->
R =
lists:map(fun(#{<<"username">> := Name, <<"objectId">> := ObjectId}) ->
case Name of
UserName ->
DelR = dgiot_parse:del_object(<<"_User">>, ObjectId),
dgiot_parse_auth:del_User_Role(ObjectId, RoleId),
#{<<"msg">> => DelR};
_ ->
#{<<"msg">> => <<"error">>}
end
end, Results),
{ok, #{<<"result">> => R}};
{ok, #{<<"results">> := [#{<<"username">> := UserName, <<"objectId">> := ObjectId} | _]}} ->
DelR = dgiot_parse:del_object(<<"_User">>, ObjectId),
dgiot_parse_auth:del_User_Role(ObjectId, RoleId),
{ok, #{<<"msg">> => DelR}};
_ ->
{error, <<"token fail">>}
{error, #{<<"msg">> => <<"token fail">>}}
end.

%% 查询企业内部用户
%%[{"X-Parse-Session-Token", Session}], [{from, rest}]
get_user(#{<<"id">> := UserId}, SessionToken) ->
case dgiot_auth:get_session(dgiot_utils:to_binary(SessionToken)) of
#{<<"roles">> := Roles} ->
R =
maps:fold(fun(RoleId, _, _) ->
ChildRoleIds = dgiot_role:get_childrole(RoleId),
lists:foldl(fun(ChildRoleId, Acc) ->
UsersQuery = #{<<"where">> => #{
<<"objectId">> => UserId,
<<"$relatedTo">> => #{
<<"key">> => <<"users">>,
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => ChildRoleId}}}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results}} ->
Acc ++ Results;
_ ->
Acc
end
end, [], ChildRoleIds)
end, #{}, Roles),
{ok, #{<<"data">> => R, <<"msg">> => <<"success">>, <<"status">> => 0}};
_ ->
{ok, #{<<"msg">> => <<"token fail">>}}
end.

%% 修改企业内部用户
%%[{"X-Parse-Session-Token", Session}], [{from, rest}]
Expand All @@ -897,13 +934,13 @@ put_user(#{<<"username">> := UserName} = Body, SessionToken) ->
ChildRoleIds = dgiot_role:get_childrole(RoleId),
ChildUsers =
lists:foldl(fun(ChildRoleId, Acc) ->
%% Acc ++ dgiot_parse_auth:get_UserIds(ChildRoleId)
UsersQuery = #{<<"where">> => #{<<"$relatedTo">> =>
#{<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => ChildRoleId},
<<"key">> => <<"users">>}}},
UsersQuery = #{<<"where">> => #{
<<"$relatedTo">> => #{
<<"key">> => <<"users">>,
<<"object">> => #{
<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => ChildRoleId}}}},
case dgiot_parse:query_object(<<"_User">>, UsersQuery) of
{ok, #{<<"results">> := Results}} ->
Acc ++ Results;
Expand Down
21 changes: 11 additions & 10 deletions apps/dgiot_parse/src/dgiot_parse_rest.erl
Original file line number Diff line number Diff line change
Expand Up @@ -128,25 +128,26 @@ get_newwhere(Header, Where) ->
Map = dgiot_json:decode(Where),
case dgiot_auth:get_session(SessionToken) of
#{<<"roles">> := Roles} ->
ViewIds =
RoleIds =
maps:fold(fun(RoleId, Role, Acc) ->
case maps:find(<<"level">>, Role) of
{ok, Level} when Level < 3 ->
Acc ++ [true];
_ ->
case dgiot_role:get_role_views(RoleId) of
not_find ->
Acc;
Ids ->
Acc ++ Ids
end
Acc ++ [RoleId]
end
end, [], Roles),
case lists:member(true, ViewIds) of
case lists:member(true, RoleIds) of
true ->
Where;
_ ->
dgiot_json:encode(Map#{<<"objectId">> => #{<<"$in">> => ViewIds}})
dgiot_json:encode(Map#{<<"$relatedTo">> => #{
<<"object">> =>
#{<<"__type">> => <<"Pointer">>,
<<"className">> => <<"_Role">>,
<<"objectId">> => #{<<"$in">> => RoleIds}},
<<"key">> => <<"views">>
}})
end;
_ ->
Where
Expand Down Expand Up @@ -423,7 +424,7 @@ do_request_after(Method0, Path, Header, NewQueryData, ResBody, Options) ->
method(Method0, atom)
end,
{match, PathList} = re:run(Path, <<"([^/]+)">>, [global, {capture, all_but_first, binary}]),
%% io:format("~s ~p ~p ~p ~n",[?FILE, ?LINE, Path, NewQueryData]),
%% io:format("~s ~p ~p ~p ~n",[?FILE, ?LINE, Path, NewQueryData]),
dgiot_parse_hook:do_request_hook('after', lists:concat(PathList), Method, dgiot_parse:get_token(Header), NewQueryData, ResBody).


Expand Down
33 changes: 19 additions & 14 deletions apps/dgiot_parse/src/handler/dgiot_user_handler.erl
Original file line number Diff line number Diff line change
Expand Up @@ -144,19 +144,14 @@ do_request(post_user, #{<<"username">> := _UserName, <<"password">> := _Password
{error, Error} -> {500, Error}
end;

do_request(delete_user, #{<<"username">> := UserName} = Body, #{<<"sessionToken">> := SessionToken}, _Req) ->
DefaultUsers = ?DEFUser,
case lists:member(UserName, DefaultUsers) of
true ->
{ok, #{<<"code">> => 401, <<"msg">> => <<"dgiot_admin PROHIBITED DELETE">>}};
_ ->
case dgiot_parse_auth:delete_user(Body, SessionToken) of
{ok, _Data} ->
dgiot_role:load_user(),
dgiot_parse_auth:load_roleuser(),
{200, #{<<"result">> => true}};
{error, Error} -> {error, Error}
end
do_request(delete_user, Body, #{<<"sessionToken">> := SessionToken}, _Req) ->
case dgiot_parse_auth:delete_user(Body, SessionToken) of
{ok, Data} ->
dgiot_role:load_user(),
dgiot_parse_auth:load_roleuser(),
{200, Data};
{error, Error} ->
{error, Error}
end;

do_request(put_user, #{<<"username">> := UserName} = Body, #{<<"sessionToken">> := SessionToken}, _Req) ->
Expand All @@ -174,6 +169,15 @@ do_request(put_user, #{<<"username">> := UserName} = Body, #{<<"sessionToken">>
end
end;

do_request(get_users_id, Body, #{<<"sessionToken">> := SessionToken}, _Req) ->
case dgiot_parse_auth:get_user(Body, SessionToken) of
{ok, Data} ->
dgiot_role:load_user(),
dgiot_parse_auth:load_roleuser(),
{200, Data};
{error, Error} -> {500, Error}
end;

%% IoTDevice 概要: 禁用账号
%% OperationId: get_disableuser
%% Disuserid 被禁用账号
Expand Down Expand Up @@ -268,6 +272,7 @@ do_request(get_usertree, _Arg, #{<<"sessionToken">> := SessionToken} = _Context,

%% 服务器不支持的API接口
do_request(_OperationId, _Args, _Context, _Req) ->
?LOG(info, "_Args ~p", [_Args]),
%% io:format("~s ~p _OperationId = ~p.~n", [?FILE, ?LINE, _OperationId]),
%% io:format("~s ~p _Args = ~p.~n", [?FILE, ?LINE, _Args]),
{error, <<"Not Allowed.">>}.

Loading

0 comments on commit 0dbc2cc

Please sign in to comment.