From b79cfc467ad9f32f0592d9f24cb9c6cdf24ffe89 Mon Sep 17 00:00:00 2001 From: Justin Gasper Date: Wed, 17 May 2023 17:21:58 +1000 Subject: [PATCH] Redirect fix https://github.com/topcoder-platform/work-manager/issues/1527 --- src/routes.js | 9 +++++---- src/util/tc.js | 9 +++++++++ 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/src/routes.js b/src/routes.js index df3d102c..34e995da 100644 --- a/src/routes.js +++ b/src/routes.js @@ -16,7 +16,7 @@ import { getFreshToken, decodeToken } from 'tc-auth-lib' import { saveToken } from './actions/auth' import { loadChallengeDetails } from './actions/challenges' import { connect } from 'react-redux' -import { checkAllowedRoles, checkReadOnlyRoles } from './util/tc' +import { checkAllowedRoles, checkOnlyReadOnlyRoles, checkReadOnlyRoles } from './util/tc' import { setCookie, removeCookie, isBetaMode } from './util/cookie' import IdleTimer from 'react-idle-timer' import modalStyles from './styles/modal.module.scss' @@ -38,7 +38,7 @@ class RedirectToChallenge extends React.Component { componentWillReceiveProps (nextProps) { const { token } = nextProps - const isReadOnly = checkReadOnlyRoles(token) + const isReadOnly = checkOnlyReadOnlyRoles(token) const projectId = _.get(nextProps.challengeDetails, 'projectId') const challengeId = _.get(nextProps.challengeDetails, 'id') if (projectId && challengeId && isReadOnly) { @@ -52,8 +52,9 @@ class RedirectToChallenge extends React.Component { } } -let mapStateToProps = ({ challenges: { challengeDetails } }) => ({ - challengeDetails +let mapStateToProps = ({ challenges: { challengeDetails }, auth }) => ({ + challengeDetails, + ...auth }) let mapDispatchToProps = { diff --git a/src/util/tc.js b/src/util/tc.js index a3823d79..862271c2 100644 --- a/src/util/tc.js +++ b/src/util/tc.js @@ -162,6 +162,15 @@ export const checkReadOnlyRoles = token => { return roles.some(val => READ_ONLY_ROLES.indexOf(val.toLowerCase()) > -1) } +/** + * Checks if read only role is present in roles + * @param token + */ +export const checkOnlyReadOnlyRoles = token => { + const roles = _.get(decodeToken(token), 'roles') + return roles.some(val => READ_ONLY_ROLES.indexOf(val.toLowerCase()) > -1) +} + /** * Checks if token has any of the admin roles * @param token