Skip to content

Commit

Permalink
added auth check for retrieveTranslationStatus
Browse files Browse the repository at this point in the history
  • Loading branch information
zingmane committed Aug 29, 2019
1 parent c98fc50 commit db1414c
Show file tree
Hide file tree
Showing 5 changed files with 120 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ class StructureController(
logger.info(s"retrieveTables")

for {
tableSeq: Seq[Table] <- tableStruc.retrieveAll()
tableSeq: Seq[Table] <- tableStruc.retrieveAll(isInternalCall = false)
} yield {
TableSeq(tableSeq)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -124,13 +124,13 @@ class TableauxController(
logger.info(s"retrieveTranslationStatus")

for {
tables <- repository.retrieveTables()
tables <- repository.retrieveTables(isInternalCall = true)

tablesWithMultiLanguageColumnCount <- Future.sequence(
tables.map(
table =>
repository
.retrieveColumns(table)
.retrieveColumns(table, isInternalCall = true)
.map(columns => {
val multiLanguageColumnsCount = columns.count({
case MultiLanguageColumn(_) => true
Expand All @@ -147,7 +147,10 @@ class TableauxController(
case _ => false
})

tablesWithCellAnnotationCount <- repository.retrieveTablesWithCellAnnotationCount(relevantTables.unzip._1)
tablesForWhichViewIsGranted = roleModel
.filterDomainObjects[Table](ScopeTable, relevantTables.map(_._1), isInternalCall = false)

tablesWithCellAnnotationCount <- repository.retrieveTablesWithCellAnnotationCount(relevantTables.map(_._1))
} yield {
val tablesWithMultiLanguageColumnCountMap = tablesWithMultiLanguageColumnCount.toMap

Expand Down Expand Up @@ -185,13 +188,18 @@ class TableauxController(
(table, multiLanguageColumnsCount, totalSize, needsTranslationStatusByLangtag)
})

val translationStatusByTableJson = translationStatusByTable.map({
case (table, _, _, needsTranslationStatusForLangtags) =>
table.getJson.mergeIn(
Json.obj(
"translationStatus" -> Json.obj(needsTranslationStatusForLangtags: _*)
))
})
val translationStatusByTableJson = translationStatusByTable
.filter({
case (table, _, _, _) if tablesForWhichViewIsGranted.contains(table) => true
case _ => false
})
.map({
case (table, _, _, needsTranslationStatusForLangtags) =>
table.getJson.mergeIn(
Json.obj(
"translationStatus" -> Json.obj(needsTranslationStatusForLangtags: _*)
))
})

val mergedLangtags = tables.foldLeft(Seq.empty[String])({
case (langtagsAcc, table) =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ sealed trait StructureDelegateModel extends DatabaseQuery {
structureModel.tableStruc.retrieve(tableId, isInternalCall)
}

def retrieveTables(): Future[Seq[Table]] = {
structureModel.tableStruc.retrieveAll()
def retrieveTables(isInternalCall: Boolean = false): Future[Seq[Table]] = {
structureModel.tableStruc.retrieveAll(isInternalCall)
}

def createColumns(table: Table, columns: Seq[CreateColumn]): Future[Seq[ColumnType[_]]] = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,11 +164,12 @@ class TableModel(val connection: DatabaseConnection)(
valueOpt.map(value => Json.fromArrayString(value).asScala.map(_.toString).toSeq).getOrElse(Seq.empty))
}

def retrieveAll(): Future[Seq[Table]] = {
def retrieveAll(isInternalCall: Boolean): Future[Seq[Table]] = {
for {
defaultLangtags <- retrieveGlobalLangtags()
tables <- getTablesWithDisplayInfos(defaultLangtags)
filteredTables: Seq[Table] = roleModel.filterDomainObjects[Table](ScopeTable, tables, isInternalCall = false)
filteredTables: Seq[Table] = roleModel
.filterDomainObjects[Table](ScopeTable, tables, isInternalCall = isInternalCall)
} yield filteredTables
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import io.vertx.scala.SQLConnection
import org.junit.Assert._
import org.junit.Test
import org.junit.runner.RunWith
import org.vertx.scala.core.json.{Json, JsonObject}
import org.vertx.scala.core.json.{Json, JsonArray, JsonObject}

trait TableauxControllerAuthTest extends TableauxTestBase {

Expand Down Expand Up @@ -2029,3 +2029,98 @@ class TableauxControllerAuthTest_attachmentCell extends MediaTestBase with Table
}

}

@RunWith(classOf[VertxUnitRunner])
class TableauxControllerAuthTest_translation extends TableauxControllerAuthTest {

@Test
def retrieveTranslationStatus_forAllTables_ok(implicit c: TestContext): Unit =
okTest {

val roleModel = initRoleModel("""
|{
| "view-only-table-1": [
| {
| "type": "grant",
| "action": ["view"],
| "scope": "table"
| }
| ]
|}""".stripMargin)

val controller = createTableauxController(roleModel)

val annotation =
Json.fromObjectString("""{"langtags": ["en-GB"], "type": "flag", "value": "needs_translation"}""")

for {
_ <- createTestTable("table1")
_ <- createTestTable("table2")
_ <- sendRequest("POST", s"/tables/1/columns/2/rows/1/annotations", annotation)
_ <- sendRequest("POST", s"/tables/1/columns/2/rows/2/annotations", annotation)

translationStatus <- controller.retrieveTranslationStatus()
} yield {
val globalTranslationStatus = Json.obj("de-DE" -> 1.0, "en-GB" -> 0.75)
val table1TranslationStatus = Json.obj("de-DE" -> 1.0, "en-GB" -> 0.5)
val table2TranslationStatus = Json.obj("de-DE" -> 1.0, "en-GB" -> 1.0)

val tablesTranslationStatus: JsonArray = translationStatus.getJson.getJsonArray("tables")

assertJSONEquals(globalTranslationStatus, translationStatus.getJson.getJsonObject("translationStatus"))
assertEquals(2, tablesTranslationStatus.size())

assertJSONEquals(table1TranslationStatus,
tablesTranslationStatus.getJsonObject(0).getJsonObject("translationStatus"))
assertJSONEquals(table2TranslationStatus,
tablesTranslationStatus.getJsonObject(1).getJsonObject("translationStatus"))
}
}

@Test
def retrieveTranslationStatus_forAllTablesEvenIfViewingIsNotGrantedForTable2_ok(implicit c: TestContext): Unit =
okTest {

val roleModel = initRoleModel("""
|{
| "view-only-table-2": [
| {
| "type": "grant",
| "action": ["view"],
| "scope": "table",
| "condition": {
| "table": {
| "id": "2"
| }
| }
| }
| ]
|}""".stripMargin)

val controller = createTableauxController(roleModel)

val annotation =
Json.fromObjectString("""{"langtags": ["en-GB"], "type": "flag", "value": "needs_translation"}""")

for {
_ <- createTestTable("table1")
_ <- createTestTable("table2")
_ <- sendRequest("POST", s"/tables/1/columns/2/rows/1/annotations", annotation)
_ <- sendRequest("POST", s"/tables/1/columns/2/rows/2/annotations", annotation)

translationStatus <- controller.retrieveTranslationStatus()
} yield {
val globalTranslationStatus = Json.obj("de-DE" -> 1.0, "en-GB" -> 0.75)
val table2TranslationStatus = Json.obj("de-DE" -> 1.0, "en-GB" -> 1.0)

val tablesTranslationStatus: JsonArray = translationStatus.getJson.getJsonArray("tables")

assertJSONEquals(globalTranslationStatus, translationStatus.getJson.getJsonObject("translationStatus"))
assertEquals(1, tablesTranslationStatus.size())

assertJSONEquals(table2TranslationStatus,
tablesTranslationStatus.getJsonObject(0).getJsonObject("translationStatus"))
}
}

}

0 comments on commit db1414c

Please sign in to comment.