Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Merge pull request #5 from lunserv/add_Tags_Feature

Added a range of new tags to have parity with those available in the regular Java Shiro edition.
  • Loading branch information...
commit 4b3b422ddfa8650b1180d8bb617c54e9fb6fc976 2 parents 13e5dc8 + c68662b
Timothy Perrett authored
9 library/src/main/scala/shiro/shiro.scala
View
@@ -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,10 @@ object Shiro extends Factory {
case "has_permission" | "hasPermission" | "HasPermission" => HasPermission
case "lacks_permission" | "lacksPermission" | "LacksPermission" => LacksPermission
case "has_any_roles" | "hasAnyRoles" | "HasAnyRoles" => HasAnyRoles
+ case "is_guest" | "isGuest" | "IsGuest" => IsGuest
+ case "is_user" | "isUser" | "IsUser" => IsUser
+ case "is_authenticated" | "isAuthenticated" | "IsAuthenticated" => IsAuthenticated
+ case "is_not_authenticated" | "isNotAuthenticated" | "IsNotAuthenticated" => IsNotAuthenticated
}
}
10 library/src/main/scala/shiro/sitemap/locs.scala
View
@@ -38,11 +38,11 @@ object Locs {
() => RedirectToIndexURL)
val RequireRemembered = If(
- () => isRemembered || isAuthenticated,
+ () => isAuthenticatedOrRemembered,
() => RedirectBackToReferrer)
val RequireNotRemembered = If(
- () => !(isRemembered || isAuthenticated),
+ () => !isAuthenticatedOrRemembered,
() => RedirectToIndexURL)
def logoutMenu = Menu(Loc("Logout", logoutURL,
@@ -50,7 +50,7 @@ object Locs {
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."))
}
49 library/src/main/scala/shiro/snippet/snippets.scala
View
@@ -1,30 +1,17 @@
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 =
- (for {
- attr <- S.attr(attribute) if f(attr)
- } yield xhtml) openOr NodeSeq.Empty
-}
+ def verification(xhtml: NodeSeq)(f: Boolean): NodeSeq =
+ if (f) xhtml else 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
-// }
+ def serve(xhtml: NodeSeq, attribute: String = "name")(f: String => Boolean): NodeSeq =
+ if (S.attr(attribute) exists f) xhtml else NodeSeq.Empty
+}
trait SubjectSnippet extends DispatchSnippet with ShiroShippet {
def dispatch = {
@@ -66,3 +53,27 @@ object HasAnyRoles extends SubjectSnippet {
}
}
+object IsGuest extends SubjectSnippet {
+ def render(xhtml: NodeSeq): NodeSeq = verification(xhtml){
+ !isAuthenticatedOrRemembered
+ }
+}
+
+object IsUser extends SubjectSnippet {
+ def render(xhtml: NodeSeq): NodeSeq = verification(xhtml){
+ isAuthenticatedOrRemembered
+ }
+}
+
+object IsAuthenticated extends SubjectSnippet {
+ def render(xhtml: NodeSeq): NodeSeq = verification(xhtml){
+ isAuthenticated
+ }
+}
+
+object IsNotAuthenticated extends SubjectSnippet {
+ def render(xhtml: NodeSeq): NodeSeq = verification(xhtml){
+ !isAuthenticated
+ }
+}
+
10 library/src/main/scala/shiro/utils.scala
View
@@ -20,6 +20,10 @@ private[shiro] trait Utils {
def isRemembered =
test { _.isRemembered }
+ def isAuthenticatedOrRemembered = {
+ isAuthenticated || isRemembered
+ }
+
def hasRole(role: String) =
test { _.hasRole(role) }
@@ -32,10 +36,8 @@ private[shiro] trait Utils {
def lacksPermission(permission: String) =
!hasPermission(permission)
- def hasAnyRoles(roles: Seq[String]) = test { subject =>
- roles.map(r => subject.hasRole(r.trim)
- ).contains(true)
- }
+ def hasAnyRoles(roles: Seq[String]) =
+ roles exists (r => hasRole(r.trim))
}
import net.liftweb.common.{Box,Failure,Full}
Please sign in to comment.
Something went wrong with that request. Please try again.