2016-07-06 31 views
1

我有型號如下:爲密碼字段綁定播放框架scala表單對象?

case class User(uid: Option[Int], email: String, password: String, created_at: Timestamp, updated_at: Timestamp) 

case class UserProfile(firstname: String, lastname: String, gender: Int, user_id: Long) 

我所定義的形式結合如下:

val date = new Date() 
    val currentTimestamp= new Timestamp(date.getTime()); 
    val registerForm = Form(
    tuple(
      "user" -> mapping(
      "uid" -> optional(number), 
      "email" -> email, 
      "password" -> nonEmptyText, 
      "created_at" -> ignored(currentTimestamp), 
      "updated_at" -> ignored(currentTimestamp) 
     ) (User.apply)(User.unapply), 
     "profile" -> mapping(
      "firstname"->nonEmptyText, 
      "lastname"->nonEmptyText, 
      "gender" -> ignored(0), 
      "user_id" -> ignored(0L) 
     )(UserProfile.apply)(UserProfile.unapply)) 
    ) 

現在,我想存儲使用散列密碼,然後纔會慢慢使用保存/插入數據庫光滑。 我可以嘗試通過創建一個新的用戶對象來實現,但這聽起來不像是有效的方法。

還有其他方法嗎?

在此先感謝

--------------------------------------- ---編輯1 --------------------------------------------- ------------- 這是我插入邏輯用油滑:

def insert(user: User): Future[Any] = { 
    println("coming inside insert of user dao") 
    println(user) 
// insertUP(user) 
    db.run((Users returning Users.map(_.uid)) += user) 
    } 
+0

如何調用一個方法在加密密碼你在做插入/更新的地步?你可以粘貼你的插入邏輯? –

+0

@RobertUdah嘗試分配哈希密碼,但由於插入的用戶對象值不能被重新分配,它不能成爲解決方案。我在我的問題中更新了插入邏輯的編輯。 – Maverick

回答

1

如何像:

def insert(user: User): Future[Any] = { 
    val hashedPassword = hashPassword(user.password) 
    val updatedUser = user.copy(password = hashedPassword) 
    db.run((Users returning Users.map(_.uid)) += updatedUser) 
} 
+0

斯卡拉的魔力! :)它是一個很好的解決方案,謝謝。我爲此使用了Bcrypt庫。 – Maverick