generated from UniversityOfHelsinkiCS/toskaboiler
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: update access logic to allow for multiple levels of access …
…rights -refs: #1392
- Loading branch information
Showing
2 changed files
with
32 additions
and
42 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,51 +1,33 @@ | ||
const { Access } = require('./Access') | ||
|
||
const getAccess = async ({ userFeedbackTarget, user, feedbackTarget }) => { | ||
if (user.dataValues.isAdmin) return Access.ADMIN | ||
const accesses = [] | ||
|
||
if (user.dataValues.isAdmin) { | ||
accesses.push(Access.ADMIN) | ||
} | ||
|
||
const accessStatus = userFeedbackTarget?.accessStatus | ||
let uftAccess = null | ||
if (accessStatus) { | ||
uftAccess = Access.For(accessStatus) | ||
accesses.push(Access.For(accessStatus)) | ||
} | ||
|
||
// User not directly associated. Lets check if they have access through organisation | ||
// User not directly associated. Let's check if they have access through organisation | ||
const organisationAccess = await user.getOrganisationAccessByCourseUnitId(feedbackTarget.courseUnitId) | ||
|
||
let orgAccess = null | ||
if (organisationAccess) { | ||
if (organisationAccess.admin) { | ||
orgAccess = Access.ORGANISATION_ADMIN | ||
accesses.push(Access.ORGANISATION_ADMIN) | ||
} else if (organisationAccess.read) { | ||
orgAccess = Access.ORGANISATION_READ | ||
accesses.push(Access.ORGANISATION_READ) | ||
} | ||
} | ||
|
||
// only direct access, return that | ||
if (uftAccess !== null && orgAccess === null) { | ||
return uftAccess | ||
} | ||
// access only through organisation, return that | ||
if (uftAccess === null && orgAccess !== null) { | ||
return orgAccess | ||
} | ||
// both direct access and access through organisation, return highest | ||
if (uftAccess !== null && orgAccess !== null) { | ||
if (orgAccess === Access.ORGANISATION_ADMIN) { | ||
if (uftAccess === Access.ADMIN || uftAccess === Access.RESPONSIBLE_TEACHER) { | ||
return uftAccess | ||
} | ||
return orgAccess | ||
} | ||
if (orgAccess === Access.ORGANISATION_READ) { | ||
if (uftAccess === Access.STUDENT) { | ||
return orgAccess | ||
} | ||
return uftAccess | ||
} | ||
if (accesses.length === 0) { | ||
return Access.NONE | ||
} | ||
|
||
return null | ||
// Merge all accesses into one Access object | ||
return Access.mergeAccesses(accesses) | ||
} | ||
|
||
module.exports = { getAccess } |