-
Notifications
You must be signed in to change notification settings - Fork 746
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2000 from Infisical/daniel/default-org
Feat: Default organization slug for LDAP/SAML
- Loading branch information
Showing
16 changed files
with
367 additions
and
128 deletions.
There are no files selected for viewing
27 changes: 27 additions & 0 deletions
27
backend/src/db/migrations/20240620142418_default-saml-ldap-org.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TableName } from "../schemas"; | ||
|
||
const DEFAULT_AUTH_ORG_ID_FIELD = "defaultAuthOrgId"; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
const hasDefaultOrgColumn = await knex.schema.hasColumn(TableName.SuperAdmin, DEFAULT_AUTH_ORG_ID_FIELD); | ||
|
||
await knex.schema.alterTable(TableName.SuperAdmin, (t) => { | ||
if (!hasDefaultOrgColumn) { | ||
t.uuid(DEFAULT_AUTH_ORG_ID_FIELD).nullable(); | ||
t.foreign(DEFAULT_AUTH_ORG_ID_FIELD).references("id").inTable(TableName.Organization).onDelete("SET NULL"); | ||
} | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
const hasDefaultOrgColumn = await knex.schema.hasColumn(TableName.SuperAdmin, DEFAULT_AUTH_ORG_ID_FIELD); | ||
|
||
await knex.schema.alterTable(TableName.SuperAdmin, (t) => { | ||
if (hasDefaultOrgColumn) { | ||
t.dropForeign([DEFAULT_AUTH_ORG_ID_FIELD]); | ||
t.dropColumn(DEFAULT_AUTH_ORG_ID_FIELD); | ||
} | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,57 @@ | ||
import { Knex } from "knex"; | ||
|
||
import { TDbClient } from "@app/db"; | ||
import { TableName } from "@app/db/schemas"; | ||
import { TableName, TSuperAdmin, TSuperAdminUpdate } from "@app/db/schemas"; | ||
import { DatabaseError } from "@app/lib/errors"; | ||
import { ormify } from "@app/lib/knex"; | ||
|
||
export type TSuperAdminDALFactory = ReturnType<typeof superAdminDALFactory>; | ||
|
||
export const superAdminDALFactory = (db: TDbClient) => ormify(db, TableName.SuperAdmin, {}); | ||
export const superAdminDALFactory = (db: TDbClient) => { | ||
const superAdminOrm = ormify(db, TableName.SuperAdmin); | ||
|
||
const findById = async (id: string, tx?: Knex) => { | ||
const config = await (tx || db)(TableName.SuperAdmin) | ||
.where(`${TableName.SuperAdmin}.id`, id) | ||
.leftJoin(TableName.Organization, `${TableName.SuperAdmin}.defaultAuthOrgId`, `${TableName.Organization}.id`) | ||
.select( | ||
db.ref("*").withSchema(TableName.SuperAdmin) as unknown as keyof TSuperAdmin, | ||
db.ref("slug").withSchema(TableName.Organization).as("defaultAuthOrgSlug") | ||
) | ||
.first(); | ||
|
||
if (!config) { | ||
return null; | ||
} | ||
|
||
return { | ||
...config, | ||
defaultAuthOrgSlug: config?.defaultAuthOrgSlug || null | ||
} as TSuperAdmin & { defaultAuthOrgSlug: string | null }; | ||
}; | ||
|
||
const updateById = async (id: string, data: TSuperAdminUpdate, tx?: Knex) => { | ||
const updatedConfig = await (superAdminOrm || tx).transaction(async (trx: Knex) => { | ||
await superAdminOrm.updateById(id, data, trx); | ||
const config = await findById(id, trx); | ||
|
||
if (!config) { | ||
throw new DatabaseError({ | ||
error: "Failed to find updated super admin config", | ||
message: "Failed to update super admin config", | ||
name: "UpdateById" | ||
}); | ||
} | ||
|
||
return config; | ||
}); | ||
|
||
return updatedConfig; | ||
}; | ||
|
||
return { | ||
...superAdminOrm, | ||
findById, | ||
updateById | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
export type { SelectItemProps, SelectProps } from "./Select"; | ||
export { Select, SelectItem } from "./Select"; | ||
export { Select, SelectClear, SelectItem } from "./Select"; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.