diff --git a/adminSiteServer/authentication.tsx b/adminSiteServer/authentication.tsx index 8118ff66e5e..605fb4c6747 100644 --- a/adminSiteServer/authentication.tsx +++ b/adminSiteServer/authentication.tsx @@ -273,6 +273,7 @@ interface TailscaleStatus { User?: { [key: string]: { DisplayName?: string + LoginName?: string } } } @@ -301,14 +302,22 @@ export async function tailscaleAuthMiddleware( return next() } - const user = await db - .knexInstance() - .table("users") - .where({ fullName: githubUserName }) - .first() + let user + try { + // Look up user by 'githubUsername' + user = await db + .knexInstance() + .table("users") + .where({ githubUsername: githubUserName }) + .first() + } catch (error) { + console.error(`Error looking up user by githubUsername: ${error}`) + return next() + } + if (!user) { console.error( - `User with name ${githubUserName} not found in MySQL. Please change your Github profile name to match your MySQL user.` + `User with githubUsername ${githubUserName} not found in MySQL.` ) return next() } @@ -349,15 +358,14 @@ async function getTailscaleIpToUserMap(): Promise> { const ipToUser: Record = {} // Map UserIDs to LoginNames - const userIdToDisplayName: Record = {} + const userIdToLoginName: Record = {} if (tailscaleStatus.User) { for (const [userId, userInfo] of Object.entries( tailscaleStatus.User )) { - if (userInfo.DisplayName) { - userIdToDisplayName[parseInt(userId)] = - userInfo.DisplayName + if (userInfo.LoginName) { + userIdToLoginName[parseInt(userId)] = userInfo.LoginName } } } @@ -365,10 +373,10 @@ async function getTailscaleIpToUserMap(): Promise> { // Include Peers if (tailscaleStatus.Peer) { for (const peer of Object.values(tailscaleStatus.Peer)) { - if (peer.UserID in userIdToDisplayName) { - const displayName = userIdToDisplayName[peer.UserID] + if (peer.UserID in userIdToLoginName) { + const LoginName = userIdToLoginName[peer.UserID] for (const ip of peer.TailscaleIPs) { - ipToUser[ip] = displayName + ipToUser[ip] = LoginName } } } diff --git a/db/migration/1731317168994-AddGithubUsernameToUsers.ts b/db/migration/1731317168994-AddGithubUsernameToUsers.ts new file mode 100644 index 00000000000..2287a20d673 --- /dev/null +++ b/db/migration/1731317168994-AddGithubUsernameToUsers.ts @@ -0,0 +1,102 @@ +import { MigrationInterface, QueryRunner } from "typeorm" + +export class AddGithubUsernameToUsers1731317168994 + implements MigrationInterface +{ + public async up(queryRunner: QueryRunner): Promise { + // Add 'githubUsername' column using raw SQL + await queryRunner.query(` + ALTER TABLE users ADD COLUMN githubUsername VARCHAR(255) NULL; + `) + + // Update 'githubUsername' for specific users + const updates = [ + { fullName: "Martin Račák", githubUsername: "rakyi@github" }, + { + fullName: "Marcel Gerber", + githubUsername: "marcelgerber@github", + }, + { fullName: "Lars Yencken", githubUsername: "larsyencken@github" }, + { fullName: "Matthieu Bergel", githubUsername: "mlbrgl@github" }, + { + fullName: "Simon van Teutem", + githubUsername: "simonvanteutem@github", + }, + { + fullName: "Lucas Rodés-Guirao", + githubUsername: "lucasrodes@github", + }, + { + fullName: "Veronika Samborska", + githubUsername: "veronikasamborska1994@github", + }, + { fullName: "Saloni Dattani", githubUsername: "saloni-nd@github" }, + { + fullName: "Charlie Giattino", + githubUsername: "CGiattino@github", + }, + { + fullName: "Hannah Ritchie", + githubUsername: "HannahRitchie@github", + }, + { fullName: "Mojmir Vinkler", githubUsername: "Marigold@github" }, + { fullName: "Ike Saunders", githubUsername: "ikesau@github" }, + { + fullName: "Sophia Mersmann", + githubUsername: "sophiamersmann@github", + }, + { fullName: "Joe Hasell", githubUsername: "JoeHasell@github" }, + { fullName: "Edouard Mathieu", githubUsername: "edomt@github" }, + { fullName: "Daniel Bachler", githubUsername: "danyx23@github" }, + { fullName: "Fiona Spooner", githubUsername: "spoonerf@github" }, + { fullName: "Marwa Boukarim", githubUsername: "mrwbkrm@github" }, + { fullName: "Max Roser", githubUsername: "maxroser@github" }, + { + fullName: "Pablo Arriagada", + githubUsername: "paarriagadap@github", + }, + { fullName: "Tuna Acisu", githubUsername: "antea04@github" }, + { + fullName: "Esteban Ortiz-Ospina", + githubUsername: "eoo-owid@github", + }, + { + fullName: "Natalie Reynolds-Garcia", + githubUsername: "natreygar@github", + }, + { fullName: "Bertha Rohenkohl", githubUsername: "bertharc@github" }, + { + fullName: "Bobbie Macdonald", + githubUsername: "bnjmacdonald@github", + }, + { + fullName: "Angela Wenham", + githubUsername: "angelawenham@github", + }, + { fullName: "Pablo Rosado", githubUsername: "pabloarosado@github" }, + { + fullName: "Bastian Herre", + githubUsername: "bastianherre@github", + }, + { fullName: "Valerie Muigai", githubUsername: "ValRMuigai@github" }, + ] + + for (const { fullName, githubUsername } of updates) { + await queryRunner.query( + ` + UPDATE users + SET githubUsername = ? + WHERE fullName = ?; + `, + [githubUsername, fullName] + ) + } + } + + public async down(queryRunner: QueryRunner): Promise { + // Remove 'githubUsername' column using raw SQL + await queryRunner.query(` + ALTER TABLE users DROP COLUMN githubUsername; + `) + } +}