diff --git a/apps/dgiot_device/src/dgiot_device_channel.erl b/apps/dgiot_device/src/dgiot_device_channel.erl index 13fdce51..36c2c1d2 100644 --- a/apps/dgiot_device/src/dgiot_device_channel.erl +++ b/apps/dgiot_device/src/dgiot_device_channel.erl @@ -139,7 +139,7 @@ handle_message(check, #state{env = #{<<"offline">> := OffLine, <<"checktime">> : handle_message({sync_parse, Pid, 'after', get, _Token, <<"Device">>, #{<<"results">> := Results} = ResBody}, State) -> -%% io:format("~s ~p ~p ~p ~n", [?FILE, ?LINE, Pid,Header]), +%% io:format("~s ~p ~p ~p ~n", [?FILE, ?LINE, Pid,Results]), NewResults = lists:foldl(fun(#{<<"objectId">> := DeviceId} = Device, Acc) -> case dgiot_device:lookup(DeviceId) of {ok, #{<<"status">> := Status, <<"isEnable">> := IsEnable, <<"longitude">> := Longitude, <<"latitude">> := Latitude, <<"time">> := Time}} -> diff --git a/apps/dgiot_device/src/utils/dgiot_device_cache.erl b/apps/dgiot_device/src/utils/dgiot_device_cache.erl index cd508e4b..24827a38 100644 --- a/apps/dgiot_device/src/utils/dgiot_device_cache.erl +++ b/apps/dgiot_device/src/utils/dgiot_device_cache.erl @@ -101,29 +101,30 @@ post(Device) -> IsEnable = maps:get(<<"isEnable">>, Device, false), insert_mnesia(DeviceId, dgiot_role:get_acls(Device), Status, dgiot_datetime:now_secs(), IsEnable, ProductId, Devaddr, DeviceSecret, node(), Longitude, Latitude). +post(#{<<"ACL">> := _Acl} = Device, _SessionToken) -> + dgiot_device_cache:post(Device); + post(Device, SessionToken) -> Devaddr = maps:get(<<"devaddr">>, Device), Product = maps:get(<<"product">>, Device), ProductId = maps:get(<<"objectId">>, Product), DeviceId = maps:get(<<"objectId">>, Device, dgiot_parse_id:get_deviceid(ProductId, Devaddr)), - ACL = maps:get(<<"ACL">>, Product, #{}), - DefaultAcl = #{<<"*">> => #{<<"read">> => true}, <<"role:admin">> => #{<<"read">> => true, <<"write">> => true}}, - SetAcl = case dgiot_auth:get_session(dgiot_utils:to_binary(SessionToken)) of - #{<<"roles">> := Roles} = _User -> - [#{<<"name">> := Role} | _] = maps:values(Roles), - GetAcl = ACL#{ - <<"role:", Role/binary>> => #{ - <<"read">> => true, - <<"write">> => true - } - }, - GetAcl; - Err -> - io:format("~s ~p R = ~p.~n", [?FILE, ?LINE, Err]), - DefaultAcl - end, + SetAcl = + case dgiot_auth:get_session(dgiot_utils:to_binary(SessionToken)) of + #{<<"roles">> := Roles} = _User -> + [#{<<"name">> := Role} | _] = maps:values(Roles), + #{ + <<"role:", Role/binary>> => #{ + <<"read">> => true, + <<"write">> => true + } + }; + Err -> + ?LOG(error, "~s ~p DeviceId ~p Err = ~p.~n", [?FILE, ?LINE, DeviceId, Err]), + #{<<"role:admin">> => #{<<"read">> => true, <<"write">> => true}} + end, dgiot_parse:update_object(<<"Device">>, DeviceId, #{<<"ACL">> => SetAcl}), - dgiot_device_cache:post(Device). + dgiot_device_cache:post(Device#{<<"ACL">> => SetAcl}). put(Device) -> DeviceId = maps:get(<<"objectId">>, Device),