Skip to content
This repository
Browse code

Added a function to add a new email verification code

  • Loading branch information...
commit 2a0886ba7debb50b9e91a7d642f4e6b7a499a20b 1 parent 811c00b
Jonathan McAllister authored July 28, 2009
BIN  Mnesia.nitrogen@localhost/DECISION_TAB.LOG
Binary file not shown
BIN  Mnesia.nitrogen@localhost/LATEST.LOG
Binary file not shown
BIN  ebin/db_users.beam
Binary file not shown
BIN  ebin/db_utils.beam
Binary file not shown
2  include/config.inc
@@ -5,6 +5,6 @@
5 5
 -record(users, { username, email_address, password }).
6 6
 -record(chronology_users, { username, date_joined, last_logged_in }).
7 7
 -record(verification_levels, { username, verified_email=false }).
8  
--record(verification_codes_email, { verification_code, username }).
  8
+-record(verification_codes_email, { username, verification_code }).
9 9
  
10 10
 -endif.
29  src/db_users.erl
@@ -20,7 +20,7 @@
20 20
 -module(db_users).
21 21
 -include("wf.inc").
22 22
 -include("config.inc").
23  
--export([init/0, add_user/3, validate_user/2, delete_user/1, is_username_used/1, is_email_used/1, get_email_address/1, verify_email/1, update_verification_code/2, delete_verification_code/1, invalidate_email/1]).
  23
+-export([init/0, add_user/3, validate_user/2, delete_user/1, is_username_used/1, is_email_used/1, get_email_address/1, verify_email/1, update_verification_code/2, invalidate_email/1, delete_email_verification_code/1, new_email_verification_code/1]).
24 24
 
25 25
 -include_lib("stdlib/include/qlc.hrl").
26 26
 
@@ -45,7 +45,7 @@ add_user(Username, EmailAddress, Password) ->
45 45
     UsersRow = #users { username=Username, email_address=EmailAddress, password=PasswordDigest },
46 46
     ChronologyUsersRow = #chronology_users { username=Username, date_joined=CurrentTime, last_logged_in=CurrentTime },
47 47
     VerificationLevelsRow = #verification_levels { username=Username },
48  
-    VerificationCodesEmailRow = #verification_codes_email { verification_code=VerificationCode, username=Username },
  48
+    VerificationCodesEmailRow = #verification_codes_email { username=Username, verification_code=VerificationCode },
49 49
     F = fun() ->
50 50
 		mnesia:write(UsersRow),
51 51
 		mnesia:write(ChronologyUsersRow),
@@ -95,12 +95,6 @@ update_last_logged_in(Username) ->
95 95
 	end,
96 96
     mnesia:transaction(F).
97 97
 
98  
-delete_verification_code(VerificationCode) ->
99  
-    F = fun() ->
100  
-		mnesia:delete(verification_codes_email, VerificationCode, write)
101  
-	end,
102  
-    mnesia:transaction(F).
103  
-
104 98
 delete_user(Username) ->
105 99
     F = fun() ->
106 100
 		mnesia:delete(users, Username, write)
@@ -161,9 +155,8 @@ get_email_address(Username) ->
161 155
 
162 156
 
163 157
 verify_email(Code) ->
164  
-    % get the username from the verification_codes table
165 158
     FGetUsername = fun() ->
166  
-		mnesia:read(verification_codes_email, Code)
  159
+		mnesia:match_object({verification_codes_email, '_', Code})
167 160
 	end,
168 161
     {atomic, [VerificationCodesEmailRow]} = mnesia:transaction(FGetUsername),
169 162
     Username = VerificationCodesEmailRow#verification_codes_email.username,
@@ -173,7 +166,14 @@ verify_email(Code) ->
173 166
 		VerificationLevelsUpdate = VerificationLevels#verification_levels{verified_email=true},
174 167
 		mnesia:write(VerificationLevelsUpdate)
175 168
 	end,
176  
-    io:format("Test: ~w~n", [mnesia:transaction(FUpdateVerifiedEmail)]).
  169
+    io:format("Test: ~w~n", [mnesia:transaction(FUpdateVerifiedEmail)]),
  170
+    delete_email_verification_code(Username).
  171
+
  172
+delete_email_verification_code(Username) ->
  173
+    F = fun() ->
  174
+		mnesia:delete({verification_codes_email, Username})
  175
+	end,
  176
+    io:format("Delete email verification code: ~w~n", [mnesia:transaction(F)]).
177 177
 
178 178
 invalidate_email(Username) ->
179 179
     FUpdateVerifiedEmail = fun() ->
@@ -183,3 +183,10 @@ invalidate_email(Username) ->
183 183
 	end,
184 184
     mnesia:transaction(FUpdateVerifiedEmail).
185 185
 
  186
+new_email_verification_code(Username) ->
  187
+    VerificationCode = string_utils:md5_hex(string_utils:generate_random_string(32)),
  188
+    VerificationCodesEmailRow = #verification_codes_email { username=Username, verification_code=VerificationCode },
  189
+    F = fun() ->
  190
+		mnesia:write(VerificationCodesEmailRow)
  191
+	end,
  192
+    mnesia:transaction(F).

0 notes on commit 2a0886b

Please sign in to comment.
Something went wrong with that request. Please try again.