kunwp1 commented on code in PR #9480:
URL: https://github.com/apache/texera/pull/9480#discussion_r4593028532


##########
common/resource/src/main/scala/org/apache/texera/service/resource/ResourceAccess.scala:
##########
@@ -41,6 +41,24 @@ object ResourceAccess {
   /** One shared grant, as returned by the access-list endpoints. */
   case class AccessEntry(email: String, name: String, privilege: EnumType) {}
 
+  /** All matching IDs in each resource table, so mounts can reject ambiguous 
legacy names. */
+  private[service] def repositoryIds(
+      ctx: DSLContext,
+      repositoryName: String
+  ): (java.util.List[Integer], java.util.List[Integer]) = {
+    val datasetIds = ctx
+      .select(DATASET.DID)
+      .from(DATASET)
+      .where(DATASET.REPOSITORY_NAME.eq(repositoryName))
+      .fetch(DATASET.DID)
+    val modelIds = ctx
+      .select(MODEL.MID)
+      .from(MODEL)
+      .where(MODEL.REPOSITORY_NAME.eq(repositoryName))
+      .fetch(MODEL.MID)
+    (datasetIds, modelIds)

Review Comment:
   Could this return Scala lists? Then both callers keep their original `(did 
:: Nil, Nil)` and `(Nil, mid :: Nil)` patterns instead of matching on sizes and 
calling `get(0)`. That shrinks the diffs in `S3ProxyServlet` and 
`ComputingUnitMountResource` to the lookup line:
   
   ```scala
     ): (List[Integer], List[Integer]) = {
       ...
       (datasetIds.asScala.toList, modelIds.asScala.toList)
   ```



##########
common/resource/src/main/scala/org/apache/texera/service/resource/ResourceAccess.scala:
##########
@@ -136,7 +152,10 @@ object ResourceAccess {
       requesterUid: Integer
   ): String = {
     requireReadAccess(ctx, resource, id, requesterUid)
-    Option(owner(ctx, resource, id)).map(_.getEmail).getOrElse("")
+    ownerUid(ctx, resource, id)
+      .flatMap(uid => 
Option(ctx.select(USER.EMAIL).from(USER).where(USER.UID.eq(uid)).fetchOne()))
+      .map(_.value1())
+      .getOrElse("")

Review Comment:
   Could the owner's email be one join? Here and in 
`WorkflowAccessResource.getOwner`, it's now an owner-UID query followed by an 
email query. Joining the user table on the owner column does it in one query:
   
   ```scala
       Option(
         ctx
           .select(USER.EMAIL)
           .from(resource.table)
           .join(USER)
           .on(USER.UID.eq(resource.ownerUidField))
           .where(resource.idField.eq(id))
           .fetchOne(USER.EMAIL)
       ).getOrElse("")
   ```
   
   `accessList` also still has its own copy of the `ownerUid` select, in a 
local `val ownerUid` that shadows the new helper. Separately, `owner` still 
loads the whole user row even though its callers in `DatasetResource` and 
`ModelResource` only read `.getEmail`, so an unchecked version of this join 
could replace it as a follow-up.



##########
common/resource/src/main/scala/org/apache/texera/service/resource/ResourceAccess.scala:
##########
@@ -256,11 +275,15 @@ object ResourceAccess {
       requesterUid: Integer
   ): Response = {
     requireWriteAccess(ctx, resource, id, requesterUid)
-    val grantee = new UserDao(ctx.configuration()).fetchOneByEmail(email)
-    if (grantee == null || grantee.getIsPlaceholder) {
+    val grantee = ctx
+      .select(USER.UID, USER.IS_PLACEHOLDER)
+      .from(USER)
+      .where(USER.EMAIL.eq(email))
+      .fetchOne()
+    if (grantee == null || grantee.value2()) {
       throw new BadRequestException(s"No registered user with email $email")
     }
-    val granteeUid = grantee.getUid
+    val granteeUid = grantee.value1()

Review Comment:
   This lookup and its placeholder rule are now written out here and again in 
`WorkflowAccessResource.grantAccess`. A small helper in `OwnerAccess`, next to 
the other shared sharing rules, would keep it in one place. Each caller would 
still throw its own `BadRequestException` (javax in amber, jakarta here):
   
   ```scala
     def registeredUid(ctx: DSLContext, email: String): Option[Integer] =
       Option(
         ctx
           .select(USER.UID)
           .from(USER)
           .where(USER.EMAIL.eq(email).and(USER.IS_PLACEHOLDER.isFalse))
           .fetchOne(USER.UID)
       )
   ```
   
   In `WorkflowAccessResource`, `readContext` is the same `context` the other 
methods use, so it can go too.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to