scala-news-reader
rss/atom news reader in scala
git clone https://9o.is/git/scala-news-reader.git
UserPassword.scala
(1466B)
1 package com.joereader.snippet
2
3 import com.joereader.model._
4
5 /* Mixin when handling passwords. */
6 trait UserPassword {
7
8 // Checks if password is stealthy enough
9 def validPassword(pw: String): (Boolean, String) = {
10 if (pw.length < 1)
11 (false, "You forgot to enter a password")
12 else if (pw.length < 8)
13 (false, "Password must be at least 8 figures long")
14 else
15 (true, "")
16 }
17
18 def resettablePassword(
19 oldPw: String,
20 newPw: String,
21 confirmPw: String) = {
22 if (!correctPassword(oldPw))
23 (false, "Your old password is incorrect")
24 else if (newPw != confirmPw)
25 (false, "Your passwords do not match")
26 else
27 (true, "")
28 }
29
30 def savePassword(
31 oldPw: String,
32 newPw: String,
33 confirmPw: String): (Boolean, String) = {
34 val (valid, error) =
35 resettablePassword(oldPw, newPw, confirmPw)
36 if (valid)
37 savePassword(newPw)
38 else
39 (false, error)
40 }
41
42 def savePassword(pw: String): (Boolean, String) =
43 User.currentUser.map {
44 user =>
45 val (valid, error) = validPassword(pw)
46 if (valid) {
47 user.password(pw)
48 user.password.hashIt
49 user.update
50 (true, "")
51 } else (false, error)
52 } openOr(false, "???")
53
54 def correctPassword(pw: String) =
55 User.currentUser.exists(_.password.isMatch(pw))
56 }