2013-03-04 2 views
0

У меня есть класс user.java как таковойPlay 1.2.4 + CRUD модуль: Генерация паролей хэшируются

@javax.persistence.Entity 
@Table(name="users") 
public class User extends Model implements RoleHolder { 


    public User(String email, String password, String firstName, String lastName, Status status, List<UserRole> roles){ 
    this.email = email; 
    this.password = Crypto.passwordHash(password+email); 
    this.firstName = firstName; 
    this.lastName = lastName; 
    this.status = status; 
    this.roles = roles; 
} 

И в users.java

public class Users extends CRUD { 

}

Однако, когда Я создаю пользователя, он хранит пароль незашифрованного текста в базе данных, а не соленую и хешированную? Любая идея почему?

+1

ли что-нибудь (то есть "CRUD") установите 'password' поле * позже *? То есть, возможно, конструктор не является подходящим местом для установки производных значений. – 2013-03-05 00:15:06

ответ

0

Вы должны переписать create() и save() способ из класса CRUD внутри Users контроллер класс.

Может быть, решение выглядит следующим образом:

@CRUD.For(User.class) 
public class Users extends CRUD { 
    /** 
    * Re-implement Create (C) method 
    * @throws Exception 
    */ 
    public static void create() throws Exception { 
     // Get model type 
     ObjectType type = ObjectType.get(getControllerClass()); 
     notFoundIfNull(type); // render not found error if framework can't determine model type 
     Constructor<?> constructor = type.entityClass.getDeclaredConstructor(); 
     constructor.setAccessible(true); 

     // Create new instance of model 
     User object = (User) constructor.newInstance(); 
     // Bind all parameter value from submitted form 
     Binder.bindBean(params.getRootParamNode(), "object", object); 
     // Hash the password 
     object.password = Crypto.passwordHash(object.email + object.password); 

     // Check validity of model 
     validation.valid(object); 
     if (validation.hasErrors()) { 
     renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors")); 
     try { 
      render(request.controller.replace(".", "/") + "/blank.html", type, object); 
     } catch (TemplateNotFoundException e) { 
      render("CRUD/blank.html", type, object); 
     } 
     } 
     object._save(); // Finally, save the model 
     flash.success(play.i18n.Messages.get("crud.created", type.modelName)); 
     if (params.get("_save") != null) { 
     redirect(request.controller + ".list"); 
     } 
     if (params.get("_saveAndAddAnother") != null) { 
     redirect(request.controller + ".blank"); 
     } 
     redirect(request.controller + ".show", object._key()); 
    } 

    /** 
    * Re-implement Update (U) mehod 
    * @param id 
    * @throws Exception 
    */ 
    public static void save(String id) throws Exception { 
     // Get model type 
     ObjectType type = ObjectType.get(getControllerClass()); 
     notFoundIfNull(type); // render not found error if framework can't determine model type 

     // Find the model to be updated 
     User object = (User) type.findById(id); 
     notFoundIfNull(object); // render not found error if framework can't determine model record 
     // Bind all parameter value from submitted form 
     Binder.bindBean(params.getRootParamNode(), "object", object); 
     // Hash the password 
     object.password = Crypto.passwordHash(object.email + object.password); 

     // Check validity of model 
     validation.valid(object); 
     if (validation.hasErrors()) { 
     renderArgs.put("error", play.i18n.Messages.get("crud.hasErrors")); 
     try { 
      render(request.controller.replace(".", "/") + "/show.html", type, object); 
     } catch (TemplateNotFoundException e) { 
      render("CRUD/show.html", type, object); 
     } 
     } 
     object._save(); // Finally, save changes 
     flash.success(play.i18n.Messages.get("crud.saved", type.modelName)); 
     if (params.get("_save") != null) { 
     redirect(request.controller + ".list"); 
     } 
     redirect(request.controller + ".show", object._key()); 
    } 
} 
Смежные вопросы