Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

✨ add column githubUsername to users table #4132

Merged
merged 4 commits into from
Nov 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 21 additions & 13 deletions adminSiteServer/authentication.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -273,6 +273,7 @@ interface TailscaleStatus {
User?: {
[key: string]: {
DisplayName?: string
LoginName?: string
}
}
}
Expand Down Expand Up @@ -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()
}
Expand Down Expand Up @@ -349,26 +358,25 @@ async function getTailscaleIpToUserMap(): Promise<Record<string, string>> {
const ipToUser: Record<string, string> = {}

// Map UserIDs to LoginNames
const userIdToDisplayName: Record<string, string> = {}
const userIdToLoginName: Record<string, string> = {}

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
}
}
}

// 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
}
}
}
Expand Down
102 changes: 102 additions & 0 deletions db/migration/1731317168994-AddGithubUsernameToUsers.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
import { MigrationInterface, QueryRunner } from "typeorm"

export class AddGithubUsernameToUsers1731317168994
implements MigrationInterface
{
public async up(queryRunner: QueryRunner): Promise<void> {
// 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<void> {
// Remove 'githubUsername' column using raw SQL
await queryRunner.query(`
ALTER TABLE users DROP COLUMN githubUsername;
`)
}
}
Loading