From 86576299592d3f62a1ca29aa59710fa56b6cf18c Mon Sep 17 00:00:00 2001 From: Vincent Doba Date: Sun, 19 Oct 2014 15:39:07 +0200 Subject: [PATCH] [#34] begin implementation of silhouette, the project builds and application is launched --- app/Global.scala | 25 ++++- app/ProductionModule.scala | 80 ++++++++----- app/actors/ActivityActor.scala | 6 +- app/auth/WithDomain.scala | 25 ----- app/controllers/AccountsController.scala | 9 +- app/controllers/ActivityController.scala | 14 +-- app/controllers/Application.scala | 15 ++- .../AuthenticationController.scala | 25 +++++ app/controllers/ClientController.scala | 13 +-- app/controllers/ConfigurationController.scala | 14 +-- app/controllers/ContributionsController.scala | 14 +-- app/controllers/ExpenseController.scala | 16 +-- app/controllers/InvoiceController.scala | 14 +-- app/controllers/MembersController.scala | 13 +-- app/controllers/MovementsController.scala | 16 +-- app/controllers/WelcomeController.scala | 21 ++-- .../api/AccountApiController.scala | 25 ++--- .../api/ActivityApiController.scala | 34 +++--- .../api/AuthenticationApiController.scala | 11 ++ app/controllers/api/ClientApiController.scala | 23 ++-- .../api/ConfigurationApiController.scala | 19 ++-- .../api/ContributionsApiController.scala | 19 ++-- .../api/ExpenseApiController.scala | 21 ++-- .../api/InvoiceApiController.scala | 39 ++++--- .../api/MembersApiController.scala | 14 +-- .../api/MovementsApiController.scala | 15 ++- app/domain/accounts.scala | 13 +-- app/domain/activities.scala | 4 +- app/domain/invoices.scala | 1 - app/domain/users.scala | 29 +++++ app/engine/AffectationEngine.scala | 3 - app/service/MongoBasedUserService.scala | 105 +++--------------- app/util/pdf/PDF.scala | 2 +- app/util/pdf/drive.scala | 13 +-- app/views/activity/index.scala.html | 2 +- app/views/clients.scala.html | 2 +- app/views/configurations/index.scala.html | 2 +- app/views/contributions/index.scala.html | 2 +- app/views/expense/index.scala.html | 2 +- app/views/index.scala.html | 2 +- app/views/invoice/index.scala.html | 2 +- app/views/login.scala.html | 2 +- app/views/main.scala.html | 40 ++++--- app/views/members/index.scala.html | 2 +- app/views/movements/index.scala.html | 2 +- app/views/unauthorized.scala.html | 2 +- app/views/welcome/index.scala.html | 2 +- build.sbt | 4 +- conf/application.conf | 2 +- conf/routes | 10 +- conf/silhouette.conf | 23 ++++ .../modules/members/templates/dashboard.html | 2 +- test/domain/BasicProfileSerializerTest.scala | 16 +-- test/domain/MemberSerializerTest.scala | 4 +- .../testkit/AlwaysValidIdentityProvider.scala | 2 +- 55 files changed, 431 insertions(+), 406 deletions(-) delete mode 100644 app/auth/WithDomain.scala create mode 100644 app/controllers/AuthenticationController.scala create mode 100644 app/controllers/api/AuthenticationApiController.scala create mode 100644 app/domain/users.scala create mode 100644 conf/silhouette.conf diff --git a/app/Global.scala b/app/Global.scala index 18b348f..0a50b89 100644 --- a/app/Global.scala +++ b/app/Global.scala @@ -1,14 +1,21 @@ import actors.{ActivityActor, InvoiceActor} import akka.actor.Props +import com.mohiva.play.silhouette.api.SecuredSettings import com.softwaremill.macwire.MacwireMacros._ import controllers.api.ClientApiController +import controllers.routes import play.Logger +import play.api.i18n.{Lang, Messages} +import play.api.mvc.Results._ +import play.api.mvc.{RequestHeader, Result} import play.api.{Application, GlobalSettings} import play.libs.Akka import search.SimpleSearchEngine + import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.Future -object Global extends GlobalSettings { +object Global extends GlobalSettings with SecuredSettings { val wired = wiredInModule(new ProductionModule) override def onStart(app: Application) = { @@ -32,4 +39,20 @@ object Global extends GlobalSettings { override def getControllerInstance[A](controllerClass: Class[A]) = { wired.lookupSingleOrThrow(controllerClass) } + + override def onNotAuthenticated(request: RequestHeader, lang: Lang): Option[Future[Result]] = { + Some(Future.successful(Redirect(routes.AuthenticationController.signIn))) + } + /** + * Called when a user is authenticated but not authorized. + * + * As defined by RFC 2616, the status code of the response should be 403 Forbidden. + * + * @param request The request header. + * @param lang The currently selected language. + * @return The result to send to the client. + */ + override def onNotAuthorized(request: RequestHeader, lang: Lang): Option[Future[Result]] = { + Some(Future.successful(Redirect(routes.AuthenticationController.signIn).flashing("error" -> Messages("access.denied")))) + } } diff --git a/app/ProductionModule.scala b/app/ProductionModule.scala index 0196ad5..ccc3fb4 100644 --- a/app/ProductionModule.scala +++ b/app/ProductionModule.scala @@ -1,44 +1,68 @@ +import com.mohiva.play.silhouette.api.services.{AuthenticatorService, IdentityService} +import com.mohiva.play.silhouette.api.util.{Clock, FingerprintGenerator, IDGenerator, PlayHTTPLayer} +import com.mohiva.play.silhouette.api.{Environment, EventBus, Provider} +import com.mohiva.play.silhouette.impl.authenticators._ +import com.mohiva.play.silhouette.impl.providers.oauth2.GoogleProvider +import com.mohiva.play.silhouette.impl.providers.oauth2.state.{CookieStateProvider, CookieStateSettings} +import com.mohiva.play.silhouette.impl.providers.{OAuth2Settings, OAuth2StateProvider} +import com.mohiva.play.silhouette.impl.util.{DefaultFingerprintGenerator, SecureRandomIDGenerator} import com.softwaremill.macwire.MacwireMacros._ import controllers._ import controllers.api._ -import play.api.data.Form -import play.api.i18n.Lang -import play.api.mvc.{Flash, RequestHeader} +import domain.User +import play.api.Play +import play.api.Play.current import search.SimpleSearchEngine -import securesocial.controllers._ -import securesocial.core._ -import securesocial.core.providers._ import service.MongoBasedUserService -import scala.collection.immutable.ListMap - class ProductionModule { - object ProductionRuntimeEnvironment extends RuntimeEnvironment.Default[BasicProfile] { - override lazy val userService = new MongoBasedUserService - //override lazy val eventListeners = List(new MyEventListener()) - override lazy val providers = ListMap( - // oauth 2 client providers - include(new GoogleProvider(routes, cacheService, oauth2ClientFor(GoogleProvider.Google))) - ) - override lazy val viewTemplates = new ViewTemplates.Default(this){ - override def getLoginPage(form: Form[(String, String)], msg: Option[String])(implicit request: RequestHeader, lang: Lang)= { - views.html.login(form, msg)(request, lang, env, request.flash) - } - - override def getNotAuthorizedPage(implicit request : RequestHeader, lang : Lang) = { - views.html.unauthorized()(request, lang, env, request.flash) - } + object ProductionRuntimeEnvironment extends Environment[User, SessionAuthenticator] { + override def identityService: IdentityService[User] = new MongoBasedUserService + + override def authenticatorService: AuthenticatorService[SessionAuthenticator] = { + provideAuthenticatorService(new DefaultFingerprintGenerator(false)) + } + + def provideAuthenticatorService( + fingerprintGenerator: FingerprintGenerator): SessionAuthenticatorService = { + new SessionAuthenticatorService(SessionAuthenticatorSettings( + sessionKey = Play.configuration.getString("silhouette.authenticator.sessionKey").get, + encryptAuthenticator = Play.configuration.getBoolean("silhouette.authenticator.encryptAuthenticator").get, + useFingerprinting = Play.configuration.getBoolean("silhouette.authenticator.useFingerprinting").get, + authenticatorIdleTimeout = Play.configuration.getInt("silhouette.authenticator.authenticatorIdleTimeout"), + authenticatorExpiry = Play.configuration.getInt("silhouette.authenticator.authenticatorExpiry").get + ), fingerprintGenerator, Clock()) + } + + override def providers: Map[String, Provider] = { + val googleProvider = GoogleProvider(new PlayHTTPLayer(), provideOAuth2StateProvider(new SecureRandomIDGenerator()), OAuth2Settings( + authorizationURL = Play.configuration.getString("silhouette.google.authorizationURL").get, + accessTokenURL = Play.configuration.getString("silhouette.google.accessTokenURL").get, + redirectURL = Play.configuration.getString("silhouette.google.redirectURL").get, + clientID = Play.configuration.getString("silhouette.google.clientID").get, + clientSecret = Play.configuration.getString("silhouette.google.clientSecret").get, + scope = Play.configuration.getString("silhouette.google.scope"))) + Map(googleProvider.id -> googleProvider) + } + + def provideOAuth2StateProvider(idGenerator: IDGenerator): OAuth2StateProvider = { + new CookieStateProvider(CookieStateSettings( + cookieName = Play.configuration.getString("silhouette.oauth2StateProvider.cookieName").get, + cookiePath = Play.configuration.getString("silhouette.oauth2StateProvider.cookiePath").get, + cookieDomain = Play.configuration.getString("silhouette.oauth2StateProvider.cookieDomain"), + secureCookie = Play.configuration.getBoolean("silhouette.oauth2StateProvider.secureCookie").get, + httpOnlyCookie = Play.configuration.getBoolean("silhouette.oauth2StateProvider.httpOnlyCookie").get, + expirationTime = Play.configuration.getInt("silhouette.oauth2StateProvider.expirationTime").get + ), idGenerator, Clock()) } + + override def eventBus: EventBus = EventBus() } implicit val env = ProductionRuntimeEnvironment - // secure-social modules - lazy val loginPage = wire[LoginPage] - lazy val providerController = wire[ProviderController] - // application modules lazy val app = wire[Application] lazy val clientController = wire[ClientController] @@ -60,4 +84,6 @@ class ProductionModule { lazy val configurationApiController = wire[ConfigurationApiController] lazy val membersApiController = wire[MembersApiController] lazy val contributionsApiController = wire[ContributionsApiController] + lazy val authenticationController = wire[AuthenticationController] + lazy val authenticationApiController = wire[AuthenticationApiController] } diff --git a/app/actors/ActivityActor.scala b/app/actors/ActivityActor.scala index d193b21..45f2e6a 100644 --- a/app/actors/ActivityActor.scala +++ b/app/actors/ActivityActor.scala @@ -2,16 +2,16 @@ package actors import akka.actor.Actor import domain.{Activity, ActivitySerializer} +import play.Logger +import play.api.libs.json.Json import play.modules.reactivemongo.ReactiveMongoPlugin import play.modules.reactivemongo.json.collection.JSONCollection -import play.api.libs.json.Json -import play.Logger case class ActivityActor() extends Actor with ActivitySerializer { import play.api.Play.current - import scala.concurrent.ExecutionContext.Implicits.global +import scala.concurrent.ExecutionContext.Implicits.global def receive = { diff --git a/app/auth/WithDomain.scala b/app/auth/WithDomain.scala deleted file mode 100644 index 68b3a9a..0000000 --- a/app/auth/WithDomain.scala +++ /dev/null @@ -1,25 +0,0 @@ -package auth - -import play.api.{Play, Logger} -import play.api.mvc.RequestHeader -import securesocial.core.{BasicProfile, Authorization} - -case class WithDomain() extends Authorization[BasicProfile] { - - private val DOMAIN = Play.maybeApplication.flatMap(_.configuration.getString("application.auth.domain")).getOrElse("all") - - override def isAuthorized(user: BasicProfile, request: RequestHeader): Boolean = { - if (DOMAIN.equals("all")) { - true - } else { - user.email match { - case Some(email) => email.endsWith("@" + DOMAIN) - case None => { - Logger.warn("No email found in user profile, will refuse authorization") - false - } - } - } - - } -} diff --git a/app/controllers/AccountsController.scala b/app/controllers/AccountsController.scala index 246717f..da1ff38 100644 --- a/app/controllers/AccountsController.scala +++ b/app/controllers/AccountsController.scala @@ -1,13 +1,14 @@ package controllers -import play.api.mvc.Controller +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import securesocial.core.{BasicProfile, RuntimeEnvironment} -class AccountsController (override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller +class AccountsController (override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator] with MongoController - with securesocial.core.SecureSocial[BasicProfile]{ + { def collection = db.collection[JSONCollection]("accounts") diff --git a/app/controllers/ActivityController.scala b/app/controllers/ActivityController.scala index 4dacf58..de4fb37 100644 --- a/app/controllers/ActivityController.scala +++ b/app/controllers/ActivityController.scala @@ -1,15 +1,15 @@ package controllers -import auth.WithDomain -import securesocial.core.{BasicProfile, RuntimeEnvironment} -import play.api.mvc._ +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User -class ActivityController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller -with securesocial.core.SecureSocial[BasicProfile] { +class ActivityController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator] + { - def index = SecuredAction(WithDomain()) { + def index = SecuredAction { implicit request => - Ok(views.html.activity.index(request.user)) + Ok(views.html.activity.index(request.identity)) } } diff --git a/app/controllers/Application.scala b/app/controllers/Application.scala index 56ce896..35cd37c 100644 --- a/app/controllers/Application.scala +++ b/app/controllers/Application.scala @@ -1,15 +1,14 @@ package controllers -import auth.WithDomain -import play.api.mvc.Controller -import securesocial.core.{BasicProfile, RuntimeEnvironment} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User -class Application(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller - with securesocial.core.SecureSocial[BasicProfile] { +class Application(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] { - def index = SecuredAction(WithDomain()) { + def index = SecuredAction() { implicit request => - Ok(views.html.index(request.user)).flashing("welcome" -> "connard") + Ok(views.html.index(request.identity)).flashing("welcome" -> "connard") } } \ No newline at end of file diff --git a/app/controllers/AuthenticationController.scala b/app/controllers/AuthenticationController.scala new file mode 100644 index 0000000..40d2685 --- /dev/null +++ b/app/controllers/AuthenticationController.scala @@ -0,0 +1,25 @@ +package controllers + +import com.mohiva.play.silhouette.api.{LogoutEvent, Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User + +import scala.concurrent.Future + +class AuthenticationController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] { + + def signOut = SecuredAction.async { implicit request => + val result = Future.successful(Redirect(routes.Application.index)) + env.eventBus.publish(LogoutEvent(request.identity, request, request2lang)) + env.authenticatorService.discard(request.authenticator, result) + } + + def signIn() = play.mvc.Results.TODO + + def signUp() = play.mvc.Results.TODO + + def authenticate(provider: String) = play.mvc.Results.TODO + + def credentials() = play.mvc.Results.TODO +} diff --git a/app/controllers/ClientController.scala b/app/controllers/ClientController.scala index 6d6a3ba..e1e8600 100644 --- a/app/controllers/ClientController.scala +++ b/app/controllers/ClientController.scala @@ -1,9 +1,8 @@ package controllers -import auth.WithDomain +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator import domain._ -import play.api.mvc._ -import securesocial.core.{BasicProfile, RuntimeEnvironment} // Reactive Mongo imports @@ -11,13 +10,13 @@ import securesocial.core.{BasicProfile, RuntimeEnvironment} import play.modules.reactivemongo.MongoController -class ClientController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller +class ClientController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator] with InvoiceSerializer with MongoController - with securesocial.core.SecureSocial[BasicProfile] { + { - def clientsView = SecuredAction(WithDomain()) { + def clientsView = SecuredAction { implicit request => - Ok(views.html.clients(request.user)) + Ok(views.html.clients(request.identity)) } } diff --git a/app/controllers/ConfigurationController.scala b/app/controllers/ConfigurationController.scala index 8463cd5..847e745 100644 --- a/app/controllers/ConfigurationController.scala +++ b/app/controllers/ConfigurationController.scala @@ -1,15 +1,15 @@ package controllers -import auth.WithDomain -import play.api.mvc._ -import securesocial.core.{BasicProfile, RuntimeEnvironment} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User -class ConfigurationController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller - with securesocial.core.SecureSocial[BasicProfile] { +class ConfigurationController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator] + { - def index = SecuredAction(WithDomain()) { + def index = SecuredAction { implicit request => - Ok(views.html.configurations.index(request.user)) + Ok(views.html.configurations.index(request.identity)) } } \ No newline at end of file diff --git a/app/controllers/ContributionsController.scala b/app/controllers/ContributionsController.scala index 1941d56..2c4a100 100644 --- a/app/controllers/ContributionsController.scala +++ b/app/controllers/ContributionsController.scala @@ -1,14 +1,14 @@ package controllers -import auth.WithDomain -import play.api.mvc._ -import securesocial.core.{BasicProfile, RuntimeEnvironment} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User -class ContributionsController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller - with securesocial.core.SecureSocial[BasicProfile] { +class ContributionsController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator] + { - def index = SecuredAction(WithDomain()) { + def index = SecuredAction { implicit request => - Ok(views.html.contributions.index(request.user)) + Ok(views.html.contributions.index(request.identity)) } } \ No newline at end of file diff --git a/app/controllers/ExpenseController.scala b/app/controllers/ExpenseController.scala index 48645c1..61b70fc 100644 --- a/app/controllers/ExpenseController.scala +++ b/app/controllers/ExpenseController.scala @@ -1,16 +1,16 @@ package controllers -import auth.WithDomain -import play.api.mvc._ -import securesocial.core.{BasicProfile, RuntimeEnvironment} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User -class ExpenseController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller - with securesocial.core.SecureSocial[BasicProfile] { +class ExpenseController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] + { - def index = SecuredAction(WithDomain()) { + def index = SecuredAction { implicit request => - Ok(views.html.expense.index(request.user)) + Ok(views.html.expense.index(request.identity)) } } diff --git a/app/controllers/InvoiceController.scala b/app/controllers/InvoiceController.scala index f9d2c81..036a1f8 100644 --- a/app/controllers/InvoiceController.scala +++ b/app/controllers/InvoiceController.scala @@ -1,15 +1,15 @@ package controllers -import auth.WithDomain -import play.api.mvc._ -import securesocial.core.{BasicProfile, RuntimeEnvironment} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User -class InvoiceController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller - with securesocial.core.SecureSocial[BasicProfile] { +class InvoiceController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator] + { - def index = SecuredAction(WithDomain()) { + def index = SecuredAction { implicit request => - Ok(views.html.invoice.index(request.user)) + Ok(views.html.invoice.index(request.identity)) } } \ No newline at end of file diff --git a/app/controllers/MembersController.scala b/app/controllers/MembersController.scala index 5b04ec7..0cf0909 100644 --- a/app/controllers/MembersController.scala +++ b/app/controllers/MembersController.scala @@ -1,15 +1,14 @@ package controllers -import auth.WithDomain -import play.api.mvc._ -import securesocial.core.{BasicProfile, RuntimeEnvironment} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User -class MembersController(override implicit val env: RuntimeEnvironment[BasicProfile]) extends Controller - with securesocial.core.SecureSocial[BasicProfile] { +class MembersController(override implicit val env: Environment[User, SessionAuthenticator]) extends Silhouette[User, SessionAuthenticator] { - def index = SecuredAction(WithDomain()) { + def index = SecuredAction { implicit request => - Ok(views.html.members.index(request.user)) + Ok(views.html.members.index(request.identity)) } } \ No newline at end of file diff --git a/app/controllers/MovementsController.scala b/app/controllers/MovementsController.scala index a2f2b02..d0b1678 100644 --- a/app/controllers/MovementsController.scala +++ b/app/controllers/MovementsController.scala @@ -1,16 +1,16 @@ package controllers -import auth.WithDomain -import play.api.mvc.Controller -import securesocial.core.{BasicProfile, RuntimeEnvironment} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User -class MovementsController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller - with securesocial.core.SecureSocial[BasicProfile] { +class MovementsController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] + { - def index = SecuredAction(WithDomain()) { implicit request => - Ok(views.html.movements.index(request.user)) + def index = SecuredAction { implicit request => + Ok(views.html.movements.index(request.identity)) } } diff --git a/app/controllers/WelcomeController.scala b/app/controllers/WelcomeController.scala index 62d474f..b04ac16 100644 --- a/app/controllers/WelcomeController.scala +++ b/app/controllers/WelcomeController.scala @@ -1,23 +1,20 @@ package controllers -import auth.WithDomain -import domain.{LT, Human, Account} -import play.api.libs.json.Json -import play.api.mvc.{Controller, Action} -import play.modules.reactivemongo.json.collection.JSONCollection +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User import reactivemongo.api.DefaultDB -import securesocial.core.{BasicProfile, RuntimeEnvironment} -class WelcomeController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller - with securesocial.core.SecureSocial[BasicProfile] { +class WelcomeController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] + { - def index = SecuredAction(WithDomain()) { implicit request => - Ok(views.html.welcome.index(request.user)) + def index = SecuredAction { implicit request => + Ok(views.html.welcome.index(request.identity)) } - private def createDedicatedAccounts(profile: BasicProfile, db: DefaultDB) = { + private def createDedicatedAccounts(profile: User, db: DefaultDB) = { /*val account = Account("Revenue", Human(profile), affectable = true) val ltAccount = LT("LT") diff --git a/app/controllers/api/AccountApiController.scala b/app/controllers/api/AccountApiController.scala index 38b47df..5cce795 100644 --- a/app/controllers/api/AccountApiController.scala +++ b/app/controllers/api/AccountApiController.scala @@ -1,28 +1,25 @@ package controllers.api -import auth.WithDomain -import domain._ -import play.api.libs.json.Json -import play.api.mvc.Controller +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.{Account, Human, _} +import play.api.libs.json.{JsObject, Json} import play.modules.reactivemongo.MongoController -import securesocial.core.{RuntimeEnvironment, BasicProfile} -import domain.Account import play.modules.reactivemongo.json.collection.JSONCollection -import play.api.libs.json.JsObject -import domain.Human + import scala.concurrent.Future -class AccountApiController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller +class AccountApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController with AccountSerializer - with securesocial.core.SecureSocial[BasicProfile] { + { import scala.concurrent.ExecutionContext.Implicits.global val ACCOUNT = "accounts" - def findAll = SecuredAction(WithDomain()).async { + def findAll = SecuredAction.async { db .collection[JSONCollection](ACCOUNT) .find(Json.obj(), Json.obj()) @@ -31,7 +28,7 @@ class AccountApiController(override implicit val env: RuntimeEnvironment[BasicPr .map(accounts => Ok(Json.toJson(accounts))) } - def add = SecuredAction(WithDomain()).async(parse.urlFormEncoded) { implicit request => + def add = SecuredAction.async(parse.urlFormEncoded) { implicit request => val form = request.body @@ -40,7 +37,7 @@ class AccountApiController(override implicit val env: RuntimeEnvironment[BasicPr db .collection[JSONCollection]("users") .find(Json.obj("_id" -> Json.obj("$oid" -> userId.head))) - .one[BasicProfile] + .one[User] .flatMap { case Some(user) => val accounts = for { diff --git a/app/controllers/api/ActivityApiController.scala b/app/controllers/api/ActivityApiController.scala index 4417bea..c0c55b5 100644 --- a/app/controllers/api/ActivityApiController.scala +++ b/app/controllers/api/ActivityApiController.scala @@ -1,33 +1,31 @@ package controllers.api -import auth.WithDomain -import securesocial.core.RuntimeEnvironment -import play.api.mvc.Controller +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.{ActivityRequest, _} +import org.bouncycastle.util.encoders.Base64 +import play.api.libs.json.{JsError, JsObject, JsResult, Json} +import play.libs.Akka import play.modules.reactivemongo.MongoController -import domain._ -import util.pdf.GoogleDriveInteraction -import play.api.libs.json.{JsObject, Json, JsResult, JsError} -import securesocial.core.BasicProfile -import domain.ActivityRequest import play.modules.reactivemongo.json.collection.JSONCollection -import org.bouncycastle.util.encoders.Base64 -import scala.concurrent.Future import reactivemongo.bson.BSONObjectID -import play.libs.Akka +import util.pdf.GoogleDriveInteraction + +import scala.concurrent.Future -class ActivityApiController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller +class ActivityApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController with ActivitySerializer with GoogleDriveInteraction - with securesocial.core.SecureSocial[BasicProfile] { + { implicit val context = scala.concurrent.ExecutionContext.Implicits.global private val akkaSystem = Akka.system private lazy val activityActor = akkaSystem.actorSelection(akkaSystem / "activity") - def createAndPushCRA = SecuredAction(WithDomain()).async(parse.json) { implicit request => + def createAndPushCRA = SecuredAction.async(parse.json) { implicit request => request.body.validate(activityReqFormat) match { case errors: JsError => @@ -45,7 +43,7 @@ class ActivityApiController(override implicit val env: RuntimeEnvironment[BasicP } - def getPdfByCRA(oid: String) = SecuredAction(WithDomain()).async { + def getPdfByCRA(oid: String) = SecuredAction.async { db .collection[JSONCollection]("activities") .find(Json.obj("_id" -> Json.obj("$oid" -> oid)), Json.obj("pdfDocument" -> 1)) @@ -60,7 +58,7 @@ class ActivityApiController(override implicit val env: RuntimeEnvironment[BasicP } - def findAll = SecuredAction(WithDomain()).async { + def findAll = SecuredAction.async { db .collection[JSONCollection]("activities") .find(Json.obj(), Json.obj("activity" -> 1, "id" -> 1, "invoiceId" -> 1)) @@ -69,7 +67,7 @@ class ActivityApiController(override implicit val env: RuntimeEnvironment[BasicP .map(users => Ok(Json.toJson(users))) } - def delete(oid: String) = SecuredAction(WithDomain()).async { + def delete(oid: String) = SecuredAction.async { db .collection[JSONCollection]("activities") .remove(Json.obj("_id" -> Json.obj("$oid" -> oid))) diff --git a/app/controllers/api/AuthenticationApiController.scala b/app/controllers/api/AuthenticationApiController.scala new file mode 100644 index 0000000..c7a7660 --- /dev/null +++ b/app/controllers/api/AuthenticationApiController.scala @@ -0,0 +1,11 @@ +package controllers.api + +import com.mohiva.play.silhouette.api.{Silhouette, Environment} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User + +class AuthenticationApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] { + + def signUp() = play.mvc.Results.TODO +} diff --git a/app/controllers/api/ClientApiController.scala b/app/controllers/api/ClientApiController.scala index c3ac0a2..9d7e1d3 100644 --- a/app/controllers/api/ClientApiController.scala +++ b/app/controllers/api/ClientApiController.scala @@ -1,26 +1,25 @@ package controllers.api -import auth.WithDomain -import domain.{ClientRequest, Client, InvoiceSerializer} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.{Client, ClientRequest, InvoiceSerializer, User} import play.Logger -import play.api.libs.json.{JsObject, JsError, JsResult, Json} -import play.api.mvc.Controller +import play.api.libs.json.{JsError, JsObject, JsResult, Json} import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import reactivemongo.bson.{BSONDocument, BSONObjectID} +import reactivemongo.bson.BSONObjectID import search.SimpleSearchEngine -import securesocial.core.{BasicProfile, RuntimeEnvironment} import scala.concurrent.Future import scala.util.{Failure, Success} class ClientApiController(engine: SimpleSearchEngine) - (override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller + (override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController with InvoiceSerializer - with securesocial.core.SecureSocial[BasicProfile] { + { import play.modules.reactivemongo.json.BSONFormats._ import scala.concurrent.ExecutionContext.Implicits.global @@ -34,7 +33,7 @@ class ClientApiController(engine: SimpleSearchEngine) .collect[List]() } - def search(q: Option[String]) = SecuredAction(WithDomain()).async { + def search(q: Option[String]) = SecuredAction.async { implicit request => q match { case Some(query) => @@ -57,7 +56,7 @@ class ClientApiController(engine: SimpleSearchEngine) } } - def addClient() = SecuredAction(WithDomain()).async(parse.json) { implicit request => + def addClient() = SecuredAction.async(parse.json) { implicit request => request.body.validate(clientRequestFormat) match { case errors:JsError => Future(BadRequest(errors.toString).as("application/json")) @@ -82,7 +81,7 @@ class ClientApiController(engine: SimpleSearchEngine) } } - def modifyClient(id: String) = SecuredAction(WithDomain())(parse.json) { + def modifyClient(id: String) = SecuredAction(parse.json) { implicit request => val clientJsonModified = request.body val idSelector = Json.obj("_id" -> BSONObjectID(id)) diff --git a/app/controllers/api/ConfigurationApiController.scala b/app/controllers/api/ConfigurationApiController.scala index a490681..ffadf0d 100644 --- a/app/controllers/api/ConfigurationApiController.scala +++ b/app/controllers/api/ConfigurationApiController.scala @@ -1,24 +1,23 @@ package controllers.api -import auth.WithDomain -import domain.{RatioConfiguration, RatioConfigurationSerializer} -import play.api.libs.json.{JsError, JsSuccess, JsObject, Json} -import play.api.mvc.{Action, Controller} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.{RatioConfiguration, RatioConfigurationSerializer, User} +import play.api.libs.json.{JsError, JsObject, JsSuccess, Json} import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import securesocial.core.{BasicProfile, RuntimeEnvironment} import scala.concurrent.Future -class ConfigurationApiController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller +class ConfigurationApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController with RatioConfigurationSerializer - with securesocial.core.SecureSocial[BasicProfile] { + { import scala.concurrent.ExecutionContext.Implicits.global - def getCurrentConfiguration = SecuredAction(WithDomain()).async { + def getCurrentConfiguration = SecuredAction.async { db .collection[JSONCollection]("configuration") .find(Json.obj()) @@ -27,7 +26,7 @@ class ConfigurationApiController(override implicit val env: RuntimeEnvironment[B .map(configs => Ok(Json.toJson(configs))) } - def agoraCallback = SecuredAction(WithDomain()).async(parse.json) { implicit request => + def agoraCallback = SecuredAction.async(parse.json) { implicit request => request.body.validate(ratioConfigFormatter) match { case obj: JsSuccess[RatioConfiguration] => db diff --git a/app/controllers/api/ContributionsApiController.scala b/app/controllers/api/ContributionsApiController.scala index 240b27b..807f49a 100644 --- a/app/controllers/api/ContributionsApiController.scala +++ b/app/controllers/api/ContributionsApiController.scala @@ -1,25 +1,24 @@ package controllers.api -import auth.WithDomain -import domain.{Contribution, ContributionSerializer, Client} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.{Contribution, ContributionSerializer, User} import play.Logger -import play.api.libs.json.{JsResult, JsError, JsObject, Json} -import play.api.mvc.Controller +import play.api.libs.json.{JsError, JsObject, JsResult, Json} import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import securesocial.core.{BasicProfile, RuntimeEnvironment} import scala.concurrent.Future -class ContributionsApiController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller +class ContributionsApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController with ContributionSerializer - with securesocial.core.SecureSocial[BasicProfile] { + { import scala.concurrent.ExecutionContext.Implicits.global - def findByType(mayContributionType: Option[String]) = SecuredAction(WithDomain()).async { + def findByType(mayContributionType: Option[String]) = SecuredAction.async { db .collection[JSONCollection]("contributions") .find(mayContributionType.map(contributionType => Json.obj("type" -> contributionType)).getOrElse(Json.obj())) @@ -28,7 +27,7 @@ class ContributionsApiController(override implicit val env: RuntimeEnvironment[B .map(contributions => Ok(Json.toJson(contributions))) } - def createContribution() = SecuredAction(WithDomain()).async(parse.json) { implicit request => + def createContribution() = SecuredAction.async(parse.json) { implicit request => request.body.validate(contributionFormatter) match { case errors:JsError => Future(BadRequest(errors.toString).as("application/json")) diff --git a/app/controllers/api/ExpenseApiController.scala b/app/controllers/api/ExpenseApiController.scala index 0c406a3..290e55c 100644 --- a/app/controllers/api/ExpenseApiController.scala +++ b/app/controllers/api/ExpenseApiController.scala @@ -1,31 +1,30 @@ package controllers.api -import java.io.{BufferedInputStream, FileInputStream, File} +import java.io.{BufferedInputStream, FileInputStream} -import auth.WithDomain +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator import domain._ import org.bouncycastle.util.encoders.Base64 import org.joda.time.DateTime import play.api.libs.Files.TemporaryFile import play.api.libs.json.{JsObject, Json} -import play.api.mvc.{Result, MultipartFormData, Controller} +import play.api.mvc.{MultipartFormData, Result} import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import securesocial.core.{BasicProfile, RuntimeEnvironment} import scala.concurrent.Future -import scala.io.Source -class ExpenseApiController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller +class ExpenseApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController with AccountSerializer with ExpenseSerializer - with securesocial.core.SecureSocial[BasicProfile] { + { implicit val context = scala.concurrent.ExecutionContext.Implicits.global - def createExpense = SecuredAction(WithDomain()).async(parse.multipartFormData) { implicit request => + def createExpense = SecuredAction.async(parse.multipartFormData) { implicit request => request.body.asFormUrlEncoded.get("accountId") match { case Some(accountList) => val accountId = accountList.head @@ -43,7 +42,7 @@ class ExpenseApiController(override implicit val env: RuntimeEnvironment[BasicPr } - def findAll = SecuredAction(WithDomain()).async { + def findAll = SecuredAction.async { db .collection[JSONCollection]("expenses") .find(Json.obj(), Json.obj()) @@ -52,7 +51,7 @@ class ExpenseApiController(override implicit val env: RuntimeEnvironment[BasicPr .map(expenses => Ok(Json.toJson(expenses))) } - def getAttachment(oid: String) = SecuredAction(WithDomain()).async { implicit request => + def getAttachment(oid: String) = SecuredAction.async { implicit request => db .collection[JSONCollection]("expenses") .find(Json.obj("_id" -> Json.obj("$oid" -> oid))) diff --git a/app/controllers/api/InvoiceApiController.scala b/app/controllers/api/InvoiceApiController.scala index cd74e02..b216037 100644 --- a/app/controllers/api/InvoiceApiController.scala +++ b/app/controllers/api/InvoiceApiController.scala @@ -1,35 +1,34 @@ package controllers.api -import auth.WithDomain +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator import domain._ import engine.AffectationEngine import org.bouncycastle.util.encoders.Base64 import org.joda.time.DateTime import play.Logger import play.api.libs.json._ -import play.api.mvc.{Action, Controller} import play.libs.Akka import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import securesocial.core.{BasicProfile, RuntimeEnvironment} import scala.concurrent.Future -class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller +class InvoiceApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController with InvoiceSerializer with AccountSerializer with AffectationSerializer with AffectationEngine - with securesocial.core.SecureSocial[BasicProfile] { + { implicit val context = scala.concurrent.ExecutionContext.Implicits.global private val akkaSystem = Akka.system private lazy val invoiceActor = akkaSystem.actorSelection(akkaSystem / "invoice") - def createAndPushInvoice = SecuredAction(WithDomain()) { implicit request => + def createAndPushInvoice = SecuredAction { implicit request => request.body.asJson match { case Some(json) => json.validate(invoiceReqFormat) match { @@ -48,8 +47,8 @@ class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicPr val generatedPdfDocument = invoiceRequestToPdfBytes(invoiceRequest) if (shouldUpload) { - val status = domain.Status("created", DateTime.now(), request.user.email.get) - val accessToken: String = request.user.oAuth2Info.map( _.accessToken ).get + val status = domain.Status("created", DateTime.now(), request.identity.email.get) + val accessToken: String = request.identity.oAuth2Info.map( _.accessToken ).get invoiceActor ! ( Invoice(invoiceRequest, Attachment("application/pdf", stub = false, generatedPdfDocument), List(status), status), accessToken @@ -63,21 +62,21 @@ class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicPr } } - def getLastInvoiceNumber = SecuredAction(WithDomain()).async { + def getLastInvoiceNumber = SecuredAction.async { db.collection[JSONCollection]("invoiceNumber") .find(Json.obj()) .one[InvoiceNumber] .map(mayBeObj => Ok(Json.toJson(mayBeObj.get))) } - def reset(value: Int) = SecuredAction(WithDomain()) { + def reset(value: Int) = SecuredAction { Logger.info(s"reset value of invoiceNumber to $value") db.collection[JSONCollection]("invoiceNumber") .update(Json.obj(), Json.toJson(InvoiceNumber(value))) Ok } - def getCanceledInvoices = SecuredAction(WithDomain()).async { implicit request => + def getCanceledInvoices = SecuredAction.async { implicit request => db .collection[JSONCollection]("invoices") .find(Json.obj("canceled" -> true), Json.obj("invoice" -> 1, "statuses" -> 1)) @@ -86,7 +85,7 @@ class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicPr .map(invoices => Ok(Json.toJson(invoices))) } - def findByStatus(status: Option[String], exclude: Option[Boolean]) = SecuredAction(WithDomain()).async { implicit request => + def findByStatus(status: Option[String], exclude: Option[Boolean]) = SecuredAction.async { implicit request => val selector = (status: String) => { val selectorField = if(List("paid", "unpaid") contains status) @@ -111,13 +110,13 @@ class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicPr .map(invoices => Ok(Json.toJson(invoices))) } - def addStatusToInvoice(oid: String, status: String) = SecuredAction(WithDomain()) { implicit request => - setStatusToInvoice(oid, status, request.user.email.get) + def addStatusToInvoice(oid: String, status: String) = SecuredAction { implicit request => + setStatusToInvoice(oid, status, request.identity.email.get) Ok } - def cancelInvoice(oid: String) = SecuredAction(WithDomain()).async(parse.json) { implicit request => + def cancelInvoice(oid: String) = SecuredAction.async(parse.json) { implicit request => val selector = Json.obj("_id" -> Json.obj("$oid" -> oid)) db .collection[JSONCollection]("invoices") @@ -127,7 +126,7 @@ class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicPr case (mayBeInvoice: Option[Invoice]) => mayBeInvoice match { case Some(invoice) => { Logger.info("Loaded invoice, canceling...") - val lastStatus = Json.toJson(domain.Status("canceled", DateTime.now(), request.user.email.get)) + val lastStatus = Json.toJson(domain.Status("canceled", DateTime.now(), request.identity.email.get)) val generatedPdfDocument = addCanceledWatermark(invoice.pdfDocument.data) @@ -152,7 +151,7 @@ class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicPr } } - def affectToAccount(oid: String) = SecuredAction(WithDomain()).async(parse.json) { implicit request => + def affectToAccount(oid: String) = SecuredAction.async(parse.json) { implicit request => db .collection[JSONCollection]("invoices") .find(Json.obj("_id" -> Json.obj("$oid" -> oid))) @@ -175,7 +174,7 @@ class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicPr futureHasAtLeastOneFailure.map { case true => InternalServerError case false => - setStatusToInvoice(oid, "affected", request.user.email.get) + setStatusToInvoice(oid, "affected", request.identity.email.get) Ok } }).getOrElse(Future(InternalServerError)) @@ -183,7 +182,7 @@ class InvoiceApiController(override implicit val env: RuntimeEnvironment[BasicPr } } - def getPdfByInvoice(oid: String) = SecuredAction(WithDomain()).async { + def getPdfByInvoice(oid: String) = SecuredAction.async { db .collection[JSONCollection]("invoices") .find(Json.obj("_id" -> Json.obj("$oid" -> oid)), Json.obj("pdfDocument" -> 1)) diff --git a/app/controllers/api/MembersApiController.scala b/app/controllers/api/MembersApiController.scala index c754068..c12a861 100644 --- a/app/controllers/api/MembersApiController.scala +++ b/app/controllers/api/MembersApiController.scala @@ -1,20 +1,20 @@ package controllers.api -import auth.WithDomain +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain.User import play.api.libs.json.{JsObject, Json} -import play.api.mvc.Controller import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import securesocial.core.{BasicProfile, RuntimeEnvironment} -class MembersApiController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller +class MembersApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController - with securesocial.core.SecureSocial[BasicProfile] { + { import scala.concurrent.ExecutionContext.Implicits.global - def findAll = SecuredAction(WithDomain()).async { + def findAll = SecuredAction.async { db .collection[JSONCollection]("users") .find(Json.obj()) diff --git a/app/controllers/api/MovementsApiController.scala b/app/controllers/api/MovementsApiController.scala index 2ca664e..a65c6e2 100644 --- a/app/controllers/api/MovementsApiController.scala +++ b/app/controllers/api/MovementsApiController.scala @@ -1,25 +1,24 @@ package controllers.api -import auth.WithDomain -import domain.{Account, AccountSerializer, Movement, MovementsSerializer} +import com.mohiva.play.silhouette.api.{Environment, Silhouette} +import com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator +import domain._ import play.api.libs.json.{JsObject, Json} -import play.api.mvc.Controller import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import securesocial.core.{BasicProfile, RuntimeEnvironment} import scala.concurrent.Future -class MovementsApiController(override implicit val env: RuntimeEnvironment[BasicProfile]) - extends Controller +class MovementsApiController(override implicit val env: Environment[User, SessionAuthenticator]) + extends Silhouette[User, SessionAuthenticator] with MongoController with AccountSerializer with MovementsSerializer - with securesocial.core.SecureSocial[BasicProfile] { + { import scala.concurrent.ExecutionContext.Implicits.global - def findAll = SecuredAction(WithDomain()).async { + def findAll = SecuredAction.async { db .collection[JSONCollection]("movements") .find(Json.obj()) diff --git a/app/domain/accounts.scala b/app/domain/accounts.scala index 5cda7db..56660cb 100644 --- a/app/domain/accounts.scala +++ b/app/domain/accounts.scala @@ -1,27 +1,24 @@ package domain -import securesocial.core._ +import com.mohiva.play.silhouette.impl.providers.OAuth2Info import julienrf.variants.Variants import play.api.libs.json.{Format, Json} sealed trait Member -case class Human(user: BasicProfile) extends Member +case class Human(user: User) extends Member case class LT(underlying: String) extends Member case class Account(name: String, stakeholder: Member, affectable: Boolean = false) -trait BasicProfileSerializer { - implicit val passwordInfoFormatter = Json.format[PasswordInfo] - implicit val oAuth1InfoFormatter = Json.format[OAuth1Info] +trait UserSerializer { implicit val oAuth2InfoFormatter = Json.format[OAuth2Info] - implicit val authMethodFormatter = Json.format[AuthenticationMethod] - implicit val basicProfileFormatter = Json.format[BasicProfile] + implicit val userFormatter = Json.format[User] } -trait MemberSerializer extends BasicProfileSerializer { +trait MemberSerializer extends UserSerializer { // Should explicitly declare Format[Member] type in order to avoid https://github.com/LateralThoughts/systemi/issues/24 implicit val memberFormatter: Format[Member] = Variants.format[Member] } diff --git a/app/domain/activities.scala b/app/domain/activities.scala index 1e33393..43481e1 100644 --- a/app/domain/activities.scala +++ b/app/domain/activities.scala @@ -1,10 +1,10 @@ package domain import org.joda.time.LocalDate -import play.api.libs.json.{Reads, Json} import org.joda.time.format.ISODateTimeFormat -import util.pdf.PDF +import play.api.libs.json.{Json, Reads} import reactivemongo.bson.BSONObjectID +import util.pdf.PDF case class ActivityDay(day: LocalDate, halfUp : Boolean, halfDown: Boolean) diff --git a/app/domain/invoices.scala b/app/domain/invoices.scala index 15e3ccb..614306f 100644 --- a/app/domain/invoices.scala +++ b/app/domain/invoices.scala @@ -5,7 +5,6 @@ import java.awt.Color import _root_.util.pdf.PDF import org.bouncycastle.util.encoders.Base64 import org.joda.time.DateTime -import play.Logger import play.api.libs.json._ case class InvoiceNumber(value: Int) { diff --git a/app/domain/users.scala b/app/domain/users.scala new file mode 100644 index 0000000..29ede80 --- /dev/null +++ b/app/domain/users.scala @@ -0,0 +1,29 @@ +package domain + + +import java.util.UUID + +import com.mohiva.play.silhouette.api.{Identity, LoginInfo} +import com.mohiva.play.silhouette.impl.providers.{OAuth2Info, SocialProfile} + +/** + * The user object. + * + * @param id The unique ID of the user. + * @param loginInfo The linked login info. + * @param firstName Maybe the first name of the authenticated user. + * @param lastName Maybe the last name of the authenticated user. + * @param fullName Maybe the full name of the authenticated user. + * @param email Maybe the email of the authenticated provider. + * @param avatarURL Maybe the avatar URL of the authenticated provider. + */ +case class User( + id: UUID, + loginInfo: LoginInfo, + firstName: Option[String] = None, + lastName: Option[String] = None, + fullName: Option[String] = None, + email: Option[String] = None, + avatarURL: Option[String] = None, + oAuth2Info: Option[OAuth2Info] = None, + gender: Option[String] = None) extends SocialProfile with Identity \ No newline at end of file diff --git a/app/engine/AffectationEngine.scala b/app/engine/AffectationEngine.scala index ac5bad3..d0c6412 100644 --- a/app/engine/AffectationEngine.scala +++ b/app/engine/AffectationEngine.scala @@ -1,13 +1,10 @@ package engine import domain._ -import play.Logger import play.api.libs.json.Json import play.modules.reactivemongo.MongoController import play.modules.reactivemongo.json.collection.JSONCollection -import scala.concurrent.Future - trait AffectationEngine extends RatioConfigurationSerializer with InvoiceSerializer diff --git a/app/service/MongoBasedUserService.scala b/app/service/MongoBasedUserService.scala index 3c0e0e8..8a91f83 100644 --- a/app/service/MongoBasedUserService.scala +++ b/app/service/MongoBasedUserService.scala @@ -1,56 +1,43 @@ package service +import com.mohiva.play.silhouette.api.LoginInfo +import com.mohiva.play.silhouette.api.services.IdentityService +import domain._ import play.Logger import play.api.Play.current -import reactivemongo.api.DefaultDB -import securesocial.core._ -import securesocial.core.services._ -import securesocial.core.providers.{MailToken} +import play.api.libs.json.Json +import play.modules.reactivemongo.ReactiveMongoPlugin +import play.modules.reactivemongo.json.collection.JSONCollection import scala.concurrent.Future -import play.modules.reactivemongo.json.collection.JSONCollection -import play.modules.reactivemongo.ReactiveMongoPlugin -import domain._ -import play.api.libs.json.Json class MongoBasedUserService() - extends UserService[BasicProfile] - with BasicProfileSerializer { + extends IdentityService[User] + with UserSerializer { import scala.concurrent.ExecutionContext.Implicits.global /** * Finds a SocialUser that matches the specified id * - * @param providerId the provider id - * @param userId the user id + * @param loginInfo object containing the provider id and the user id * @return an optional profile */ - def find(providerId: String, userId: String): Future[Option[BasicProfile]]= { + def retrieve(loginInfo: LoginInfo): Future[Option[User]]= { val db = ReactiveMongoPlugin.db db.collection[JSONCollection]("users") - .find(Json.obj("providerId" -> providerId, "userId" -> userId)) - .one[BasicProfile] + .find(Json.obj("providerId" -> loginInfo.providerID, "userId" -> loginInfo.providerKey)) + .one[User] } - /** - * Finds a profile by email and provider - * - * @param email - the user email - * @param providerId - the provider id - * @return an optional profile - */ - def findByEmailAndProvider(email: String, providerId: String): Future[Option[BasicProfile]]= ??? - /** * Saves a profile. This method gets called when a user logs in, registers or changes his password. * This is your chance to save the user information in your backing store. * * @param profile the user profile - * @param mode a mode that tells you why the save method was called */ - def save(profile: BasicProfile, mode: SaveMode): Future[BasicProfile] = { + def save(profile: User): Future[User] = { val db = ReactiveMongoPlugin.db @@ -67,70 +54,4 @@ class MongoBasedUserService() Future(profile) } - /** - * Links the current user to another profile - * - * @param current The current user instance - * @param to the profile that needs to be linked to - */ - def link(current: BasicProfile, to: BasicProfile): Future[BasicProfile]= ??? - - /** - * Returns an optional PasswordInfo instance for a given user - * - * @param user a user instance - * @return returns an optional PasswordInfo - */ - def passwordInfoFor(user: BasicProfile): Future[Option[PasswordInfo]]= ??? - - /** - * Updates the PasswordInfo for a given user - * - * @param user a user instance - * @param info the password info - * @return - */ - def updatePasswordInfo(user: BasicProfile, info: PasswordInfo): Future[Option[BasicProfile]]= ??? - - /** - * Saves a mail token. This is needed for users that - * are creating an account in the system or trying to reset a password - * - * Note: If you do not plan to use the UsernamePassword provider just provide en empty - * implementation - * - * @param token The token to save - */ - def saveToken(token: MailToken): Future[MailToken] = ??? - - /** - * Finds a token - * - * Note: If you do not plan to use the UsernamePassword provider just provide en empty - * implementation - * - * @param token the token id - * @return - */ - def findToken(token: String): Future[Option[MailToken]]= ??? - - /** - * Deletes a token - * - * Note: If you do not plan to use the UsernamePassword provider just provide en empty - * implementation - * - * @param uuid the token id - */ - def deleteToken(uuid: String): Future[Option[MailToken]]= ??? - - /** - * Deletes all expired tokens - * - * Note: If you do not plan to use the UsernamePassword provider just provide en empty - * implementation - * - */ - def deleteExpiredTokens() {} - } \ No newline at end of file diff --git a/app/util/pdf/PDF.scala b/app/util/pdf/PDF.scala index f50eef8..d85c6e5 100644 --- a/app/util/pdf/PDF.scala +++ b/app/util/pdf/PDF.scala @@ -4,8 +4,8 @@ import java.awt.Color import java.io.{ByteArrayOutputStream, File, IOException, InputStream, OutputStream, StringReader, StringWriter} import java.net.{MalformedURLException, URL} +import com.lowagie.text.pdf.{BaseFont, PdfGState, PdfReader, PdfStamper} import com.lowagie.text.{Element, Image} -import com.lowagie.text.pdf.{PdfGState, PdfStamper, PdfReader, BaseFont} import org.w3c.tidy.Tidy import org.xhtmlrenderer.pdf.{ITextFSImage, ITextFontResolver, ITextOutputDevice, ITextRenderer, ITextUserAgent} import org.xhtmlrenderer.resource.{CSSResource, ImageResource, XMLResource} diff --git a/app/util/pdf/drive.scala b/app/util/pdf/drive.scala index 825349b..e7e0fa2 100644 --- a/app/util/pdf/drive.scala +++ b/app/util/pdf/drive.scala @@ -1,17 +1,14 @@ package util.pdf -import domain.{NextInvoiceNumbersParser, InvoiceRequest} -import com.google.api.client.http.ByteArrayContent -import com.google.api.services.drive.model._ +import java.util.Arrays + import com.google.api.client.googleapis.auth.oauth2.GoogleCredential +import com.google.api.client.http.ByteArrayContent import com.google.api.client.http.javanet.NetHttpTransport import com.google.api.client.json.jackson.JacksonFactory import com.google.api.services.drive.Drive -import play.api.mvc.RequestHeader - -import play.api.libs.oauth._ -import java.util.Arrays - +import com.google.api.services.drive.model._ +import domain.{InvoiceRequest, NextInvoiceNumbersParser} import play.api._ trait GoogleDriveInteraction extends NextInvoiceNumbersParser { diff --git a/app/views/activity/index.scala.html b/app/views/activity/index.scala.html index a79730e..8493870 100644 --- a/app/views/activity/index.scala.html +++ b/app/views/activity/index.scala.html @@ -1,4 +1,4 @@ -@(user: securesocial.core.BasicProfile)(implicit request: play.api.mvc.RequestHeader, env: securesocial.core.RuntimeEnvironment[securesocial.core.BasicProfile], flash: Flash) +@(user: domain.User)(implicit request: play.api.mvc.RequestHeader, env: com.mohiva.play.silhouette.api.Environment[domain.User, com.mohiva.play.silhouette.impl.authenticators.SessionAuthenticator], flash: Flash) @main(user, "activity") {