Skip to content

Commit

Permalink
Merge pull request #111 from kitoranaoki/rename-trait
Browse files Browse the repository at this point in the history
rename trait. Permission -> Role
  • Loading branch information
gakuzzzz committed Dec 5, 2014
2 parents 5b742e0 + fdaa66c commit 04cff40
Show file tree
Hide file tree
Showing 12 changed files with 44 additions and 44 deletions.
12 changes: 6 additions & 6 deletions README.ja.md
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,11 @@ For example: `Build.scala`
* 認可(権限チェック)を行う際に、アクション毎に設定するオブジェクトの型です。
* このサンプルでは例として以下のような trait を使用しています。
*
* sealed trait Permission
* case object Administrator extends Permission
* case object NormalUser extends Permission
* sealed trait Role
* case object Administrator extends Role
* case object NormalUser extends Role
*/
type Authority = Permission
type Authority = Role

/**
* CacheからユーザIDを取り出すための ClassTag です。
Expand Down Expand Up @@ -143,7 +143,7 @@ For example: `Build.scala`
* 任意の処理を記述してください。
*/
def authorize(user: User, authority: Authority)(implicit ctx: ExecutionContext): Future[Boolean] = Future.successful {
(user.permission, authority) match {
(user.role, authority) match {
case (Administrator, _) => true
case (NormalUser, NormalUser) => true
case _ => false
Expand Down Expand Up @@ -733,7 +733,7 @@ Play2 の現在の仕組みではできません。

アカウントは以下の3アカウントが登録されています。

Email | Password | Permission
Email | Password | Role
[email protected] | secret | Administrator
[email protected] | secret | NormalUser
[email protected] | secret | NormalUser
Expand Down
12 changes: 6 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,11 +98,11 @@ Usage
* A type that is defined by every action for authorization.
* This sample uses the following trait:
*
* sealed trait Permission
* case object Administrator extends Permission
* case object NormalUser extends Permission
* sealed trait Role
* case object Administrator extends Role
* case object NormalUser extends Role
*/
type Authority = Permission
type Authority = Role

/**
* A `ClassTag` is used to retrieve an id from the Cache API.
Expand Down Expand Up @@ -150,7 +150,7 @@ Usage
* You should alter this procedure to suit your application.
*/
def authorize(user: User, authority: Authority)(implicit ctx: ExecutionContext): Future[Boolean] = Future.successful {
(user.permission, authority) match {
(user.role, authority) match {
case (Administrator, _) => true
case (NormalUser, NormalUser) => true
case _ => false
Expand Down Expand Up @@ -532,7 +532,7 @@ Running The Sample Application

defined accounts

Email | Password | Permission
Email | Password | Role
[email protected] | secret | Administrator
[email protected] | secret | NormalUser
[email protected] | secret | NormalUser
Expand Down
4 changes: 2 additions & 2 deletions sample/app/Global.scala
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import play.api._

import jp.t2v.lab.play2.auth.sample._
import jp.t2v.lab.play2.auth.sample.Permission._
import jp.t2v.lab.play2.auth.sample.Role._
import scalikejdbc._

object Global extends GlobalSettings {
Expand All @@ -17,4 +17,4 @@ object Global extends GlobalSettings {

}

}
}
8 changes: 4 additions & 4 deletions sample/app/controllers/BaseAuthConfig.scala
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package controllers

import jp.t2v.lab.play2.auth.AuthConfig
import jp.t2v.lab.play2.auth.sample.{Permission, Account}
import jp.t2v.lab.play2.auth.sample.Permission._
import jp.t2v.lab.play2.auth.sample.{Role, Account}
import jp.t2v.lab.play2.auth.sample.Role._
import play.api.mvc.RequestHeader
import play.api.mvc.Results._

Expand All @@ -13,14 +13,14 @@ trait BaseAuthConfig extends AuthConfig {

type Id = Int
type User = Account
type Authority = Permission
type Authority = Role

val idTag: ClassTag[Id] = classTag[Id]
val sessionTimeoutInSeconds = 3600

def resolveUser(id: Id)(implicit ctx: ExecutionContext) = Future.successful(Account.findById(id))
def authorizationFailed(request: RequestHeader)(implicit ctx: ExecutionContext) = Future.successful(Forbidden("no permission"))
def authorize(user: User, authority: Authority)(implicit ctx: ExecutionContext) = Future.successful((user.permission, authority) match {
def authorize(user: User, authority: Authority)(implicit ctx: ExecutionContext) = Future.successful((user.role, authority) match {
case (Administrator, _) => true
case (NormalUser, NormalUser) => true
case _ => false
Expand Down
4 changes: 2 additions & 2 deletions sample/app/controllers/basic/Messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import controllers.stack.Pjax
import jp.t2v.lab.play2.auth.AuthElement
import play.api.mvc.Controller
import views.html
import jp.t2v.lab.play2.auth.sample.Permission._
import jp.t2v.lab.play2.auth.sample.Role._

trait Messages extends Controller with Pjax with AuthElement with AuthConfigImpl {

Expand All @@ -31,4 +31,4 @@ trait Messages extends Controller with Pjax with AuthElement with AuthConfigImpl
protected val fullTemplate: User => Template = html.basic.fullTemplate.apply

}
object Messages extends Messages
object Messages extends Messages
2 changes: 1 addition & 1 deletion sample/app/controllers/builder/Messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package controllers.builder

import jp.t2v.lab.play2.auth.AuthActionBuilders
import jp.t2v.lab.play2.auth.sample.Account
import jp.t2v.lab.play2.auth.sample.Permission._
import jp.t2v.lab.play2.auth.sample.Role._
import play.api.mvc._
import play.twirl.api.Html
import scalikejdbc.{DB, DBSession}
Expand Down
2 changes: 1 addition & 1 deletion sample/app/controllers/csrf/PreventingCsrfSample.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package controllers.csrf

import controllers.stack.TokenValidateElement
import jp.t2v.lab.play2.auth.AuthElement
import jp.t2v.lab.play2.auth.sample.Permission._
import jp.t2v.lab.play2.auth.sample.Role._
import play.api.data.Form
import play.api.data.Forms._
import play.api.mvc.Controller
Expand Down
4 changes: 2 additions & 2 deletions sample/app/controllers/ephemeral/Messages.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import controllers.stack.Pjax
import jp.t2v.lab.play2.auth.AuthElement
import play.api.mvc.Controller
import views.html
import jp.t2v.lab.play2.auth.sample.Permission._
import jp.t2v.lab.play2.auth.sample.Role._

trait Messages extends Controller with Pjax with AuthElement with AuthConfigImpl {

Expand All @@ -31,4 +31,4 @@ trait Messages extends Controller with Pjax with AuthElement with AuthConfigImpl
protected val fullTemplate: User => Template = html.ephemeral.fullTemplate.apply

}
object Messages extends Messages
object Messages extends Messages
6 changes: 3 additions & 3 deletions sample/app/jp/t2v/lab/play2/auth/sample/Account.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package jp.t2v.lab.play2.auth.sample
import org.mindrot.jbcrypt.BCrypt
import scalikejdbc._

case class Account(id: Int, email: String, password: String, name: String, permission: Permission)
case class Account(id: Int, email: String, password: String, name: String, role: Role)

object Account extends SQLSyntaxSupport[Account] {

Expand All @@ -15,7 +15,7 @@ object Account extends SQLSyntaxSupport[Account] {
email = rs.string(a.email),
password = rs.string(a.password),
name = rs.string(a.name),
permission = Permission.valueOf(rs.string(a.permission))
role = Role.valueOf(rs.string(a.role))
)

private val auto = AutoSession
Expand All @@ -40,7 +40,7 @@ object Account extends SQLSyntaxSupport[Account] {
withSQL {
import account._
val pass = BCrypt.hashpw(account.password, BCrypt.gensalt())
insert.into(Account).values(id, email, pass, name, permission.toString)
insert.into(Account).values(id, email, pass, name, role.toString)
}.update.apply()
}

Expand Down
16 changes: 0 additions & 16 deletions sample/app/jp/t2v/lab/play2/auth/sample/Permission.scala

This file was deleted.

16 changes: 16 additions & 0 deletions sample/app/jp/t2v/lab/play2/auth/sample/Role.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package jp.t2v.lab.play2.auth.sample

sealed trait Role

object Role {

case object Administrator extends Role
case object NormalUser extends Role

def valueOf(value: String): Role = value match {
case "Administrator" => Administrator
case "NormalUser" => NormalUser
case _ => throw new IllegalArgumentException()
}

}
2 changes: 1 addition & 1 deletion sample/conf/db/migration/default/V1__create_tables.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ CREATE TABLE account (
email varchar NOT NULL UNIQUE,
password varchar NOT NULL,
name varchar NOT NULL,
permission varchar NOT NULL
role varchar NOT NULL
);

0 comments on commit 04cff40

Please sign in to comment.