From 0c3a3546e6a631db9dfbf50f32f42c0306f37a2d Mon Sep 17 00:00:00 2001 From: Yanis Rigaudeau Date: Sun, 16 Oct 2022 15:28:42 +0200 Subject: [PATCH] user role + module alias --- README.md | 8 +++++++- api/api.service | 2 +- api/package-lock.json | 24 ++++++++++++++++++++++++ api/package.json | 4 +++- api/src/app.ts | 1 + api/src/entities/user.ts | 7 ++++++- api/src/paths.ts | 5 +++++ api/tsconfig.json | 10 +++------- core/src/user.ts | 19 +++++++++++++------ core/tsconfig.json | 3 +-- scripts/build.py | 2 +- scripts/install.py | 2 +- scripts/prettier.py | 2 +- www/tsconfig.json | 10 +++------- 14 files changed, 70 insertions(+), 29 deletions(-) create mode 100644 api/src/paths.ts diff --git a/README.md b/README.md index 027803f..0fd6b13 100644 --- a/README.md +++ b/README.md @@ -1,19 +1,25 @@ # RA:TE (Réalité Augmenté: Truc en) +Version Live : https://html5.yayeet.cf + ## Installation des dépendances +Dans ./ + ```bash -python install.py +python ./scripts/install.py ``` ## Pour lancer le projet Dans ./api + ```bash npm run dev ``` Dans ./www + ```bash npm run dev ``` diff --git a/api/api.service b/api/api.service index a8f853c..4940d61 100644 --- a/api/api.service +++ b/api/api.service @@ -11,7 +11,7 @@ Group=node Environment="CONFIGFILE=/home/node/config/config.json" WorkingDirectory=/home/node/app -ExecStart=/usr/bin/node /home/node/app/dist/app.js +ExecStart=/usr/bin/node /home/node/app/dist/api/src/app.js [Install] WantedBy=multi-user.target diff --git a/api/package-lock.json b/api/package-lock.json index edbb758..13c2122 100644 --- a/api/package-lock.json +++ b/api/package-lock.json @@ -13,6 +13,7 @@ "express-session": "^1.17.3", "express-validator": "^6.14.2", "ip": "^1.1.8", + "module-alias": "^2.2.2", "mongodb": "^4.10.0", "uuid": "^9.0.0" }, @@ -21,6 +22,7 @@ "@types/express": "^4.17.14", "@types/express-session": "^1.17.5", "@types/ip": "^1.1.0", + "@types/module-alias": "^2.0.1", "@types/node": "^18.7.18", "prettier": "^2.7.1", "ts-node": "^10.9.1", @@ -161,6 +163,12 @@ "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==", "dev": true }, + "node_modules/@types/module-alias": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@types/module-alias/-/module-alias-2.0.1.tgz", + "integrity": "sha512-DN/CCT1HQG6HquBNJdLkvV+4v5l/oEuwOHUPLxI+Eub0NED+lk0YUfba04WGH90EINiUrNgClkNnwGmbICeWMQ==", + "dev": true + }, "node_modules/@types/node": { "version": "18.7.18", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.18.tgz", @@ -1011,6 +1019,11 @@ "node": ">=10" } }, + "node_modules/module-alias": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/module-alias/-/module-alias-2.2.2.tgz", + "integrity": "sha512-A/78XjoX2EmNvppVWEhM2oGk3x4lLxnkEA4jTbaK97QKSDjkIoOsKQlfylt/d3kKKi596Qy3NP5XrXJ6fZIC9Q==" + }, "node_modules/mongodb": { "version": "4.10.0", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-4.10.0.tgz", @@ -1836,6 +1849,12 @@ "integrity": "sha512-Y4XFY5VJAuw0FgAqPNd6NNoV44jbq9Bz2L7Rh/J6jLTiHBSBJa9fxqQIvkIld4GsoDOcCbvzOUAbLPsSKKg+uA==", "dev": true }, + "@types/module-alias": { + "version": "2.0.1", + "resolved": "https://registry.npmjs.org/@types/module-alias/-/module-alias-2.0.1.tgz", + "integrity": "sha512-DN/CCT1HQG6HquBNJdLkvV+4v5l/oEuwOHUPLxI+Eub0NED+lk0YUfba04WGH90EINiUrNgClkNnwGmbICeWMQ==", + "dev": true + }, "@types/node": { "version": "18.7.18", "resolved": "https://registry.npmjs.org/@types/node/-/node-18.7.18.tgz", @@ -2467,6 +2486,11 @@ "integrity": "sha512-vVqVZQyf3WLx2Shd0qJ9xuvqgAyKPLAiqITEtqW0oIUjzo3PePDd6fW9iFz30ef7Ysp/oiWqbhszeGWW2T6Gzw==", "dev": true }, + "module-alias": { + "version": "2.2.2", + "resolved": "https://registry.npmjs.org/module-alias/-/module-alias-2.2.2.tgz", + "integrity": "sha512-A/78XjoX2EmNvppVWEhM2oGk3x4lLxnkEA4jTbaK97QKSDjkIoOsKQlfylt/d3kKKi596Qy3NP5XrXJ6fZIC9Q==" + }, "mongodb": { "version": "4.10.0", "resolved": "https://registry.npmjs.org/mongodb/-/mongodb-4.10.0.tgz", diff --git a/api/package.json b/api/package.json index 3773981..d4eba30 100644 --- a/api/package.json +++ b/api/package.json @@ -7,7 +7,7 @@ "scripts": { "build": "tsc", "dev": "CONFIGFILE=config/config.json ts-node-dev --respawn --transpile-only ./src/app.ts", - "start": "npm run build && CONFIGFILE=config/config.json node ./dist/app.js", + "start": "npm run build && CONFIGFILE=config/config.json node ./dist/api/src/app.js", "prettier": "prettier -w ./src" }, "devDependencies": { @@ -15,6 +15,7 @@ "@types/express": "^4.17.14", "@types/express-session": "^1.17.5", "@types/ip": "^1.1.0", + "@types/module-alias": "^2.0.1", "@types/node": "^18.7.18", "prettier": "^2.7.1", "ts-node": "^10.9.1", @@ -27,6 +28,7 @@ "express-session": "^1.17.3", "express-validator": "^6.14.2", "ip": "^1.1.8", + "module-alias": "^2.2.2", "mongodb": "^4.10.0", "uuid": "^9.0.0" } diff --git a/api/src/app.ts b/api/src/app.ts index b260673..7907bb0 100644 --- a/api/src/app.ts +++ b/api/src/app.ts @@ -1,3 +1,4 @@ +import './paths'; import Server from './framework/express/server'; import ip from 'ip'; import UserModel from './framework/mongo/user'; diff --git a/api/src/entities/user.ts b/api/src/entities/user.ts index d07fed1..22ea6b8 100644 --- a/api/src/entities/user.ts +++ b/api/src/entities/user.ts @@ -2,6 +2,7 @@ import { UserCtor, UserInfo, UserInfoWithPassword, + UserRoles, UserWithPasswordCtor, } from '@core'; import { generateHash } from '../functions/password'; @@ -9,17 +10,20 @@ import { Entity } from './entity'; export class User extends Entity implements UserInfo { username: string; + role: keyof typeof UserRoles; constructor(raw: UserCtor) { super(raw); - this.username = raw.username ? raw.username : ''; + this.username = raw.username || ''; + this.role = raw.role || UserRoles.USER; } Info(): UserInfo { return { uuid: this.uuid, username: this.username, + role: this.role, }; } } @@ -37,6 +41,7 @@ export class UserWithPassword extends User implements UserInfoWithPassword { return { uuid: this.uuid, username: this.username, + role: this.role, password: this.password, }; } diff --git a/api/src/paths.ts b/api/src/paths.ts new file mode 100644 index 0000000..c976aaf --- /dev/null +++ b/api/src/paths.ts @@ -0,0 +1,5 @@ +import { addAliases } from 'module-alias'; + +addAliases({ + '@core': `${__dirname}/../../core/src`, +}); diff --git a/api/tsconfig.json b/api/tsconfig.json index d5029f7..bb2c40c 100644 --- a/api/tsconfig.json +++ b/api/tsconfig.json @@ -7,16 +7,12 @@ "outDir": "dist", "sourceMap": false, "strictNullChecks": true, + "baseUrl": "./src", "paths": { - "@core": ["../core/src"] + "@core": ["../../core/src"] } }, "lib": ["es2015"], "include": ["src/**/*"], - "exclude": ["node_modules/*", "dist/*"], - "references": [ - { - "path": "../core" - } - ] + "exclude": ["node_modules/*", "dist/*"] } diff --git a/core/src/user.ts b/core/src/user.ts index f3c27e8..77e326b 100644 --- a/core/src/user.ts +++ b/core/src/user.ts @@ -1,28 +1,35 @@ import { EntityInfo } from './entity'; +export enum UserRoles { + ADMIN = 'ADMIN', + USER = 'USER', +} + export type UserInfo = { uuid: string; username: string; + role: keyof typeof UserRoles; }; +export type User = UserInfo & EntityInfo; + +export type UserCtor = Partial; + export type UserInfoWithPassword = { password: string; } & UserInfo; -export type User = UserInfo & EntityInfo; - export type UserWithPassword = UserInfoWithPassword & EntityInfo; +export type UserWithPasswordCtor = Partial; + export type CreateUserBody = { username: string; password: string; + role: keyof typeof UserRoles; }; export type LoginUserBody = { username: string; password: string; }; - -export type UserCtor = Partial; - -export type UserWithPasswordCtor = Partial; diff --git a/core/tsconfig.json b/core/tsconfig.json index bdc7461..0f537d0 100644 --- a/core/tsconfig.json +++ b/core/tsconfig.json @@ -8,6 +8,5 @@ "outDir": "dist" }, "include": ["src/**/*"], - "exclude": ["node_modules/*", "dist/*"], - "references": [] + "exclude": ["node_modules/*", "dist/*"] } diff --git a/scripts/build.py b/scripts/build.py index 1349ae1..b7d78b1 100755 --- a/scripts/build.py +++ b/scripts/build.py @@ -2,7 +2,7 @@ import os import json -apps = json.load(open('apps.json'))['apps'] +apps: list[str] = json.load(open('apps.json'))['apps'] print(len(apps), 'apps:', apps) diff --git a/scripts/install.py b/scripts/install.py index fc08757..eed8f3c 100755 --- a/scripts/install.py +++ b/scripts/install.py @@ -2,7 +2,7 @@ import os import json -apps = json.load(open('apps.json'))['apps'] +apps: list[str] = json.load(open('apps.json'))['apps'] print(len(apps), 'apps:', apps) diff --git a/scripts/prettier.py b/scripts/prettier.py index 1a32d9e..7356397 100755 --- a/scripts/prettier.py +++ b/scripts/prettier.py @@ -2,7 +2,7 @@ import os import json -apps = json.load(open('apps.json'))['apps'] +apps: list[str] = json.load(open('apps.json'))['apps'] print(len(apps), 'apps:', apps) diff --git a/www/tsconfig.json b/www/tsconfig.json index d8473f3..e41e4ed 100644 --- a/www/tsconfig.json +++ b/www/tsconfig.json @@ -3,15 +3,11 @@ "compilerOptions": { "sourceMap": false, "strictNullChecks": true, + "baseUrl": "./src", "paths": { - "@core": ["../core/src"] + "@core": ["../../core/src"] } }, "include": ["src/**/*"], - "exclude": ["node_modules/*", "public/*"], - "references": [ - { - "path": "../core" - } - ] + "exclude": ["node_modules/*", "public/*"] }