filter staff forum posts in search depending on user credentials

This commit is contained in:
Thibault Duplessis 2013-01-22 00:18:41 +01:00
parent 5797aee441
commit 5f94d4ac81
7 changed files with 29 additions and 16 deletions

View file

@ -14,7 +14,7 @@ private[cli] case class Forum(env: ForumEnv) {
def searchReset: IO[String] = env.indexer.rebuildAll inject "Search index reset"
def search(text: String) = io {
val paginator = env.searchPaginator(text, 1)
val paginator = env.searchPaginator(text, 1, true)
(paginator.nbResults + " results") :: paginator.currentPageResults.map(_.show)
} map (_ mkString "\n")
}

View file

@ -16,7 +16,7 @@ object ForumPost extends LilaController with forum.Controller {
implicit def req = ctx.body
forms.search.bindFromRequest.fold(
failure Redirect(routes.ForumCateg.index),
text Ok(html.forum.search(text, searchPaginator(text, page)))
text Ok(html.forum.search(text, searchPaginator(text, page, isGranted(_.StaffForum))))
)
}

View file

@ -23,6 +23,8 @@ case class Post(
def showUsernameOrAuthor = userId | showAuthor
def isTeam = categId startsWith "team-"
def isStaff = categId == "staff"
}
object Post {

View file

@ -10,6 +10,7 @@ private[forum] object SearchMapping {
val topic = "to"
val topicId = "ti"
val author = "au"
val staff = "st"
}
import fields._
import Mapping._
@ -19,7 +20,8 @@ private[forum] object SearchMapping {
boost(body, "string", 2),
boost(topic, "string", 4),
boost(author, "string"),
field(topicId, "string")
field(topicId, "string"),
field(staff, "boolean")
).toMap,
"analyzer" -> "snowball"
)
@ -28,6 +30,7 @@ private[forum] object SearchMapping {
body -> view.post.text,
topic -> view.topic.name,
author -> ~(view.post.userId orElse view.post.author),
topicId -> view.topic.id
topicId -> view.topic.id,
staff -> view.post.isStaff
)
}

View file

@ -7,10 +7,10 @@ private[forum] final class SearchPaginatorBuilder(
indexer: SearchIndexer,
maxPerPage: Int) {
def apply(text: String, page: Int): Paginator[PostView] = Paginator(
adapter = new ESAdapter(indexer, SearchQuery(text)),
def apply(text: String, page: Int, staff: Boolean): Paginator[PostView] = Paginator(
adapter = new ESAdapter(indexer, SearchQuery(text, staff)),
currentPage = page,
maxPerPage = maxPerPage).fold(_ apply(text, 0), identity)
maxPerPage = maxPerPage).fold(_ apply(text, 0, staff), identity)
private class ESAdapter(indexer: SearchIndexer, query: SearchQuery) extends Adapter[PostView] {

View file

@ -4,27 +4,31 @@ package forum
import search.ElasticSearch
import SearchMapping.fields
import org.elasticsearch.index.query._, QueryBuilders._
import org.elasticsearch.index.query._, QueryBuilders._, FilterBuilders._
private[forum] final class SearchQuery private(text: String) {
private[forum] final class SearchQuery private(text: String, staff: Boolean) {
def searchRequest(from: Int = 0, size: Int = 10) = ElasticSearch.Request.Search(
query = makeQuery,
filter = makeFilters,
from = from,
size = size)
def countRequest = ElasticSearch.Request.Count(makeQuery)
private def makeQuery = {
import SearchMapping.fields._
boolQuery
.should(fuzzyQuery(body, text))
.should(fuzzyQuery(topic, text))
.should(fuzzyQuery(author, text))
.should(fuzzyQuery(fields.body, text))
.should(fuzzyQuery(fields.topic, text))
.should(fuzzyQuery(fields.author, text))
}
private def makeFilters = !staff option termFilter(fields.staff, false)
}
object SearchQuery {
def apply(text: String): SearchQuery = new SearchQuery(text.trim.toLowerCase)
def apply(text: String, staff: Boolean): SearchQuery =
new SearchQuery(text.trim.toLowerCase, staff)
}

View file

@ -75,13 +75,17 @@ final class TypeIndexer(
private def doClear {
try {
es.createIndex(indexName, settings = Map())
}
catch {
case e: org.elasticsearch.indices.IndexAlreadyExistsException
}
try {
es.deleteByQuery(Seq(indexName), Seq(typeName))
es.waitTillActive()
es.deleteMapping(indexName :: Nil, typeName.some)
}
catch {
case e: org.elasticsearch.indices.IndexAlreadyExistsException
case e: org.elasticsearch.indices.TypeMissingException
case e: org.elasticsearch.indices.TypeMissingException
}
es.putMapping(indexName, typeName, Json generate Map(typeName -> mapping))
es.refresh()