add admin admin settings
This commit is contained in:
@@ -2,7 +2,16 @@ import { DataTypes } from 'sequelize';
|
||||
import { env } from '$env/dynamic/private';
|
||||
import { building, dev } from '$app/environment';
|
||||
import * as bcrypt from 'bcrypt';
|
||||
import { BeforeCreate, BeforeUpdate, Column, Model, Sequelize, Table } from 'sequelize-typescript';
|
||||
import {
|
||||
BeforeCreate,
|
||||
BeforeUpdate,
|
||||
Column,
|
||||
Model,
|
||||
Sequelize,
|
||||
Table,
|
||||
Unique
|
||||
} from 'sequelize-typescript';
|
||||
import { Permissions } from '$lib/permissions';
|
||||
|
||||
@Table({ modelName: 'user' })
|
||||
export class User extends Model {
|
||||
@@ -26,18 +35,28 @@ export class User extends Model {
|
||||
|
||||
@Table({ modelName: 'admin' })
|
||||
export class Admin extends Model {
|
||||
@Column({ type: DataTypes.STRING, allowNull: false })
|
||||
@Column({ type: DataTypes.STRING, allowNull: false, unique: true })
|
||||
declare username: string;
|
||||
@Column({ type: DataTypes.STRING, allowNull: false })
|
||||
declare password: string;
|
||||
@Column({ type: DataTypes.BIGINT, allowNull: false })
|
||||
declare permissions: number;
|
||||
@Column({
|
||||
type: DataTypes.BIGINT,
|
||||
allowNull: false,
|
||||
get(this: Admin): Permissions | null {
|
||||
const permissions = this.getDataValue('permissions');
|
||||
return permissions != null ? new Permissions(permissions) : null;
|
||||
},
|
||||
set(this: Admin, value: Permissions) {
|
||||
this.setDataValue('permissions', value.value);
|
||||
}
|
||||
})
|
||||
declare permissions: Permissions;
|
||||
|
||||
@BeforeCreate
|
||||
@BeforeUpdate
|
||||
static hashPassword(instance: Admin) {
|
||||
if (instance.password != null) {
|
||||
instance.username = bcrypt.hashSync(instance.password, 10);
|
||||
instance.password = bcrypt.hashSync(instance.password, 10);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -1,11 +1,30 @@
|
||||
const sessions: string[] = [];
|
||||
import type { Permissions } from '$lib/permissions';
|
||||
import type { Cookies } from '@sveltejs/kit';
|
||||
|
||||
export function addSession(): string {
|
||||
const sessions: Map<string, Permissions> = new Map();
|
||||
|
||||
export function addSession(permissions: Permissions): string {
|
||||
const session = 'AAA';
|
||||
sessions.push(session);
|
||||
sessions.set(session, permissions);
|
||||
return session;
|
||||
}
|
||||
|
||||
export function hasSession(session: string): boolean {
|
||||
return sessions.find((v) => v == session) != undefined;
|
||||
export function getSession(session: string | Cookies, permissions?: number[]): Permissions | null {
|
||||
let sess: Permissions | null;
|
||||
if (typeof session == 'string') {
|
||||
sess = sessions.get(session) || null;
|
||||
} else {
|
||||
const sessionId = session.get('session');
|
||||
sess = sessionId ? sessions.get(sessionId) || null : null;
|
||||
}
|
||||
|
||||
if (!sess) {
|
||||
return null;
|
||||
}
|
||||
for (const perm of permissions || []) {
|
||||
if ((sess.value & perm) == 0) {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
return sess;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user