3
3
*/
4
4
package com .cronos .onlinereview .actions .project ;
5
5
6
+ import java .util .ArrayList ;
7
+ import java .util .Arrays ;
8
+ import java .util .List ;
9
+
10
+ import org .apache .http .HttpHeaders ;
11
+
12
+ import com .auth0 .jwt .interfaces .Claim ;
13
+ import com .auth0 .jwt .interfaces .DecodedJWT ;
14
+ import com .cronos .onlinereview .util .AuthorizationHelper ;
6
15
import com .topcoder .onlinereview .component .exception .BaseException ;
7
16
import com .topcoder .onlinereview .component .grpcclient .GrpcHelper ;
8
17
@@ -26,11 +35,47 @@ public SyncProjectAction() {
26
35
27
36
public String execute () throws BaseException {
28
37
String projectId = request .getParameter ("projectId" );
38
+ String tables = request .getParameter ("tables" );
39
+ List <String > tableNames = new ArrayList <>();
40
+ if (tables != null && !tables .isEmpty ()) {
41
+ tableNames = Arrays .asList (tables .split ("," ));
42
+ }
43
+ if (projectId .isEmpty () || tableNames .isEmpty ()) {
44
+ return NONE ;
45
+ }
46
+
47
+ String authHeader = request .getHeader (HttpHeaders .AUTHORIZATION );
48
+ if (authHeader == null || authHeader .isEmpty ()) {
49
+ return NONE ;
50
+ }
51
+ String [] headerParts = authHeader .split (" " );
52
+ if (headerParts .length < 2 ) {
53
+ return NONE ;
54
+ }
55
+ String token = headerParts [1 ];
29
56
30
- GrpcHelper .getSyncServiceRpc ().saveProjectSync (Long .valueOf (projectId ), false , false ,
31
- false , false , true , false , false , false );
57
+ DecodedJWT jwt ;
58
+ try {
59
+ jwt = AuthorizationHelper .validateJWTToken (token );
60
+ } catch (Exception e ) {
61
+ return NONE ;
62
+ }
63
+ boolean hasAccess = false ;
64
+ for (String claimName : jwt .getClaims ().keySet ()) {
65
+ if (claimName .endsWith ("/roles" )) {
66
+ Claim claim = jwt .getClaim (claimName );
67
+ for (String role : claim .asArray (String .class )) {
68
+ if (role .equals ("administrator" )) {
69
+ hasAccess = true ;
70
+ }
71
+ }
72
+ }
73
+ }
74
+ if (!hasAccess ) {
75
+ return NONE ;
76
+ }
32
77
33
- // Signal about successful execution of the Action
34
- return "syncResult" ;
78
+ GrpcHelper . getSyncServiceRpc (). manualSync ( Long . valueOf ( projectId ), tableNames );
79
+ return NONE ;
35
80
}
36
81
}
0 commit comments