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/routes/route.admin.ts

181 lines
7.0 KiB

2 years ago
import { Request, Response, Router } from "express"
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 multer from "multer"
import { authCheck, adminCheck, wrap } from "../utils/middleware"
import { checkIfUserExistInASS, checkIfUserExistInDICK, createUserInASS, createUserInDICK, getSettingsDatabase } from "../utils/database"
import { TEMPLATE } from "../constants"
2 years ago
import { Pager } from "../Pager"
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 { defaultPPStorage, defaultPPStorageDist, imageFileFilter, logoStorage, logoStorageDist } from "../utils/uploads"
const settingsDatabaseLocation = path.resolve(`./src/database/settings.json`)
2 years ago
export const adminRoutes = (app: Router) => {
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
app.get(
"/admin",
authCheck,
adminCheck,
wrap,
async (req: Request, res: Response) => {
return Pager.render(res, req, TEMPLATE.USER, {})
}
)
// Save button on app settings page
app.post(
"/admin/save/settings",
authCheck,
adminCheck,
(req: Request, res: Response) => {
const settingsDatabase = getSettingsDatabase()
const { name, appEmoji, siteTitle, siteDescription, loginText, captchaCheckbox, captchaSiteID, captchaSecretKey, privateModeEnabled, registrationEnabled } = req.body
/*
* This code is for if I ever decide to add changing the location of the image urls (such as calling an external URL from local files)
\
if (logo) {
if (!/\.(jpg|jpeg|png|webp|avif|gif|svg)$/.test(logo)) {
req.flash('error_message', 'Logo URL is not a valid picture.')
return res.redirect('/admin')
}
settingsDatabase.logo = logo
}
if (defaultProfilePicture) {
if (!/\.(jpg|jpeg|png|webp|avif|gif|svg)$/.test(logo)) {
req.flash('error_message', 'Default profile picture is not a valid picture.')
return res.redirect('/admin')
}
settingsDatabase.defaultProfilePicture = defaultProfilePicture
}
*/
if (captchaCheckbox) {
// If they do not have a capatcha site id set, they can not enable and save capatcha preventing it not working.
if (!settingsDatabase.captchaSiteID) {
if (!captchaSiteID) {
req.flash('error_message', 'You must include a captcha site ID to enable captcha.')
return res.redirect('/admin')
}
2 years ago
}
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
if (!settingsDatabase.captchaSecretKey) {
if (!captchaSecretKey) {
req.flash('error_message', 'You must include a captcha secret key to enable captcha.')
return res.redirect('/admin')
}
}
settingsDatabase.captchaEnabled = true
} else {
settingsDatabase.captchaEnabled = false
}
name ? settingsDatabase.name = name : null
appEmoji ? settingsDatabase.appEmoji = appEmoji : null
siteTitle ? settingsDatabase.siteTitle = siteTitle : null
siteDescription ? settingsDatabase.siteDescription = siteDescription : null
loginText ? settingsDatabase.loginText = loginText : null
captchaSiteID ? settingsDatabase.captchaSiteID = captchaSiteID : null
captchaSecretKey ? settingsDatabase.captchaSecretKey = captchaSecretKey : null
privateModeEnabled ? settingsDatabase.privateModeEnabled = true : settingsDatabase.privateModeEnabled = false
registrationEnabled ? settingsDatabase.registrationEnabled = true : settingsDatabase.registrationEnabled = false
fs.writeJsonSync(settingsDatabaseLocation, settingsDatabase, { spaces: 4 })
req.flash('success_alert_message', 'Settings successfully saved')
return res.redirect('/admin')
}
)
// App logo upload on app settings page
app.post(
"/admin/upload/logo",
authCheck,
adminCheck,
(req: Request, res: Response) => {
const uploadLogo = multer({ storage: logoStorage, fileFilter: imageFileFilter }).fields([{ name: 'app-logo', maxCount: 1 }])
const uploadLogoDist = multer({ storage: logoStorageDist, fileFilter: imageFileFilter }).fields([{ name: 'app-logo', maxCount: 1 }])
uploadLogo(req, res, (err) => {
if (err) {
console.log(err)
req.flash('error_message', 'Logo failed to upload')
return res.redirect('/admin')
}
})
uploadLogoDist(req, res, (err) => {
if (err) {
console.log(err)
req.flash('error_message', 'Logo failed to upload')
return res.redirect('/admin')
}
})
req.flash('success_alert_message', 'Logo successfully uploaded and saved. Please clear cache to see the new change!')
return res.redirect('/admin')
}
)
// Default profile picture upload on app settings page
app.post(
"/admin/upload/default-pp",
authCheck,
adminCheck,
(req: Request, res: Response) => {
const uploadDefaultPP = multer({ storage: defaultPPStorage, fileFilter: imageFileFilter }).fields([{ name: 'default-pp', maxCount: 1 }])
const uploadDefaultPPDist = multer({ storage: defaultPPStorageDist, fileFilter: imageFileFilter }).fields([{ name: 'default-pp', maxCount: 1 }])
uploadDefaultPP(req, res, (err) => {
if (err) {
console.log(err)
req.flash('error_message', 'Profile picture failed to upload. Please clear cache to see the new change!')
return res.redirect('/admin')
}
})
uploadDefaultPPDist(req, res, (err) => {
if (err) {
console.log(err)
req.flash('error_message', 'Logo failed to upload')
return res.redirect('/admin')
}
})
req.flash('success_alert_message', 'Logo successfully uploaded and saved')
return res.redirect('/admin')
}
)
// Add new user via add user modal
app.post('/admin/add/user', async (req, res) => {
// Check if the form is filled our properly
if (!req.body.username) {
req.flash('error_message', 'You did not include a username!')
return res.redirect("/admin")
}
if (!req.body.password) {
req.flash('error_message', 'You did not include a password!')
return res.redirect("/admin")
}
if (req.body.username > 20) {
req.flash('error_messge', 'Username can not be more than 20 characters!')
return res.redirect("/admin")
}
if (req.body.password < 5) {
req.flash('error_messge', 'Secret key can not be less than 5 characters!')
return res.redirect("/admin")
}
// Check if user exists in ass or dick, if it does then we throw error
if (await checkIfUserExistInASS(req.body.username, req.body.password) || await checkIfUserExistInDICK(req.body.username)) {
req.flash('error_message', 'User already exists!')
return res.redirect("/admin")
}
// Create the user
await createUserInASS(req.body.username, req.body.password)
await createUserInDICK(req.body.username)
// Redirect them to the login screen
req.flash('success_alert_message', `You have sucesfully created a user with the name ${req.body.username}. They can now log in with the token you provided.`)
return res.redirect("/admin")
})
2 years ago
}