Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add tags feature, refactoring some code #5

Merged
merged 4 commits into from Oct 4, 2011
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
9 changes: 6 additions & 3 deletions library/src/main/scala/shiro/shiro.scala
Expand Up @@ -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);

Expand All @@ -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
}
}

Expand Down
10 changes: 7 additions & 3 deletions library/src/main/scala/shiro/sitemap/locs.scala
Expand Up @@ -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

Expand All @@ -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 changes: 30 additions & 19 deletions library/src/main/scala/shiro/snippet/snippets.scala
@@ -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 = {
Expand Down Expand Up @@ -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 changes: 6 additions & 4 deletions library/src/main/scala/shiro/utils.scala
Expand Up @@ -20,6 +20,10 @@ private[shiro] trait Utils {
def isRemembered =
test { _.isRemembered }

def isAuthenticatedOrRemembered = {
isAuthenticated || isRemembered
}

def hasRole(role: String) =
test { _.hasRole(role) }

Expand All @@ -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}
Expand Down