Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Converted to multi-projects #2

Open
wants to merge 13 commits into
base: master
Choose a base branch
from
19 changes: 12 additions & 7 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
Silhouette Slick Seed Template
==============================
MultiProject Silhouette Slick Seed Template
===========================================

This is a fork of the official Silhouette Seed project. If you want to have a first look at Silhouette, I suggest you have a look at the [official project](https://github.com/mohiva/play-silhouette-seed).
This is a fork a the fork of the official Silhouette Seed project.
This fork shows how to work with Silhouette & Slick as a subproject in a multiproject SBT project.
If you want to have a first look at Silhouette, I suggest you have a look at the [official project](https://github.com/mohiva/play-silhouette-seed).

The Silhouette Seed project is an Activator template which shows how [Silhouette](https://github.com/mohiva/play-silhouette) can be implemented in a Play Framework application. It's a starting point which can be extended to fit your needs.
The Silhouette Seed project is an Activator template which shows how [Silhouette](https://github.com/mohiva/play-silhouette) can be implemented in a Play Framework application.
It's a starting point which can be extended to fit your needs.
It uses the [play-slick](https://github.com/playframework/play-slick) library for database access.

## Example
Expand All @@ -30,13 +33,15 @@ Currently, there is no live example of this template.

## Documentation

Consulate the [Silhouette documentation](http://silhouette.mohiva.com/docs) for more information. If you need help with the integration of Silhouette into your project, don't hesitate and ask questions in our [mailing list](https://groups.google.com/forum/#!forum/play-silhouette) or on [Stack Overflow](http://stackoverflow.com/questions/tagged/playframework).
Consult the [Silhouette documentation](http://silhouette.mohiva.com/docs) for more information. If you need help with the integration of Silhouette into your project, don't hesitate and ask questions in our [mailing list](https://groups.google.com/forum/#!forum/play-silhouette) or on [Stack Overflow](http://stackoverflow.com/questions/tagged/playframework).

### Slick

The template stores all authentication information in a database via [Slick](http://slick.typesafe.com/) It uses an in memory [H2](www.h2database.com/) database by default.
The template stores all authentication information in a database via [Slick](http://slick.typesafe.com/)
It uses an in memory [H2](www.h2database.com/) database by default.

In order to use another database supported by Slick, you need to change the driver in your application.conf and add the corresponding JDBC driver to your dependencies. The [Play Slick documentation](https://www.playframework.com/documentation/2.4.x/PlaySlick) has more information about database configuration.
In order to use another database supported by Slick, you need to change the driver in your application.conf and add the corresponding JDBC driver to your dependencies.
The [Play Slick documentation](https://www.playframework.com/documentation/2.4.x/PlaySlick) has more information about database configuration.

## Activator

Expand Down
55 changes: 10 additions & 45 deletions build.sbt
Original file line number Diff line number Diff line change
@@ -1,57 +1,22 @@
import scalariform.formatter.preferences._

name := "play-silhouette-slick-seed"

version := "3.0.1"

scalaVersion := "2.11.7"

resolvers := ("Atlassian Releases" at "https://maven.atlassian.com/public/") +: resolvers.value

resolvers += "scalaz-bintray" at "https://dl.bintray.com/scalaz/releases"

resolvers += Resolver.sonatypeRepo("snapshots")
Common.settings

libraryDependencies ++= Seq(
"com.mohiva" %% "play-silhouette" % "3.0.2",
"org.webjars" %% "webjars-play" % "2.4.0-1",
"net.codingwell" %% "scala-guice" % "4.0.0",
"net.ceedubs" %% "ficus" % "1.1.2",
"com.adrianhurt" %% "play-bootstrap3" % "0.4.4-P24",
"com.mohiva" %% "play-silhouette-testkit" % "3.0.2" % "test",
specs2 % Test,
"com.typesafe.play" %% "play-slick" % "1.0.1",
"com.typesafe.play" %% "play-slick-evolutions" % "1.0.1",
"com.h2database" % "h2" % "1.4.188",
cache,
evolutions,
filters
)
lazy val admin = (project in file("modules/admin")).enablePlugins(PlayScala).dependsOn(auth)
lazy val open = (project in file("modules/open")) .enablePlugins(PlayScala).dependsOn(auth)
lazy val auth = (project in file("modules/auth")) .enablePlugins(PlayScala)

lazy val root = (project in file(".")).enablePlugins(PlayScala)
lazy val root = (project in file("."))
.enablePlugins(PlayScala)
.aggregate(admin, open)
.dependsOn(admin, open)

routesGenerator := InjectedRoutesGenerator

scalacOptions ++= Seq(
"-deprecation", // Emit warning and location for usages of deprecated APIs.
"-feature", // Emit warning and location for usages of features that should be imported explicitly.
"-unchecked", // Enable additional warnings where generated code depends on assumptions.
"-Xfatal-warnings", // Fail the compilation if there are any warnings.
"-Xlint", // Enable recommended additional warnings.
"-Ywarn-adapted-args", // Warn if an argument list is modified to match the receiver.
"-Ywarn-dead-code", // Warn when dead code is identified.
"-Ywarn-inaccessible", // Warn about inaccessible types in method signatures.
"-Ywarn-nullary-override", // Warn when non-nullary overrides nullary, e.g. def foo() over def foo.
"-Ywarn-numeric-widen" // Warn when numerics are widened.
)

//********************************************************
// Scalariform settings
//********************************************************

import scalariform.formatter.preferences._
defaultScalariformSettings

ScalariformKeys.preferences := ScalariformKeys.preferences.value
.setPreference(FormatXml, false)
.setPreference(DoubleIndentClassDeclaration, false)
.setPreference(PreserveDanglingCloseParenthesis, true)

3 changes: 3 additions & 0 deletions conf/messages
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ access.denied = Access denied!
user.exists = There already exists a user with this email!
could.not.authenticate = Could not authenticate with social provider! Please try again!

admin.title = Admin Supbroject - Home
open.title = Open Subproject - Home
home.title = Silhouette - Home
sign.up.title = Silhouette - Sign Up
sign.in.title = Silhouette - Sign In
Expand All @@ -15,6 +17,7 @@ welcome.signed.in = Welcome, you are now signed in!
sign.up.account = Sign up for a new account
sign.in.credentials = Sign in with your credentials

admin = Admin
error = Error!
home = Home
first.name = First name
Expand Down
20 changes: 7 additions & 13 deletions conf/routes
Original file line number Diff line number Diff line change
@@ -1,16 +1,10 @@
# Routes
# This file defines all application routes (Higher priority routes first)
# ~~~~
# Define all application routes (Higher priority routes first)

# Home page
GET / controllers.ApplicationController.index
GET /signIn controllers.ApplicationController.signIn
GET /signUp controllers.ApplicationController.signUp
GET /signOut controllers.ApplicationController.signOut
GET /authenticate/:provider controllers.SocialAuthController.authenticate(provider)
POST /authenticate/credentials controllers.CredentialsAuthController.authenticate
POST /signUp controllers.SignUpController.signUp
# Delegate routes to subprojects
-> / open.Routes
-> /admin admin.Routes
-> /auth auth.Routes

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /webjars/*file controllers.WebJarAssets.at(file)
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /webjars/*file controllers.WebJarAssets.at(file)
27 changes: 27 additions & 0 deletions modules/admin/app/controllers/AdminController.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package controllers.admin

import javax.inject.Inject

import com.mohiva.play.silhouette.api.{ Environment, Silhouette }
import com.mohiva.play.silhouette.impl.authenticators.CookieAuthenticator
import com.mohiva.play.silhouette.impl.providers.SocialProviderRegistry
import models.User
import play.api.i18n.MessagesApi

import scala.concurrent.Future

/** Controls administrative user actions.
* @param messagesApi The Play messages API.
* @param env The Silhouette environment.
* @param socialProviderRegistry The social provider registry. */
class AdminController @Inject() (
val messagesApi: MessagesApi,
val env: Environment[User, CookieAuthenticator],
socialProviderRegistry: SocialProviderRegistry)
extends Silhouette[User, CookieAuthenticator] {

/** Handle administrative user functions. */
def index = SecuredAction.async { implicit request =>
Future.successful(Ok(views.html.adminHome(request.identity)))
}
}
13 changes: 13 additions & 0 deletions modules/admin/app/views/adminHome.scala.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
@(user: models.User)(implicit messages: Messages)
@import controllers.admin.{ routes => adminRoutes }
@main(Messages("admin.title"), Some(user)) {
<div class="user col-md-6 col-md-offset-3">
<div class="row data">
<div class="col-md-12">
<div class="row">
<p class="col-md-12">This is the admin page, rendered by a view template in the <code>admin</code> SBT subproject.</p>
</div>
</div>
</div>
</div>
}
6 changes: 6 additions & 0 deletions modules/admin/build.sbt
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
Common.settings
DB.settings

libraryDependencies ++= Seq(
"com.adrianhurt" %% "play-bootstrap3" % "0.4.4-P24"
)
5 changes: 5 additions & 0 deletions modules/admin/conf/admin.routes
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
GET / @controllers.admin.AdminController.index()

# Map static resources from the /public folder to the /assets URL path
GET /assets/*file controllers.Assets.at(path="/public", file)
GET /webjars/*file controllers.WebJarAssets.at(file)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package controllers.auth

import javax.inject.Inject

Expand Down Expand Up @@ -57,7 +57,7 @@ class CredentialsAuthController @Inject() (
data => {
val credentials = Credentials(data.email, data.password)
credentialsProvider.authenticate(credentials).flatMap { loginInfo =>
val result = Redirect(routes.ApplicationController.index())
val result = Redirect(routes.SilhouetteController.index())
userService.retrieve(loginInfo).flatMap {
case Some(user) =>
val c = configuration.underlying
Expand All @@ -79,7 +79,7 @@ class CredentialsAuthController @Inject() (
}
}.recover {
case e: ProviderException =>
Redirect(routes.ApplicationController.signIn()).flashing("error" -> Messages("invalid.credentials"))
Redirect(routes.SilhouetteController.signIn()).flashing("error" -> Messages("invalid.credentials"))
}
}
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
package controllers
package controllers.auth

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.services.AvatarService
Expand All @@ -15,7 +14,6 @@ import models.services.UserService
import play.api.i18n.{ MessagesApi, Messages }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Action

import scala.concurrent.Future

/**
Expand Down Expand Up @@ -49,7 +47,7 @@ class SignUpController @Inject() (
val loginInfo = LoginInfo(CredentialsProvider.ID, data.email)
userService.retrieve(loginInfo).flatMap {
case Some(user) =>
Future.successful(Redirect(routes.ApplicationController.signUp()).flashing("error" -> Messages("user.exists")))
Future.successful(Redirect(routes.SilhouetteController.signUp()).flashing("error" -> Messages("user.exists")))
case None =>
val authInfo = passwordHasher.hash(data.password)
val user = User(
Expand All @@ -67,7 +65,7 @@ class SignUpController @Inject() (
authInfo <- authInfoRepository.add(loginInfo, authInfo)
authenticator <- env.authenticatorService.create(loginInfo)
value <- env.authenticatorService.init(authenticator)
result <- env.authenticatorService.embed(value, Redirect(routes.ApplicationController.index()))
result <- env.authenticatorService.embed(value, Redirect(routes.SilhouetteController.index()))
} yield {
env.eventBus.publish(SignUpEvent(user, request, request2Messages))
env.eventBus.publish(LoginEvent(user, request, request2Messages))
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package controllers
package controllers.auth

import javax.inject.Inject

Expand All @@ -8,7 +8,6 @@ import com.mohiva.play.silhouette.impl.providers.SocialProviderRegistry
import forms._
import models.User
import play.api.i18n.MessagesApi

import scala.concurrent.Future

/**
Expand All @@ -18,18 +17,17 @@ import scala.concurrent.Future
* @param env The Silhouette environment.
* @param socialProviderRegistry The social provider registry.
*/
class ApplicationController @Inject() (
class SilhouetteController @Inject() (
val messagesApi: MessagesApi,
val env: Environment[User, CookieAuthenticator],
socialProviderRegistry: SocialProviderRegistry)
extends Silhouette[User, CookieAuthenticator] {

/**
* Handles the index action.
*
* @return The result to display.
*/
/** Index action handler.
* @return Future of the rendered index page. */
def index = SecuredAction.async { implicit request =>


Future.successful(Ok(views.html.home(request.identity)))
}

Expand All @@ -40,8 +38,8 @@ class ApplicationController @Inject() (
*/
def signIn = UserAwareAction.async { implicit request =>
request.identity match {
case Some(user) => Future.successful(Redirect(routes.ApplicationController.index()))
case None => Future.successful(Ok(views.html.signIn(SignInForm.form, socialProviderRegistry)))
case Some(user) => Future.successful(Redirect(routes.SilhouetteController.index()))
case None => Future.successful(Ok(views.html.signIn(SignInForm.form, socialProviderRegistry)))
}
}

Expand All @@ -52,8 +50,8 @@ class ApplicationController @Inject() (
*/
def signUp = UserAwareAction.async { implicit request =>
request.identity match {
case Some(user) => Future.successful(Redirect(routes.ApplicationController.index()))
case None => Future.successful(Ok(views.html.signUp(SignUpForm.form)))
case Some(user) => Future.successful(Redirect(routes.SilhouetteController.index()))
case None => Future.successful(Ok(views.html.signUp(SignUpForm.form)))
}
}

Expand All @@ -63,9 +61,8 @@ class ApplicationController @Inject() (
* @return The result to display.
*/
def signOut = SecuredAction.async { implicit request =>
val result = Redirect(routes.ApplicationController.index())
val result = Redirect(routes.SilhouetteController.index())
env.eventBus.publish(LogoutEvent(request.identity, request, request2Messages))

env.authenticatorService.discard(request.authenticator, result)
}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package controllers
package controllers.auth

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
Expand All @@ -12,7 +11,6 @@ import models.services.UserService
import play.api.i18n.{ MessagesApi, Messages }
import play.api.libs.concurrent.Execution.Implicits._
import play.api.mvc.Action

import scala.concurrent.Future

/**
Expand Down Expand Up @@ -49,7 +47,7 @@ class SocialAuthController @Inject() (
authInfo <- authInfoRepository.save(profile.loginInfo, authInfo)
authenticator <- env.authenticatorService.create(profile.loginInfo)
value <- env.authenticatorService.init(authenticator)
result <- env.authenticatorService.embed(value, Redirect(routes.ApplicationController.index()))
result <- env.authenticatorService.embed(value, Redirect(routes.SilhouetteController.index()))
} yield {
env.eventBus.publish(LoginEvent(user, request, request2Messages))
result
Expand All @@ -59,7 +57,7 @@ class SocialAuthController @Inject() (
}).recover {
case e: ProviderException =>
logger.error("Unexpected provider error", e)
Redirect(routes.ApplicationController.signIn()).flashing("error" -> Messages("could.not.authenticate"))
Redirect(routes.SilhouetteController.signIn()).flashing("error" -> Messages("could.not.authenticate"))
}
}
}
File renamed without changes.
File renamed without changes.
File renamed without changes.
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,13 @@ package utils
import javax.inject.Inject

import com.mohiva.play.silhouette.api.SecuredErrorHandler
import controllers.routes
import controllers.auth.routes
import play.api.http.DefaultHttpErrorHandler
import play.api.i18n.Messages
import play.api.mvc.Results._
import play.api.mvc.{ Result, RequestHeader }
import play.api.routing.Router
import play.api.{ OptionalSourceMapper, Configuration }

import scala.concurrent.Future

/**
Expand All @@ -34,7 +33,7 @@ class ErrorHandler @Inject() (
* @return The result to send to the client.
*/
override def onNotAuthenticated(request: RequestHeader, messages: Messages): Option[Future[Result]] = {
Some(Future.successful(Redirect(routes.ApplicationController.signIn())))
Some(Future.successful(Redirect(routes.SilhouetteController.signIn())))
}

/**
Expand All @@ -47,6 +46,6 @@ class ErrorHandler @Inject() (
* @return The result to send to the client.
*/
override def onNotAuthorized(request: RequestHeader, messages: Messages): Option[Future[Result]] = {
Some(Future.successful(Redirect(routes.ApplicationController.signIn()).flashing("error" -> Messages("access.denied")(messages))))
Some(Future.successful(Redirect(routes.SilhouetteController.signIn()).flashing("error" -> Messages("access.denied")(messages))))
}
}
File renamed without changes.
Loading