diff --git a/backend/config/Config.ts b/backend/config/Config.ts index 19947741..a4c717c5 100644 --- a/backend/config/Config.ts +++ b/backend/config/Config.ts @@ -8,7 +8,16 @@ Config.Server = { port: 80, imagesFolder: "demo/images", thumbnailFolder: "demo/TEMP", - databaseType: DatabaseType.memory + database: { + type: DatabaseType.mysql, + mysql: { + host: "localhost", + username: "root", + password: "root", + database: "pigallery2" + + } + } }; ConfigLoader.init(Config, path.join(__dirname, './../../config.json'), [["PORT", "Server-port"]]); diff --git a/backend/middlewares/user/AuthenticationMWs.ts b/backend/middlewares/user/AuthenticationMWs.ts index 4229ae58..e96429fc 100644 --- a/backend/middlewares/user/AuthenticationMWs.ts +++ b/backend/middlewares/user/AuthenticationMWs.ts @@ -1,26 +1,26 @@ /// import {NextFunction, Request, Response} from "express"; import {Error, ErrorCodes} from "../../../common/entities/Error"; -import {UserRoles, User} from "../../../common/entities/User"; +import {UserRoles, UserDTO} from "../../../common/entities/UserDTO"; import {ObjectManagerRepository} from "../../model/ObjectManagerRepository"; import {Config} from "../../config/Config"; export class AuthenticationMWs { - public static authenticate(req:Request, res:Response, next:NextFunction) { + public static authenticate(req: Request, res: Response, next: NextFunction) { if (Config.Client.authenticationRequired === false) { - req.session.user = new User("", "", UserRoles.Admin); + req.session.user = {name: "", role: UserRoles.Admin}; return next(); } if (typeof req.session.user === 'undefined') { return next(new Error(ErrorCodes.NOT_AUTHENTICATED)); - } + } return next(); } - public static authorise(role:UserRoles) { - return (req:Request, res:Response, next:NextFunction) => { + public static authorise(role: UserRoles) { + return (req: Request, res: Response, next: NextFunction) => { if (req.session.user.role < role) { return next(new Error(ErrorCodes.NOT_AUTHORISED)); } @@ -28,14 +28,14 @@ export class AuthenticationMWs { }; } - public static inverseAuthenticate(req:Request, res:Response, next:NextFunction) { + public static inverseAuthenticate(req: Request, res: Response, next: NextFunction) { if (typeof req.session.user !== 'undefined') { return next(new Error(ErrorCodes.ALREADY_AUTHENTICATED)); } return next(); } - public static login(req:Request, res:Response, next:NextFunction) { + public static login(req: Request, res: Response, next: NextFunction) { //not enough parameter if ((typeof req.body === 'undefined') || (typeof req.body.loginCredential === 'undefined') || (typeof req.body.loginCredential.username === 'undefined') || (typeof req.body.loginCredential.password === 'undefined')) { @@ -48,6 +48,7 @@ export class AuthenticationMWs { password: req.body.loginCredential.password }, (err, result) => { if ((err) || (!result)) { + console.error(err); return next(new Error(ErrorCodes.CREDENTIAL_NOT_FOUND)); } @@ -58,7 +59,7 @@ export class AuthenticationMWs { }); } - public static logout(req:Request, res:Response, next:NextFunction) { + public static logout(req: Request, res: Response, next: NextFunction) { delete req.session.user; return next(); } diff --git a/backend/middlewares/user/UserMWs.ts b/backend/middlewares/user/UserMWs.ts index 0047351a..e833c9d7 100644 --- a/backend/middlewares/user/UserMWs.ts +++ b/backend/middlewares/user/UserMWs.ts @@ -1,7 +1,7 @@ import {NextFunction, Request, Response} from "express"; import {Error, ErrorCodes} from "../../../common/entities/Error"; import {ObjectManagerRepository} from "../../model/ObjectManagerRepository"; -import {User} from "../../../common/entities/User"; +import {UserDTO} from "../../../common/entities/UserDTO"; import {Config} from "../../config/Config"; import {Utils} from "../../../common/Utils"; @@ -88,7 +88,7 @@ export class UserMWs { if (Config.Client.authenticationRequired === false) { return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED)); } - ObjectManagerRepository.getInstance().getUserManager().find({}, (err, result:Array) => { + ObjectManagerRepository.getInstance().getUserManager().find({}, (err, result: Array) => { if ((err) || (!result)) { return next(new Error(ErrorCodes.GENERAL_ERROR)); } diff --git a/backend/middlewares/user/UserRequestConstrainsMWs.ts b/backend/middlewares/user/UserRequestConstrainsMWs.ts index a6315ce1..b20ae716 100644 --- a/backend/middlewares/user/UserRequestConstrainsMWs.ts +++ b/backend/middlewares/user/UserRequestConstrainsMWs.ts @@ -1,6 +1,6 @@ import {NextFunction, Request, Response} from "express"; import {Error, ErrorCodes} from "../../../common/entities/Error"; -import {UserRoles} from "../../../common/entities/User"; +import {UserRoles} from "../../../common/entities/UserDTO"; import {ObjectManagerRepository} from "../../model/ObjectManagerRepository"; export class UserRequestConstrainsMWs { diff --git a/backend/model/IUserManager.ts b/backend/model/IUserManager.ts deleted file mode 100644 index 4433387d..00000000 --- a/backend/model/IUserManager.ts +++ /dev/null @@ -1,9 +0,0 @@ -import {User, UserRoles} from "../../common/entities/User"; -export interface IUserManager { - findOne(filter: any, cb: (error: any, result: User) => void): void; - find(filter: any, cb: (error: any, result: Array) => void): void; - createUser(user: User, cb: (error: any, result: User) => void): void; - deleteUser(id: number, cb: (error: any, result: string) => void): void; - changeRole(id: number, newRole: UserRoles, cb: (error: any) => void): void; - changePassword(request: any, cb: (error: any, result: string) => void): void; -} \ No newline at end of file diff --git a/backend/model/ObjectManagerRepository.ts b/backend/model/ObjectManagerRepository.ts index 734a6a17..b2fba7f6 100644 --- a/backend/model/ObjectManagerRepository.ts +++ b/backend/model/ObjectManagerRepository.ts @@ -1,24 +1,39 @@ -import {IUserManager} from "./IUserManager"; -import {IGalleryManager} from "./IGalleryManager"; -import {ISearchManager} from "./ISearchManager"; +import {IUserManager} from "./interfaces/IUserManager"; +import {IGalleryManager} from "./interfaces/IGalleryManager"; +import {ISearchManager} from "./interfaces/ISearchManager"; +import {MySQLConnection} from "./mysql/MySQLConnection"; export class ObjectManagerRepository { - private _galleryManager:IGalleryManager; - private _userManager:IUserManager; - private _searchManager:ISearchManager; - private static _instance:ObjectManagerRepository = null; + private _galleryManager: IGalleryManager; + private _userManager: IUserManager; + private _searchManager: ISearchManager; + private static _instance: ObjectManagerRepository = null; - public static MemoryMongoManagers() { - let GalleryManager = require("./memory/GalleryManager").GalleryManager; - let UserManager = require("./memory/UserManager").UserManager; - let SearchManager = require("./memory/SearchManager").SearchManager; + public static InitMemoryManagers() { + const GalleryManager = require("./memory/GalleryManager").GalleryManager; + const UserManager = require("./memory/UserManager").UserManager; + const SearchManager = require("./memory/SearchManager").SearchManager; ObjectManagerRepository.getInstance().setGalleryManager(new GalleryManager()); ObjectManagerRepository.getInstance().setUserManager(new UserManager()); ObjectManagerRepository.getInstance().setSearchManager(new SearchManager()); } + public static InitMySQLManagers(): Promise { + return new Promise((resolve, reject) => { + MySQLConnection.init().then(() => { + const GalleryManager = require("./memory/GalleryManager").GalleryManager; + const UserManager = require("./mysql/UserManager").UserManager; + const SearchManager = require("./memory/SearchManager").SearchManager; + ObjectManagerRepository.getInstance().setGalleryManager(new GalleryManager()); + ObjectManagerRepository.getInstance().setUserManager(new UserManager()); + ObjectManagerRepository.getInstance().setSearchManager(new SearchManager()); + resolve(true); + }).catch(err => reject(err)); + }); + } + public static getInstance() { if (this._instance === null) { this._instance = new ObjectManagerRepository(); @@ -31,27 +46,27 @@ export class ObjectManagerRepository { } - getGalleryManager():IGalleryManager { + getGalleryManager(): IGalleryManager { return this._galleryManager; } - setGalleryManager(value:IGalleryManager) { + setGalleryManager(value: IGalleryManager) { this._galleryManager = value; } - getUserManager():IUserManager { + getUserManager(): IUserManager { return this._userManager; } - setUserManager(value:IUserManager) { + setUserManager(value: IUserManager) { this._userManager = value; } - getSearchManager():ISearchManager { + getSearchManager(): ISearchManager { return this._searchManager; } - setSearchManager(value:ISearchManager) { + setSearchManager(value: ISearchManager) { this._searchManager = value; } diff --git a/backend/model/IGalleryManager.ts b/backend/model/interfaces/IGalleryManager.ts similarity index 68% rename from backend/model/IGalleryManager.ts rename to backend/model/interfaces/IGalleryManager.ts index c3722adb..22a26d58 100644 --- a/backend/model/IGalleryManager.ts +++ b/backend/model/interfaces/IGalleryManager.ts @@ -1,4 +1,4 @@ -import {Directory} from "../../common/entities/Directory"; +import {Directory} from "../../../common/entities/Directory"; export interface IGalleryManager { listDirectory(relativeDirectoryName: string, cb: (error: any, result: Directory) => void): void; } \ No newline at end of file diff --git a/backend/model/ISearchManager.ts b/backend/model/interfaces/ISearchManager.ts similarity index 67% rename from backend/model/ISearchManager.ts rename to backend/model/interfaces/ISearchManager.ts index 1512ae14..2c0443e2 100644 --- a/backend/model/ISearchManager.ts +++ b/backend/model/interfaces/ISearchManager.ts @@ -1,5 +1,5 @@ -import {AutoCompleteItem, SearchTypes} from "../../common/entities/AutoCompleteItem"; -import {SearchResult} from "../../common/entities/SearchResult"; +import {AutoCompleteItem, SearchTypes} from "../../../common/entities/AutoCompleteItem"; +import {SearchResult} from "../../../common/entities/SearchResult"; export interface ISearchManager { autocomplete(text: string, cb: (error: any, result: Array) => void): void; search(text: string, searchType: SearchTypes, cb: (error: any, result: SearchResult) => void): void; diff --git a/backend/model/interfaces/IUserManager.ts b/backend/model/interfaces/IUserManager.ts new file mode 100644 index 00000000..005b1121 --- /dev/null +++ b/backend/model/interfaces/IUserManager.ts @@ -0,0 +1,9 @@ +import {UserDTO, UserRoles} from "../../../common/entities/UserDTO"; +export interface IUserManager { + findOne(filter: any, cb: (error: any, result: UserDTO) => void): void; + find(filter: any, cb: (error: any, result: Array) => void): void; + createUser(user: UserDTO, cb: (error: any, result: UserDTO) => void): void; + deleteUser(id: number, cb: (error: any, result: string) => void): void; + changeRole(id: number, newRole: UserRoles, cb: (error: any) => void): void; + changePassword(request: any, cb: (error: any, result: string) => void): void; +} \ No newline at end of file diff --git a/backend/model/memory/GalleryManager.ts b/backend/model/memory/GalleryManager.ts index 43823d09..dfccd8cd 100644 --- a/backend/model/memory/GalleryManager.ts +++ b/backend/model/memory/GalleryManager.ts @@ -1,5 +1,5 @@ import {Directory} from "../../../common/entities/Directory"; -import {IGalleryManager} from "../IGalleryManager"; +import {IGalleryManager} from "../interfaces/IGalleryManager"; import {DiskManager} from "../DiskManger"; export class GalleryManager implements IGalleryManager { diff --git a/backend/model/memory/SearchManager.ts b/backend/model/memory/SearchManager.ts index 315f39d4..0b8864f1 100644 --- a/backend/model/memory/SearchManager.ts +++ b/backend/model/memory/SearchManager.ts @@ -1,5 +1,5 @@ import {AutoCompleteItem, SearchTypes} from "../../../common/entities/AutoCompleteItem"; -import {ISearchManager} from "../ISearchManager"; +import {ISearchManager} from "../interfaces/ISearchManager"; import {SearchResult} from "../../../common/entities/SearchResult"; export class SearchManager implements ISearchManager { diff --git a/backend/model/memory/UserManager.ts b/backend/model/memory/UserManager.ts index 1187b0fa..56d89fe4 100644 --- a/backend/model/memory/UserManager.ts +++ b/backend/model/memory/UserManager.ts @@ -1,6 +1,6 @@ /// -import {User, UserRoles} from "../../../common/entities/User"; -import {IUserManager} from "../IUserManager"; +import {UserDTO, UserRoles} from "../../../common/entities/UserDTO"; +import {IUserManager} from "../interfaces/IUserManager"; import {ProjectPath} from "../../ProjectPath"; import {Utils} from "../../../common/Utils"; import * as flatfile from "flat-file-db"; @@ -31,37 +31,37 @@ export class UserManager implements IUserManager { if (!this.db.has("users")) { this.db.put("users", []); //TODO: remove defaults - this.createUser(new User("developer", "developer", UserRoles.Developer)); - this.createUser(new User("admin", "admin", UserRoles.Admin)); - this.createUser(new User("user", "user", UserRoles.User)); - this.createUser(new User("guest", "guest", UserRoles.Guest)); + this.createUser({name: "developer", password: "developer", role: UserRoles.Developer}); + this.createUser({name: "admin", password: "admin", role: UserRoles.Admin}); + this.createUser({name: "user", password: "user", role: UserRoles.User}); + this.createUser({name: "guest", password: "guest", role: UserRoles.Guest}); } } - public findOne(filter: any, cb: (error: any, result: User) => void) { - this.find(filter, (error, result: Array) => { + public findOne(filter: any, cb: (error: any, result: UserDTO) => void) { + this.find(filter, (error, result: Array) => { if (error) { return cb(error, null); } if (result.length == 0) { - return cb("User not found", null); + return cb("UserDTO not found", null); } return cb(null, result[0]); }); } - public find(filter: any, cb: (error: any, result: Array) => void) { + public find(filter: any, cb: (error: any, result: Array) => void) { - let users = this.db.get("users").filter((u: User) => Utils.equalsFilter(u, filter)); + let users = this.db.get("users").filter((u: UserDTO) => Utils.equalsFilter(u, filter)); return cb(null, users); } - public createUser(user: User, cb: (error: any, result: User) => void = (e, r) => { + public createUser(user: UserDTO, cb: (error: any, result: UserDTO) => void = (e, r) => { }) { user.id = parseInt(this.db.get("idCounter")) + 1; this.db.put("idCounter", user.id); @@ -73,14 +73,14 @@ export class UserManager implements IUserManager { } public deleteUser(id: number, cb: (error: any) => void) { - let users = this.db.get("users").filter((u: User) => u.id != id); + let users = this.db.get("users").filter((u: UserDTO) => u.id != id); this.db.put("users", users); return cb(null); } public changeRole(id: number, newRole: UserRoles, cb: (error: any, result: string) => void) { - let users: Array = this.db.get("users"); + let users: Array = this.db.get("users"); for (let i = 0; i < users.length; i++) { if (users[i].id == id) { diff --git a/backend/model/mysql/MySQLConnection.ts b/backend/model/mysql/MySQLConnection.ts new file mode 100644 index 00000000..c4248fa8 --- /dev/null +++ b/backend/model/mysql/MySQLConnection.ts @@ -0,0 +1,65 @@ +import "reflect-metadata"; +import {createConnection, Connection} from "typeorm"; +import {Config} from "../../config/Config"; +import {UserEntity} from "./enitites/UserEntity"; +import {UserRoles} from "../../../common/entities/UserDTO"; + + +export class MySQLConnection { + + constructor() { + + + } + + private static connection: Connection = null; + + public static getConnection(): Promise { + return new Promise((resolve, reject) => { + + if (this.connection != null) { + return resolve(this.connection); + } + + createConnection({ + driver: { + type: "mysql", + host: Config.Server.database.mysql.host, + port: 3306, + username: Config.Server.database.mysql.username, + password: Config.Server.database.mysql.password, + database: Config.Server.database.mysql.database + }, + entities: [ + UserEntity + ], + autoSchemaSync: true, + }).then((conn) => { + this.connection = conn; + return resolve(this.connection); + + }).catch(err => reject(err)); + }); + } + + public static init(): Promise { + return new Promise((resolve, reject) => { + this.getConnection().then(async connection => { + + let userRepository = connection.getRepository(UserEntity); + let admins = await userRepository.find({role: UserRoles.Admin}); + if (admins.length == 0) { + let a = new UserEntity(); + a.name = "admin"; + a.password = "admin"; + a.role = UserRoles.Admin; + await userRepository.persist(a); + } + + resolve(); + }).catch(err => reject(err)); + }); + } + + +} \ No newline at end of file diff --git a/backend/model/mysql/UserManager.ts b/backend/model/mysql/UserManager.ts new file mode 100644 index 00000000..095d3fa9 --- /dev/null +++ b/backend/model/mysql/UserManager.ts @@ -0,0 +1,83 @@ +import {UserDTO, UserRoles} from "../../../common/entities/UserDTO"; +import {IUserManager} from "../interfaces/IUserManager"; +import {UserEntity} from "./enitites/UserEntity"; +import {MySQLConnection} from "./MySQLConnection"; + +export class UserManager implements IUserManager { + + constructor() { + } + + + public findOne(filter: any, cb: (error: any, result: UserDTO) => void) { + MySQLConnection.getConnection().then(async connection => { + + let userRepository = connection.getRepository(UserEntity); + return cb(null, await userRepository.findOne(filter)); + + }).catch((error) => { + return cb(error, null); + }); + + } + + public find(filter: any, cb: (error: any, result: Array) => void) { + MySQLConnection.getConnection().then(async connection => { + + let userRepository = connection.getRepository(UserEntity); + return cb(null, await userRepository.find(filter)); + + }).catch((error) => { + return cb(error, null); + }); + } + + public createUser(user: UserDTO, cb: (error: any, result: UserDTO) => void = (e, r) => { + }) { + MySQLConnection.getConnection().then(connection => { + + let userRepository = connection.getRepository(UserEntity); + userRepository.persist(user).then(u => cb(null, u)).catch(err => cb(err, null)); + + }).catch((error) => { + return cb(error, null); + }); + } + + public deleteUser(id: number, cb: (error: any) => void) { + MySQLConnection.getConnection().then(connection => { + + + let userRepository = connection.getRepository(UserEntity); + userRepository.findOne({id: id}).then((user) => { + userRepository.remove(user).catch(err => cb(err)); + }).catch(err => cb(err)); + + + }).catch((error) => { + return cb(error); + }); + } + + public changeRole(id: number, newRole: UserRoles, cb: (error: any, result: string) => void) { + + MySQLConnection.getConnection().then(async connection => { + + + let userRepository = connection.getRepository(UserEntity); + let user = await userRepository.findOne({id: id}); + user.role = newRole; + await userRepository.persist(user); + return cb(null, "ok"); + + + }).catch((error) => { + return cb(error, null); + }); + } + + public changePassword(request: any, cb: (error: any, result: string) => void) { + throw new Error("not implemented"); //TODO: implement + } + +} \ No newline at end of file diff --git a/backend/model/mysql/enitites/UserEntity.ts b/backend/model/mysql/enitites/UserEntity.ts new file mode 100644 index 00000000..f30a1264 --- /dev/null +++ b/backend/model/mysql/enitites/UserEntity.ts @@ -0,0 +1,23 @@ +import {UserDTO, UserRoles} from "../../../../common/entities/UserDTO"; +import {Table, Column, PrimaryGeneratedColumn} from "typeorm"; + +@Table() +export class UserEntity implements UserDTO { + + @PrimaryGeneratedColumn() + id: number; + + @Column({ + length: 500 + }) + name: string; + + @Column({ + length: 500 + }) + password: string; + + @Column("int") + role: UserRoles; + +} \ No newline at end of file diff --git a/backend/routes/AdminRouter.ts b/backend/routes/AdminRouter.ts index 03efbccf..8e213035 100644 --- a/backend/routes/AdminRouter.ts +++ b/backend/routes/AdminRouter.ts @@ -1,5 +1,5 @@ import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs"; -import {UserRoles} from "../../common/entities/User"; +import {UserRoles} from "../../common/entities/UserDTO"; export class AdminRouter { constructor(private app: any) { diff --git a/backend/routes/SharingRouter.ts b/backend/routes/SharingRouter.ts index f28490e5..a3a35792 100644 --- a/backend/routes/SharingRouter.ts +++ b/backend/routes/SharingRouter.ts @@ -1,5 +1,5 @@ import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs"; -import {UserRoles} from "../../common/entities/User"; +import {UserRoles} from "../../common/entities/UserDTO"; export class SharingRouter { constructor(private app: any) { diff --git a/backend/routes/UserRouter.ts b/backend/routes/UserRouter.ts index eec7ec7e..69f263ce 100644 --- a/backend/routes/UserRouter.ts +++ b/backend/routes/UserRouter.ts @@ -1,5 +1,5 @@ import {UserMWs} from "../middlewares/user/UserMWs"; -import {UserRoles} from "../../common/entities/User"; +import {UserRoles} from "../../common/entities/UserDTO"; import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs"; import {UserRequestConstrainsMWs} from "../middlewares/user/UserRequestConstrainsMWs"; import {RenderingMWs} from "../middlewares/RenderingMWs"; diff --git a/backend/server.ts b/backend/server.ts index 4c4e98f7..52fd3773 100644 --- a/backend/server.ts +++ b/backend/server.ts @@ -54,11 +54,11 @@ export class Server { this.app.use(_bodyParser.json()); - if (Config.Server.databaseType === DatabaseType.memory) { - ObjectManagerRepository.MemoryMongoManagers(); - } else { - throw new Error("not implemented alternative mangers"); - } + ObjectManagerRepository.InitMySQLManagers().catch(() => { + console.error("Erro during initailizing mysql falling back to memory DB"); + Config.setDatabaseType(DatabaseType.memory); + ObjectManagerRepository.InitMemoryManagers(); + }); new PublicRouter(this.app); diff --git a/backend/tsconfig.json b/backend/tsconfig.json deleted file mode 100644 index 30660b3f..00000000 --- a/backend/tsconfig.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "compilerOptions": { - "target": "es5", - "module": "commonjs", - "moduleResolution": "node", - "sourceMap": true, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "lib": [ - "es2015", - "dom" - ], - "suppressImplicitAnyIndexErrors": true - } -} diff --git a/common/common-classes.d.ts b/common/common-classes.d.ts deleted file mode 100644 index 385eb636..00000000 --- a/common/common-classes.d.ts +++ /dev/null @@ -1,12 +0,0 @@ -/// -/// -/// - -/// - -/// -/// -/// -/// -/// -/// diff --git a/common/config/Config.ts b/common/config/Config.ts index a239efe3..fbbcd2ab 100644 --- a/common/config/Config.ts +++ b/common/config/Config.ts @@ -1,12 +1,23 @@ export enum DatabaseType{ - memory + memory = 0, mysql = 1 +} + +interface MySQLConfig { + host: string; + database: string; + username: string; + password: string; +} +interface DataBaseConfig { + type: DatabaseType; + mysql?: MySQLConfig; } interface ServerConfig { port:number; imagesFolder:string; thumbnailFolder:string; - databaseType:DatabaseType; + database: DataBaseConfig; } interface SearchConfig { @@ -43,7 +54,7 @@ export class ConfigClass { }; public setDatabaseType(type:DatabaseType) { - this.Server.databaseType = type; + this.Server.database.type = type; if (type === DatabaseType.memory) { this.Client.Search.searchEnabled = false; this.Client.Search.instantSearchEnabled = false; diff --git a/common/entities/User.ts b/common/entities/User.ts deleted file mode 100644 index 8291ad40..00000000 --- a/common/entities/User.ts +++ /dev/null @@ -1,14 +0,0 @@ -export enum UserRoles{ - Guest = 1, - User = 2, - Admin = 3, - Developer = 4, - -} - -export class User { - public id:number; - - constructor(public name?:string, public password?:string, public role:UserRoles = UserRoles.User) { - } -} \ No newline at end of file diff --git a/common/entities/UserDTO.ts b/common/entities/UserDTO.ts new file mode 100644 index 00000000..f886b3cd --- /dev/null +++ b/common/entities/UserDTO.ts @@ -0,0 +1,14 @@ +export enum UserRoles{ + Guest = 1, + User = 2, + Admin = 3, + Developer = 4, + +} + +export interface UserDTO { + id: number; + name: string; + password: string; + role: UserRoles; +} \ No newline at end of file diff --git a/common/tsconfig.json b/common/tsconfig.json deleted file mode 100644 index 30660b3f..00000000 --- a/common/tsconfig.json +++ /dev/null @@ -1,15 +0,0 @@ -{ - "compilerOptions": { - "target": "es5", - "module": "commonjs", - "moduleResolution": "node", - "sourceMap": true, - "emitDecoratorMetadata": true, - "experimentalDecorators": true, - "lib": [ - "es2015", - "dom" - ], - "suppressImplicitAnyIndexErrors": true - } -} diff --git a/frontend/app/admin/admin.component.ts b/frontend/app/admin/admin.component.ts index 170f7251..44be25ce 100644 --- a/frontend/app/admin/admin.component.ts +++ b/frontend/app/admin/admin.component.ts @@ -1,7 +1,7 @@ import {Component, OnInit} from "@angular/core"; import {AuthenticationService} from "../model/network/authentication.service"; import {Router} from "@angular/router"; -import {UserRoles} from "../../../common/entities/User"; +import {UserRoles} from "../../../common/entities/UserDTO"; import {Config} from "../config/Config"; @Component({ selector: 'admin', @@ -17,7 +17,7 @@ export class AdminComponent implements OnInit { ngOnInit() { if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) { - this._router.navigate(['Login']); + this._router.navigate(['login']); return; } } diff --git a/frontend/app/app.component.ts b/frontend/app/app.component.ts index 8e36fe8a..cbdb2ce5 100644 --- a/frontend/app/app.component.ts +++ b/frontend/app/app.component.ts @@ -1,6 +1,6 @@ import {Component, OnInit} from "@angular/core"; import {AuthenticationService} from "./model/network/authentication.service"; -import {User} from "../../common/entities/User"; +import {UserDTO} from "../../common/entities/UserDTO"; import {Router} from "@angular/router"; @Component({ @@ -14,7 +14,7 @@ export class AppComponent implements OnInit { } ngOnInit() { - this._authenticationService.OnUserChanged.on((user: User) => { + this._authenticationService.OnUserChanged.on((user: UserDTO) => { if (user != null) { if (this._router.isActive('login', true)) { console.log("routing"); diff --git a/frontend/app/frame/frame.component.ts b/frontend/app/frame/frame.component.ts index 7a5fe58e..a1f38d10 100644 --- a/frontend/app/frame/frame.component.ts +++ b/frontend/app/frame/frame.component.ts @@ -1,7 +1,7 @@ import {Component, ViewEncapsulation} from "@angular/core"; import {RouterLink} from "@angular/router"; import {AuthenticationService} from "../model/network/authentication.service"; -import {User} from "../../../common/entities/User"; +import {UserDTO} from "../../../common/entities/UserDTO"; import {Config} from "../config/Config"; @Component({ @@ -12,7 +12,7 @@ import {Config} from "../config/Config"; }) export class FrameComponent { - user:User; + user: UserDTO; authenticationRequired:boolean = false; constructor(private _authService:AuthenticationService) { diff --git a/frontend/app/gallery/grid/photo/loading/loading.photo.grid.gallery.component.ts b/frontend/app/gallery/grid/photo/loading/loading.photo.grid.gallery.component.ts index 47f5246a..01ff0587 100644 --- a/frontend/app/gallery/grid/photo/loading/loading.photo.grid.gallery.component.ts +++ b/frontend/app/gallery/grid/photo/loading/loading.photo.grid.gallery.component.ts @@ -1,5 +1,3 @@ -/// - import {Component, Input} from "@angular/core"; @Component({ diff --git a/frontend/app/gallery/lightbox/photo/photo.lightbox.gallery.component.ts b/frontend/app/gallery/lightbox/photo/photo.lightbox.gallery.component.ts index f50cb183..e84af2fb 100644 --- a/frontend/app/gallery/lightbox/photo/photo.lightbox.gallery.component.ts +++ b/frontend/app/gallery/lightbox/photo/photo.lightbox.gallery.component.ts @@ -1,5 +1,3 @@ -/// - import {Component, OnChanges, Input, ViewChild, ElementRef} from "@angular/core"; import {GridPhoto} from "../../grid/GridPhoto"; diff --git a/frontend/app/gallery/search/autocomplete.service.ts b/frontend/app/gallery/search/autocomplete.service.ts index b2ce3300..2b363fda 100644 --- a/frontend/app/gallery/search/autocomplete.service.ts +++ b/frontend/app/gallery/search/autocomplete.service.ts @@ -1,5 +1,3 @@ -/// - import {Injectable} from "@angular/core"; import {NetworkService} from "../../model/network/network.service"; import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem"; diff --git a/frontend/app/login/login.component.ts b/frontend/app/login/login.component.ts index e9e3382b..30517eee 100644 --- a/frontend/app/login/login.component.ts +++ b/frontend/app/login/login.component.ts @@ -3,7 +3,7 @@ import {LoginCredential} from "../../../common/entities/LoginCredential"; import {AuthenticationService} from "../model/network/authentication.service"; import {Router} from "@angular/router"; import {Message} from "../../../common/entities/Message"; -import {User} from "../../../common/entities/User"; +import {UserDTO} from "../../../common/entities/UserDTO"; import {ErrorCodes} from "../../../common/entities/Error"; @Component({ @@ -27,7 +27,7 @@ export class LoginComponent implements OnInit { onLogin() { this.loginError = null; - this._authService.login(this.loginCredential).then((message: Message) => { + this._authService.login(this.loginCredential).then((message: Message) => { if (message.error) { if (message.error.code === ErrorCodes.CREDENTIAL_NOT_FOUND) { this.loginError = "Wrong username or password"; diff --git a/frontend/app/model/network/autehentication.service.spec.ts b/frontend/app/model/network/autehentication.service.spec.ts index 3eebffe4..b8fb2024 100644 --- a/frontend/app/model/network/autehentication.service.spec.ts +++ b/frontend/app/model/network/autehentication.service.spec.ts @@ -1,6 +1,6 @@ import {inject, TestBed} from "@angular/core/testing"; import {UserService} from "./user.service"; -import {User} from "../../../../common/entities/User"; +import {UserDTO} from "../../../../common/entities/UserDTO"; import {Message} from "../../../../common/entities/Message"; import "rxjs/Rx"; import {LoginCredential} from "../../../../common/entities/LoginCredential"; @@ -8,7 +8,7 @@ import {AuthenticationService} from "./authentication.service"; class MockUserService { public login(credential: LoginCredential) { - return Promise.resolve(new Message(null, new User("testUserName"))) + return Promise.resolve(new Message(null, {name: "testUserName"})) } } @@ -22,7 +22,7 @@ describe('AuthenticationService', () => { }); - it('should call User service login', inject([AuthenticationService, UserService], (authService, userService) => { + it('should call UserDTO service login', inject([AuthenticationService, UserService], (authService, userService) => { spyOn(userService, "login").and.callThrough(); expect(userService.login).not.toHaveBeenCalled(); diff --git a/frontend/app/model/network/authentication.service.ts b/frontend/app/model/network/authentication.service.ts index 66880e00..547291f5 100644 --- a/frontend/app/model/network/authentication.service.ts +++ b/frontend/app/model/network/authentication.service.ts @@ -1,5 +1,5 @@ import {Injectable} from "@angular/core"; -import {User, UserRoles} from "../../../../common/entities/User"; +import {UserDTO, UserRoles} from "../../../../common/entities/UserDTO"; import {Event} from "../../../../common/event/Event"; import {UserService} from "./user.service"; import {LoginCredential} from "../../../../common/entities/LoginCredential"; @@ -9,14 +9,14 @@ import {ErrorCodes} from "../../../../common/entities/Error"; import {Config} from "../../config/Config"; declare module ServerInject { - export let user: User; + export let user: UserDTO; } @Injectable() export class AuthenticationService { - private _user: User = null; - public OnUserChanged: Event; + private _user: UserDTO = null; + public OnUserChanged: Event; constructor(private _userService: UserService) { this.OnUserChanged = new Event(); @@ -34,7 +34,7 @@ export class AuthenticationService { } private getSessionUser() { - this._userService.getSessionUser().then((message: Message) => { + this._userService.getSessionUser().then((message: Message) => { if (message.error) { console.log(message.error); } else { @@ -44,13 +44,13 @@ export class AuthenticationService { }); } - private setUser(user: User) { + private setUser(user: UserDTO) { this._user = user; this.OnUserChanged.trigger(this._user); } public login(credential: LoginCredential) { - return this._userService.login(credential).then((message: Message) => { + return this._userService.login(credential).then((message: Message) => { if (message.error) { console.log(ErrorCodes[message.error.code] + ", message: ", message.error.message); } else { @@ -70,7 +70,7 @@ export class AuthenticationService { public getUser() { if (Config.Client.authenticationRequired === false) { - return new User("", "", UserRoles.Admin); + return {name: "", password: "", role: UserRoles.Admin}; } return this._user; } diff --git a/frontend/app/model/network/user.service.ts b/frontend/app/model/network/user.service.ts index ed03e363..c34b70db 100644 --- a/frontend/app/model/network/user.service.ts +++ b/frontend/app/model/network/user.service.ts @@ -1,7 +1,7 @@ import {Injectable} from "@angular/core"; import {LoginCredential} from "../../../../common/entities/LoginCredential"; import {NetworkService} from "./network.service"; -import {User} from "../../../../common/entities/User"; +import {UserDTO} from "../../../../common/entities/UserDTO"; import {Message} from "../../../../common/entities/Message"; @Injectable() @@ -16,11 +16,11 @@ export class UserService { return this._networkService.postJson("/user/logout"); } - public login(credential: LoginCredential): Promise> { + public login(credential: LoginCredential): Promise> { return this._networkService.postJson("/user/login", {"loginCredential": credential}); } - public getSessionUser(): Promise> { + public getSessionUser(): Promise> { return this._networkService.getJson("/user/login"); } diff --git a/frontend/app/notification.service.ts b/frontend/app/notification.service.ts index 0a6dbc24..6f49003e 100644 --- a/frontend/app/notification.service.ts +++ b/frontend/app/notification.service.ts @@ -1,5 +1,3 @@ -/// - import {Injectable} from "@angular/core"; @Injectable() diff --git a/frontend/app/pipes/StringifyRolePipe.ts b/frontend/app/pipes/StringifyRolePipe.ts index 58e496f3..64b9ffa0 100644 --- a/frontend/app/pipes/StringifyRolePipe.ts +++ b/frontend/app/pipes/StringifyRolePipe.ts @@ -1,5 +1,5 @@ import {Pipe, PipeTransform} from "@angular/core"; -import {UserRoles} from "../../../common/entities/User"; +import {UserRoles} from "../../../common/entities/UserDTO"; @Pipe({name: 'stringifyRole'}) diff --git a/frontend/app/settings/usermanager/usermanager.settings.component.html b/frontend/app/settings/usermanager/usermanager.settings.component.html index 4c8e78d7..fdf4e514 100644 --- a/frontend/app/settings/usermanager/usermanager.settings.component.html +++ b/frontend/app/settings/usermanager/usermanager.settings.component.html @@ -15,7 +15,7 @@ {{user.name}} - + {{repository.value}} diff --git a/frontend/app/settings/usermanager/usermanager.settings.component.ts b/frontend/app/settings/usermanager/usermanager.settings.component.ts index 24436a5e..6e0ea4dd 100644 --- a/frontend/app/settings/usermanager/usermanager.settings.component.ts +++ b/frontend/app/settings/usermanager/usermanager.settings.component.ts @@ -1,7 +1,7 @@ import {Component, OnInit} from "@angular/core"; import {AuthenticationService} from "../../model/network/authentication.service"; import {Router} from "@angular/router"; -import {User, UserRoles} from "../../../../common/entities/User"; +import {UserDTO, UserRoles} from "../../../../common/entities/UserDTO"; import {Utils} from "../../../../common/Utils"; import {Message} from "../../../../common/entities/Message"; import {UserManagerSettingsService} from "./usermanager.settings.service"; @@ -14,16 +14,16 @@ import {UserManagerSettingsService} from "./usermanager.settings.service"; }) export class UserMangerSettingsComponent implements OnInit { - private newUser = new User(); + private newUser = {}; private userRoles: Array = []; - private users: Array = []; + private users: Array = []; constructor(private _authService: AuthenticationService, private _router: Router, private _userSettings: UserManagerSettingsService) { } ngOnInit() { if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) { - this._router.navigate(['Login']); + this._router.navigate(['login']); return; } this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role); @@ -31,13 +31,13 @@ export class UserMangerSettingsComponent implements OnInit { } private getUsersList() { - this._userSettings.getUsers().then((result: Message>) => { + this._userSettings.getUsers().then((result: Message>) => { this.users = result.result; }); } - canModifyUser(user: User): boolean { + canModifyUser(user: UserDTO): boolean { let currentUser = this._authService.getUser(); if (!currentUser) { return false; @@ -47,8 +47,7 @@ export class UserMangerSettingsComponent implements OnInit { } initNewUser() { - this.newUser = new User(); - this.newUser.role = UserRoles.User; + this.newUser = {role: UserRoles.User}; } addNewUser() { @@ -57,13 +56,13 @@ export class UserMangerSettingsComponent implements OnInit { }); } - updateRole(user: User) { + updateRole(user: UserDTO) { this._userSettings.updateRole(user).then(() => { this.getUsersList(); }); } - deleteUser(user: User) { + deleteUser(user: UserDTO) { this._userSettings.deleteUser(user).then(() => { this.getUsersList(); }); diff --git a/frontend/app/settings/usermanager/usermanager.settings.service.ts b/frontend/app/settings/usermanager/usermanager.settings.service.ts index be7332e7..e83cc0cc 100644 --- a/frontend/app/settings/usermanager/usermanager.settings.service.ts +++ b/frontend/app/settings/usermanager/usermanager.settings.service.ts @@ -1,7 +1,5 @@ -/// - import {Injectable} from "@angular/core"; -import {User} from "../../../../common/entities/User"; +import {UserDTO} from "../../../../common/entities/UserDTO"; import {NetworkService} from "../../model/network/network.service"; import {Message} from "../../../../common/entities/Message"; @@ -12,21 +10,21 @@ export class UserManagerSettingsService { constructor(private _networkService:NetworkService) { } - public createUser(user:User):Promise> { + public createUser(user: UserDTO): Promise> { return this._networkService.putJson("/user", {newUser: user}); } - public getUsers():Promise>> { + public getUsers(): Promise>> { return this._networkService.getJson("/user/list"); } - public deleteUser(user:User) { + public deleteUser(user: UserDTO) { return this._networkService.deleteJson("/user/" + user.id); } - public updateRole(user:User) { + public updateRole(user: UserDTO) { return this._networkService.postJson("/user/" + user.id + "/role", {newRole: user.role}); } } diff --git a/frontend/browser.d.ts b/frontend/browser.d.ts deleted file mode 100644 index df8a3e66..00000000 --- a/frontend/browser.d.ts +++ /dev/null @@ -1,6 +0,0 @@ -/// -/// -/// -/// -/// - diff --git a/frontend/index.ejs b/frontend/index.ejs index 60f62a81..430b97eb 100644 --- a/frontend/index.ejs +++ b/frontend/index.ejs @@ -25,6 +25,7 @@ + +