Skip to content

Commit

Permalink
use cms directives inside emails
Browse files Browse the repository at this point in the history
  • Loading branch information
Zwiterrion committed Dec 4, 2024
1 parent 276e0a5 commit 1bed0a6
Show file tree
Hide file tree
Showing 28 changed files with 1,786 additions and 1,692 deletions.
20 changes: 20 additions & 0 deletions daikoku/app/actions/actions.scala
Original file line number Diff line number Diff line change
Expand Up @@ -89,6 +89,26 @@ case class DaikokuTenantActionContext[A](
override def user: User = null
}

case class DaikokuInternalActionMaybeWithoutUserContext[A](
requestPath: String,
requestQueryString: Map[String, Seq[String]],
requestMethod: String,
requestHeaders: Map[String, String],
user: Option[User],
tenant: Tenant,
session: Option[UserSession],
impersonator: Option[User],
isTenantAdmin: Boolean,
apiCreationPermitted: Boolean = false,
ctx: TrieMap[String, String] = new TrieMap[String, String]()
) {
def setCtxValue(key: String, value: Any): Unit = {
if (value != null) {
ctx.put(key, value.toString)
}
}
}

case class DaikokuActionMaybeWithoutUserContext[A](
request: Request[A],
user: Option[User],
Expand Down
4 changes: 2 additions & 2 deletions daikoku/app/audit/audit.scala
Original file line number Diff line number Diff line change
Expand Up @@ -404,8 +404,8 @@ class AuditActor(implicit
"mail.apikey.rotation.body",
tenant,
Map(
"apiName" -> api.name,
"planName" -> plan.customName.getOrElse(plan.typeName)
"apiName" -> JsString(api.name),
"planName" -> JsString(plan.customName.getOrElse(plan.typeName))
)
)
)
Expand Down
1 change: 1 addition & 0 deletions daikoku/app/controllers/AdminApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import play.api.http.HttpEntity
import play.api.libs.json._
import play.api.libs.streams.Accumulator
import play.api.mvc._
import services.CmsPage
import storage.drivers.postgres.PostgresDataStore
import storage.{DataStore, Repo}

Expand Down
28 changes: 14 additions & 14 deletions daikoku/app/controllers/ApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2955,10 +2955,10 @@ class ApiController(
"mail.api.access.body",
ctx.tenant,
Map(
"user" -> ctx.user.name,
"apiName" -> api.name,
"teamName" -> team.name,
"link" -> env.getDaikokuUrl(ctx.tenant, "/notifications")
"user" -> JsString(ctx.user.name),
"apiName" -> JsString(api.name),
"teamName" -> JsString(team.name),
"link" -> JsString(env.getDaikokuUrl(ctx.tenant, "/notifications"))
)
)
} yield {
Expand Down Expand Up @@ -3715,13 +3715,13 @@ class ApiController(
"mail.create.post.body",
ctx.tenant,
Map(
"user" -> ctx.user.name,
"apiName" -> api.get.humanReadableId,
"teamName" -> api.get.team.value, //not sure
"link" -> env.getDaikokuUrl(
"user" -> JsString(ctx.user.name),
"apiName" -> JsString(api.get.humanReadableId),
"teamName" -> JsString(api.get.team.value), //not sure
"link" -> JsString(env.getDaikokuUrl(
ctx.tenant,
"/" + api.get.team.value + "/" + api.get.humanReadableId + "/" + api.get.currentVersion.value + "/news"
) //same
)) //same
)
)
} yield {
Expand Down Expand Up @@ -4131,13 +4131,13 @@ class ApiController(
"mail.new.issue.body",
ctx.tenant,
Map(
"user" -> ctx.user.name,
"apiName" -> api.name,
"teamName" -> api.team.value, // not sure if it's okay
"link" -> env.getDaikokuUrl(
"user" -> JsString(ctx.user.name),
"apiName" -> JsString(api.name),
"teamName" -> JsString(api.team.value), // not sure if it's okay
"link" -> JsString(env.getDaikokuUrl(
ctx.tenant,
"/" + api.team.value + "/" + api.humanReadableId + "/" + api.currentVersion.value + "/issues"
) //same
)) //same
)
)
} yield {
Expand Down
1 change: 0 additions & 1 deletion daikoku/app/controllers/AssetsController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ class TeamAssetsController(
)
}
case None =>
println("HEHE")
NotFound(Json.obj("error" -> "Asset not found!"))
}
}
Expand Down
19 changes: 3 additions & 16 deletions daikoku/app/controllers/CmsApiController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,9 @@ package fr.maif.otoroshi.daikoku.ctrls
import cats.data.EitherT
import cats.implicits.toBifunctorOps
import controllers.{AppError, Assets}
import fr.maif.otoroshi.daikoku.actions.{
ApiActionContext,
CmsApiAction,
DaikokuActionMaybeWithoutUser
}
import fr.maif.otoroshi.daikoku.audit.AuditTrailEvent
import fr.maif.otoroshi.daikoku.ctrls.authorizations.async.TenantAdminOnly
import fr.maif.otoroshi.daikoku.actions.{ApiActionContext, CmsApiAction, DaikokuActionMaybeWithoutUser}
import fr.maif.otoroshi.daikoku.domain.json.{CmsFileFormat, CmsPageFormat}
import fr.maif.otoroshi.daikoku.domain.{
CmsPage,
CmsPageId,
Tenant,
TenantMode,
User,
UserSession
}
import fr.maif.otoroshi.daikoku.domain.{CmsPageId, Tenant, TenantMode, User}
import fr.maif.otoroshi.daikoku.env.Env
import fr.maif.otoroshi.daikoku.logger.AppLogger
import fr.maif.otoroshi.daikoku.login.AuthProvider.{OAuth2, Otoroshi}
Expand All @@ -34,6 +21,7 @@ import org.apache.pekko.util.ByteString
import play.api.Logger
import play.api.libs.json._
import play.api.mvc._
import services.CmsPage
import storage.{DataStore, Repo}

import scala.collection.concurrent.TrieMap
Expand Down Expand Up @@ -89,7 +77,6 @@ class CmsApiController(
case raw: AnyContentAsRaw =>
Source.single(raw.raw.asBytes().getOrElse(ByteString.empty))
case e =>
println(e)
throw new IllegalArgumentException("Request body is not raw data")
}
}
Expand Down
24 changes: 20 additions & 4 deletions daikoku/app/controllers/HomeController.scala
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package fr.maif.otoroshi.daikoku.ctrls

import cats.implicits.catsSyntaxOptionId
import controllers.Assets
import daikoku.BuildInfo
import fr.maif.otoroshi.daikoku.actions.{DaikokuAction, DaikokuActionMaybeWithGuest, DaikokuActionMaybeWithoutUser, DaikokuActionMaybeWithoutUserContext}
Expand All @@ -12,8 +13,10 @@ import fr.maif.otoroshi.daikoku.logger.AppLogger
import fr.maif.otoroshi.daikoku.utils.Errors
import org.apache.pekko.http.scaladsl.util.FastFuture
import play.api.i18n.{I18nSupport, MessagesApi}
import play.api.libs
import play.api.libs.json._
import play.api.mvc._
import services.{CmsPage, CmsRequestRendering}

import scala.collection.mutable
import scala.concurrent.{ExecutionContext, Future}
Expand Down Expand Up @@ -186,8 +189,7 @@ class HomeController(
}
}
)
println("matching routes")
matchingRoutes.foreach(p => println(p._1.mkString(", ")))

if (matchingRoutes.nonEmpty)
matchingRoutes.map(p => (p._1.tail, p._2))
else {
Expand Down Expand Up @@ -323,7 +325,14 @@ class HomeController(
.findById(p.notFoundCmsPage.get)
.flatMap {
case Some(page) =>
page.render(ctx, req = None).map(res => Ok(res._1).as(res._2))
page.render(page.maybeWithoutUserToUserContext(ctx.tenant,
ctx.request.asInstanceOf[Request[libs.json.JsValue]].some,
ctx.user,
ctx.session,
ctx.impersonator,
ctx.isTenantAdmin,
ctx.apiCreationPermitted,
ctx.ctx), req = None).map(res => Ok(res._1).as(res._2))
case _ =>
Errors.craftResponseResult(
"Page not found !",
Expand All @@ -350,7 +359,14 @@ class HomeController(
req: Option[CmsRequestRendering] = None,
fields: Map[String, JsValue] = Map.empty[String, JsValue]
) = {
r.render(ctx, None, req = req, jsonToCombine = fields)
r.render(r.maybeWithoutUserToUserContext(ctx.tenant,
ctx.request.asInstanceOf[Request[libs.json.JsValue]].some,
ctx.user,
ctx.session,
ctx.impersonator,
ctx.isTenantAdmin,
ctx.apiCreationPermitted,
ctx.ctx), None, req = req, fields = fields, jsonToCombine = fields)
.map(res => {
Ok(res._1).as(res._2)
})
Expand Down
17 changes: 6 additions & 11 deletions daikoku/app/controllers/LoginController.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,7 @@ package fr.maif.otoroshi.daikoku.ctrls
import com.eatthepath.otp.TimeBasedOneTimePasswordGenerator
import com.google.common.base.Charsets
import controllers.Assets
import fr.maif.otoroshi.daikoku.actions.{
DaikokuAction,
DaikokuActionMaybeWithoutUser,
DaikokuTenantAction,
DaikokuTenantActionContext
}
import fr.maif.otoroshi.daikoku.actions.{DaikokuAction, DaikokuActionMaybeWithoutUser, DaikokuTenantAction, DaikokuTenantActionContext}
import fr.maif.otoroshi.daikoku.audit.{AuditTrailEvent, AuthorizationLevel}
import fr.maif.otoroshi.daikoku.domain.TeamPermission.Administrator
import fr.maif.otoroshi.daikoku.domain._
Expand All @@ -21,7 +16,7 @@ import org.apache.commons.codec.binary.Base32
import org.apache.pekko.http.scaladsl.util.FastFuture
import org.joda.time.DateTime
import org.mindrot.jbcrypt.BCrypt
import play.api.libs.json.{JsObject, JsValue, Json}
import play.api.libs.json.{JsObject, JsString, JsValue, Json}
import play.api.mvc._

import java.net.URLEncoder
Expand Down Expand Up @@ -512,17 +507,17 @@ class LoginController(
title <- translator.translate(
"mail.new.user.title",
ctx.tenant,
Map("tenant" -> ctx.tenant.name)
Map("tenant" -> JsString(ctx.tenant.name))
)
body <- translator.translate(
"mail.new.user.body",
ctx.tenant,
Map(
"tenant" -> ctx.tenant.name,
"link" -> env.getDaikokuUrl(
"tenant" -> JsString(ctx.tenant.name),
"link" -> JsString(env.getDaikokuUrl(
ctx.tenant,
s"/account/validate?id=$randomId"
)
))
)
)
} yield {
Expand Down
Loading

0 comments on commit 1bed0a6

Please sign in to comment.