mirror of
https://github.com/awatertrevi/infisical.git
synced 2026-10-14 14:34:09 +00:00
feat: changed backend-pg to backend
This commit is contained in:
@@ -0,0 +1,134 @@
|
||||
import { Knex } from "knex";
|
||||
|
||||
import { TDbClient } from "@app/db";
|
||||
import {
|
||||
TableName,
|
||||
TUserActionsInsert,
|
||||
TUserActionsUpdate,
|
||||
TUserEncryptionKeys,
|
||||
TUserEncryptionKeysInsert,
|
||||
TUserEncryptionKeysUpdate
|
||||
} from "@app/db/schemas";
|
||||
import { DatabaseError } from "@app/lib/errors";
|
||||
import { ormify } from "@app/lib/knex";
|
||||
|
||||
export type TUserDALFactory = ReturnType<typeof userDALFactory>;
|
||||
|
||||
export const userDALFactory = (db: TDbClient) => {
|
||||
const userOrm = ormify(db, TableName.Users);
|
||||
const findUserByEmail = async (email: string, tx?: Knex) => userOrm.findOne({ email }, tx);
|
||||
|
||||
// USER ENCRYPTION FUNCTIONS
|
||||
// -------------------------
|
||||
const findUserEncKeyByEmail = async (email: string) => {
|
||||
try {
|
||||
return await db(TableName.Users)
|
||||
.where({ email })
|
||||
.join(
|
||||
TableName.UserEncryptionKey,
|
||||
`${TableName.Users}.id`,
|
||||
`${TableName.UserEncryptionKey}.userId`
|
||||
)
|
||||
.first();
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Find user enc by email" });
|
||||
}
|
||||
};
|
||||
|
||||
const findUserEncKeyByUserId = async (userId: string) => {
|
||||
try {
|
||||
const user = await db(TableName.Users)
|
||||
.where(`${TableName.Users}.id`, userId)
|
||||
.join(
|
||||
TableName.UserEncryptionKey,
|
||||
`${TableName.Users}.id`,
|
||||
`${TableName.UserEncryptionKey}.userId`
|
||||
)
|
||||
.first();
|
||||
if (user?.id) {
|
||||
// change to user id
|
||||
user.id = user.userId;
|
||||
}
|
||||
return user;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Find user enc by user id" });
|
||||
}
|
||||
};
|
||||
|
||||
const createUserEncryption = async (data: TUserEncryptionKeysInsert, tx?: Knex) => {
|
||||
try {
|
||||
const [userEnc] = await (tx || db)(TableName.UserEncryptionKey).insert(data).returning("*");
|
||||
return userEnc;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Create user encryption" });
|
||||
}
|
||||
};
|
||||
|
||||
const updateUserEncryptionByUserId = async (
|
||||
userId: string,
|
||||
data: TUserEncryptionKeysUpdate,
|
||||
tx?: Knex
|
||||
) => {
|
||||
try {
|
||||
const [userEnc] = await (tx || db)(TableName.UserEncryptionKey)
|
||||
.where({ userId })
|
||||
.update({ ...data })
|
||||
.returning("*");
|
||||
return userEnc;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Update user enc by user id" });
|
||||
}
|
||||
};
|
||||
|
||||
const upsertUserEncryptionKey = async (
|
||||
userId: string,
|
||||
data: Omit<TUserEncryptionKeysUpdate, "userId">,
|
||||
tx?: Knex
|
||||
) => {
|
||||
try {
|
||||
const [userEnc] = await (tx
|
||||
? tx(TableName.UserEncryptionKey)
|
||||
: db(TableName.UserEncryptionKey)
|
||||
)
|
||||
// if user insert make sure to pass all required data
|
||||
.insert({ userId, ...data } as TUserEncryptionKeys)
|
||||
.onConflict("userId")
|
||||
.merge()
|
||||
.returning("*");
|
||||
return userEnc;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Upsert user enc key" });
|
||||
}
|
||||
};
|
||||
|
||||
// USER ACTION FUNCTIONS
|
||||
// ---------------------
|
||||
const findOneUserAction = (filter: TUserActionsUpdate, tx?: Knex) => {
|
||||
try {
|
||||
return (tx || db)(TableName.UserAction).where(filter).first("*");
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Find one user action" });
|
||||
}
|
||||
};
|
||||
|
||||
const createUserAction = async (data: TUserActionsInsert, tx?: Knex) => {
|
||||
try {
|
||||
const [userAction] = await (tx || db)(TableName.UserAction).insert(data).returning("*");
|
||||
return userAction;
|
||||
} catch (error) {
|
||||
throw new DatabaseError({ error, name: "Create user action" });
|
||||
}
|
||||
};
|
||||
|
||||
return {
|
||||
...userOrm,
|
||||
findUserByEmail,
|
||||
findUserEncKeyByEmail,
|
||||
findUserEncKeyByUserId,
|
||||
updateUserEncryptionByUserId,
|
||||
upsertUserEncryptionKey,
|
||||
createUserEncryption,
|
||||
findOneUserAction,
|
||||
createUserAction
|
||||
};
|
||||
};
|
||||
@@ -0,0 +1,84 @@
|
||||
import { BadRequestError } from "@app/lib/errors";
|
||||
|
||||
import { AuthMethod } from "../auth/auth-type";
|
||||
import { TUserDALFactory } from "./user-dal";
|
||||
|
||||
type TUserServiceFactoryDep = {
|
||||
userDAL: TUserDALFactory;
|
||||
};
|
||||
|
||||
export type TUserServiceFactory = ReturnType<typeof userServiceFactory>;
|
||||
|
||||
export const userServiceFactory = ({ userDAL }: TUserServiceFactoryDep) => {
|
||||
const toggleUserMfa = async (userId: string, isMfaEnabled: boolean) => {
|
||||
const updatedUser = await userDAL.updateById(userId, {
|
||||
isMfaEnabled,
|
||||
mfaMethods: isMfaEnabled ? ["email"] : []
|
||||
});
|
||||
return updatedUser;
|
||||
};
|
||||
|
||||
const updateUserName = async (userId: string, firstName: string, lastName: string) => {
|
||||
const updatedUser = await userDAL.updateById(userId, {
|
||||
firstName,
|
||||
lastName
|
||||
});
|
||||
return updatedUser;
|
||||
};
|
||||
|
||||
const updateAuthMethods = async (userId: string, authMethods: AuthMethod[]) => {
|
||||
const user = await userDAL.findById(userId);
|
||||
if (!user) throw new BadRequestError({ name: "Update auth methods" });
|
||||
|
||||
const hasSamlEnabled = user?.authMethods?.some((method) =>
|
||||
[AuthMethod.OKTA_SAML, AuthMethod.AZURE_SAML, AuthMethod.JUMPCLOUD_SAML].includes(
|
||||
method as AuthMethod
|
||||
)
|
||||
);
|
||||
if (hasSamlEnabled)
|
||||
throw new BadRequestError({
|
||||
name: "Update auth method",
|
||||
message: "Failed to update auth methods due to SAML SSO "
|
||||
});
|
||||
|
||||
const updatedUser = await userDAL.updateById(userId, { authMethods });
|
||||
return updatedUser;
|
||||
};
|
||||
|
||||
const getMe = async (userId: string) => {
|
||||
const user = await userDAL.findUserEncKeyByUserId(userId);
|
||||
if (!user) throw new BadRequestError({ message: "user not found", name: "Get Me" });
|
||||
return user;
|
||||
};
|
||||
|
||||
const deleteMe = async (userId: string) => {
|
||||
const user = await userDAL.deleteById(userId);
|
||||
return user;
|
||||
};
|
||||
|
||||
// user actions operations
|
||||
const createUserAction = async (userId: string, action: string) => {
|
||||
const userAction = await userDAL.transaction(async (tx) => {
|
||||
const existingAction = await userDAL.findOneUserAction({ action, userId }, tx);
|
||||
if (existingAction) return existingAction;
|
||||
return userDAL.createUserAction({ action, userId }, tx);
|
||||
});
|
||||
|
||||
return userAction;
|
||||
};
|
||||
|
||||
const getUserAction = async (userId: string, action: string) => {
|
||||
const userAction = await userDAL.findOneUserAction({ action, userId });
|
||||
return userAction;
|
||||
};
|
||||
|
||||
return {
|
||||
toggleUserMfa,
|
||||
updateUserName,
|
||||
updateAuthMethods,
|
||||
deleteMe,
|
||||
getMe,
|
||||
createUserAction,
|
||||
getUserAction
|
||||
};
|
||||
};
|
||||
Reference in New Issue
Block a user