Skip to content
This repository was archived by the owner on Mar 13, 2025. It is now read-only.

Commit 2158e6e

Browse files
Merge pull request #270 from topcoder-platform/feature/fix-job-edit-permissions
[PROD] Patch 1.7.0.1
2 parents 9eb4998 + 1e8ef72 commit 2158e6e

File tree

6 files changed

+85
-7
lines changed

6 files changed

+85
-7
lines changed

src/constants/index.js

+5
Original file line numberDiff line numberDiff line change
@@ -210,6 +210,11 @@ export const ACTION_TYPE = {
210210
AUTH_LOAD_TEAM_MEMBERS_ERROR: "AUTH_LOAD_TEAM_MEMBERS_ERROR",
211211
AUTH_CLEAR_TEAM_MEMBERS: "AUTH_CLEAR_TEAM_MEMBERS",
212212

213+
AUTH_LOAD_V5_USER_PROFILE: "AUTH_LOAD_V5_USER_PROFILE",
214+
AUTH_LOAD_V5_USER_PROFILE_PENDING: "AUTH_LOAD_V5_USER_PROFILE_PENDING",
215+
AUTH_LOAD_V5_USER_PROFILE_SUCCESS: "AUTH_LOAD_V5_USER_PROFILE_SUCCESS",
216+
AUTH_LOAD_V5_USER_PROFILE_ERROR: "AUTH_LOAD_V5_USER_PROFILE_ERROR",
217+
213218
/*
214219
Email Popup
215220
*/

src/hoc/withAuthentication/actions/index.js

+14-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
* Auth User actions
33
*/
44
import { ACTION_TYPE } from "constants";
5-
import { getTeamMembers } from "services/teams";
5+
import { getTeamMembers, getV5UserProfile } from "services/teams";
66

77
/**
88
* Action to set auth user data
@@ -40,6 +40,19 @@ export const authLoadTeamMembers = (teamId) => ({
4040
},
4141
});
4242

43+
/**
44+
* Loads v5 user profile for authentication/permission purposes
45+
*
46+
* @returns {Promise} loaded user profile
47+
*/
48+
export const authLoadV5UserProfile = () => ({
49+
type: ACTION_TYPE.AUTH_LOAD_V5_USER_PROFILE,
50+
payload: async () => {
51+
const res = await getV5UserProfile();
52+
return res.data;
53+
},
54+
});
55+
4356
/**
4457
* Clear team members for authentication/permission purposes
4558
*

src/hoc/withAuthentication/index.js

+28-5
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import {
1919
authUserSuccess,
2020
authUserError,
2121
authLoadTeamMembers,
22+
authLoadV5UserProfile,
2223
authClearTeamMembers,
2324
} from "./actions";
2425
import { decodeToken } from "tc-auth-lib";
@@ -34,6 +35,9 @@ export default function withAuthentication(Component) {
3435
teamId,
3536
teamMembersLoaded,
3637
teamMembersLoadingError,
38+
v5UserProfile,
39+
v5UserProfileLoading,
40+
v5UserProfileLoadingError,
3741
} = useSelector((state) => state.authUser);
3842
const params = useParams();
3943

@@ -86,18 +90,37 @@ export default function withAuthentication(Component) {
8690
}
8791
}, [params.teamId, teamId, dispatch, isLoggedIn]);
8892

93+
/*
94+
Load V5 User Profile
95+
*/
96+
useEffect(() => {
97+
// is user is logged-in, but V5 user profile is not loaded yet, then load it
98+
if (isLoggedIn && !v5UserProfileLoading && !v5UserProfile) {
99+
dispatch(authLoadV5UserProfile());
100+
}
101+
}, [dispatch, isLoggedIn, v5UserProfileLoading, v5UserProfile]);
102+
89103
return (
90104
<>
91105
{/* Show loading indicator until we know if user is logged-in or no.
92106
Also, show loading indicator if we need to know team members but haven't loaded them yet.
107+
or load v5 user profile but haven't loaded them yet.
93108
In we got error during this process, show error */}
94-
{isLoggedIn === null ||
95-
(params.teamId && !teamMembersLoaded && (
96-
<LoadingIndicator error={authError || teamMembersLoadingError} />
97-
))}
109+
{(isLoggedIn === null ||
110+
(params.teamId && !teamMembersLoaded) ||
111+
v5UserProfileLoading ||
112+
v5UserProfileLoadingError) && (
113+
<LoadingIndicator
114+
error={
115+
authError || teamMembersLoadingError || v5UserProfileLoadingError
116+
}
117+
/>
118+
)}
98119

99120
{/* Show component only if user is logged-in and if we don't need team members or we already loaded them */}
100-
{isLoggedIn === true && (!params.teamId || teamMembersLoaded) ? (
121+
{isLoggedIn === true &&
122+
v5UserProfile &&
123+
(!params.teamId || teamMembersLoaded) ? (
101124
<Component {...props} />
102125
) : null}
103126
</>

src/hoc/withAuthentication/reducers/index.js

+25
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,9 @@ const initialState = {
1616
teamMembersLoading: undefined,
1717
teamMembersLoadingError: undefined,
1818
teamMembersLoaded: false,
19+
v5UserProfile: undefined,
20+
v5UserProfileLoading: false,
21+
v5UserProfileLoadingError: false,
1922
};
2023

2124
const authInitialState = _.pick(initialState, [
@@ -95,6 +98,28 @@ const reducer = (state = initialState, action) => {
9598
};
9699
}
97100

101+
case ACTION_TYPE.AUTH_LOAD_V5_USER_PROFILE_PENDING: {
102+
return {
103+
...state,
104+
v5UserProfileLoading: true,
105+
};
106+
}
107+
case ACTION_TYPE.AUTH_LOAD_V5_USER_PROFILE_SUCCESS: {
108+
return {
109+
...state,
110+
v5UserProfile: action.payload,
111+
v5UserProfileLoading: false,
112+
v5UserProfileLoadingError: false,
113+
};
114+
}
115+
case ACTION_TYPE.AUTH_LOAD_V5_USER_PROFILE_ERROR: {
116+
return {
117+
...state,
118+
v5UserProfileLoadingError: action.payload,
119+
v5UserProfileLoading: false,
120+
};
121+
}
122+
98123
default:
99124
return state;
100125
}

src/routes/JobDetails/index.jsx

+4-1
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* It gets `teamId` and `jobId` from the router.
66
*/
77
import React, { useEffect, useState } from "react";
8+
import { useSelector } from "react-redux";
89
import PT from "prop-types";
910
import _ from "lodash";
1011
import Page from "../../components/Page";
@@ -32,6 +33,7 @@ const JobDetails = ({ teamId, jobId }) => {
3233
const [job, loadingError] = useData(getJobById, jobId);
3334
const [skills] = useData(getSkills);
3435
const [skillSet, setSkillSet] = useState(null);
36+
const { id: userId } = useSelector((state) => state.authUser.v5UserProfile);
3537

3638
useEffect(() => {
3739
if (!!skills && !!job) {
@@ -106,7 +108,8 @@ const JobDetails = ({ teamId, jobId }) => {
106108
{job.status}
107109
</DataItem>
108110
</div>
109-
{hasPermission(PERMISSIONS.UPDATE_JOB_NOT_OWN) && (
111+
{(hasPermission(PERMISSIONS.UPDATE_JOB_NOT_OWN) ||
112+
userId === job.createdBy) && (
110113
<div styleName="actions">
111114
<Button
112115
target="_blank"

src/services/teams.js

+9
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ export const getMyTeams = (name, page = 1, perPage) => {
2222
return axios.get(`${config.API.V5}/taas-teams?${query}`);
2323
};
2424

25+
/**
26+
* Get v5 user profile.
27+
*
28+
* @returns {Promise<{}>} user profile object
29+
*/
30+
export const getV5UserProfile = () => {
31+
return axios.get(`${config.API.V5}/taas-teams/me`);
32+
};
33+
2534
/**
2635
* Get team by id.
2736
*

0 commit comments

Comments
 (0)