diff --git a/dudeswave_users/src/dudeswave_users_user.erl b/dudeswave_users/src/dudeswave_users_user.erl index d707cf1..c732adb 100644 --- a/dudeswave_users/src/dudeswave_users_user.erl +++ b/dudeswave_users/src/dudeswave_users_user.erl @@ -18,7 +18,7 @@ -include_lib("dudeswave_backend/include/defines.hrl"). -include_lib("storage/include/storage.hrl"). --export([details/1, new/3, update/4, delete/1]). +-export([details/2, new/4, update/5, delete/1]). -doc """ Return user details. @@ -26,22 +26,30 @@ Return user details. Spec: ``` --spec details(User) -> Value | {error, Reason} when +-spec details(User, Host) -> {ok, Value} | {error, Reason} when User :: binary(), + Host :: binary(), Value :: term(), Reason :: term(). ``` """. --spec details(User) -> Value | {error, Reason} when +-spec details(User, Host) -> {ok, Value} | {error, Reason} when User :: binary(), + Host :: binary(), Value :: term(), Reason :: term(). -details(User) -> - case storage:read(?USERSBUCK, User) of - {ok, [R]} -> R#object.value; - {error, Reason} -> {error, Reason}; - {ok, []} -> {error, not_found} +details(User, Host) -> + ComplUser = <>, + + case storage:read(?USERSBUCK, ComplUser) of + {ok, [R]} -> + {details, Details} = proplists:lookup(details, R#object.value), + {ok, Details}; + {error, Reason} -> + {error, Reason}; + {ok, []} -> + {error, not_found} end. -doc """ @@ -50,8 +58,9 @@ Create a new user. Spec: ``` --spec new(User, Password, Email) -> ok | {error, Reason} when +-spec new(User, Host, Password, Email) -> ok | {error, Reason} when User :: binary(), + Host :: binary(), Password :: binary(), Email :: binary(), Reason :: term(). @@ -62,20 +71,23 @@ The `User` is created, and stored in the application's users bucket The new user is saved with a metadata `approved` of `false`, """. --spec new(User, Password, Email) -> ok | {error, Reason} when +-spec new(User, Host, Password, Email) -> ok | {error, Reason} when User :: binary(), + Host :: binary(), Password :: binary(), Email :: binary(), Reason :: term(). -new(User, Password, Email) -> +new(User, Host, Password, Email) -> + ComplUser = <>, + Salt = rand:bytes(?RANDBYTES), Hash = crypto:hash(sha256, <>), - Data = #{<<"email">> => Email}, - Metadata = [{salt, Salt}, {hash, Hash}, {approved, false}], + Data = [{details, #{<<"email">> => Email}}, {salt, Salt}, {hash, Hash}], + Metadata = [{approved, false}], - storage:write(?USERSBUCK, User, Data, Metadata). + storage:write(?USERSBUCK, ComplUser, Data, Metadata). -doc """ Update user's details @@ -83,8 +95,9 @@ Update user's details Spec: ``` --spec update(User, Name, Email, Desc) -> ok | {error, Reason} when +-spec update(User, Host, Name, Email, Desc) -> ok | {error, Reason} when User :: binary(), + Host :: binary(), Name :: binary(), Email :: binary(), Desc :: binary(), @@ -95,24 +108,29 @@ The details apart from `User` are updated. The username itself is immutable and cannot be modified. All the other fields, excluding the e-mail, are the ones that can be seen in the public page. """. --spec update(User, Name, Email, Desc) -> ok | {error, Reason} when +-spec update(User, Host, Name, Email, Desc) -> ok | {error, Reason} when User :: binary(), + Host :: binary(), Name :: binary(), Email :: binary(), Desc :: binary(), Reason :: term(). -update(User, Name, Email, Desc) -> - {ok, CurData, Metadata} = case storage:read(?USERSBUCK, User) of +update(User, Host, Name, Email, Desc) -> + ComplUser = <>, + + {ok, Value, Metadata} = case storage:read(?USERSBUCK, ComplUser) of {ok, [R]} -> {ok, R#object.value, R#object.metadata}; {error, Reason} -> {error, Reason} end, - Data = CurData#{<<"email">> => Email, <<"name">> => Name, + {details, CurDets} = proplists:lookup(details, Value), + NewDets = CurDets#{<<"email">> => Email, <<"name">> => Name, <<"description">> => Desc}, + NewData = lists:keyreplace(details, 1, Value, {details, NewDets}), - storage:write(?USERSBUCK, User, Data, Metadata). + storage:write(?USERSBUCK, ComplUser, NewData, Metadata). -doc """ Delete an existing user from the database.