Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ class WorkflowAccessResource() {
@PathParam("privilege") privilege: String,
@Auth user: SessionUser
): Unit = {
val isModifyingOwnAccess = email.equals(user.getEmail)
val isModifyingOwnAccess = email.equalsIgnoreCase(user.getEmail)
val currentPrivilege = getPrivilege(wid, user.getUid)
val hasExistingAccess = !currentPrivilege.eq(PrivilegeEnum.NONE)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,16 @@ class WorkflowAccessResourceSpec extends WorkflowAccessDatabaseSpec(withTargetUs
)
}

it should "grant access when the email's casing differs from the account's (issue #9738)" in {
seedUserWithoutAccess("new_grantee", "newgrantee@test.com")

grant("NewGrantee@Test.com", "READ", new SessionUser(userWithWrite))

assert(
WorkflowAccess.getPrivilege(testWorkflowWid, newGranteeUid) == PrivilegeEnum.READ
)
}

it should "change an existing grantee's privilege on re-grant (READ -> WRITE)" in {
grant("read@test.com", "WRITE", new SessionUser(userWithWrite))

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import jakarta.ws.rs.ForbiddenException
import org.apache.texera.dao.jooq.generated.Tables.USER
import org.apache.texera.dao.jooq.generated.enums.PrivilegeEnum
import org.jooq.DSLContext
import org.jooq.impl.DSL

/** The rules every shared resource applies to its owner and to the users it is shared with. */
object OwnerAccess {
Expand All @@ -39,13 +40,18 @@ object OwnerAccess {

/** The uid of the registered account with this email, or None when the email is unknown or
* belongs to a placeholder; neither can be granted access.
*
* Matched ignoring case, like sign-up, since stored emails keep the casing the user typed. If
* two accounts differ only in case, the exact match wins.
*/
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))
.where(DSL.lower(USER.EMAIL).eq(email.trim.toLowerCase).and(USER.IS_PLACEHOLDER.isFalse))
.orderBy(DSL.when(USER.EMAIL.eq(email), 0).otherwise(1))
.limit(1)
.fetchOne(USER.UID)
)
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,75 @@
// 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.auth.util

import org.apache.texera.dao.MockTexeraDB
import org.apache.texera.dao.jooq.generated.enums.UserRoleEnum
import org.apache.texera.dao.jooq.generated.tables.daos.UserDao
import org.apache.texera.dao.jooq.generated.tables.pojos.User
import org.scalatest.BeforeAndAfterAll
import org.scalatest.flatspec.AnyFlatSpec
import org.scalatest.matchers.should.Matchers

class OwnerAccessRegisteredUidSpec
extends AnyFlatSpec
with Matchers
with BeforeAndAfterAll
with MockTexeraDB {

private def insertUser(uid: Int, email: String, placeholder: Boolean = false): Unit = {
val u = new User
u.setUid(Integer.valueOf(uid))
u.setName(s"user$uid")
u.setEmail(email)
u.setRole(UserRoleEnum.REGULAR)
u.setIsPlaceholder(placeholder)
new UserDao(getDSLContext.configuration()).insert(u)
}

override def beforeAll(): Unit = {
initializeDBAndReplaceDSLContext()
insertUser(1, "alice@example.com")
insertUser(2, "Bob.Smith@example.com")
insertUser(3, "carol@example.com", placeholder = true)
// Two accounts that differ only in case, which the plain UNIQUE on email allows.
insertUser(4, "dave@example.com")
insertUser(5, "Dave@example.com")
}

override def afterAll(): Unit = closeConnectionPool()

private def uidOf(email: String): Option[Int] =
OwnerAccess.registeredUid(getDSLContext, email).map(_.intValue())

"registeredUid" should "find an account whatever the casing typed (issue #9738)" in {
uidOf("alice@example.com") shouldBe Some(1)
uidOf("Alice@Example.com") shouldBe Some(1)
uidOf("bob.smith@example.com") shouldBe Some(2)
uidOf("BOB.SMITH@EXAMPLE.COM") shouldBe Some(2)
}

it should "prefer the exact match when two accounts differ only in case" in {
uidOf("dave@example.com") shouldBe Some(4)
uidOf("Dave@example.com") shouldBe Some(5)
}

it should "not find a placeholder or an unknown email" in {
uidOf("Carol@example.com") shouldBe None
uidOf("nobody@example.com") shouldBe None
}
}
Loading