Implemented ts_api:get_user/2, ts_api:put_user/1, and ts_api:post_user/1.
Created a utility function for OK results, ts_api:make_json_200/2 Created ts_common:new/1 and ts_common:update/1 to generalize record creation and update. Refactored ts_timeline:new/1 and ts_timeline:update/1 to use the ts_common functions. Implemented ts_json:record_to_ejson/1 and ts_json:ejson_to_record/1 for ts_user records. Implemented ts_user module.
This commit is contained in:
@ -1,5 +1,5 @@
|
||||
-module(ts_user).
|
||||
-export([]).
|
||||
-export([create_table/1, new/1, update/1, lookup/1, list/2]).
|
||||
|
||||
-include("ts_db_records.hrl").
|
||||
-include_lib("stdlib/include/qlc.hrl").
|
||||
@ -9,4 +9,38 @@ create_table(TableOpts) ->
|
||||
TableOpts ++ [{attributes, record_info(fields, ts_user)},
|
||||
{type, ordered_set}]).
|
||||
|
||||
%new(TR = #ts_user
|
||||
new(UR = #ts_user{}) ->
|
||||
case mnesia:dirty_read(ts_user, UR#ts_user.username) of
|
||||
[ExistingRecord] -> {error, {record_exists, ExistingRecord}};
|
||||
[] -> mnesia:dirty_write(hash_input_record(UR))
|
||||
end.
|
||||
|
||||
update(UR = #ts_user{}) ->
|
||||
case mnesia:dirty_read(ts_user, UR#ts_user.username) of
|
||||
[] -> no_record;
|
||||
[_Record] -> mnesia:dirty_write(hash_input_record(UR))
|
||||
end.
|
||||
|
||||
lookup(Username) ->
|
||||
case mnesia:dirty_read(ts_user, Username) of
|
||||
[] -> no_record;
|
||||
[User] -> User
|
||||
end.
|
||||
|
||||
list(Start, Length) -> ts_common:list(ts_user, Start, Length).
|
||||
|
||||
hash_input_record(User=#ts_user{}) ->
|
||||
% generate the password salt
|
||||
Salt = generate_salt(),
|
||||
% hash the password
|
||||
HashedPwd = hash_pwd(User#ts_user.username, Salt),
|
||||
% create a new User record
|
||||
User#ts_user{pwd = HashedPwd, pwd_salt = Salt}.
|
||||
|
||||
generate_salt() ->
|
||||
"This is a worthless salt value only suitable for testing.".
|
||||
|
||||
hash_pwd(Password, Salt) -> do_hash(Password ++ Salt, []).
|
||||
|
||||
do_hash([], Hashed) -> Hashed;
|
||||
do_hash([Char|Pwd], Hashed) -> do_hash(Pwd, [Char + 13 | Hashed]).
|
||||
|
Reference in New Issue
Block a user