Permalink
Browse files

Add tags feature, refactoring some code

  • Loading branch information...
1 parent 13e5dc8 commit 6c52e5fec6fe51b9df586ba9ef1c258e04610719 @lunserv lunserv committed Sep 21, 2011
@@ -2,19 +2,18 @@ package shiro
import net.liftweb.common.{Box,Full,Empty}
import net.liftweb.util.Helpers
-import net.liftweb.http.{LiftRules, S, SessionVar, RequestVar, Factory}
+import net.liftweb.http.{LiftRules, SessionVar, Factory}
import org.apache.shiro.SecurityUtils
import org.apache.shiro.util.{Factory => ShiroFactory}
import org.apache.shiro.config.IniSecurityManagerFactory
import org.apache.shiro.mgt.SecurityManager
-import org.apache.shiro.subject.Subject
object Shiro extends Factory {
def init(factory: ShiroFactory[SecurityManager]){
import Utils._
- import shiro.snippet.{HasRole,LacksRole,HasPermission,LacksPermission,HasAnyRoles}
+ import shiro.snippet._
SecurityUtils.setSecurityManager(factory.getInstance);
@@ -26,6 +25,11 @@ object Shiro extends Factory {
case "has_permission" | "hasPermission" | "HasPermission" => HasPermission
case "lacks_permission" | "lacksPermission" | "LacksPermission" => LacksPermission
case "has_any_roles" | "hasAnyRoles" | "HasAnyRoles" => HasAnyRoles
+ case "guest_tag" | "guestTag" | "GuestTag" => GuestTag
+ case "user_tag" | "userTag" | "UserTag" => UserTag
+ case "authenticated_tag" | "authenticatedTag" | "AuthenticatedTag" => AuthenticatedTag
+ case "not_authenticated_tag" | "notAuthenticatedTag" | "NotAuthenticatedTag" => NotAuthenticatedTag
+ case "principal_tag" | "principalTag" | "PrincipalTag" => PrincipalTag
}
}
@@ -38,19 +38,19 @@ object Locs {
() => RedirectToIndexURL)
val RequireRemembered = If(
- () => isRemembered || isAuthenticated,
+ () => isAuthenticatedOrRemembered,
() => RedirectBackToReferrer)
val RequireNotRemembered = If(
- () => !(isRemembered || isAuthenticated),
+ () => !isAuthenticatedOrRemembered,
() => RedirectToIndexURL)
def logoutMenu = Menu(Loc("Logout", logoutURL,
S.??("logout"), logoutLocParams))
private val logoutLocParams = RequireRemembered ::
EarlyResponse(() => {
- if(isAuthenticated || isRemembered){ subject.logout() }
+ if(isAuthenticatedOrRemembered){ subject.logout() }
Full(RedirectResponse(Shiro.indexURL.vend))
}) :: Nil
@@ -77,4 +77,8 @@ object Locs {
def LacksPermission(permission: String) =
If(() => lacksPermission(permission),
DisplayError("Overqualified permissions to access that resource."))
+
+ def HasAnyRoles(roles: Seq[String]) =
+ If(() => hasAnyRoles(roles),
+ DisplayError("You are the wrong role to access that resource."))
}
@@ -1,31 +1,20 @@
package shiro.snippet
import scala.xml.NodeSeq
-import net.liftweb.common.{Box,Full,Empty,Failure}
import net.liftweb.http.{DispatchSnippet,S}
-import net.liftweb.util.Helpers.tryo
-import org.apache.shiro.SecurityUtils
-import org.apache.shiro.subject.Subject
+import net.liftweb.util.Helpers._
import shiro.Utils._
sealed trait ShiroShippet {
- def serve(xhtml: NodeSeq, attribute: String = "name")(f: String => Boolean): NodeSeq =
+ def serve(xhtml: NodeSeq)(f: Boolean): NodeSeq =
+ if (f) xhtml else NodeSeq.Empty
+
+ def serve(xhtml: NodeSeq, attribute: String)(f: String => Boolean): NodeSeq =
(for {
attr <- S.attr(attribute) if f(attr)
} yield xhtml) openOr NodeSeq.Empty
}
-// sealed trait Utils {
-// protected def serve(xhtml: NodeSeq)(f: (Subject, String) => Boolean): NodeSeq =
-// serve("name", xhtml)(f)
-//
-// protected def serve(attribute: String, xhtml: NodeSeq)(f: (Subject, String) => Boolean): NodeSeq =
-// (for {
-// s <- Box.!!(SecurityUtils.getSubject)
-// attr <- S.attr(attribute) if f(s,attr)
-// } yield xhtml) getOrElse NodeSeq.Empty
-// }
-
trait SubjectSnippet extends DispatchSnippet with ShiroShippet {
def dispatch = {
case _ => render _
@@ -34,25 +23,25 @@ trait SubjectSnippet extends DispatchSnippet with ShiroShippet {
}
object HasRole extends SubjectSnippet {
- def render(xhtml: NodeSeq): NodeSeq = serve(xhtml){
+ def render(xhtml: NodeSeq): NodeSeq = serve(xhtml, "name"){
hasRole(_)
}
}
object LacksRole extends SubjectSnippet {
- def render(xhtml: NodeSeq): NodeSeq = serve(xhtml){
+ def render(xhtml: NodeSeq): NodeSeq = serve(xhtml, "name"){
lacksRole(_)
}
}
object HasPermission extends SubjectSnippet {
- def render(xhtml: NodeSeq): NodeSeq = serve(xhtml){
+ def render(xhtml: NodeSeq): NodeSeq = serve(xhtml, "name"){
hasPermission(_)
}
}
object LacksPermission extends SubjectSnippet {
- def render(xhtml: NodeSeq): NodeSeq = serve(xhtml){
+ def render(xhtml: NodeSeq): NodeSeq = serve(xhtml, "name"){
lacksPermission(_)
}
}
@@ -66,3 +55,35 @@ object HasAnyRoles extends SubjectSnippet {
}
}
+object GuestTag extends SubjectSnippet {
+ def render(xhtml: NodeSeq): NodeSeq = serve(xhtml){
+ !isAuthenticatedOrRemembered
+ }
+}
+
+object UserTag extends SubjectSnippet {
+ def render(xhtml: NodeSeq): NodeSeq = serve(xhtml){
+ isAuthenticatedOrRemembered
+ }
+}
+
+object AuthenticatedTag extends SubjectSnippet {
+ def render(xhtml: NodeSeq): NodeSeq = serve(xhtml){
+ isAuthenticated
+ }
+}
+
+object NotAuthenticatedTag extends SubjectSnippet {
+ def render(xhtml: NodeSeq): NodeSeq = serve(xhtml){
+ !isAuthenticated
+ }
+}
+
+object PrincipalTag extends DispatchSnippet {
+ def dispatch = {
+ case _ => render
+ }
+
+ def render = "*" #> (principal openOr S.attr("name").openOr("Principal or default value not found")).toString
+}
+
@@ -20,6 +20,10 @@ private[shiro] trait Utils {
def isRemembered =
test { _.isRemembered }
+ def isAuthenticatedOrRemembered = {
+ isAuthenticated || isRemembered
+ }
+
def hasRole(role: String) =
test { _.hasRole(role) }

0 comments on commit 6c52e5f

Please sign in to comment.