Skip to content

Commit

Permalink
Give all employees access to arena
Browse files Browse the repository at this point in the history
  • Loading branch information
gunnarvelle committed Jan 30, 2025
1 parent e82b57e commit efe5eeb
Show file tree
Hide file tree
Showing 9 changed files with 47 additions and 47 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ import sttp.tapir.*
import sttp.tapir.server.PartialServerEndpoint

trait MyNDLAAuthHelpers {
this: UserService with TapirErrorHandling =>
this: UserService & TapirErrorHandling =>

object MyNDLAAuthHelpers {
implicit class authlessEndpointFeideExtension[A, I, E, O, R](self: Endpoint[Unit, I, AllErrors, O, R]) {
type MaybeFeideToken = Option[FeideAccessToken]
type PartialFeideEndpoint[F[_]] = PartialServerEndpoint[MaybeFeideToken, MyNDLAUser, I, AllErrors, O, R, F]
private type MaybeFeideToken = Option[FeideAccessToken]
private type PartialFeideEndpoint[F[_]] =
PartialServerEndpoint[MaybeFeideToken, MyNDLAUser, I, AllErrors, O, R, F]
def requireMyNDLAUser[F[_]](
requireArena: Boolean = false,
requireArenaAdmin: Boolean = false
Expand All @@ -37,7 +38,7 @@ trait MyNDLAAuthHelpers {
case Left(err) => Left(err)
}
} else if (requireArena) userService.getArenaEnabledUser(maybeToken).handleErrorsOrOk
else userService.getMyNdlaUserDataDomain(maybeToken, List.empty).handleErrorsOrOk
else userService.getMyNdlaUserDataDomain(maybeToken).handleErrorsOrOk
}
val securityLogic = (m: MonadError[F]) => (a: Option[FeideAccessToken]) => m.unit(authFunc(a))
PartialServerEndpoint(newEndpoint, securityLogic)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ trait ArenaReadService {
for {
feideToken <- feideApiClient.getFeideAccessTokenOrFail(feideAccessToken)
feideId <- feideApiClient.getFeideID(feideAccessToken)
user <- userService.getOrCreateMyNDLAUserIfNotExist(feideId, feideAccessToken, List.empty)(session)
user <- userService.getOrCreateMyNDLAUserIfNotExist(feideId, feideAccessToken)(session)
nodebbUserId <- nodebb.getUserId(feideToken)
_ <- arenaRepository.disconnectPostsByUser(user.id)(session)
_ <- arenaRepository.disconnectTopicsByUser(user.id)(session)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -180,7 +180,7 @@ trait FolderConverterService {
}

def toApiUserData(
domainUserData: DomainMyNDLAUser,
domainUserData: DomainMyNDLAUser
): model.api.myndla.MyNDLAUserDTO = {
model.api.myndla.MyNDLAUserDTO(
id = domainUserData.id,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,12 +271,10 @@ trait FolderReadService {
feideAccessToken: Option[FeideAccessToken]
): Try[MyNDLAUserDTO] =
for {
users <- configService.getMyNDLAEnabledUsers
user <- userRepository.rollbackOnFailure(session =>
userService.getOrCreateMyNDLAUserIfNotExist(feideId, feideAccessToken, users)(session)
userService.getOrCreateMyNDLAUserIfNotExist(feideId, feideAccessToken)(session)
)
orgs <- configService.getMyNDLAEnabledOrgs
} yield folderConverterService.toApiUserData(user, orgs)
} yield folderConverterService.toApiUserData(user)

def getStats: Option[api.StatsDTO] = {
implicit val session: DBSession = folderRepository.getSession(true)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ trait FolderWriteService {

private def getMyNDLAUser(feideId: FeideID, feideAccessToken: Option[FeideAccessToken]): Try[MyNDLAUser] = {
userRepository.rollbackOnFailure(session =>
userService.getOrCreateMyNDLAUserIfNotExist(feideId, feideAccessToken, List.empty)(session)
userService.getOrCreateMyNDLAUserIfNotExist(feideId, feideAccessToken)(session)
)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ trait UserService {
}

def getMyNdlaUserDataDomain(
feideAccessToken: Option[FeideAccessToken],
feideAccessToken: Option[FeideAccessToken]
): Try[MyNDLAUser] = {
for {
feideId <- feideApiClient.getFeideID(feideAccessToken)
Expand All @@ -70,7 +70,8 @@ trait UserService {
def getArenaEnabledUser(feideAccessToken: Option[FeideAccessToken]): Try[MyNDLAUser] = {
for {
userData <- getMyNdlaUserDataDomain(feideAccessToken)
user <- if (userData.arenaEnabled) Success(userData) else Failure(AccessDeniedException("User is not arena enabled"))
user <-
if (userData.arenaEnabled) Success(userData) else Failure(AccessDeniedException("User is not arena enabled"))
} yield user
}

Expand All @@ -83,7 +84,7 @@ trait UserService {

def getOrCreateMyNDLAUserIfNotExist(
feideId: FeideID,
feideAccessToken: Option[FeideAccessToken],
feideAccessToken: Option[FeideAccessToken]
)(implicit session: DBSession): Try[MyNDLAUser] = {
userRepository.reserveFeideIdIfNotExists(feideId)(session).flatMap {
case false => createMyNDLAUser(feideId, feideAccessToken)(session)
Expand Down Expand Up @@ -148,7 +149,7 @@ trait UserService {
updaterMyNdla: Option[MyNDLAUser]
)(session: DBSession = AutoSession): Try[myndla.MyNDLAUserDTO] = {
for {
existing <- userService.getUserById(userId)(session)
existing <- userService.getUserById(userId)(session)
converted <- folderConverterService.mergeUserData(
existing,
updatedUser,
Expand Down Expand Up @@ -191,7 +192,7 @@ trait UserService {

private def createMyNDLAUser(
feideId: FeideID,
feideAccessToken: Option[FeideAccessToken],
feideAccessToken: Option[FeideAccessToken]
)(implicit
session: DBSession
): Try[MyNDLAUser] = {
Expand Down Expand Up @@ -220,14 +221,14 @@ trait UserService {
private def fetchDataAndUpdateMyNDLAUser(
feideId: FeideID,
feideAccessToken: Option[FeideAccessToken],
userData: MyNDLAUser,
userData: MyNDLAUser
)(implicit
session: DBSession
): Try[MyNDLAUser] = {
val feideUser = feideApiClient.getFeideExtendedUser(feideAccessToken).?
val organization = feideApiClient.getOrganization(feideAccessToken).?
val feideGroups = feideApiClient.getFeideGroups(feideAccessToken).?
val userRole = if (feideUser.isTeacher) UserRole.EMPLOYEE else UserRole.STUDENT
val userRole = if (feideUser.isTeacher) UserRole.EMPLOYEE else UserRole.STUDENT
val updatedMyNDLAUser = MyNDLAUser(
id = userData.id,
feideId = userData.feideId,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -448,7 +448,7 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
arenaGroups = List.empty
)

service.toApiUserData(domainUserData, List.empty) should be(expectedUserData)
service.toApiUserData(domainUserData) should be(expectedUserData)
}

test("That mergeUserData works correctly") {
Expand Down Expand Up @@ -563,13 +563,13 @@ class FolderConverterServiceTest extends UnitTestSuite with TestEnvironment {
arenaAccepted = true
)

service.mergeUserData(domainUserData, updatedUserData1, None, None, List.empty, List.empty, None).get should be(
service.mergeUserData(domainUserData, updatedUserData1, None, None, None).get should be(
expectedUserData1
)
service.mergeUserData(domainUserData, updatedUserData2, None, None, List.empty, List.empty, None).get should be(
service.mergeUserData(domainUserData, updatedUserData2, None, None, None).get should be(
expectedUserData2
)
service.mergeUserData(domainUserData, updatedUserData3, None, None, List.empty, List.empty, None).get should be(
service.mergeUserData(domainUserData, updatedUserData3, None, None, None).get should be(
expectedUserData3
)
}
Expand Down
Loading

0 comments on commit efe5eeb

Please sign in to comment.