Skip to content

Instantly share code, notes, and snippets.

@heralight
Created March 19, 2013 14:02
Show Gist options
  • Save heralight/5196339 to your computer and use it in GitHub Desktop.
Save heralight/5196339 to your computer and use it in GitHub Desktop.
add permission on all files with lift like for an admin part
package code.lib
import net.liftweb.common._
import net.liftweb.http._
import S._
import net.liftweb.util._
import Helpers._
import code.model.User
object ProxyGuard extends Logger {
def register = {
LiftRules.liftRequest.append {
case Req("private" :: _, _, _) => true
}
LiftRules.dispatch.prepend(NamedPF("File Dispatch") {
case r@Req("private" :: _, _, _) if canRender(r) && User.loggedIn_? => {
val res = tryo {
val pls = r.path.partPath.mkString("/", "/", "." + r.path.suffix)
LiftRules.loadResource(pls) match {
case Full(b) => FileResponse(b, Nil, 200, getMimeType(r.path.suffix))
case _ => NotFoundResponse()
}
}
() => res
}
})
def getMimeType(suffix: String): String = {
suffix match {
case "css" => "text/css"
case "js" => "application/x-javascript"
case _ => "application/octet-stream"
}
}
def canRender(r: Req): Boolean = {
r.path.suffix match {
case "css" => true
case "js" => true
case _ => false
}
}
}
case class FileResponse(b: Array[Byte], headers: List[(String, String)] = Nil, code: Int = 200, contentType: String = "text/html;") extends LiftResponse {
def toResponse = {
InMemoryResponse(b, ("Content-Length", b.length.toString) ::("Content-Type", contentType) :: headers, Nil, code)
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment