33 */
44
55/**
6- * This controller exposes Gitlab REST endpoints.
6+ * This controller exposes Azure REST endpoints.
77 *
88 * @author TCSCODER
99 * @version 1.0
@@ -17,12 +17,11 @@ const errors = require('../common/errors');
1717const constants = require ( '../common/constants' ) ;
1818const config = require ( '../config' ) ;
1919const AzureService = require ( '../services/AzureService' ) ;
20- const GitlabService = require ( '../services/GitlabService' ) ;
2120const UserService = require ( '../services/UserService' ) ;
2221const User = require ( '../models' ) . User ;
2322const 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 ;
2625
2726const request = superagentPromise ( superagent , Promise ) ;
2827
@@ -40,7 +39,7 @@ async function ownerUserLogin(req, res) {
4039 if ( ! req . session . state ) {
4140 req . session . state = helper . generateIdentifier ( ) ;
4241 }
43- // redirect to GitLab OAuth
42+ // redirect to Azure OAuth
4443 const callbackUri = `${ config . WEBSITE_SECURE } ${ constants . AZURE_OWNER_CALLBACK_URL } ` ;
4544 res . redirect ( `https://app.vssps.visualstudio.com/oauth2/authorize?client_id=${
4645 config . AZURE_APP_ID
@@ -50,7 +49,7 @@ async function ownerUserLogin(req, res) {
5049}
5150
5251/**
53- * Owner user login callback, redirected by GitLab .
52+ * Owner user login callback, redirected by Azure .
5453 * @param {Object } req the request
5554 * @param {Object } res the response
5655 */
@@ -104,7 +103,7 @@ async function ownerUserLoginCallback(req, res) {
104103async function listOwnerUserTeams ( req ) {
105104 const user = await UserService . getAccessTokenByHandle ( req . currentUser . handle , constants . USER_TYPES . AZURE ) ;
106105 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 .' ) ;
108107 }
109108 return await AzureService . listOwnerUserTeams ( user , req . query . page , req . query . perPage ) ;
110109}
@@ -136,7 +135,7 @@ async function addUserToTeam(req, res) {
136135 // store identifier to session, to be compared in callback
137136 req . session . identifier = identifier ;
138137
139- // redirect to GitLab OAuth
138+ // redirect to Azure OAuth
140139 const callbackUri = `${ config . WEBSITE_SECURE } /api/${ config . API_VERSION } /azure/normaluser/callback` ;
141140 res . redirect ( `https://app.vssps.visualstudio.com/oauth2/authorize?client_id=${
142141 config . AZURE_USER_APP_ID
@@ -146,7 +145,7 @@ async function addUserToTeam(req, res) {
146145}
147146
148147/**
149- * Normal user callback, to be added to group. Redirected by GitLab .
148+ * Normal user callback, to be added to group. Redirected by Azure .
150149 * @param {Object } req the request
151150 * @param {Object } res the response
152151 */
@@ -197,29 +196,15 @@ async function addUserToTeamCallback(req, res) {
197196 . end ( )
198197 . then ( ( resp ) => resp . body ) ;
199198
200- // PATCH https://vsaex.dev.azure.com/{organization}/_apis/userentitlements/{userId}?api-version=5.1-preview.2
201199 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 ` )
204202 . send ( [ {
205- from : "" ,
203+ from : '' ,
206204 op : 0 ,
207- path : "" ,
205+ path : `/ ${ userProfile . id } /projectEntitlements/ ${ team . githubOrgId } /teamRefs` ,
208206 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
223208 }
224209 } ] )
225210 . set ( 'Content-Type' , 'application/json-patch+json' )
@@ -229,37 +214,70 @@ async function addUserToTeamCallback(req, res) {
229214 catch ( err ) {
230215 console . log ( err ) ; // eslint-disable-line no-console
231216 }
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+
232250 // 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 } ` ) ;
234252}
235253
236254
237255/**
238- * Delete users from a group .
256+ * Delete users from a team .
239257 * @param {Object } req the request
240258 * @param {Object } res the response
241259 */
242260async function deleteUsersFromTeam ( req , res ) {
243- const groupId = req . params . id ;
244- let groupInDB ;
261+ const teamId = req . params . id ;
262+ let teamInDB ;
245263 try {
246- groupInDB = await helper . ensureExists ( OwnerUserTeam , { groupId } , 'OwnerUserTeam' ) ;
264+ teamInDB = await helper . ensureExists ( OwnerUserTeam , { teamId } , 'OwnerUserTeam' ) ;
247265 } catch ( err ) {
248266 if ( ! ( err instanceof errors . NotFoundError ) ) {
249267 throw err ;
250268 }
251269 }
252- // If groupInDB not exists, then just return
253- if ( groupInDB ) {
270+ // If teamInDB not exists, then just return
271+ if ( teamInDB ) {
254272 try {
255273 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 } ) ;
259277 // 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 } ) ;
263281 }
264282 } catch ( err ) {
265283 throw err ;
0 commit comments