You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
dick/src/utils/database.ts

203 lines
6.7 KiB

V1.1.0 (#24) ## Update Info 🍆 **DICK** v1.1.0 🍆 Our number is getting bigger! Though, I hear size is not what matters but how you use it. So, I am happy to introduce you to a bunch of new stuff within the front end! 🔀 **UPDATING** Updating your instance should be easy, unless you already edited the code base, at that point your on your own. If you have a direct clone of the master of the current 1.0.2(old master) branch, then all you need to do is 1. Browse to your DICK folder 2. Run `git pull` to pull new changes 3. Run `npm i` to install new dependancies 4. Delete the `dist` folder 5. Start DICK using `npm start` 6. Enjoy > **Note** > If you load your instance and styling seems wrong, please clear your browsers cache, and reload the page. > **Note** > The first user to log into your DICK WebUI will be marked as the instance admin. You can change which users are admin by editing the user database file located at `/src/database/users.json`. This file will only appear once you've started your instance for the first time. ✏️ **CHANGELOG** ```diff ADDITIONS + Admin Dashboard > This page will be where system administrators can view their syatem settings and stats! + Database > Added DICK database, inside JSON files with management utils. + Added new app settings page to Admin Dashboard > This will allow administrators to customize their instance on the fly without having to edit the codebase. White labeling! + Added user list to Admin Dashboard > This allows administrators to view which users are registered in their ASS currently, and their roles set. You can also create new users from this page. (There are a lot of hidden divs in this page so imstance admins can add extra code to dick to enable stuff like deletion of users) + Registrations > Administrators can toggle registrations into their ASS from their DICK UI via the /register page! + Captcha > By default, when a user gets login information wrong they will be forwarded to a Rick Roll. Now you can add a hCaptcha site key to DICK to enable hCaptcha for your login and register public pages! + Added a "default profile picture" > Every users default profile picture. This is planned to be able to be set per user in the future, so users can pick their own seperate from the default. REMOVALS - Removeed STAFF_IDs from codebase. > This means you can remove this CONSTANT from your instance CONSTANTS file, please see the repo's constants example file to see if yours matches it. ``` ```fix CHANGES = Large codebase cleanup = Seperated js for components into their own files based on job = Redid some naming for tailwind colour theme classes to provide proper theming from the tailwind config file = Cleaned up a lot of the utils = Added embed gen page as a hidden extra for devs to add themselves in their own time if they wish (please PR if you achieve this 🤘) = Fixed the flash message warning colours to actually be red or green depending on error/success ``` ## Issues Resolved / Fixes In This Release Resolves #17 , Resolves #14 , Resolves #10 , Resolves #7
2 years ago
import path from "path"
import fs from "fs-extra"
import { ISettingsDatabase, IUsersDatabase, IUserSettings } from "typings/database"
import { ASS_LOCATION } from "../constants"
import { parseAuthFile } from "./assJSONStructure"
import { Log } from "@callmekory/logger/lib"
const settingsDatabaseLocation = path.resolve(`./src/database/settings.json`)
const userDatabaseLocation = path.resolve(`./src/database/users.json`)
/////////////////////////////
//
// Get Database Files
//
/////////////////////////////
// Get settings database, creating a default one if it doesnt exist
export const getSettingsDatabase = (): ISettingsDatabase => {
try {
const databaseFile = fs.readFileSync(settingsDatabaseLocation).toString()
const database: ISettingsDatabase = JSON.parse(databaseFile)
return database
} catch (error) {
const defaultDatabase: ISettingsDatabase = {
name: "dick",
appEmoji: "🍆",
siteTitle: "DICK (Directly Integrated Client for Keisters)",
siteDescription: "The frontend for your backend",
loginText: "Sign in to easily manage your nudes.",
captchaEnabled: false,
captchaSiteID: null,
captchaSecretKey: null,
registrationEnabled: false,
privateModeEnabled: false,
logo: "./images/logo.png",
defaultProfilePicture: "./images/profile.png"
}
fs.writeJsonSync(settingsDatabaseLocation, defaultDatabase, { spaces: 4 })
return defaultDatabase
}
}
// Get users database, creating a default one if it doesnt exist
export const getUserDatabase = (): IUsersDatabase => {
try {
const databaseFile = fs.readFileSync(userDatabaseLocation).toString()
const database: IUsersDatabase = JSON.parse(databaseFile)
return database
} catch (error) {
const defaultDatabase: IUsersDatabase = []
fs.writeJsonSync(userDatabaseLocation, defaultDatabase, { spaces: 4 })
return defaultDatabase
}
}
// Get users database object, creating a default one if it doesnt exist
export const getUserDatabaseObj = (username: string): IUserSettings | null => {
try {
const database = getUserDatabase()
const user = database.find((e: IUserSettings) => e.username === username)
if (!user) {
let newUser: IUserSettings
// If there are no users in the database yet, we will make this user the admin (first user to login will always be admin)
if (database.length == 0) {
newUser = {
username: username,
role: "admin",
profilePicture: null
}
} else {
// Else we add the user to the database as a regular user
newUser = {
username: username,
role: "user",
profilePicture: null
}
}
database.push(newUser)
fs.writeJsonSync(userDatabaseLocation, database, { spaces: 4 })
return user
}
return user
} catch (error) {
Log.error(error)
return null
}
}
/////////////////////////////
//
// MISC
//
/////////////////////////////
/**
* Checks if the user is in ASS database, returns true if the user exists, or false if it does not
* @param username Username we are checking exists
* @param token? If passed, will also check ASS token
*/
export const checkIfUserExistInASS = async (username: string, token?: string): Promise<boolean> => {
const assUserDatabase = parseAuthFile()
let result = false
// Check if the user exists in the ASS Database
const assUsernameResult = assUserDatabase.find((user) => user.username === username) ? true : false
if (token) {
const assTokenResult = assUserDatabase.find((user) => user.password === token) ? true : false
if (assUsernameResult || assTokenResult) {
result = true
}
}
if (assUsernameResult) {
result = true
}
// Return true if any of the checks above found the user, else return false
return result
}
/**
* Checks if the user is in our local JSON database, returns true if the user exists, or false if it does not
* @param username Username we are checking exists
*/
export const checkIfUserExistInDICK = async (username: string): Promise<boolean> => {
const dickUserDatabase = getUserDatabase()
let result = false
// Check if the user exists in the DICK Database
if (dickUserDatabase.find((e: IUserSettings) => e.username === username)) {
result = true
}
// Return true if any of the checks above found the user, else return false
return result
}
/**
* Registers a new user to ASS
* @param username Username we will create
* @param password Password we will use
*/
export const createUserInASS = async (username: string, password: string) => {
const AUTH_FILE_PATH = path.resolve(`${ASS_LOCATION}/auth.json`)
fs.readJson(AUTH_FILE_PATH)
.then((auth) => {
auth.users[password] = { username, count: 0 }
fs.writeJsonSync(AUTH_FILE_PATH, auth, { spaces: 4 })
})
}
/**
* Registers a new user to DICK
* @param username Username we will create
*/
export const createUserInDICK = async (username: string) => {
const userDatabase = getUserDatabase()
// If user does not exist in our database, we create it
if (!userDatabase.find((e: IUserSettings) => e.username === username)) {
let newUser: IUserSettings
// If there are no users in the database yet, we will make this user the admin (first user to login will always be admin)
if (userDatabase.length == 0) {
newUser = {
username: username,
role: "admin",
profilePicture: null
}
} else {
// Else we add the user to the database as a regular user
newUser = {
username: username,
role: "user",
profilePicture: null
}
}
userDatabase.push(newUser)
fs.writeJsonSync(userDatabaseLocation, userDatabase, { spaces: 4 })
}
}
/**
* Cycles through all users in the ASS auth database and adds them to the DICk user database
* if they are not already in it.
*/
export const syncAssUsersToDick = () => {
const assUserDatabase = parseAuthFile()
const dickUserDatabase = getUserDatabase()
if(dickUserDatabase.length !== 0){
for (const user of assUserDatabase) {
if (!dickUserDatabase.find((e: IUserSettings) => e.username === user.username)) {
createUserInDICK(user.username)
}
}
}
}