2023-02-21 21:23:53 +13:00
|
|
|
import {
|
|
|
|
AdminUser,
|
2023-07-19 05:10:15 +12:00
|
|
|
AdminOnlyUser,
|
2023-02-21 21:23:53 +13:00
|
|
|
BuilderUser,
|
|
|
|
SSOAuthDetails,
|
|
|
|
SSOUser,
|
2023-09-18 23:10:27 +12:00
|
|
|
User,
|
2023-02-21 21:23:53 +13:00
|
|
|
} from "@budibase/types"
|
2023-02-25 06:31:58 +13:00
|
|
|
import { authDetails } from "./sso"
|
2023-09-18 23:10:27 +12:00
|
|
|
import { uuid } from "./common"
|
|
|
|
import { generator } from "./generator"
|
|
|
|
import { tenant } from "."
|
2023-02-21 21:23:53 +13:00
|
|
|
|
2023-09-18 23:10:27 +12:00
|
|
|
export const newEmail = () => {
|
2023-11-10 00:05:42 +13:00
|
|
|
return `${uuid()}@example.com`
|
2023-09-18 23:10:27 +12:00
|
|
|
}
|
|
|
|
|
|
|
|
export const user = (userProps?: Partial<Omit<User, "userId">>): User => {
|
2023-09-25 20:48:27 +13:00
|
|
|
const userId = userProps?._id
|
2023-09-18 23:10:27 +12:00
|
|
|
return {
|
|
|
|
_id: userId,
|
|
|
|
userId,
|
|
|
|
email: newEmail(),
|
2024-01-04 00:06:23 +13:00
|
|
|
password: "password",
|
2023-09-18 23:10:27 +12:00
|
|
|
roles: { app_test: "admin" },
|
|
|
|
firstName: generator.first(),
|
|
|
|
lastName: generator.last(),
|
2024-02-09 05:32:14 +13:00
|
|
|
pictureUrl: "http://example.com",
|
2023-09-18 23:10:27 +12:00
|
|
|
tenantId: tenant.id(),
|
|
|
|
...userProps,
|
|
|
|
}
|
|
|
|
}
|
2023-02-21 21:23:53 +13:00
|
|
|
|
|
|
|
export const adminUser = (userProps?: any): AdminUser => {
|
|
|
|
return {
|
|
|
|
...user(userProps),
|
|
|
|
admin: {
|
|
|
|
global: true,
|
|
|
|
},
|
|
|
|
builder: {
|
|
|
|
global: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 05:10:15 +12:00
|
|
|
export const adminOnlyUser = (userProps?: any): AdminOnlyUser => {
|
|
|
|
return {
|
|
|
|
...user(userProps),
|
|
|
|
admin: {
|
|
|
|
global: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-09-25 20:48:27 +13:00
|
|
|
export const builderUser = (userProps?: Partial<User>): BuilderUser => {
|
2023-02-21 21:23:53 +13:00
|
|
|
return {
|
|
|
|
...user(userProps),
|
|
|
|
builder: {
|
|
|
|
global: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-07-19 05:10:15 +12:00
|
|
|
export const appBuilderUser = (appId: string, userProps?: any): BuilderUser => {
|
|
|
|
return {
|
|
|
|
...user(userProps),
|
|
|
|
builder: {
|
|
|
|
apps: [appId],
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-02-21 21:23:53 +13:00
|
|
|
export function ssoUser(
|
|
|
|
opts: { user?: any; details?: SSOAuthDetails } = {}
|
|
|
|
): SSOUser {
|
|
|
|
const base = user(opts.user)
|
|
|
|
delete base.password
|
|
|
|
|
|
|
|
if (!opts.details) {
|
2023-02-25 06:31:58 +13:00
|
|
|
opts.details = authDetails(base)
|
2023-02-21 21:23:53 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
return {
|
|
|
|
...base,
|
|
|
|
forceResetPassword: false,
|
|
|
|
oauth2: opts.details?.oauth2,
|
|
|
|
provider: opts.details?.provider!,
|
|
|
|
providerType: opts.details?.providerType!,
|
|
|
|
thirdPartyProfile: {
|
|
|
|
email: base.email,
|
|
|
|
picture: base.pictureUrl,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
}
|