Skip to content

Commit

Permalink
Move organization queries into dao generator
Browse files Browse the repository at this point in the history
  • Loading branch information
mbryzek committed Aug 1, 2024
1 parent 41d43fa commit f217c85
Show file tree
Hide file tree
Showing 58 changed files with 1,158 additions and 667 deletions.
22 changes: 11 additions & 11 deletions api/app/controllers/ApiBuilderController.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package controllers

import javax.inject.Inject
import com.google.inject.ImplementedBy
import db.{Authorization, MembershipsDao, OrganizationsDao}
import io.apibuilder.api.v0.models.{Organization, User}
import db.{Authorization, InternalOrganization, InternalOrganizationsDao, MembershipsDao}
import io.apibuilder.api.v0.models.User
import io.apibuilder.api.v0.models.json._
import io.apibuilder.common.v0.models.MembershipRole
import lib.{RequestAuthenticationUtil, Validation}
import play.api.libs.json.{JsValue, Json}
import play.api.mvc._

import javax.inject.Inject
import scala.concurrent.{ExecutionContext, Future}

/**
Expand All @@ -28,9 +28,9 @@ trait ApiBuilderController extends BaseController {
def controllerComponents: ControllerComponents = apiBuilderControllerComponents.controllerComponents

def membershipsDao: MembershipsDao = apiBuilderControllerComponents.membershipsDao
def organizationsDao: OrganizationsDao = apiBuilderControllerComponents.organizationsDao
def organizationsDao: InternalOrganizationsDao = apiBuilderControllerComponents.organizationsDao

def withOrg(auth: Authorization, orgKey: String)(f: Organization => Result): Result = {
def withOrg(auth: Authorization, orgKey: String)(f: InternalOrganization => Result): Result = {
organizationsDao.findByKey(auth, orgKey) match {
case None => Results.NotFound(
jsonError(s"Organization[$orgKey] does not exist or you are not authorized to access it")
Expand All @@ -42,25 +42,25 @@ trait ApiBuilderController extends BaseController {
}
}

def withOrgMember(user: User, orgKey: String)(f: Organization => Result): Result = {
def withOrgMember(user: User, orgKey: String)(f: InternalOrganization => Result): Result = {
withOrg(Authorization.User(user.guid), orgKey) { org =>
withRole(org, user, MembershipRole.all) {
f(org)
}
}
}

def withOrgAdmin(user: User, orgKey: String)(f: Organization => Result): Result = {
def withOrgAdmin(user: User, orgKey: String)(f: InternalOrganization => Result): Result = {
withOrg(Authorization.User(user.guid), orgKey) { org =>
withRole(org, user, Seq(MembershipRole.Admin)) {
f(org)
}
}
}

private def withRole(org: Organization, user: User, roles: Seq[MembershipRole])(f: => Result): Result = {
private def withRole(org: InternalOrganization, user: User, roles: Seq[MembershipRole])(f: => Result): Result = {
val actualRoles = membershipsDao.findByOrganizationAndUserAndRoles(
Authorization.All, org, user, roles
Authorization.All, org.reference, user, roles
).map(_.role)

if (actualRoles.isEmpty) {
Expand Down Expand Up @@ -92,15 +92,15 @@ trait ApiBuilderControllerComponents {
def identifiedActionBuilder: IdentifiedActionBuilder
def controllerComponents: ControllerComponents
def membershipsDao: MembershipsDao
def organizationsDao: OrganizationsDao
def organizationsDao: InternalOrganizationsDao
}

class ApiBuilderDefaultControllerComponents @Inject() (
val controllerComponents: ControllerComponents,
val anonymousActionBuilder: AnonymousActionBuilder,
val identifiedActionBuilder: IdentifiedActionBuilder,
val membershipsDao: MembershipsDao,
val organizationsDao: OrganizationsDao
val organizationsDao: InternalOrganizationsDao
) extends ApiBuilderControllerComponents

case class AnonymousRequest[A](
Expand Down
8 changes: 4 additions & 4 deletions api/app/controllers/Applications.scala
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,10 @@ import java.util.UUID

@Singleton
class Applications @Inject() (
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
applicationsDao: ApplicationsDao,
versionsDao: VersionsDao,
model: ApplicationsModel
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
applicationsDao: InternalApplicationsDao,
versionsDao: VersionsDao,
model: ApplicationsModel
) extends ApiBuilderController {

def get(
Expand Down
19 changes: 10 additions & 9 deletions api/app/controllers/Domains.scala
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
package controllers

import io.apibuilder.api.v0.models.json._
import db.OrganizationDomainsDao
import io.apibuilder.api.v0.models.Domain
import db.{MembershipsDao, OrganizationDomainsDao, OrganizationsDao}
import javax.inject.{Inject, Singleton}

import io.apibuilder.api.v0.models.json._
import lib.Validation
import play.api.mvc._
import play.api.libs.json._
import play.api.mvc._

import javax.inject.{Inject, Singleton}

@Singleton
class Domains @Inject() (
Expand Down Expand Up @@ -42,10 +42,11 @@ class Domains @Inject() (

def deleteByName(orgKey: String, name: String): Action[AnyContent] = Identified { request =>
withOrgAdmin(request.user, orgKey) { org =>
org.domains.find(_.name == name).foreach { domain =>
organizationDomainsDao.findAll(organizationGuid = Some(org.guid), domain = Some(domain.name)).foreach { orgDomain =>
organizationDomainsDao.softDelete(request.user, orgDomain)
}
organizationDomainsDao.findAll(
organizationGuid = Some(org.guid),
domain = Some(name)
).foreach { domain =>
organizationDomainsDao.softDelete(request.user, domain)
}
NoContent
}
Expand Down
6 changes: 3 additions & 3 deletions api/app/controllers/Healthchecks.scala
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
package controllers

import db.{Authorization, OrganizationsDao, VersionsDao}
import db.{Authorization, InternalOrganizationsDao, VersionsDao}
import play.api.libs.json._
import play.api.mvc._

import javax.inject.{Inject, Named, Singleton}

@Singleton
class Healthchecks @Inject() (
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
organizationsDao: OrganizationsDao,
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
organizationsDao: InternalOrganizationsDao,
) extends ApiBuilderController {

private val Result = Json.toJson(Map("status" -> "healthy"))
Expand Down
14 changes: 7 additions & 7 deletions api/app/controllers/MembershipRequests.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package controllers

import db.{MembershipRequestsDao, OrganizationsDao, UsersDao}
import db.{MembershipRequestsDao, InternalOrganizationsDao, UsersDao}
import io.apibuilder.api.v0.models.json._
import io.apibuilder.api.v0.models.{Organization, User}
import io.apibuilder.common.v0.models.MembershipRole
Expand All @@ -14,11 +14,11 @@ import javax.inject.{Inject, Singleton}

@Singleton
class MembershipRequests @Inject() (
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
membershipRequestsDao: MembershipRequestsDao,
organizationsDao: OrganizationsDao,
usersDao: UsersDao,
model: MembershipRequestsModel
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
membershipRequestsDao: MembershipRequestsDao,
organizationsDao: InternalOrganizationsDao,
usersDao: UsersDao,
model: MembershipRequestsModel
) extends ApiBuilderController {

case class MembershipRequestForm(
Expand Down Expand Up @@ -64,7 +64,7 @@ class MembershipRequests @Inject() (
Conflict(Json.toJson(Validation.error("Organization not found or not authorized to make changes to this org")))
}

case Some(org: Organization) => {
case Some(org) => {
usersDao.findByGuid(form.user_guid) match {

case None => {
Expand Down
4 changes: 2 additions & 2 deletions api/app/controllers/Memberships.scala
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package controllers

import db.MembershipsDao
import db.{MembershipsDao, OrganizationReference}
import io.apibuilder.api.v0.models.json._
import io.apibuilder.common.v0.models.MembershipRole
import models.MembershipsModel
Expand Down Expand Up @@ -46,7 +46,7 @@ class Memberships @Inject() (
membershipsDao.findByGuid(request.authorization, guid).flatMap(model.toModel) match {
case None => NotFound
case Some(membership) => {
if (membershipsDao.isUserAdmin(user = request.user, organization = membership.organization)) {
if (membershipsDao.isUserAdmin(user = request.user, organization = OrganizationReference(membership.organization))) {
Ok(Json.toJson(membership))
} else {
Unauthorized
Expand Down
33 changes: 19 additions & 14 deletions api/app/controllers/Organizations.scala
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,20 @@ import io.apibuilder.api.v0.models._
import io.apibuilder.api.v0.models.json._
import lib.Validation
import db._
import javax.inject.{Inject, Singleton}
import models.OrganizationsModel

import javax.inject.{Inject, Singleton}
import play.api.mvc._
import play.api.libs.json._

import java.util.UUID

@Singleton
class Organizations @Inject() (
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
attributesDao: AttributesDao,
organizationAttributeValuesDao: OrganizationAttributeValuesDao
organizationAttributeValuesDao: OrganizationAttributeValuesDao,
model: OrganizationsModel
) extends ApiBuilderController {

def get(
Expand All @@ -28,23 +31,25 @@ class Organizations @Inject() (
): Action[AnyContent] = Anonymous { request =>
Ok(
Json.toJson(
organizationsDao.findAll(
request.authorization,
userGuid = userGuid,
guid = guid,
key = key,
name = name,
namespace = namespace,
limit = Some(limit),
offset = offset
model.toModels(
organizationsDao.findAll(
request.authorization,
userGuid = userGuid,
guid = guid,
key = key,
name = name,
namespace = namespace,
limit = Some(limit),
offset = offset
)
)
)
)
}

def getByKey(key: String): Action[AnyContent] = Anonymous { request =>
withOrg(request.authorization, key) { org =>
Ok(Json.toJson(org))
Ok(Json.toJson(model.toModel(org)))
}
}

Expand All @@ -58,7 +63,7 @@ class Organizations @Inject() (
val errors = organizationsDao.validate(form)
if (errors.isEmpty) {
val org = organizationsDao.createWithAdministrator(request.user, form)
Ok(Json.toJson(org))
Ok(Json.toJson(model.toModel(org)))
} else {
Conflict(Json.toJson(errors))
}
Expand All @@ -79,7 +84,7 @@ class Organizations @Inject() (
val errors = organizationsDao.validate(form, Some(existing))
if (errors.isEmpty) {
val org = organizationsDao.update(request.user, existing, form)
Ok(Json.toJson(org))
Ok(Json.toJson(model.toModel(org)))
} else {
Conflict(Json.toJson(errors))
}
Expand Down
20 changes: 10 additions & 10 deletions api/app/controllers/Versions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,13 @@ import javax.inject.{Inject, Singleton}

@Singleton
class Versions @Inject() (
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
apiBuilderServiceImportResolver: ApiBuilderServiceImportResolver,
applicationsDao: ApplicationsDao,
databaseServiceFetcher: DatabaseServiceFetcher,
versionsDao: VersionsDao,
versionValidator: VersionValidator,
model: VersionsModel,
val apiBuilderControllerComponents: ApiBuilderControllerComponents,
apiBuilderServiceImportResolver: ApiBuilderServiceImportResolver,
applicationsDao: InternalApplicationsDao,
databaseServiceFetcher: DatabaseServiceFetcher,
versionsDao: VersionsDao,
versionValidator: VersionValidator,
model: VersionsModel,
) extends ApiBuilderController {

private val DefaultVisibility = Visibility.Organization
Expand Down Expand Up @@ -205,7 +205,7 @@ class Versions @Inject() (

private def upsertVersion(
user: User,
org: Organization,
org: InternalOrganization,
versionName: String,
form: VersionForm,
original: Original,
Expand Down Expand Up @@ -253,11 +253,11 @@ class Versions @Inject() (
}

private def toServiceConfiguration(
org: Organization,
org: InternalOrganization,
version: String
) = ServiceConfiguration(
orgKey = org.key,
orgNamespace = org.namespace,
orgNamespace = org.db.namespace,
version = version
)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package db
import anorm._
import cats.data.ValidatedNec
import cats.implicits._
import io.apibuilder.api.v0.models.{AppSortBy, ApplicationForm, Error, MoveForm, Organization, SortOrder, User, Version, Visibility}
import io.apibuilder.api.v0.models.{AppSortBy, ApplicationForm, Error, MoveForm, SortOrder, User, Version, Visibility}
import io.apibuilder.common.v0.models.{Audit, ReferenceGuid}
import io.apibuilder.task.v0.models.{EmailDataApplicationCreated, TaskType}
import io.flow.postgresql.Query
Expand All @@ -27,11 +27,10 @@ case class InternalApplication(
audit: Audit
)

@Singleton
class ApplicationsDao @Inject() (
class InternalApplicationsDao @Inject()(
@NamedDatabase("default") db: Database,
emailQueue: EmailProcessorQueue,
organizationsDao: OrganizationsDao,
organizationsDao: InternalOrganizationsDao,
tasksDao: InternalTasksDao,
) {

Expand Down Expand Up @@ -96,7 +95,7 @@ class ApplicationsDao @Inject() (
where guid = {guid}::uuid
"""

private def validateOrganizationByKey(auth: Authorization, key: String): ValidatedNec[Error, Organization] = {
private def validateOrganizationByKey(auth: Authorization, key: String): ValidatedNec[Error, InternalOrganization] = {
organizationsDao.findByKey(auth, key).toValidNec(Validation.singleError(s"Organization[$key] not found"))
}

Expand All @@ -105,7 +104,7 @@ class ApplicationsDao @Inject() (
authorization: Authorization,
app: InternalApplication,
form: MoveForm
): ValidatedNec[Error, Organization] = {
): ValidatedNec[Error, InternalOrganization] = {
validateOrganizationByKey(authorization, form.orgKey).andThen { org =>
findByOrganizationKeyAndApplicationKey(Authorization.All, org.key, app.key) match {
case None => org.validNec
Expand All @@ -121,7 +120,7 @@ class ApplicationsDao @Inject() (
}

def validate(
org: Organization,
org: InternalOrganization,
form: ApplicationForm,
existing: Option[InternalApplication] = None
): Seq[Error] = {
Expand Down Expand Up @@ -250,7 +249,7 @@ class ApplicationsDao @Inject() (

def create(
createdBy: User,
org: Organization,
org: InternalOrganization,
form: ApplicationForm
): InternalApplication = {
val errors = validate(org, form)
Expand Down Expand Up @@ -288,7 +287,7 @@ class ApplicationsDao @Inject() (
findAll(Authorization.User(user.guid), key = Some(app.key), limit = Some(1)).nonEmpty
}

private[db] def findByOrganizationAndName(authorization: Authorization, org: Organization, name: String): Option[InternalApplication] = {
private[db] def findByOrganizationAndName(authorization: Authorization, org: InternalOrganization, name: String): Option[InternalApplication] = {
findAll(authorization, orgKey = Some(org.key), name = Some(name), limit = Some(1)).headOption
}

Expand Down
Loading

0 comments on commit f217c85

Please sign in to comment.