Skip to content

Commit 78bd322

Browse files
authored
Merge pull request #384 from nrotstan/bug-383-restrict-challenge-admin-visibility
Ensure only managers can view challenge admin.
2 parents 054d775 + 2d0dc5d commit 78bd322

File tree

1 file changed

+23
-4
lines changed

1 file changed

+23
-4
lines changed

src/components/AdminPane/HOCs/WithCurrentProject/WithCurrentProject.js

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,8 @@ import { fetchProjectChallenges,
1616
from '../../../../services/Challenge/Challenge'
1717
import AppErrors from '../../../../services/Error/AppErrors'
1818
import { addError } from '../../../../services/Error/Error'
19+
import WithCurrentUser from '../../../HOCs/WithCurrentUser/WithCurrentUser'
20+
import AsManager from '../../../../interactions/User/AsManager'
1921

2022
/**
2123
* WithCurrentProject makes available to the WrappedComponent the current
@@ -42,6 +44,9 @@ const WithCurrentProject = function(WrappedComponent, options={}) {
4244
routedProjectId = props =>
4345
parseInt(_get(props, 'match.params.projectId'), 10)
4446

47+
routedChallengeId = props =>
48+
parseInt(_get(props, 'match.params.challengeId'), 10)
49+
4550
currentProjectId = props => {
4651
let projectId = this.routedProjectId(props)
4752

@@ -62,7 +67,7 @@ const WithCurrentProject = function(WrappedComponent, options={}) {
6267
return projectId
6368
}
6469

65-
updateProject = props => {
70+
loadProject = props => {
6671
const projectId = this.currentProjectId(props)
6772

6873
if (_isFinite(this.routedProjectId(props)) && projectId === null) {
@@ -80,6 +85,20 @@ const WithCurrentProject = function(WrappedComponent, options={}) {
8085
props.fetchProject(projectId).then(normalizedProject => {
8186
const project = normalizedProject.entities.projects[normalizedProject.result]
8287

88+
const manager = AsManager(this.props.user)
89+
if (!manager.canManage(project)) {
90+
// If we have a challenge id too, route to the browse url for the challenge
91+
const challengeId = this.routedChallengeId(this.props)
92+
if (_isFinite(challengeId)) {
93+
props.history.replace(`/browse/challenges/${challengeId}`)
94+
}
95+
else {
96+
this.props.notManagerError()
97+
props.history.push('/admin/projects')
98+
}
99+
return
100+
}
101+
83102
if (options.includeActivity) {
84103
// Used for daily heatmap
85104
props.fetchProjectActivity(projectId, new Date(project.created)).then(() =>
@@ -114,15 +133,15 @@ const WithCurrentProject = function(WrappedComponent, options={}) {
114133
}
115134

116135
componentWillMount() {
117-
this.updateProject(this.props)
136+
this.loadProject(this.props)
118137
}
119138

120139
componentWillReceiveProps(nextProps) {
121140
const nextProjectId = this.currentProjectId(nextProps)
122141

123142
if ( _isFinite(nextProjectId) &&
124143
nextProjectId !== this.currentProjectId(this.props)) {
125-
this.updateProject(nextProps)
144+
this.loadProject(nextProps)
126145
}
127146
}
128147

@@ -172,4 +191,4 @@ const mapDispatchToProps = dispatch => ({
172191

173192
export default (WrappedComponent, options) =>
174193
connect(mapStateToProps,
175-
mapDispatchToProps)(WithCurrentProject(WrappedComponent, options))
194+
mapDispatchToProps)(WithCurrentUser(WithCurrentProject(WrappedComponent, options)))

0 commit comments

Comments
 (0)