From e68b409f244177366aa420d070a62d27d756c7d7 Mon Sep 17 00:00:00 2001 From: Shamis Shukoor Date: Mon, 5 Aug 2019 16:30:46 +0200 Subject: [PATCH] asynchronous calls over erlimem #276 --- src/imem_sec.erl | 10 ++++++++-- src/imem_seco.erl | 8 ++++++++ src/imem_server.erl | 3 +-- src/imem_statement.erl | 5 +++-- 4 files changed, 20 insertions(+), 6 deletions(-) diff --git a/src/imem_sec.erl b/src/imem_sec.erl index 17a7fc9..20ac3f7 100755 --- a/src/imem_sec.erl +++ b/src/imem_sec.erl @@ -923,8 +923,14 @@ seco_authorized(SKey) -> case imem_meta:read(ddSeCo@, SKey) of [#ddSeCo{pid=Pid, authState=authorized} = SeCo] when Pid == self() -> SeCo; - [#ddSeCo{pid=Pid}] -> - ?SecurityViolation({"Not logged in", {SKey,Pid}}); + [#ddSeCo{pid=Pid, authState=authorized} = SeCo] -> + {links, Links} = erlang:process_info(self(), links), + case lists:member(Pid, Links) of + true -> + SeCo; + false -> + ?SecurityViolation({"Not logged in", SKey}) + end; [] -> ?SecurityException({"Not logged in", SKey}) end. diff --git a/src/imem_seco.erl b/src/imem_seco.erl index 005f50e..6bdf3b0 100755 --- a/src/imem_seco.erl +++ b/src/imem_seco.erl @@ -59,6 +59,7 @@ , has_role/2 , has_permission/3 , has_permission/2 + , get_skey_pid/1 ]). -export([ have_role/2 @@ -296,6 +297,13 @@ if_has_child_permission([RootRoleId|OtherRoles], Permission) -> false -> if_has_child_permission(OtherRoles, Permission) end. +get_skey_pid(SKey) -> + case if_read(ddSeCo@, SKey) of + [#ddSeCo{pid=Pid}] -> + {ok, Pid}; + _ -> + not_found + end. %% --Implementation (exported helper functions) ---------------------------------------- diff --git a/src/imem_server.erl b/src/imem_server.erl index 56eeb2b..f010409 100755 --- a/src/imem_server.erl +++ b/src/imem_server.erl @@ -172,7 +172,6 @@ mfa({Ref, imem_sec = Mod, Fun, Args}, Transport) when Fun =:= login; Fun =:= auth_add_cred -> mfa(Ref, Mod, Fun, Args, Transport); mfa({Ref, Mod, Fun, Args}, Transport) -> - % mfa(Ref, Mod, Fun, Args); spawn_link(fun() -> mfa(Ref, Mod, Fun, Args, Transport) end), @@ -190,7 +189,7 @@ mfa(Ref, Mod, Fun, Args, Transport) -> ?TLog("~p MFA -> R ~n ~p:~p(~p) -> ~p~n", [Transport,Mod,Fun,NewArgs,ApplyRes]), ?TLog("~p MF -> R ~n ~p:~p -> ~p~n", [Transport,Mod,Fun,ApplyRes]), send_resp(ApplyRes, Transport), - ok. + ok. % 'ok' returned for erlimem compatibility args(R, fetch_recs_async, A, {_,_,R} = T) -> Args = lists:sublist(A, length(A)-1) ++ [T], diff --git a/src/imem_statement.erl b/src/imem_statement.erl index 8181863..4629eea 100755 --- a/src/imem_statement.erl +++ b/src/imem_statement.erl @@ -70,11 +70,12 @@ %% gen_server ----------------------------------------------------- create_stmt(Statement, SKey, IsSec) -> + {ok, SKeyPid} = imem_seco:get_skey_pid(SKey), case IsSec of false -> - gen_server:start(?MODULE, [Statement,self()], [{spawn_opt, [{fullsweep_after, 0}]}]); + gen_server:start(?MODULE, [Statement,SKeyPid], [{spawn_opt, [{fullsweep_after, 0}]}]); true -> - {ok, Pid} = gen_server:start(?MODULE, [Statement,self()], []), + {ok, Pid} = gen_server:start(?MODULE, [Statement,SKeyPid], []), NewSKey = imem_sec:clone_seco(SKey, Pid), ok = gen_server:call(Pid, {set_seco, NewSKey}), {ok, Pid}