Skip to content

Commit 966840c

Browse files
authored
Merge pull request #15 from topcoder-platform/dev
Fix Sub Groups Permission Check Problem
2 parents 20b03b3 + a378747 commit 966840c

File tree

3 files changed

+117
-32
lines changed

3 files changed

+117
-32
lines changed

conf/OnlineReview.xml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5217,6 +5217,9 @@
52175217
<Property name="user_group_membership_url">
52185218
<Value>@topcoder_api_base_url_v3@/groups?memberId=%s&amp;membershipType=User</Value>
52195219
</Property>
5220+
<Property name="parent_groups_url">
5221+
<Value>@topcoder_api_base_url_v3@/groups/%s/getParentGroup?oneLevel=false</Value>
5222+
</Property>
52205223
<Property name="v3jwt_cookie_name">
52215224
<Value>v3jwt</Value>
52225225
</Property>

src/java/main/com/cronos/onlinereview/actions/project/SaveProjectAction.java

Lines changed: 66 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1396,7 +1396,7 @@ private Date parseDatetimeFormProperties(int propertyIndex, String dateProperty,
13961396
* @throws BaseException if any error occurs
13971397
* @return the Set<Long> result contains the group ids
13981398
*/
1399-
private Set<Long> getGroups(HttpServletRequest request, long userId) throws BaseException {
1399+
private Set<Long> getGroups(long userId) throws BaseException {
14001400
try {
14011401
DefaultHttpClient httpClient = new DefaultHttpClient();
14021402
String groupEndPoint = String.format(ConfigHelper.getUserGroupMembershipUrl(), userId);
@@ -1422,12 +1422,74 @@ private Set<Long> getGroups(HttpServletRequest request, long userId) throws Base
14221422
for (JsonNode group : groups) {
14231423
groupIds.add(group.path("id").asLong());
14241424
}
1425+
1426+
Set<Long> allGroupIds = new HashSet<Long>(groupIds);
1427+
for (Long groupId : groupIds) {
1428+
allGroupIds.addAll(getParentGroups(groupId));
1429+
}
14251430

1426-
return groupIds;
1431+
return allGroupIds;
14271432
} catch (Exception exp) {
14281433
throw new BaseException(exp.getMessage(), exp);
14291434
}
1430-
1435+
}
1436+
1437+
/**
1438+
* Get parent groups for the given group id
1439+
*
1440+
* @param request the request to use
1441+
* @param groupId the user id to use
1442+
* @throws BaseException if any error occurs
1443+
* @return the Set<Long> result contains the group ids
1444+
*/
1445+
private Set<Long> getParentGroups(long groupId) throws BaseException {
1446+
try {
1447+
DefaultHttpClient httpClient = new DefaultHttpClient();
1448+
String parentGroupsEndPoint = String.format(ConfigHelper.getParentGroupsUrl(), groupId);
1449+
HttpGet getRequest = new HttpGet(parentGroupsEndPoint);
1450+
1451+
String v3Token = new JwtTokenUpdater().check().getToken();
1452+
1453+
getRequest.setHeader(HttpHeaders.AUTHORIZATION, "Bearer " + v3Token);
1454+
1455+
getRequest.addHeader(HttpHeaders.ACCEPT, "application/json");
1456+
HttpResponse httpResponse = httpClient.execute(getRequest);
1457+
1458+
HttpEntity entity = httpResponse.getEntity();
1459+
1460+
if (httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
1461+
throw new BaseException("Unable to get groups from the API:" + httpResponse.getStatusLine().getReasonPhrase());
1462+
}
1463+
1464+
JsonNode result = objectMapper.readTree(entity.getContent());
1465+
1466+
JsonNode groupNode = result.path("result").path("content");
1467+
Set<Long> groupIds = parseGroup(groupNode);
1468+
1469+
return groupIds;
1470+
} catch (Exception exp) {
1471+
throw new BaseException(exp.getMessage(), exp);
1472+
}
1473+
}
1474+
1475+
/**
1476+
* Parse the group from the JSON node
1477+
* @param groupNode the JSON node
1478+
* @return the group
1479+
*/
1480+
private Set<Long> parseGroup(JsonNode groupNode) {
1481+
Set<Long> parentGroupIds = new HashSet<Long>();
1482+
Long parentGroupId = groupNode.path("id").asLong();
1483+
if (parentGroupId != 0) {
1484+
// exclude null node
1485+
parentGroupIds.add(groupNode.path("id").asLong());
1486+
}
1487+
1488+
if (groupNode.has("parentGroup")) {
1489+
parentGroupIds.addAll(parseGroup(groupNode.path("parentGroup")));
1490+
}
1491+
1492+
return parentGroupIds;
14311493
}
14321494

14331495
/**
@@ -1453,7 +1515,7 @@ private boolean checkUserChallengeEligibility(HttpServletRequest request, int re
14531515
if (challengeGroupInd != null) {
14541516
if (challengeGroupInd > 0) {
14551517
Long groupId = groups.get("group_id");
1456-
Set<Long> ids = this.getGroups(request, userId);
1518+
Set<Long> ids = this.getGroups(userId);
14571519
if (!ids.contains(groupId)) {
14581520
ActionsHelper.addErrorToRequest(request, "resources_name[" + resourceIdx + "]",
14591521
"error.com.cronos.onlinereview.actions.editProject.Resource.GroupPermissionDenied");

src/java/main/com/cronos/onlinereview/util/ConfigHelper.java

Lines changed: 48 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -566,6 +566,11 @@ public class ConfigHelper {
566566
* <p>A <code>String</code> providing the name for user group memeber ship url property.</p>
567567
*/
568568
private static final String USER_GROUP_MEMBERSHIP_URL = "user_group_membership_url";
569+
570+
/**
571+
* <p>A <code>String</code> providing the name for parent groups url property.</p>
572+
*/
573+
private static final String PARENT_GROUPS_URL = "parent_groups_url";
569574

570575
/**
571576
* <p>A <code>String</code> providing the name for v2 jwt cookie name property.</p>
@@ -943,6 +948,11 @@ public class ConfigHelper {
943948
* <p>Represents the userGroupMembershipUrl.</p>
944949
*/
945950
private static String userGroupMembershipUrl;
951+
952+
/**
953+
* <p>Represents the parentGroupsUrl.</p>
954+
*/
955+
private static String parentGroupsUrl;
946956

947957
/**
948958
* <p>Represents the v3jwtCookieBame.</p>
@@ -951,17 +961,17 @@ public class ConfigHelper {
951961

952962
/**
953963
* <p>Represents the v2jwtCookieBame.</p>
954-
*/
964+
*/
955965
private static String v2jwtCookieName;
956966

957967
/**
958968
* <p>Represents the v3jwtAuthorizationUrl.</p>
959-
*/
969+
*/
960970
private static String v3jwtAuthorizationUrl;
961971

962972
/**
963973
* <p>Represents the ssoDomainForV3jwtCookie.</p>
964-
*/
974+
*/
965975
private static String ssoDomainForV3jwtCookie;
966976

967977
static {
@@ -1560,6 +1570,7 @@ public class ConfigHelper {
15601570
}
15611571

15621572
userGroupMembershipUrl = cfgMgr.getString(ONLINE_REVIEW_CFG_NS, USER_GROUP_MEMBERSHIP_URL);
1573+
parentGroupsUrl = cfgMgr.getString(ONLINE_REVIEW_CFG_NS, PARENT_GROUPS_URL);
15631574
v3jwtCookieName = cfgMgr.getString(ONLINE_REVIEW_CFG_NS, V3_JWT_COOKIE_NAME);
15641575
v2jwtCookieName = cfgMgr.getString(ONLINE_REVIEW_CFG_NS, V2_JWT_COOKIE_NAME);
15651576
ssoDomainForV3jwtCookie = cfgMgr.getString(ONLINE_REVIEW_CFG_NS, SSO_DOMAIN_FOR_V3_JWT_COOKIE);
@@ -2401,6 +2412,15 @@ public static String getUserGroupMembershipUrl() {
24012412
return userGroupMembershipUrl;
24022413
}
24032414

2415+
/**
2416+
* Get parent groups url
2417+
*
2418+
* @return the parent groups url
2419+
*/
2420+
public static String getParentGroupsUrl() {
2421+
return parentGroupsUrl;
2422+
}
2423+
24042424
/**
24052425
* Get v3jwt cookie name
24062426
*
@@ -2409,29 +2429,29 @@ public static String getUserGroupMembershipUrl() {
24092429
public static String getV3jwtCookieName() {
24102430
return v3jwtCookieName;
24112431
}
2412-
2413-
/**
2414-
* Get v2jwtCookieName.
2415-
* @return the v2jwtCookieName.
2416-
*/
2417-
public static String getV2jwtCookieName() {
2418-
return v2jwtCookieName;
2419-
}
2420-
2421-
/**
2422-
* Get v3jwtAuthorizationUrl.
2423-
* @return the v3jwtAuthorizationUrl.
2424-
*/
2425-
public static String getV3jwtAuthorizationUrl() {
2426-
return v3jwtAuthorizationUrl;
2427-
}
2428-
2429-
/**
2430-
* Get ssoDomainForV3jwtCookie.
2431-
* @return the ssoDomainForV3jwtCookie.
2432-
*/
2433-
public static String getSsoDomainForV3jwtCookie() {
2434-
return ssoDomainForV3jwtCookie;
2435-
}
2436-
2432+
2433+
/**
2434+
* Get v2jwtCookieName.
2435+
* @return the v2jwtCookieName.
2436+
*/
2437+
public static String getV2jwtCookieName() {
2438+
return v2jwtCookieName;
2439+
}
2440+
2441+
/**
2442+
* Get v3jwtAuthorizationUrl.
2443+
* @return the v3jwtAuthorizationUrl.
2444+
*/
2445+
public static String getV3jwtAuthorizationUrl() {
2446+
return v3jwtAuthorizationUrl;
2447+
}
2448+
2449+
/**
2450+
* Get ssoDomainForV3jwtCookie.
2451+
* @return the ssoDomainForV3jwtCookie.
2452+
*/
2453+
public static String getSsoDomainForV3jwtCookie() {
2454+
return ssoDomainForV3jwtCookie;
2455+
}
2456+
24372457
}

0 commit comments

Comments
 (0)