Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

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
@@ -2,19 +2,18 @@ package shiro
2 2
3 3 import net.liftweb.common.{Box,Full,Empty}
4 4 import net.liftweb.util.Helpers
5   -import net.liftweb.http.{LiftRules, S, SessionVar, RequestVar, Factory}
  5 +import net.liftweb.http.{LiftRules, SessionVar, Factory}
6 6
7 7 import org.apache.shiro.SecurityUtils
8 8 import org.apache.shiro.util.{Factory => ShiroFactory}
9 9 import org.apache.shiro.config.IniSecurityManagerFactory
10 10 import org.apache.shiro.mgt.SecurityManager
11   -import org.apache.shiro.subject.Subject
12 11
13 12 object Shiro extends Factory {
14 13 def init(factory: ShiroFactory[SecurityManager]){
15 14
16 15 import Utils._
17   - import shiro.snippet.{HasRole,LacksRole,HasPermission,LacksPermission,HasAnyRoles}
  16 + import shiro.snippet._
18 17
19 18 SecurityUtils.setSecurityManager(factory.getInstance);
20 19
@@ -26,6 +25,10 @@ object Shiro extends Factory {
26 25 case "has_permission" | "hasPermission" | "HasPermission" => HasPermission
27 26 case "lacks_permission" | "lacksPermission" | "LacksPermission" => LacksPermission
28 27 case "has_any_roles" | "hasAnyRoles" | "HasAnyRoles" => HasAnyRoles
  28 + case "is_guest" | "isGuest" | "IsGuest" => IsGuest
  29 + case "is_user" | "isUser" | "IsUser" => IsUser
  30 + case "is_authenticated" | "isAuthenticated" | "IsAuthenticated" => IsAuthenticated
  31 + case "is_not_authenticated" | "isNotAuthenticated" | "IsNotAuthenticated" => IsNotAuthenticated
29 32 }
30 33 }
31 34
10 library/src/main/scala/shiro/sitemap/locs.scala
@@ -38,11 +38,11 @@ object Locs {
38 38 () => RedirectToIndexURL)
39 39
40 40 val RequireRemembered = If(
41   - () => isRemembered || isAuthenticated,
  41 + () => isAuthenticatedOrRemembered,
42 42 () => RedirectBackToReferrer)
43 43
44 44 val RequireNotRemembered = If(
45   - () => !(isRemembered || isAuthenticated),
  45 + () => !isAuthenticatedOrRemembered,
46 46 () => RedirectToIndexURL)
47 47
48 48 def logoutMenu = Menu(Loc("Logout", logoutURL,
@@ -50,7 +50,7 @@ object Locs {
50 50
51 51 private val logoutLocParams = RequireRemembered ::
52 52 EarlyResponse(() => {
53   - if(isAuthenticated || isRemembered){ subject.logout() }
  53 + if(isAuthenticatedOrRemembered){ subject.logout() }
54 54 Full(RedirectResponse(Shiro.indexURL.vend))
55 55 }) :: Nil
56 56
@@ -77,4 +77,8 @@ object Locs {
77 77 def LacksPermission(permission: String) =
78 78 If(() => lacksPermission(permission),
79 79 DisplayError("Overqualified permissions to access that resource."))
  80 +
  81 + def HasAnyRoles(roles: Seq[String]) =
  82 + If(() => hasAnyRoles(roles),
  83 + DisplayError("You are the wrong role to access that resource."))
80 84 }
49 library/src/main/scala/shiro/snippet/snippets.scala
... ... @@ -1,30 +1,17 @@
1 1 package shiro.snippet
2 2
3 3 import scala.xml.NodeSeq
4   -import net.liftweb.common.{Box,Full,Empty,Failure}
5 4 import net.liftweb.http.{DispatchSnippet,S}
6   -import net.liftweb.util.Helpers.tryo
7   -import org.apache.shiro.SecurityUtils
8   -import org.apache.shiro.subject.Subject
  5 +import net.liftweb.util.Helpers._
9 6 import shiro.Utils._
10 7
11 8 sealed trait ShiroShippet {
12   - def serve(xhtml: NodeSeq, attribute: String = "name")(f: String => Boolean): NodeSeq =
13   - (for {
14   - attr <- S.attr(attribute) if f(attr)
15   - } yield xhtml) openOr NodeSeq.Empty
16   -}
  9 + def verification(xhtml: NodeSeq)(f: Boolean): NodeSeq =
  10 + if (f) xhtml else NodeSeq.Empty
17 11
18   -// sealed trait Utils {
19   -// protected def serve(xhtml: NodeSeq)(f: (Subject, String) => Boolean): NodeSeq =
20   -// serve("name", xhtml)(f)
21   -//
22   -// protected def serve(attribute: String, xhtml: NodeSeq)(f: (Subject, String) => Boolean): NodeSeq =
23   -// (for {
24   -// s <- Box.!!(SecurityUtils.getSubject)
25   -// attr <- S.attr(attribute) if f(s,attr)
26   -// } yield xhtml) getOrElse NodeSeq.Empty
27   -// }
  12 + def serve(xhtml: NodeSeq, attribute: String = "name")(f: String => Boolean): NodeSeq =
  13 + if (S.attr(attribute) exists f) xhtml else NodeSeq.Empty
  14 +}
28 15
29 16 trait SubjectSnippet extends DispatchSnippet with ShiroShippet {
30 17 def dispatch = {
@@ -66,3 +53,27 @@ object HasAnyRoles extends SubjectSnippet {
66 53 }
67 54 }
68 55
  56 +object IsGuest extends SubjectSnippet {
  57 + def render(xhtml: NodeSeq): NodeSeq = verification(xhtml){
  58 + !isAuthenticatedOrRemembered
  59 + }
  60 +}
  61 +
  62 +object IsUser extends SubjectSnippet {
  63 + def render(xhtml: NodeSeq): NodeSeq = verification(xhtml){
  64 + isAuthenticatedOrRemembered
  65 + }
  66 +}
  67 +
  68 +object IsAuthenticated extends SubjectSnippet {
  69 + def render(xhtml: NodeSeq): NodeSeq = verification(xhtml){
  70 + isAuthenticated
  71 + }
  72 +}
  73 +
  74 +object IsNotAuthenticated extends SubjectSnippet {
  75 + def render(xhtml: NodeSeq): NodeSeq = verification(xhtml){
  76 + !isAuthenticated
  77 + }
  78 +}
  79 +
10 library/src/main/scala/shiro/utils.scala
@@ -20,6 +20,10 @@ private[shiro] trait Utils {
20 20 def isRemembered =
21 21 test { _.isRemembered }
22 22
  23 + def isAuthenticatedOrRemembered = {
  24 + isAuthenticated || isRemembered
  25 + }
  26 +
23 27 def hasRole(role: String) =
24 28 test { _.hasRole(role) }
25 29
@@ -32,10 +36,8 @@ private[shiro] trait Utils {
32 36 def lacksPermission(permission: String) =
33 37 !hasPermission(permission)
34 38
35   - def hasAnyRoles(roles: Seq[String]) = test { subject =>
36   - roles.map(r => subject.hasRole(r.trim)
37   - ).contains(true)
38   - }
  39 + def hasAnyRoles(roles: Seq[String]) =
  40 + roles exists (r => hasRole(r.trim))
39 41 }
40 42
41 43 import net.liftweb.common.{Box,Failure,Full}

0 comments on commit 4b3b422

Please sign in to comment.
Something went wrong with that request. Please try again.