init
This commit is contained in:
commit
451223816b
338 changed files with 9938 additions and 0 deletions
17
src/lib/core/auth/domainBlocker.ts
Normal file
17
src/lib/core/auth/domainBlocker.ts
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
import { doc, getDoc } from "firebase/firestore";
|
||||
import { db } from "../client/firebase";
|
||||
|
||||
|
||||
export async function checkAllowAccess(userDomain: string): Promise<boolean> {
|
||||
|
||||
const docRef = doc(db, "whitelist", "allowedDomains");
|
||||
const snapshot = await getDoc(docRef);
|
||||
|
||||
if(snapshot.exists()){
|
||||
let domains = snapshot.data();
|
||||
// console.log(`domains: ${JSON.stringify(domains)}`);
|
||||
return domains["account_email"].includes(userDomain);
|
||||
}
|
||||
|
||||
return false;
|
||||
};
|
||||
90
src/lib/core/auth/userPermissions.ts
Normal file
90
src/lib/core/auth/userPermissions.ts
Normal file
|
|
@ -0,0 +1,90 @@
|
|||
import type { User } from "firebase/auth";
|
||||
import { addDoc, collection, doc, getDoc, setDoc, updateDoc } from "firebase/firestore";
|
||||
import { db } from "../client/firebase";
|
||||
|
||||
export enum UserPermissions {
|
||||
NO_PERMISSION,
|
||||
THAI_PERMISSION = 1 << 0,
|
||||
MALAY_PERMISSION = 1 << 1,
|
||||
AUS_PERMISSION = 1 << 2,
|
||||
ALPHA3_PERMISSION = 1 << 3,
|
||||
|
||||
VIEWER = 1 << 4,
|
||||
EDITOR = 1 << 7,
|
||||
|
||||
DUBAI_PERMISSION = 1 << 8,
|
||||
COUNTER_PERMISSION = 1 << 9,
|
||||
SINGAPORE_PERMISSION = 1 << 10,
|
||||
COCKTAIL_PERMISSION = 1 << 11,
|
||||
|
||||
// add new permission by shifting after 7. eg. 8,9,...
|
||||
// also do add at server
|
||||
|
||||
SUPER_ADMIN_PERMISSION = THAI_PERMISSION |
|
||||
MALAY_PERMISSION |
|
||||
AUS_PERMISSION |
|
||||
ALPHA3_PERMISSION |
|
||||
COUNTER_PERMISSION |
|
||||
SINGAPORE_PERMISSION |
|
||||
DUBAI_PERMISSION |
|
||||
COCKTAIL_PERMISSION |
|
||||
(EDITOR | VIEWER),
|
||||
}
|
||||
|
||||
export function getPermissions(perms: number): UserPermissions[] {
|
||||
return Object.values(UserPermissions).filter(
|
||||
(permission) =>
|
||||
typeof permission === "number" && (perms & permission) !== 0,
|
||||
) as UserPermissions[];
|
||||
}
|
||||
|
||||
export function getDefaultPermission(): UserPermissions {
|
||||
return UserPermissions.NO_PERMISSION;
|
||||
}
|
||||
|
||||
export async function getUserPermission(user: User | null): Promise<string[]> {
|
||||
if(user == null){
|
||||
return [];
|
||||
}
|
||||
let qid = user.uid;
|
||||
let defaultPerms = ["no_permission"];
|
||||
// TODO: collect only important fields
|
||||
const ignoredFields = [
|
||||
"apiKey",
|
||||
];
|
||||
const docRef = doc(db, "users", "data");
|
||||
|
||||
|
||||
const snapshot = await getDoc(docRef);
|
||||
if(snapshot.exists()){
|
||||
let user_data = snapshot.data();
|
||||
if(Object.keys(user_data).includes(qid)){
|
||||
return user_data[qid]["permissions"];
|
||||
} else {
|
||||
|
||||
let umap: any = user.toJSON();
|
||||
umap["permissions"] = defaultPerms;
|
||||
umap["role"] = "guest";
|
||||
|
||||
for(let ignoredField of ignoredFields){
|
||||
umap[ignoredField] = undefined;
|
||||
}
|
||||
|
||||
let cleaned_umap: any = {};
|
||||
for(let k of Object.keys(umap)){
|
||||
if(umap[k] != undefined){
|
||||
cleaned_umap[k] = umap[k];
|
||||
}
|
||||
}
|
||||
|
||||
let fmap: any = {};
|
||||
fmap[qid] = cleaned_umap;
|
||||
|
||||
await updateDoc(doc(db, "users", "data"), fmap);
|
||||
|
||||
return defaultPerms;
|
||||
}
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue