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 protected and private modifiers to DAO hierarchy #6698

Merged
merged 3 commits into from
Dec 15, 2022
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 0 additions & 1 deletion app/WebKnossosModule.scala
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ class WebKnossosModule extends AbstractModule {
bind(classOf[UserService]).asEagerSingleton()
bind(classOf[TaskService]).asEagerSingleton()
bind(classOf[UserDAO]).asEagerSingleton()
bind(classOf[UserTeamRolesDAO]).asEagerSingleton()
bind(classOf[UserExperiencesDAO]).asEagerSingleton()
bind(classOf[UserDataSetConfigurationDAO]).asEagerSingleton()
bind(classOf[UserCache]).asEagerSingleton()
Expand Down
6 changes: 2 additions & 4 deletions app/controllers/InitialDataController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ class InitialDataController @Inject()(initialDataService: InitialDataService, si
class InitialDataService @Inject()(userService: UserService,
userDAO: UserDAO,
multiUserDAO: MultiUserDAO,
userTeamRolesDAO: UserTeamRolesDAO,
userExperiencesDAO: UserExperiencesDAO,
taskTypeDAO: TaskTypeDAO,
dataStoreDAO: DataStoreDAO,
Expand Down Expand Up @@ -182,9 +181,8 @@ Samplecountry
_ <- multiUserDAO.insertOne(multiUser)
_ <- userDAO.insertOne(user)
_ <- userExperiencesDAO.updateExperiencesForUser(user, Map("sampleExp" -> 10))
_ <- userTeamRolesDAO.insertTeamMembership(
user._id,
TeamMembership(organizationTeam._id, isTeamManager = isTeamManager))
_ <- userDAO.insertTeamMembership(user._id,
TeamMembership(organizationTeam._id, isTeamManager = isTeamManager))
_ = logger.info("Inserted default user")
} yield ()
}
Expand Down
14 changes: 6 additions & 8 deletions app/controllers/TeamController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,19 @@ import com.mohiva.play.silhouette.api.Silhouette
import com.scalableminds.util.tools.Fox
import io.swagger.annotations._
import models.team._
import models.user.UserTeamRolesDAO
import models.user.UserDAO
import oxalis.security.WkEnv
import play.api.i18n.Messages
import play.api.libs.json._
import utils.ObjectId
import javax.inject.Inject
import play.api.mvc.{Action, AnyContent}
import utils.ObjectId

import javax.inject.Inject
import scala.concurrent.ExecutionContext

@Api
class TeamController @Inject()(teamDAO: TeamDAO,
userTeamRolesDAO: UserTeamRolesDAO,
teamService: TeamService,
sil: Silhouette[WkEnv])(implicit ec: ExecutionContext)
class TeamController @Inject()(teamDAO: TeamDAO, userDAO: UserDAO, teamService: TeamService, sil: Silhouette[WkEnv])(
implicit ec: ExecutionContext)
extends Controller {

private def teamNameReads: Reads[String] =
Expand Down Expand Up @@ -46,7 +44,7 @@ class TeamController @Inject()(teamDAO: TeamDAO,
_ <- bool2Fox(!team.isOrganizationTeam) ?~> "team.delete.organizationTeam" ~> FORBIDDEN
_ <- teamService.assertNoReferences(teamIdValidated) ?~> "team.delete.inUse" ~> FORBIDDEN
_ <- teamDAO.deleteOne(teamIdValidated)
_ <- userTeamRolesDAO.removeTeamFromAllUsers(teamIdValidated)
_ <- userDAO.removeTeamFromAllUsers(teamIdValidated)
_ <- teamDAO.removeTeamFromAllDatasetsAndFolders(teamIdValidated)
} yield JsonOk(Messages("team.deleted"))
}
Expand Down
28 changes: 12 additions & 16 deletions app/models/annotation/Annotation.scala
Original file line number Diff line number Diff line change
Expand Up @@ -144,12 +144,12 @@ class AnnotationLayerDAO @Inject()(SQLClient: SQLClient)(implicit ec: ExecutionC
class AnnotationDAO @Inject()(sqlClient: SQLClient, annotationLayerDAO: AnnotationLayerDAO)(
implicit ec: ExecutionContext)
extends SQLDAO[Annotation, AnnotationsRow, Annotations](sqlClient) {
val collection = Annotations
protected val collection = Annotations

def idColumn(x: Annotations): Rep[String] = x._Id
def isDeletedColumn(x: Annotations): Rep[Boolean] = x.isdeleted
protected def idColumn(x: Annotations): Rep[String] = x._Id
protected def isDeletedColumn(x: Annotations): Rep[Boolean] = x.isdeleted

def parse(r: AnnotationsRow): Fox[Annotation] =
protected def parse(r: AnnotationsRow): Fox[Annotation] =
for {
state <- AnnotationState.fromString(r.state).toFox
typ <- AnnotationType.fromString(r.typ).toFox
Expand Down Expand Up @@ -180,7 +180,8 @@ class AnnotationDAO @Inject()(sqlClient: SQLClient, annotationLayerDAO: Annotati
)
}

override def anonymousReadAccessQ(sharingToken: Option[String]) = s"visibility = '${AnnotationVisibility.Public}'"
override protected def anonymousReadAccessQ(sharingToken: Option[String]) =
s"visibility = '${AnnotationVisibility.Public}'"

private def listAccessQ(requestingUserId: ObjectId): String =
s"""
Expand All @@ -206,7 +207,7 @@ class AnnotationDAO @Inject()(sqlClient: SQLClient, annotationLayerDAO: Annotati
)
"""

override def readAccessQ(requestingUserId: ObjectId): String =
override protected def readAccessQ(requestingUserId: ObjectId): String =
s"""(
visibility = '${AnnotationVisibility.Public}'
or (visibility = '${AnnotationVisibility.Internal}'
Expand All @@ -218,12 +219,12 @@ class AnnotationDAO @Inject()(sqlClient: SQLClient, annotationLayerDAO: Annotati
in (select _organization from webknossos.users_ where _id = '$requestingUserId' and isAdmin)
)"""

override def deleteAccessQ(requestingUserId: ObjectId) =
override protected def deleteAccessQ(requestingUserId: ObjectId) =
s"""(_team in (select _team from webknossos.user_team_roles where isTeamManager and _user = '$requestingUserId') or _user = '$requestingUserId'
or (select _organization from webknossos.teams where webknossos.teams._id = _team)
in (select _organization from webknossos.users_ where _id = '$requestingUserId' and isAdmin))"""

override def updateAccessQ(requestingUserId: ObjectId): String =
override protected def updateAccessQ(requestingUserId: ObjectId): String =
deleteAccessQ(requestingUserId)

// read operations
Expand Down Expand Up @@ -543,20 +544,16 @@ class AnnotationDAO @Inject()(sqlClient: SQLClient, annotationLayerDAO: Annotati
_ <- run(
sqlu"insert into webknossos.annotation_contributors (_annotation, _user) values($id, $userId) on conflict do nothing")
} yield ()
}

class SharedAnnotationsDAO @Inject()(annotationDAO: AnnotationDAO, sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SimpleSQLDAO(sqlClient) {

// Does not use access query (because they dont support prefixes). Use only after separate access check!
def findAllSharedForTeams(teams: List[ObjectId]): Fox[List[Annotation]] =
for {
result <- run(
sql"""select distinct #${annotationDAO.columnsWithPrefix("a.")} from webknossos.annotations_ a
sql"""select distinct #${columnsWithPrefix("a.")} from webknossos.annotations_ a
join webknossos.annotation_sharedTeams l on a._id = l._annotation
where l._team in #${writeStructTupleWithQuotes(teams.map(t => sanitize(t.toString)))}"""
.as[AnnotationsRow])
parsed <- Fox.combined(result.toList.map(annotationDAO.parse))
parsed <- Fox.combined(result.toList.map(parse))
} yield parsed

def updateTeamsForSharedAnnotation(annotationId: ObjectId, teams: List[ObjectId])(
Expand All @@ -568,11 +565,10 @@ class SharedAnnotationsDAO @Inject()(annotationDAO: AnnotationDAO, sqlClient: SQ

val composedQuery = DBIO.sequence(List(clearQuery) ++ insertQueries)
for {
_ <- annotationDAO.assertUpdateAccess(annotationId)
_ <- assertUpdateAccess(annotationId)
_ <- run(composedQuery.transactionally.withTransactionIsolation(Serializable),
retryCount = 50,
retryIfErrorContains = List(transactionSerializationError))
} yield ()
}

}
10 changes: 5 additions & 5 deletions app/models/annotation/AnnotationPrivateLink.scala
Original file line number Diff line number Diff line change
Expand Up @@ -45,13 +45,13 @@ class AnnotationPrivateLinkService @Inject()()(implicit ec: ExecutionContext) {

class AnnotationPrivateLinkDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SQLDAO[AnnotationPrivateLink, AnnotationPrivatelinksRow, AnnotationPrivatelinks](sqlClient) {
val collection = AnnotationPrivatelinks
protected val collection = AnnotationPrivatelinks

def idColumn(x: AnnotationPrivatelinks): Rep[String] = x._Id
protected def idColumn(x: AnnotationPrivatelinks): Rep[String] = x._Id

def isDeletedColumn(x: AnnotationPrivatelinks): Rep[Boolean] = x.isdeleted
protected def isDeletedColumn(x: AnnotationPrivatelinks): Rep[Boolean] = x.isdeleted

def parse(r: AnnotationPrivatelinksRow): Fox[AnnotationPrivateLink] =
protected def parse(r: AnnotationPrivatelinksRow): Fox[AnnotationPrivateLink] =
Fox.successful(
AnnotationPrivateLink(
ObjectId(r._Id),
Expand All @@ -62,7 +62,7 @@ class AnnotationPrivateLinkDAO @Inject()(sqlClient: SQLClient)(implicit ec: Exec
)
)

override def readAccessQ(requestingUserId: ObjectId): String =
override protected def readAccessQ(requestingUserId: ObjectId): String =
s"""(_annotation in (select _id from webknossos.annotations_ where _user = '${requestingUserId.id}'))"""

def insertOne(aPL: AnnotationPrivateLink): Fox[Unit] =
Expand Down
7 changes: 3 additions & 4 deletions app/models/annotation/AnnotationService.scala
Original file line number Diff line number Diff line change
Expand Up @@ -105,8 +105,7 @@ class AnnotationService @Inject()(
nmlWriter: NmlWriter,
temporaryFileCreator: TemporaryFileCreator,
meshDAO: MeshDAO,
meshService: MeshService,
sharedAnnotationsDAO: SharedAnnotationsDAO
meshService: MeshService
)(implicit ec: ExecutionContext, val materializer: Materializer)
extends BoxImplicits
with FoxImplicits
Expand Down Expand Up @@ -589,11 +588,11 @@ class AnnotationService @Inject()(

// Does not use access query (because they dont support prefixes). Use only after separate access check!
def sharedAnnotationsFor(userTeams: List[ObjectId]): Fox[List[Annotation]] =
sharedAnnotationsDAO.findAllSharedForTeams(userTeams)
annotationDAO.findAllSharedForTeams(userTeams)

def updateTeamsForSharedAnnotation(annotationId: ObjectId, teams: List[ObjectId])(
implicit ctx: DBAccessContext): Fox[Unit] =
sharedAnnotationsDAO.updateTeamsForSharedAnnotation(annotationId, teams)
annotationDAO.updateTeamsForSharedAnnotation(annotationId, teams)

def zipAnnotations(annotations: List[Annotation], zipFileName: String, skipVolumeData: Boolean)(
implicit
Expand Down
8 changes: 4 additions & 4 deletions app/models/annotation/TracingStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -59,12 +59,12 @@ class TracingStoreService @Inject()(tracingStoreDAO: TracingStoreDAO, rpc: RPC)(

class TracingStoreDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SQLDAO[TracingStore, TracingstoresRow, Tracingstores](sqlClient) {
val collection = Tracingstores
protected val collection = Tracingstores

def idColumn(x: Tracingstores): Rep[String] = x.name
def isDeletedColumn(x: Tracingstores): Rep[Boolean] = x.isdeleted
protected def idColumn(x: Tracingstores): Rep[String] = x.name
protected def isDeletedColumn(x: Tracingstores): Rep[Boolean] = x.isdeleted

def parse(r: TracingstoresRow): Fox[TracingStore] =
protected def parse(r: TracingstoresRow): Fox[TracingStore] =
Fox.successful(
TracingStore(
r.name,
Expand Down
12 changes: 6 additions & 6 deletions app/models/binary/DataSet.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,11 +62,11 @@ class DataSetDAO @Inject()(sqlClient: SQLClient,
dataSetDataLayerDAO: DataSetDataLayerDAO,
organizationDAO: OrganizationDAO)(implicit ec: ExecutionContext)
extends SQLDAO[DataSet, DatasetsRow, Datasets](sqlClient) {
val collection = Datasets
protected val collection = Datasets

def idColumn(x: Datasets): Rep[String] = x._Id
protected def idColumn(x: Datasets): Rep[String] = x._Id

def isDeletedColumn(x: Datasets): Rep[Boolean] = x.isdeleted
protected def isDeletedColumn(x: Datasets): Rep[Boolean] = x.isdeleted

private def parseScaleOpt(literalOpt: Option[String]): Fox[Option[Vec3Double]] = literalOpt match {
case Some(literal) =>
Expand All @@ -79,7 +79,7 @@ class DataSetDAO @Inject()(sqlClient: SQLClient,
private def writeScaleLiteral(scale: Vec3Double): String =
writeStructTuple(List(scale.x, scale.y, scale.z).map(_.toString))

def parse(r: DatasetsRow): Fox[DataSet] =
protected def parse(r: DatasetsRow): Fox[DataSet] =
for {
scale <- parseScaleOpt(r.scale)
defaultViewConfigurationOpt <- Fox.runOptional(r.defaultviewconfiguration)(
Expand Down Expand Up @@ -405,7 +405,7 @@ class DataSetDAO @Inject()(sqlClient: SQLClient,

class DataSetResolutionsDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SimpleSQLDAO(sqlClient) {
def parseRow(row: DatasetResolutionsRow): Fox[Vec3Int] =
private def parseRow(row: DatasetResolutionsRow): Fox[Vec3Int] =
for {
resolution <- Vec3Int.fromList(parseArrayTuple(row.resolution).map(_.toInt)) ?~> "could not parse resolution"
} yield resolution
Expand Down Expand Up @@ -447,7 +447,7 @@ class DataSetDataLayerDAO @Inject()(sqlClient: SQLClient, dataSetResolutionsDAO:
implicit ec: ExecutionContext)
extends SimpleSQLDAO(sqlClient) {

def parseRow(row: DatasetLayersRow, dataSetId: ObjectId, skipResolutions: Boolean = false): Fox[DataLayer] = {
private def parseRow(row: DatasetLayersRow, dataSetId: ObjectId, skipResolutions: Boolean): Fox[DataLayer] = {
val result: Fox[Fox[DataLayer]] = for {
category <- Category.fromString(row.category).toFox ?~> "Could not parse Layer Category"
boundingBox <- BoundingBox
Expand Down
10 changes: 5 additions & 5 deletions app/models/binary/DataStore.scala
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,15 @@ class DataStoreService @Inject()(dataStoreDAO: DataStoreDAO)(implicit ec: Execut

class DataStoreDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SQLDAO[DataStore, DatastoresRow, Datastores](sqlClient) {
val collection = Datastores
protected val collection = Datastores

def idColumn(x: Datastores): Rep[String] = x.name
def isDeletedColumn(x: Datastores): Rep[Boolean] = x.isdeleted
protected def idColumn(x: Datastores): Rep[String] = x.name
protected def isDeletedColumn(x: Datastores): Rep[Boolean] = x.isdeleted

override def readAccessQ(requestingUserId: ObjectId): String =
override protected def readAccessQ(requestingUserId: ObjectId): String =
s"(onlyAllowedOrganization is null) OR (onlyAllowedOrganization in (select _organization from webknossos.users_ where _id = '$requestingUserId'))"

def parse(r: DatastoresRow): Fox[DataStore] =
protected def parse(r: DatastoresRow): Fox[DataStore] =
Fox.successful(
DataStore(
r.name,
Expand Down
8 changes: 4 additions & 4 deletions app/models/binary/Publication.scala
Original file line number Diff line number Diff line change
Expand Up @@ -54,13 +54,13 @@ class PublicationService @Inject()(dataSetService: DataSetService,

class PublicationDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SQLDAO[Publication, PublicationsRow, Publications](sqlClient) {
val collection = Publications
protected val collection = Publications

def idColumn(x: Publications): Rep[String] = x._Id
protected def idColumn(x: Publications): Rep[String] = x._Id

def isDeletedColumn(x: Publications): Rep[Boolean] = x.isdeleted
protected def isDeletedColumn(x: Publications): Rep[Boolean] = x.isdeleted

def parse(r: PublicationsRow): Fox[Publication] =
protected def parse(r: PublicationsRow): Fox[Publication] =
Fox.successful(
Publication(
ObjectId(r._Id),
Expand Down
16 changes: 8 additions & 8 deletions app/models/folder/Folder.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,22 +75,22 @@ class FolderService @Inject()(teamDAO: TeamDAO,
class FolderDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SQLDAO[Folder, FoldersRow, Folders](sqlClient) {

val collection = Folders
def idColumn(x: Folders): Rep[String] = x._Id
def isDeletedColumn(x: Folders): Rep[Boolean] = x.isdeleted
protected val collection = Folders
protected def idColumn(x: Folders): Rep[String] = x._Id
protected def isDeletedColumn(x: Folders): Rep[Boolean] = x.isdeleted

def parse(r: FoldersRow): Fox[Folder] =
protected def parse(r: FoldersRow): Fox[Folder] =
Fox.successful(Folder(ObjectId(r._Id), r.name))

def parseWithParent(t: (String, String, Option[String])): Fox[FolderWithParent] =
private def parseWithParent(t: (String, String, Option[String])): Fox[FolderWithParent] =
Fox.successful(FolderWithParent(ObjectId(t._1), t._2, t._3.map(ObjectId(_))))

override def readAccessQ(requestingUserId: ObjectId): String = readAccessQWithPrefix(requestingUserId, "")
override protected def readAccessQ(requestingUserId: ObjectId): String = readAccessQWithPrefix(requestingUserId, "")

def readAccessQWithPrefix(requestingUserId: ObjectId, prefix: String): String =
private def readAccessQWithPrefix(requestingUserId: ObjectId, prefix: String): String =
rawAccessQ(write = false, requestingUserId, prefix)

override def updateAccessQ(requestingUserId: ObjectId): String =
override protected def updateAccessQ(requestingUserId: ObjectId): String =
rawAccessQ(write = true, requestingUserId, prefix = "")

private def rawAccessQ(write: Boolean, requestingUserId: ObjectId, prefix: String): String = {
Expand Down
10 changes: 5 additions & 5 deletions app/models/job/Job.scala
Original file line number Diff line number Diff line change
Expand Up @@ -92,12 +92,12 @@ case class Job(

class JobDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SQLDAO[Job, JobsRow, Jobs](sqlClient) {
val collection = Jobs
protected val collection = Jobs

def idColumn(x: Jobs): Rep[String] = x._Id
def isDeletedColumn(x: Jobs): Rep[Boolean] = x.isdeleted
protected def idColumn(x: Jobs): Rep[String] = x._Id
protected def isDeletedColumn(x: Jobs): Rep[Boolean] = x.isdeleted

def parse(r: JobsRow): Fox[Job] =
protected def parse(r: JobsRow): Fox[Job] =
for {
manualStateOpt <- Fox.runOptional(r.manualstate)(JobState.fromString)
state <- JobState.fromString(r.state)
Expand All @@ -120,7 +120,7 @@ class JobDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
)
}

override def readAccessQ(requestingUserId: ObjectId) =
override protected def readAccessQ(requestingUserId: ObjectId) =
s"""_owner = '$requestingUserId'"""

override def findAll(implicit ctx: DBAccessContext): Fox[List[Job]] =
Expand Down
8 changes: 4 additions & 4 deletions app/models/job/Worker.scala
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ case class Worker(_id: ObjectId,

class WorkerDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SQLDAO[Worker, WorkersRow, Workers](sqlClient) {
val collection = Workers
protected val collection = Workers

def idColumn(x: Workers): Rep[String] = x._Id
protected def idColumn(x: Workers): Rep[String] = x._Id

def isDeletedColumn(x: Workers): Rep[Boolean] = x.isdeleted
protected def isDeletedColumn(x: Workers): Rep[Boolean] = x.isdeleted

def parse(r: WorkersRow): Fox[Worker] =
protected def parse(r: WorkersRow): Fox[Worker] =
Fox.successful(
Worker(
ObjectId(r._Id),
Expand Down
10 changes: 5 additions & 5 deletions app/models/mesh/Mesh.scala
Original file line number Diff line number Diff line change
Expand Up @@ -55,19 +55,19 @@ class MeshService @Inject()()(implicit ec: ExecutionContext) {

class MeshDAO @Inject()(sqlClient: SQLClient)(implicit ec: ExecutionContext)
extends SQLDAO[MeshInfo, MeshesRow, Meshes](sqlClient) {
val collection = Meshes
protected val collection = Meshes

def idColumn(x: Meshes): Rep[String] = x._Id
protected def idColumn(x: Meshes): Rep[String] = x._Id

def isDeletedColumn(x: Meshes): Rep[Boolean] = x.isdeleted
protected def isDeletedColumn(x: Meshes): Rep[Boolean] = x.isdeleted

private val infoColumns = (columnsList diff Seq("data")).mkString(", ")
type InfoTuple = (ObjectId, ObjectId, String, String, Instant, Boolean)

override def parse(r: MeshesRow): Fox[MeshInfo] =
override protected def parse(r: MeshesRow): Fox[MeshInfo] =
Fox.failure("not implemented, use parseInfo or get the data directly")

def parseInfo(r: InfoTuple): Fox[MeshInfo] =
private def parseInfo(r: InfoTuple): Fox[MeshInfo] =
for {
position <- Vec3Int.fromList(parseArrayTuple(r._4).map(_.toInt)) ?~> "could not parse mesh position"
} yield {
Expand Down
Loading