If values ββin Boot.scala (Scala Lift)
It seems to me that I am monopolizing the stack on Scala / Lift, so I apologize, but questions keep going. Here's the last one.
I am trying to restrict access to anything in / login / * to those users who have not yet logged in.
This is how I try to do it:
val entries = Menu(Loc("Home", List("index"), "Home")) :: //login stuff
Menu(Loc("loginBase", ("login"::""::Nil)->true, "Login Base", Hidden, anyLoggedIn))::...
This is a SiteMap entry. Then I define anyLoggedIn in Boot.scala like this:
val anyLoggedIn = If(() => !(Student.loggedIn_? || Provider.loggedIn_?),
if (sessionLoginType.is map {_ == StudentLogin} openOr false)
{
println("student")
RedirectResponse("studentHome")
}
else
{
println("provider")
RedirectResponse("providerHome")
}
I want to send suppliers and students to their "homes" respectively when they try to access any login page when they are already logged in. For some reason (maybe its logical logic) it never works and I never go for redirects.
Any ideas?
thank
A common mistake with val
is defining a variable after use:
scala> object test {
| val f = x
| val x = 1
| }
defined module test
scala> println(test.f)
0
This is quite often a mistake when working with Lift conditions SiteMap
(I personally tend to define them at the bottom). To overcome this, define yours val
as lazy
:
scala> object test {
| val f = x
| lazy val x = 1
| }
defined module test
scala> println(test.f)
1
Side note
Your second test If
doesn't look too Scalaish, it's a mix of functional and procedural styles. There are options for how to write it, please see only one possible option:
sessionLoginType.is match {
case Full(StudentLogin) =>
println("student")
RedirectResponse("studentHome")
case Full(ProviderLogin) =>
println("provider")
RedirectResponse("providerHome")
}
Another variant
You can define a static map from the login type in the uri for example.
val redirectMap = Map(StudentLogin -> "studentHome", ProviderLogin -> "providerHome")
Then you can use it in your If
like
sessionLoginType.is.flatMap{ redirectMap.get }.map{ RedirectResponse _ }.open_!
the same can be rewritten with concepts:
(for {val loginType <- sessionLoginType.is
val uri <- redirectMap.get(loginType) }
yield RedirectResponse(uri)
).open_!
But be careful, if it redirectMap
does not contain a key or yours is sessionLoginType
empty, you have a problem - it open_!
will fail as it should not be applied to empty cells. If you know a sensible default, it's better to use.openOr defaultRedirect
source to share