Guest User

Untitled

a guest
Sep 1st, 2018
116
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.04 KB | None | 0 0
  1. Get field from mnesia
  2. [{users, <<"user_name">>, <<"password">>}].
  3.  
  4. mnesia:dirty_read({users, <<"user_name">>}).
  5.  
  6. -module(auth).
  7.  
  8. -export([init/1, add_user/2, start_session/2]).
  9. -record(auth_user, {username, password}).
  10.  
  11. init(Nodes) ->
  12. mnesia:create_table(auth_user,
  13. [{disc_copies, Nodes},
  14. {attributes, record_info(fields, auth_user)}]).
  15.  
  16. add_user(Username, Password) ->
  17. T = fun() ->
  18. mnesia:write(#auth_user {
  19. username = Username,
  20. password = Password})
  21. end,
  22. mnesia:transaction(T).
  23.  
  24. start_session(Username, Password) ->
  25. T = fun() ->
  26. mnesia:read(auth_user, Username)
  27. end,
  28. {atomic, Ret} = mnesia:transaction(T),
  29. case Ret of
  30. [U] ->
  31. if (U#auth_user.password == Password) ->
  32. true;
  33. true ->
  34. false
  35. end;
  36. _Else ->
  37. false
  38. end.
  39.  
  40. -module(auth).
  41.  
  42. -export([init/1, add_user/2, start_session/2]).
  43. -record(auth_user, {username, password}).
  44.  
  45. init(Nodes) ->
  46. mnesia:create_table(auth_user,
  47. [{disc_copies, Nodes},
  48. {attributes, record_info(fields, auth_user)}]).
  49.  
  50. add_user(Username, Password) ->
  51. T = fun() ->
  52. mnesia:write(#auth_user {
  53. username = Username,
  54. password = Password})
  55. end,
  56. mnesia:transaction(T).
  57.  
  58. start_session(Username, Password) ->
  59. T = fun() ->
  60. mnesia:read(auth_user, Username)
  61. end,
  62. {atomic, Ret} = mnesia:transaction(T),
  63. case Ret of
  64. [U] ->
  65. if (U#auth_user.password == Password) ->
  66. true;
  67. true ->
  68. false
  69. end;
  70. _Else ->
  71. false
  72. end.
  73.  
  74. -module(auth).
  75.  
  76. -export([init/1, add_user/2, start_session/2]).
  77. -record(auth_user, {username, password}).
  78.  
  79. init(Nodes) ->
  80. mnesia:create_table(auth_user,
  81. [{disc_copies, Nodes},
  82. {attributes, record_info(fields, auth_user)}]).
  83.  
  84. add_user(Username, Password) ->
  85. T = fun() ->
  86. mnesia:write(#auth_user {
  87. username = Username,
  88. password = Password})
  89. end,
  90. mnesia:transaction(T).
  91.  
  92. start_session(Username, Password) ->
  93. T = fun() ->
  94. mnesia:read(auth_user, Username)
  95. end,
  96. {atomic, Ret} = mnesia:transaction(T),
  97. case Ret of
  98. [U] ->
  99. if (U#auth_user.password == Password) ->
  100. true;
  101. true ->
  102. false
  103. end;
  104. _Else ->
  105. false
  106. end.
  107.  
  108. Eshell V5.8.3 (abort with ^G)
  109. 1> mnesia:create_schema([node()]).
  110. ok
  111. 3> mnesia:start().
  112. ok
  113. 4> auth:init([node()]).
  114. {atomic,ok}
  115. 5> auth:add_user("rodericktaylor", "password").
  116. {atomic,ok}
  117. 6> true = auth:start_session("rodericktaylor", "password").
  118. true
  119. 7>
  120.  
  121. start_session_dirty(Username, Password) ->
  122. case mnesia:dirty_read(auth_user, Username) of
  123. [U] ->
  124. if (U#auth_user.password == Password) ->
  125. true;
  126. true ->
  127. false
  128. end;
  129. _Else ->
  130. false
  131. end.
  132.  
  133. 12> auth:add_user(<<"rodericktaylor">>, <<"binarypassword">>).
  134. {atomic,ok}
  135. 14> true = auth:start_session_dirty(<<"rodericktaylor">>, <<"binarypassword">>).
  136. true
  137. 15>
Add Comment
Please, Sign In to add comment