2015-10-14 70 views
0

我無法處理作用域(訪問級別,OAuth2)和Scala。 我想要有這些用例:具有play2 scala-oauth2提供程序的全局作用scala

  • 定義了控制器的全局範圍。能夠爲某些操作覆蓋此全局參數。
  • 沒有定義一個全局參數,但指定每個動作的範圍

這個代碼是我想要的一個例子:

package controllers 

import scalaoauth2.provider.OAuth2Provider 

import scala.concurrent.ExecutionContext.Implicits.global 

class MyController extends GlobalAuthController 

    // define global scope 
    implicit val scope = Scope.User 

    // By default, if no parameter, the scope is the implicit value previously defined 
    def user = ActionWithAuth { request => 
    Ok("hello user or admin") 
    } 

    def admin = ActionWithAuth(Scope.Admin) { 
    Ok("hello admin") 
    } 
} 

我用nulab/scala-oauth2-provider庫。根據該文檔,我創造了這個:

trait OAuth2ActionComposition { 
    implicit val executionContext: ExecutionContext 

    def ActionWithAuth[U](implicit scope: Scope): ActionBuilder[({type λ[A] = AuthInfoRequest[A, User]})#λ] = { 
    Logger.info("Scope : " + scope.toString) 
    AuthorizedActionFunction(new Users(scope)) compose Action 
    } 
} 

object OAuth2ActionComposition extends OAuth2ActionComposition { 
    implicit val executionContext: ExecutionContext = play.api.libs.concurrent.Execution.defaultContext 
} 

但與代碼我得到一個編譯錯誤:在一行缺少參數類型def user = ActionWithAuth { request =>see this image

當我明確地傳遞隱含參數它的工作原理(如def user = ActionWithAuth(scope) { request => )。 WTF!但它不方便。

我不明白爲什麼會出現此錯誤。我嘗試過其他方法,但沒有成功。

回答

0

我「固定」它得益於Silhouette內置com.mohiva.play.silhouette.api.Authorization。 見http://silhouette.mohiva.com/docs/authorization

事實上,感謝Silhouette,可以定義授權。例如。 :

case class WithProvider(provider: String) extends Authorization[User, CookieAuthenticator] { 
    def isAuthorized[B](user: User, authenticator: CookieAuthenticator)(
    implicit request: Request[B], messages: Messages) = { 

    Future.successful(user.loginInfo.providerID == provider) 
    } 
} 

然後,使用此「過濾器:!

def myAction = SecuredAction(WithProvider("twitter")) { implicit request => 
    // do something here 
} 

這些過濾器可以組合easely

+0

歡迎堆棧溢出雖然這種聯繫可以回答這個問題,最好是在這裏包括答案的基本部分,並提供鏈接以供參考,鏈接的答案可能會失效,如果鏈接的頁面發生變化,請參閱本文:[如何編寫一個好答案](http:// stackoverflow .com/help/how-to-answer) – ByteHamster

+0

好的,我明白了,我會盡力更新我的答案以匹配所需的內容。事實上,我的回答並不是我問的問題的直接答案。這更多是一種解決方法。這就是我爲什麼這樣回答的原因。 – Dnomyar

相關問題