forked from lichess-org/lila
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathUsers.scala
35 lines (29 loc) · 1005 Bytes
/
Users.scala
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
package lila.cli
import lila.user.{ User, UserRepo }
import lila.security.Store
import scalaz.effects._
case class Users(userRepo: UserRepo, securityStore: Store) {
def enable(username: String): IO[Unit] =
perform(username, "Enable", userRepo.enable)
def disable(username: String): IO[Unit] =
perform(username, "Disable", user ⇒
userRepo disable user map { _ ⇒
securityStore deleteUsername username
}
)
def passwd(username: String, password: String) =
perform(username, "Change password", user => {
userRepo.passwd(user, password) map ( _.fold(
errors ⇒ throw new RuntimeException(errors.shows),
_ ⇒ io()
))
})
private def perform(username: String, action: String, op: User ⇒ IO[Unit]) = for {
_ ← putStrLn(action + " " + username)
userOption ← userRepo byId username
_ ← userOption.fold(
u ⇒ op(u) flatMap { _ ⇒ putStrLn("Success") },
putStrLn("Not found")
)
} yield ()
}