3
3
*/
4
4
5
5
/**
6
- * This controller exposes Gitlab REST endpoints.
6
+ * This controller exposes Azure REST endpoints.
7
7
*
8
8
* @author TCSCODER
9
9
* @version 1.0
@@ -17,12 +17,11 @@ const errors = require('../common/errors');
17
17
const constants = require ( '../common/constants' ) ;
18
18
const config = require ( '../config' ) ;
19
19
const AzureService = require ( '../services/AzureService' ) ;
20
- const GitlabService = require ( '../services/GitlabService' ) ;
21
20
const UserService = require ( '../services/UserService' ) ;
22
21
const User = require ( '../models' ) . User ;
23
22
const OwnerUserTeam = require ( '../models' ) . OwnerUserTeam ;
24
- // const UserMapping = require('../models').UserMapping;
25
- const UserGroupMapping = require ( '../models' ) . UserGroupMapping ;
23
+ const UserMapping = require ( '../models' ) . UserMapping ;
24
+ const UserTeamMapping = require ( '../models' ) . UserTeamMapping ;
26
25
27
26
const request = superagentPromise ( superagent , Promise ) ;
28
27
@@ -40,7 +39,7 @@ async function ownerUserLogin(req, res) {
40
39
if ( ! req . session . state ) {
41
40
req . session . state = helper . generateIdentifier ( ) ;
42
41
}
43
- // redirect to GitLab OAuth
42
+ // redirect to Azure OAuth
44
43
const callbackUri = `${ config . WEBSITE_SECURE } ${ constants . AZURE_OWNER_CALLBACK_URL } ` ;
45
44
res . redirect ( `https://app.vssps.visualstudio.com/oauth2/authorize?client_id=${
46
45
config . AZURE_APP_ID
@@ -50,7 +49,7 @@ async function ownerUserLogin(req, res) {
50
49
}
51
50
52
51
/**
53
- * Owner user login callback, redirected by GitLab .
52
+ * Owner user login callback, redirected by Azure .
54
53
* @param {Object } req the request
55
54
* @param {Object } res the response
56
55
*/
@@ -104,7 +103,7 @@ async function ownerUserLoginCallback(req, res) {
104
103
async function listOwnerUserTeams ( req ) {
105
104
const user = await UserService . getAccessTokenByHandle ( req . currentUser . handle , constants . USER_TYPES . AZURE ) ;
106
105
if ( ! user || ! user . accessToken ) {
107
- throw new errors . UnauthorizedError ( 'You have not setup for Gitlab .' ) ;
106
+ throw new errors . UnauthorizedError ( 'You have not setup for Azure .' ) ;
108
107
}
109
108
return await AzureService . listOwnerUserTeams ( user , req . query . page , req . query . perPage ) ;
110
109
}
@@ -136,7 +135,7 @@ async function addUserToTeam(req, res) {
136
135
// store identifier to session, to be compared in callback
137
136
req . session . identifier = identifier ;
138
137
139
- // redirect to GitLab OAuth
138
+ // redirect to Azure OAuth
140
139
const callbackUri = `${ config . WEBSITE_SECURE } /api/${ config . API_VERSION } /azure/normaluser/callback` ;
141
140
res . redirect ( `https://app.vssps.visualstudio.com/oauth2/authorize?client_id=${
142
141
config . AZURE_USER_APP_ID
@@ -146,7 +145,7 @@ async function addUserToTeam(req, res) {
146
145
}
147
146
148
147
/**
149
- * Normal user callback, to be added to group. Redirected by GitLab .
148
+ * Normal user callback, to be added to group. Redirected by Azure .
150
149
* @param {Object } req the request
151
150
* @param {Object } res the response
152
151
*/
@@ -197,29 +196,15 @@ async function addUserToTeamCallback(req, res) {
197
196
. end ( )
198
197
. then ( ( resp ) => resp . body ) ;
199
198
200
- // PATCH https://vsaex.dev.azure.com/{organization}/_apis/userentitlements/{userId}?api-version=5.1-preview.2
201
199
try {
202
- await request
203
- . patch ( `https://vsaex.dev.azure.com/telagaid/_apis/userentitlements/ ${ userProfile . id } ? api-version=5.1-preview.2 ` )
200
+ await request
201
+ . patch ( `https://vsaex.dev.azure.com/${ team . organizationName } /_apis/UserEntitlements?doNotSendInviteForNewUsers=true& api-version=5.1-preview.3 ` )
204
202
. send ( [ {
205
- from : "" ,
203
+ from : '' ,
206
204
op : 0 ,
207
- path : "" ,
205
+ path : `/ ${ userProfile . id } /projectEntitlements/ ${ team . githubOrgId } /teamRefs` ,
208
206
value : {
209
- projectEntitlements : {
210
- projectRef : {
211
- id : team . githubOrgId
212
- } ,
213
- teamRefs : [ {
214
- id :team . teamId
215
- } ]
216
- } ,
217
- user : {
218
- subjectKind : 'user' ,
219
- displayName : userProfile . emailAddress ,
220
- principalName : userProfile . emailAddress ,
221
- id : userProfile . id
222
- }
207
+ id :team . teamId
223
208
}
224
209
} ] )
225
210
. set ( 'Content-Type' , 'application/json-patch+json' )
@@ -229,37 +214,70 @@ async function addUserToTeamCallback(req, res) {
229
214
catch ( err ) {
230
215
console . log ( err ) ; // eslint-disable-line no-console
231
216
}
217
+
218
+ // associate azure username with TC username
219
+ const mapping = await dbHelper . scanOne ( UserMapping , {
220
+ topcoderUsername : { eq : req . session . tcUsername } ,
221
+ } ) ;
222
+ if ( mapping ) {
223
+ await dbHelper . update ( UserMapping , mapping . id , {
224
+ azureEmail : userProfile . emailAddress ,
225
+ azureUserId : userProfile . id
226
+ } ) ;
227
+ } else {
228
+ await dbHelper . create ( UserMapping , {
229
+ id : helper . generateIdentifier ( ) ,
230
+ topcoderUsername : req . session . tcUsername ,
231
+ azureEmail : userProfile . emailAddress ,
232
+ azureUserId : userProfile . id
233
+ } ) ;
234
+ }
235
+
236
+ const azureUserToTeamMapping = await dbHelper . scanOne ( UserTeamMapping , {
237
+ teamId : { eq : team . teamId } ,
238
+ azureUserId : { eq : userProfile . id } ,
239
+ } ) ;
240
+
241
+ if ( ! azureUserToTeamMapping ) {
242
+ await dbHelper . create ( UserTeamMapping , {
243
+ id : helper . generateIdentifier ( ) ,
244
+ teamId : team . teamId ,
245
+ azureUserId : userProfile . id ,
246
+ azureProjectId : team . githubOrgId
247
+ } ) ;
248
+ }
249
+
232
250
// redirect to success page
233
- res . redirect ( `${ constants . USER_ADDED_TO_TEAM_SUCCESS_URL } /azure/path ` ) ;
251
+ res . redirect ( `${ constants . USER_ADDED_TO_TEAM_SUCCESS_URL } /azure/${ team . organizationName } _ ${ team . githubOrgId } ` ) ;
234
252
}
235
253
236
254
237
255
/**
238
- * Delete users from a group .
256
+ * Delete users from a team .
239
257
* @param {Object } req the request
240
258
* @param {Object } res the response
241
259
*/
242
260
async function deleteUsersFromTeam ( req , res ) {
243
- const groupId = req . params . id ;
244
- let groupInDB ;
261
+ const teamId = req . params . id ;
262
+ let teamInDB ;
245
263
try {
246
- groupInDB = await helper . ensureExists ( OwnerUserTeam , { groupId } , 'OwnerUserTeam' ) ;
264
+ teamInDB = await helper . ensureExists ( OwnerUserTeam , { teamId } , 'OwnerUserTeam' ) ;
247
265
} catch ( err ) {
248
266
if ( ! ( err instanceof errors . NotFoundError ) ) {
249
267
throw err ;
250
268
}
251
269
}
252
- // If groupInDB not exists, then just return
253
- if ( groupInDB ) {
270
+ // If teamInDB not exists, then just return
271
+ if ( teamInDB ) {
254
272
try {
255
273
const ownerUser = await helper . ensureExists ( User ,
256
- { username : groupInDB . ownerUsername , type : constants . USER_TYPES . GITLAB , role : constants . USER_ROLES . OWNER } , 'User' ) ;
257
- await GitlabService . refreshGitlabUserAccessToken ( ownerUser ) ;
258
- const userGroupMappings = await dbHelper . scan ( UserGroupMapping , { groupId } ) ;
274
+ { username : teamInDB . ownerUsername , type : constants . USER_TYPES . AZURE , role : constants . USER_ROLES . OWNER } , 'User' ) ;
275
+ await AzureService . refreshAzureUserAccessToken ( ownerUser ) ;
276
+ const userTeamMappings = await dbHelper . scan ( UserTeamMapping , { teamId } ) ;
259
277
// eslint-disable-next-line no-restricted-syntax
260
- for ( const userGroupMapItem of userGroupMappings ) {
261
- await GitlabService . deleteUserFromGitlabGroup ( ownerUser . accessToken , groupId , userGroupMapItem . gitlabUserId ) ;
262
- await dbHelper . remove ( UserGroupMapping , { id : userGroupMapItem . id } ) ;
278
+ for ( const userTeamMapItem of userTeamMappings ) {
279
+ await AzureService . deleteUserFromAzureTeam ( ownerUser . accessToken , teamInDB , userTeamMapItem . azureUserId ) ;
280
+ await dbHelper . remove ( UserTeamMapping , { id : userTeamMapItem . id } ) ;
263
281
}
264
282
} catch ( err ) {
265
283
throw err ;
0 commit comments