/example_provider/src/main/scala/user.scala
Scala | 51 lines | 43 code | 8 blank | 0 comment | 3 complexity | 9ae7e682cf8ae171631531f7387a2f4c MD5 | raw file
- package info.whiter4bbit.oauth.scalatra.example
- import com.mongodb.casbah.Imports._
- import info.whiter4bbit.oauth.TokenGenerator
- import org.slf4j.LoggerFactory
- import scala.util.parsing.json._
- import scalaz._
- import Scalaz._
- trait UserMongoCollection {
- val users: MongoCollection
- }
- case class User(val login: String, val password: String, val consumerKey: String, val consumerSecret: String)
- trait UserService extends TokenGenerator { self: UserMongoCollection with TokenGenerator =>
- val logger = LoggerFactory.getLogger(getClass)
- def generateKey = self.generateToken(32)
- def create(login: String, password: String): Validation[String, User] = {
- val user = User(login, password, generateKey, generateKey)
- if (self.users.find(MongoDBObject("login" -> login)).size == 0) {
- self.users.insert(MongoDBObject("login" -> login,
- "password" -> password,
- "consumerKey" -> user.consumerKey,
- "consumerSecret" -> user.consumerSecret))
- logger.info("User %s has been created" format login)
- user.success
- } else {
- logger.info("User %s already exists" format login)
- "User %s already exists".format(login).fail
- }
- }
- def find(consumerKey: String): Validation[String, User] = {
- (for {
- found <- self.users.findOne(MongoDBObject("consumerKey" -> consumerKey));
- login <- found.getAs[String]("login");
- password <- found.getAs[String]("password");
- conumserKey <- found.getAs[String]("consumerKey");
- consumerSecret <- found.getAs[String]("consumerSecret")
- } yield {
- User(login, password, consumerKey, consumerSecret).success
- }).getOrElse({
- println("Consumer with key %s not found" format consumerKey)
- "Consumer with key %s not found".fail
- })
- }
- }