import scala.collection.mutable
class Session
trait SessionProvider:
def session: Session
trait DefaultSessionProvider extends SessionProvider:
val dummySession = new Session
override def session = dummySession
abstract class Identity
trait IdentityCache:
def getOrAuthenticate():Session
trait InMemoryIdentityCache extends SessionProvider:
val cache = mutable.Map.empty[Session, SessionProvider]
override def getOrAuthenticate():InMemoryIdentityCache =
cache.getOrElseUpdate(session, authenticate())
trait Authenticator:
def authenticate():Session
trait UsesSAMLIdentity:
class SAMLIdentity(val saml: String) extends Identity
trait SAMLAuthenticator extends Authenticator with UsesSAMLIdentity:
val dummySAMLIdentity = new SAMLIdentity("XXX")
override def authenticate() = dummySAMLIdentity
trait RoleManager:
def hasRole(role: String): Boolean
trait SAMLRoleManager extends RoleManager with UsesSAMLIdentity:
override def hasRole(role: String): Boolean =
val identity = getOrAuthenticate()
identity.saml == "XXX"
object WebApp extends SAMLRoleManager :
def main(args: Array[String]): Unit =
println(hasRole("YYY")) // Prints "true"
我是 Scala 的新手,我正在尝试实现上面的代码以在 main 中打印 true。我的问题是我的 IDE 在特征 InMemoryIdentityCache 中显示“未找到:身份验证”和“未找到会话”。我对如何实现这个蛋糕模式有点困惑。
我将不胜感激。