本文整理汇总了Scala中models.services.UserService类的典型用法代码示例。如果您正苦于以下问题:Scala UserService类的具体用法?Scala UserService怎么用?Scala UserService使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了UserService类的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Scala代码示例。
示例1: ChangePasswordController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.exceptions.ProviderException
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.{ Credentials, PasswordHasherRegistry, PasswordInfo }
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import models.services.UserService
import play.api.i18n.{ I18nSupport, Messages, MessagesApi }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Controller
import utils.auth.{ DefaultEnv, WithProvider }
import scala.concurrent.Future
class ChangePasswordController @Inject() (
val messagesApi: MessagesApi,
silhouette: Silhouette[DefaultEnv],
userService: UserService,
credentialsProvider: CredentialsProvider,
authInfoRepository: AuthInfoRepository,
passwordHasherRegistry: PasswordHasherRegistry
)
extends Controller with I18nSupport {
def view = silhouette.SecuredAction(WithProvider[DefaultEnv#A](CredentialsProvider.ID)) { implicit request =>
Ok(views.html.changePassword(new myform.MyChangePasswordForm(), request.identity))
}
def submit = silhouette.SecuredAction(WithProvider[DefaultEnv#A](CredentialsProvider.ID)).async { implicit request =>
new myform.MyChangePasswordForm().bindFromRequest match {
case form: myform.MyChangePasswordForm => Future.successful(BadRequest(views.html.changePassword(form, request.identity)))
case data: myform.MyChangePasswordFormData => {
val (currentPassword, newPassword) = (data.currentPassword, data.newPassword)
val credentials = Credentials(request.identity.email.getOrElse(""), currentPassword)
credentialsProvider.authenticate(credentials).flatMap { loginInfo =>
val passwordInfo = passwordHasherRegistry.current.hash(newPassword)
authInfoRepository.update[PasswordInfo](loginInfo, passwordInfo).map { _ =>
Redirect(routes.ChangePasswordController.view()).flashing("success" -> Messages("password.changed"))
}
}.recover {
case e: ProviderException =>
Redirect(routes.ChangePasswordController.view()).flashing("error" -> Messages("current.password.invalid"))
}
}
}
}
}
示例2: ResetPasswordController
//设置package包名称以及导入依赖的类
package controllers
import java.util.UUID
import javax.inject.Inject
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.{ PasswordHasherRegistry, PasswordInfo }
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import models.services.{ AuthTokenService, UserService }
import play.api.i18n.{ I18nSupport, Messages, MessagesApi }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Controller
import utils.auth.DefaultEnv
import scala.concurrent.Future
class ResetPasswordController @Inject() (
val messagesApi: MessagesApi,
silhouette: Silhouette[DefaultEnv],
userService: UserService,
authInfoRepository: AuthInfoRepository,
passwordHasherRegistry: PasswordHasherRegistry,
authTokenService: AuthTokenService
)
extends Controller with I18nSupport {
def view(token: UUID) = silhouette.UnsecuredAction.async { implicit request =>
authTokenService.validate(token).map {
case Some(authToken) => Ok(views.html.resetPassword(new myform.MyResetPasswordForm(), token))
case None => Redirect(routes.SignInController.view()).flashing("error" -> Messages("invalid.reset.link"))
}
}
def submit(token: UUID) = silhouette.UnsecuredAction.async { implicit request =>
authTokenService.validate(token).flatMap {
case Some(authToken) =>
new myform.MyResetPasswordForm().bindFromRequest match {
case form: myform.MyResetPasswordForm => Future.successful(BadRequest(views.html.resetPassword(form, token)))
case data: myform.MyResetPasswordFormData => userService.retrieve(authToken.userID).flatMap {
case Some(user) if user.loginInfo.providerID == CredentialsProvider.ID =>
val passwordInfo = passwordHasherRegistry.current.hash(data.password)
authInfoRepository.update[PasswordInfo](user.loginInfo, passwordInfo).map { _ =>
Redirect(routes.SignInController.view()).flashing("success" -> Messages("password.reset"))
}
case _ => Future.successful(Redirect(routes.SignInController.view()).flashing("error" -> Messages("invalid.reset.link")))
}
}
case None => Future.successful(Redirect(routes.SignInController.view()).flashing("error" -> Messages("invalid.reset.link")))
}
}
}
示例3: ForgotPasswordController
//设置package包名称以及导入依赖的类
package controllers
import javax.inject.Inject
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import models.services.{ AuthTokenService, UserService }
import models.daos._
import play.api.i18n.{ I18nSupport, Messages, MessagesApi }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.libs.mailer.{ Email, MailerClient }
import play.api.mvc.Controller
import utils.auth.DefaultEnv
import scala.concurrent.Future
class ForgotPasswordController @Inject() (
val messagesApi: MessagesApi,
silhouette: Silhouette[DefaultEnv],
userService: UserService,
authTokenService: AuthTokenService,
mailerClient: MailerClient,
envDAO: EnvDAO
)
extends Controller with I18nSupport {
def view = silhouette.UnsecuredAction.async { implicit request =>
Future.successful(Ok(views.html.forgotPassword(new myform.MyForgotPasswordForm())))
}
def submit = silhouette.UnsecuredAction.async { implicit request =>
new myform.MyForgotPasswordForm().bindFromRequest match {
case form: myform.MyForgotPasswordForm => Future.successful(BadRequest(views.html.forgotPassword(form)))
case data: myform.MyForgotPasswordFormData => {
val email = data.email
val loginInfo = LoginInfo(CredentialsProvider.ID, email)
val result = Redirect(routes.SignInController.view()).flashing("info" -> Messages("reset.email.sent"))
userService.retrieve(loginInfo).flatMap {
case Some(user) if user.email.isDefined =>
authTokenService.create(user.userID).map { authToken =>
val url = routes.ResetPasswordController.view(authToken.id).absoluteURL()
println("url " + url)
if (envDAO.getDosendmail) mailerClient.send(Email(
subject = Messages("email.reset.password.subject"),
from = Messages("email.from"),
to = Seq(email),
bodyText = Some(views.txt.emails.resetPassword(user, url).body),
bodyHtml = Some(views.html.emails.resetPassword(user, url).body)
))
result
}
case None => Future.successful(result)
}
}
}
}
}
示例4: UserDAOImpl
//设置package包名称以及导入依赖的类
package models.daos
import com.google.inject.Inject
import com.mohiva.play.silhouette.api.LoginInfo
import com.mohiva.play.silhouette.api.exceptions.AuthenticatorCreationException
import models.User
import models.services.UserService
import models.tables.UserTableDef
import play.api.db.slick.{DatabaseConfigProvider, HasDatabaseConfigProvider}
import slick.driver.JdbcProfile
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class UserDAOImpl @Inject()(protected val dbConfigProvider: DatabaseConfigProvider)
extends UserDAO with UserService with HasDatabaseConfigProvider[JdbcProfile] {
import dbConfig.driver.api._
val users = TableQuery[UserTableDef]
override def find(id: Long) =
dbConfig.db.run(users.filter(_.id === id).result.headOption)
override def find(username: String): Future[Option[User]] =
dbConfig.db.run(users.filter(_.username === username).result.headOption)
override def findByEmail(email: String) =
dbConfig.db.run(users.filter(_.email === email).result.headOption)
// to implement UserService
override def retrieve(loginInfo: LoginInfo): Future[Option[User]] = find(loginInfo.providerKey)
// TODO handle exceptions (-> test)
override def add(user: User): Future[User] =
dbConfig.db.run(users returning users += user)
.recover {
case e: Exception => throw new AuthenticatorCreationException("failed to create user " + user)
}
override def update(user: User) =
dbConfig.db.run(users.filter(_.id === user.id).update(user))
.map {
case 0 => None
case _ => Some(user)
}
}
示例5: LogIn
//设置package包名称以及导入依赖的类
package controllers
import com.google.inject.Inject
import com.mohiva.play.silhouette.api.exceptions.ProviderException
import com.mohiva.play.silhouette.api.{LoginEvent, Silhouette}
import com.mohiva.play.silhouette.api.util.Credentials
import com.mohiva.play.silhouette.impl.exceptions.IdentityNotFoundException
import com.mohiva.play.silhouette.impl.providers.CredentialsProvider
import models.forms.UserLogInForm
import models.services.UserService
import play.api.i18n.{I18nSupport, Messages, MessagesApi}
import play.api.mvc._
import utils.auth.DefaultEnv
import scala.concurrent.ExecutionContext.Implicits.global
import scala.concurrent.Future
class LogIn @Inject()(
silhouette: Silhouette[DefaultEnv],
userService: UserService,
credentialsProvider: CredentialsProvider,
val messagesApi: MessagesApi)
extends Controller with I18nSupport {
def view = silhouette.UnsecuredAction{ implicit request =>
Ok(views.html.login(UserLogInForm.form))
}
def submit: Action[AnyContent] = silhouette.UnsecuredAction.async { implicit request =>
UserLogInForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(views.html.login(form))),
data => {
val credentials = Credentials(data.username, data.password)
credentialsProvider.authenticate(credentials).flatMap { loginInfo =>
val result = Redirect(routes.Application.index())
userService.retrieve(loginInfo).flatMap {
case Some(user) if !user.activated =>
Future.successful(Ok(views.html.activateAccount(user.email)))
// Future.successful(Redirect(routes.LogIn.view()).flashing("error" -> "Account not activated."))
case Some(user) =>
silhouette.env.authenticatorService.create(loginInfo).flatMap {
authenticator =>
silhouette.env.eventBus.publish(LoginEvent(user, request))
silhouette.env.authenticatorService.init(authenticator).flatMap { v =>
silhouette.env.authenticatorService.embed(v, result)
}
}
case None => Future.failed(new IdentityNotFoundException("Couldn't find user"))
}
}
.recover {
case e: ProviderException =>
Redirect(routes.LogIn.view()).flashing("error" -> Messages("invalid.credentials"))
}
}
)
}
}
示例6: messagesApi
//设置package包名称以及导入依赖的类
package controllers
import com.mohiva.play.silhouette.api.actions._
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.services.AvatarService
import com.mohiva.play.silhouette.api.util.{ Clock, PasswordHasher }
import com.mohiva.play.silhouette.impl.providers.{ CredentialsProvider, SocialProviderRegistry }
import com.softwaremill.macwire._
import models.daos.AuthTokenDAOImpl
import models.services.{ AuthTokenServiceImpl, UserService }
import play.api.Configuration
import play.api.i18n.MessagesApi
import utils.auth.{ CustomSecuredErrorHandler, CustomUnsecuredErrorHandler }
trait ControllerModule {
lazy val authToken = new AuthTokenDAOImpl
lazy val authTokenService = new AuthTokenServiceImpl(authToken, clock)
lazy val securedErrorHandler: SecuredErrorHandler = wire[CustomSecuredErrorHandler]
lazy val unSecuredErrorHandler: UnsecuredErrorHandler = wire[CustomUnsecuredErrorHandler]
lazy val userAwareAction = new DefaultUserAwareAction(new DefaultUserAwareRequestHandler)
lazy val securedAction: SecuredAction = new DefaultSecuredAction(new DefaultSecuredRequestHandler(securedErrorHandler))
lazy val unsecuredAction: UnsecuredAction = new DefaultUnsecuredAction(new DefaultUnsecuredRequestHandler(unSecuredErrorHandler))
def messagesApi: MessagesApi
def socialProviderRegistry: SocialProviderRegistry
def userService: UserService
def authInfoRepository: AuthInfoRepository
def credentialsProvider: CredentialsProvider
def configuration: Configuration
def clock: Clock
def avatarService: AvatarService
def passwordHasher: PasswordHasher
}
示例7: DeadboltHandlerImpl
//设置package包名称以及导入依赖的类
package utils.auth.deadbolt
import javax.inject.{ Inject, Singleton }
import be.objectify.deadbolt.scala.models.Subject
import be.objectify.deadbolt.scala.{ AuthenticatedRequest, DeadboltHandler, DynamicResourceHandler }
import com.mohiva.play.silhouette.api.Silhouette
import com.mohiva.play.silhouette.api.util.ExtractableRequest
import models.services.UserService
import play.api.i18n.{ I18nSupport, MessagesApi }
import play.api.mvc.{ Request, Result, Results }
import utils.auth.DefaultEnv
import scala.concurrent.Future
import scala.concurrent.ExecutionContext.Implicits.global
@Singleton
class DeadboltHandlerImpl @Inject() (val messagesApi: MessagesApi, silhouette: Silhouette[DefaultEnv], userService: UserService)
extends DeadboltHandler with I18nSupport {
override def beforeAuthCheck[A](request: Request[A]): Future[Option[Result]] = Future.successful(None)
override def getSubject[A](request: AuthenticatedRequest[A]): Future[Option[Subject]] = {
implicit val extractableRequest = new ExtractableRequest(request)
silhouette.env.authenticatorService.retrieve.flatMap {
case Some(authenticator) =>
userService.retrieve(authenticator.loginInfo)
case None => Future.successful(None)
}
}
override def onAuthFailure[A](request: AuthenticatedRequest[A]): Future[Result] = {
println("Deadbolt: onAuthFailure triggered!")
Future.successful(Results.Unauthorized) // todo or does this need to accept?
}
override def getDynamicResourceHandler[A](request: Request[A]): Future[Option[DynamicResourceHandler]] =
Future.successful(None)
}
示例8: ProductController
//设置package包名称以及导入依赖的类
package controllers
import scala.concurrent.Future
import javax.inject._
import com.mohiva.play.silhouette.api.Silhouette
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.Clock
import com.mohiva.play.silhouette.impl.providers.{CredentialsProvider, SocialProviderRegistry}
import forms.ProductForm
import models.Product
import models.daos.ProductDAO
import models.services.UserService
import play.api.Configuration
import play.api.i18n.I18nSupport
import play.api.i18n.MessagesApi
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Controller
import utils.auth.DefaultEnv
@Singleton
class ProductController @Inject()(val messagesApi: MessagesApi,
silhouette: Silhouette[DefaultEnv],
userService: UserService,
authInfoRepository: AuthInfoRepository,
credentialsProvider: CredentialsProvider,
socialProviderRegistry: SocialProviderRegistry,
configuration: Configuration,
clock: Clock,
productDAO: ProductDAO,
implicit val webJarAssets: WebJarAssets)
extends Controller with I18nSupport {
private val logger = org.slf4j.LoggerFactory.getLogger(this.getClass)
def list = silhouette.SecuredAction.async { implicit request =>
productDAO.all().map { products =>
logger.info(s"Calling index: products = ${products}")
Ok(views.html.products.list(request.identity, ProductForm.form, products))
}
}
def add = silhouette.SecuredAction.async { implicit request =>
ProductForm.form.bindFromRequest.fold(
form => Future.successful(BadRequest(views.html.products.details(request.identity,ProductForm.form))),
data => {
val product = Product(
ean = data.ean,
name = data.name,
description = data.description
)
productDAO.insert(product).map(_ => Redirect(routes.ProductController.list))
}
)
}
}
示例9: Startup
//设置package包名称以及导入依赖的类
package jobs
import java.util.UUID
import javax.inject.Inject
import javax.inject.Singleton
import com.mohiva.play.silhouette.api._
import com.mohiva.play.silhouette.api.repositories.AuthInfoRepository
import com.mohiva.play.silhouette.api.util.PasswordHasherRegistry
import com.mohiva.play.silhouette.impl.providers._
import models.{ User, UserMajor }
import models.MajorType._
import models.services.{ AuthTokenService, UserService, MajorService, UserMajorService }
import utils.auth.DefaultEnv
import shared.Util.reverseEffect
import play.api.libs.concurrent.Execution.Implicits._
@Singleton
class Startup @Inject() (
silhouette: Silhouette[DefaultEnv],
userService: UserService,
majorService: MajorService,
userMajorService: UserMajorService,
authInfoRepository: AuthInfoRepository,
authTokenService: AuthTokenService,
passwordHasherRegistry: PasswordHasherRegistry) {
//Create Sample user
//Major: ??2013, ??2013
//
val loginInfo = LoginInfo(CredentialsProvider.ID, "[email protected]")
val authInfo = passwordHasherRegistry.current.hash("asdf")
for {
fmOption <- majorService.find("??????", FirstMajor, 2013)
smOption <- majorService.find("????????", SecondMajor, 2013)
userData = User(
userID = UUID.fromString("42944783-ee3f-4a92-85b9-777c94d9dc5e"),
loginInfo = loginInfo,
email = Some("[email protected]"),
classYear = 2013,
semester = 5,
activated = true
)
userMajors = Seq(fmOption, smOption).filterNot(_.isEmpty).map(_.get).map(m => UserMajor(userData.userID, m.id))
savedUserMajors <- reverseEffect(userMajors.map(userMajor => userMajorService.save(userMajor)))
user <- userService.save(userData)
authInfo <- authInfoRepository.add(loginInfo, authInfo)
authToken <- authTokenService.create(user.userID)
} yield ()
}
示例10: UserController
//设置package包名称以及导入依赖的类
package controllers
import scala.concurrent.ExecutionContext
import scala.concurrent.Future
import com.mohiva.play.silhouette.api.Silhouette
import javax.inject.Inject
import javax.inject.Singleton
import models.DbModel
import models.Domain
import models.User
import models.services.DomainService
import models.services.UserService
import play.Logger
import play.api.i18n.I18nSupport
import play.api.i18n.MessagesApi
import play.api.mvc.Controller
import viewdata.PaginateData
import utils.auth.DefaultEnv
import controllers.actions.GeneralActions
import controllers.actions.MySecuredRequest
import viewdata._
import models.services.FileService
@Singleton
class UserController @Inject() (
val messagesApi: MessagesApi,
val domainService: DomainService,
val userService: UserService,
val generalActions: GeneralActions,
val fileService: FileService,
implicit val webJarAssets: WebJarAssets)(implicit exec: ExecutionContext)
extends Controller with I18nSupport {
def list(page: Int) =
generalActions.MySecuredAction.async { implicit mySecuredRequest =>
val responses = for {
userListData <- userService.getUserList(page, mySecuredRequest.activeDomain.map(activeDomain =>
activeDomain.allowedUsers).getOrElse(Set()))
} yield Ok(views.html.users.list(userListData, UserStatus(Some(mySecuredRequest.identity), mySecuredRequest.activeDomain)))
responses recover {
case e => InternalServerError(e.getMessage())
}
}
def show(uuid: String, page: Int) =
(generalActions.MySecuredAction andThen generalActions.UserAction(uuid)).async { implicit userRequest =>
val responses = for {
domainListData <- domainService.getDomainList(page, userRequest.user)
imageExists <- fileService.imageExists(uuid)
} yield Ok(views.html.users.show(userRequest.user, imageExists, domainListData,
UserStatus(Some(userRequest.identity), userRequest.activeDomain)))
responses recover {
case e => InternalServerError(e.getMessage())
}
}
}