Skip to content

Commit fe7bd7f

Browse files
author
Karl Seguin
committed
chapter 1
1 parent 0044f4c commit fe7bd7f

File tree

5 files changed

+52
-4
lines changed

5 files changed

+52
-4
lines changed

app/controllers/Sessions.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,12 @@ public static void novus() {
1010
}
1111

1212
public static void save(String name, String password) {
13-
renderText("todo");
13+
User user = User.loadFromCredentials(name, password);
14+
if (user != null) {
15+
signin(user);
16+
}
17+
addError("invalid name or password, please try again");
18+
renderTemplate("Sessions/novus.html");
1419
}
1520

1621
public static void logout() {

app/controllers/Users.java

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,15 @@ public static void novus() {
1010
}
1111

1212
public static void save(User user) {
13-
renderText("todo");
13+
try
14+
{
15+
if(user != null && user.validateAndSave()) {
16+
signin(user);
17+
}
18+
validationError(new FieldError(validation.errors()));
19+
} catch (DuplicateKeyException ex) {
20+
validationError(new FieldError("user.name", "is already taken"));
21+
}
22+
renderTemplate("Users/novus.html");
1423
}
1524
}

app/models/User.java

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,33 @@
11
package models;
2-
2+
3+
import utils.*;
4+
import play.data.validation.*;
35
import play.modules.morphia.Model;
46
import com.google.code.morphia.annotations.*;
7+
import com.mongodb.MongoException.DuplicateKey;
58

69
@Entity(value="users", noClassnameStored=true)
7-
public class User extends Model {
10+
public class User extends Model {
11+
@Required @MaxSize(20) @Indexed(unique=true)
12+
public String name;
13+
14+
@Required @MinSize(8)
15+
public String password;
16+
17+
public static User loadFromCredentials(String name, String password) {
18+
if (name == null || password == null) { return null; }
19+
20+
User user = filter("name = ", name).first();
21+
return user == null || !BCrypt.checkpw(password, user.password) ? null : user;
22+
}
823

24+
@Override
25+
public User save() {
26+
try {
27+
password = BCrypt.hashpw(password, BCrypt.gensalt());
28+
return super.save();
29+
} catch(DuplicateKey e) {
30+
throw new DuplicateKeyException(e);
31+
}
32+
}
933
}
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
package utils;
2+
3+
public class DuplicateKeyException extends RuntimeException {
4+
public DuplicateKeyException(Throwable t) {
5+
super(t);
6+
}
7+
}

app/utils/readme.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@ These classes will be used throughout our code as we move forward.
55
### BCrypt
66
We'll use [BCrypt](http://en.wikipedia.org/wiki/Bcrypt) to hash a user's password
77

8+
## DuplicateKeyException
9+
Used whenever we want to raise a duplicate key exception. Having our own exception is cleaner, in my opinion, than having the Mongo exception exposed everywhere.
10+
811
### ExactSizeList + ExactSizeListCheck
912
Validation annotation and logic to ensure that a list is of an exact size/length.
1013

0 commit comments

Comments
 (0)