tanishqgandhi1908 commented on code in PR #6869:
URL: https://github.com/apache/texera/pull/6869#discussion_r3808201640


##########
file-service/src/main/scala/org/apache/texera/service/resource/ResourceAccess.scala:
##########
@@ -0,0 +1,312 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.texera.service.resource
+
+import jakarta.ws.rs.{BadRequestException, ForbiddenException}
+import jakarta.ws.rs.core.Response
+import org.apache.texera.dao.jooq.generated.Tables.USER
+import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum
+import org.apache.texera.dao.jooq.generated.tables.daos.UserDao
+import org.apache.texera.dao.jooq.generated.tables.pojos.User
+import org.jooq.{DSLContext, EnumType, Record}
+
+import scala.jdk.CollectionConverters._
+
+/**
+  * Ownership and privilege rules shared by every access-controlled resource.
+  *
+  * A resource is readable when it is public, or the caller owns it, or the 
caller holds an
+  * explicit grant; it is writable when the caller owns it or holds a WRITE 
grant. A missing
+  * resource resolves to "not public, unowned, ungranted" rather than an 
error, so callers
+  * decide whether absence is a 403 or a 404.
+  */
+object ResourceAccess {
+
+  /** One shared grant, as returned by the access-list endpoints. */
+  case class AccessEntry(email: String, name: String, privilege: EnumType) {}
+
+  def isPublic[R <: Record, A <: Record](
+      ctx: DSLContext,
+      resource: ManagedResource[R, A],
+      id: Integer
+  ): Boolean =
+    Option(
+      ctx
+        .select(resource.isPublicField)
+        .from(resource.table)
+        .where(resource.idField.eq(id))
+        .fetchOne()
+    ).flatMap(record => Option(record.value1()))
+      .exists(_.booleanValue())
+
+  def userOwns[R <: Record, A <: Record](
+      ctx: DSLContext,
+      resource: ManagedResource[R, A],
+      id: Integer,
+      uid: Integer
+  ): Boolean =
+    Option(
+      ctx
+        .select(resource.ownerUidField)
+        .from(resource.table)
+        .where(resource.idField.eq(id))
+        .fetchOne()
+    ).flatMap(record => Option(record.value1()))
+      .contains(uid)
+
+  def privilegeOf[R <: Record, A <: Record](
+      ctx: DSLContext,
+      resource: ManagedResource[R, A],
+      id: Integer,
+      uid: Integer
+  ): PrivilegeEnum =
+    Option(
+      ctx
+        .select(resource.privilegeField)
+        .from(resource.accessTable)
+        .where(
+          resource.accessIdField
+            .eq(id)
+            .and(resource.accessUidField.eq(uid))
+        )
+        .fetchOneInto(classOf[PrivilegeEnum])
+    ).getOrElse(PrivilegeEnum.NONE)
+
+  def userHasWriteAccess[R <: Record, A <: Record](
+      ctx: DSLContext,
+      resource: ManagedResource[R, A],
+      id: Integer,
+      uid: Integer
+  ): Boolean =
+    userOwns(ctx, resource, id, uid) ||
+      privilegeOf(ctx, resource, id, uid) == PrivilegeEnum.WRITE
+
+  def userHasReadAccess[R <: Record, A <: Record](
+      ctx: DSLContext,
+      resource: ManagedResource[R, A],
+      id: Integer,
+      uid: Integer
+  ): Boolean =
+    isPublic(ctx, resource, id) ||
+      userHasWriteAccess(ctx, resource, id, uid) ||
+      privilegeOf(ctx, resource, id, uid) == PrivilegeEnum.READ
+
+  /** The owning user, or null when the resource does not exist. */
+  def owner[R <: Record, A <: Record](
+      ctx: DSLContext,
+      resource: ManagedResource[R, A],
+      id: Integer
+  ): User = {
+    val userDao = new UserDao(ctx.configuration())
+    Option(
+      ctx
+        .select(resource.ownerUidField)
+        .from(resource.table)
+        .where(resource.idField.eq(id))
+        .fetchOne()
+    ).flatMap(record => Option(record.value1()))
+      .map(ownerUid => userDao.fetchOneByUid(ownerUid))
+      .orNull
+  }
+
+  /** The owner's email, or an empty string when the resource does not exist. 
*/

Review Comment:
   Fixed — the guard runs first, so the empty-string case was unreachable. 
ownerEmail now documents that it throws ForbiddenException and that a missing 
resource is refused the same way, and accessList / revoke document their guards.



-- 
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