mirror of
https://github.com/xuthus83/pigallery2.git
synced 2025-01-14 14:43:17 +08:00
implementing mysql user management
This commit is contained in:
parent
338e9bbb0e
commit
bb139b8159
@ -8,7 +8,16 @@ Config.Server = {
|
|||||||
port: 80,
|
port: 80,
|
||||||
imagesFolder: "demo/images",
|
imagesFolder: "demo/images",
|
||||||
thumbnailFolder: "demo/TEMP",
|
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"]]);
|
ConfigLoader.init(Config, path.join(__dirname, './../../config.json'), [["PORT", "Server-port"]]);
|
||||||
|
@ -1,15 +1,15 @@
|
|||||||
///<reference path="../customtypings/ExtendedRequest.d.ts"/>
|
///<reference path="../customtypings/ExtendedRequest.d.ts"/>
|
||||||
import {NextFunction, Request, Response} from "express";
|
import {NextFunction, Request, Response} from "express";
|
||||||
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
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 {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||||
import {Config} from "../../config/Config";
|
import {Config} from "../../config/Config";
|
||||||
|
|
||||||
export class AuthenticationMWs {
|
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) {
|
if (Config.Client.authenticationRequired === false) {
|
||||||
req.session.user = new User("", "", UserRoles.Admin);
|
req.session.user = <UserDTO>{name: "", role: UserRoles.Admin};
|
||||||
|
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
@ -19,8 +19,8 @@ export class AuthenticationMWs {
|
|||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static authorise(role:UserRoles) {
|
public static authorise(role: UserRoles) {
|
||||||
return (req:Request, res:Response, next:NextFunction) => {
|
return (req: Request, res: Response, next: NextFunction) => {
|
||||||
if (req.session.user.role < role) {
|
if (req.session.user.role < role) {
|
||||||
return next(new Error(ErrorCodes.NOT_AUTHORISED));
|
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') {
|
if (typeof req.session.user !== 'undefined') {
|
||||||
return next(new Error(ErrorCodes.ALREADY_AUTHENTICATED));
|
return next(new Error(ErrorCodes.ALREADY_AUTHENTICATED));
|
||||||
}
|
}
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
|
||||||
public static login(req:Request, res:Response, next:NextFunction) {
|
public static login(req: Request, res: Response, next: NextFunction) {
|
||||||
//not enough parameter
|
//not enough parameter
|
||||||
if ((typeof req.body === 'undefined') || (typeof req.body.loginCredential === 'undefined') || (typeof req.body.loginCredential.username === 'undefined') ||
|
if ((typeof req.body === 'undefined') || (typeof req.body.loginCredential === 'undefined') || (typeof req.body.loginCredential.username === 'undefined') ||
|
||||||
(typeof req.body.loginCredential.password === 'undefined')) {
|
(typeof req.body.loginCredential.password === 'undefined')) {
|
||||||
@ -48,6 +48,7 @@ export class AuthenticationMWs {
|
|||||||
password: req.body.loginCredential.password
|
password: req.body.loginCredential.password
|
||||||
}, (err, result) => {
|
}, (err, result) => {
|
||||||
if ((err) || (!result)) {
|
if ((err) || (!result)) {
|
||||||
|
console.error(err);
|
||||||
return next(new Error(ErrorCodes.CREDENTIAL_NOT_FOUND));
|
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;
|
delete req.session.user;
|
||||||
return next();
|
return next();
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import {NextFunction, Request, Response} from "express";
|
import {NextFunction, Request, Response} from "express";
|
||||||
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||||
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||||
import {User} from "../../../common/entities/User";
|
import {UserDTO} from "../../../common/entities/UserDTO";
|
||||||
import {Config} from "../../config/Config";
|
import {Config} from "../../config/Config";
|
||||||
import {Utils} from "../../../common/Utils";
|
import {Utils} from "../../../common/Utils";
|
||||||
|
|
||||||
@ -88,7 +88,7 @@ export class UserMWs {
|
|||||||
if (Config.Client.authenticationRequired === false) {
|
if (Config.Client.authenticationRequired === false) {
|
||||||
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
|
||||||
}
|
}
|
||||||
ObjectManagerRepository.getInstance().getUserManager().find({}, (err, result:Array<User>) => {
|
ObjectManagerRepository.getInstance().getUserManager().find({}, (err, result: Array<UserDTO>) => {
|
||||||
if ((err) || (!result)) {
|
if ((err) || (!result)) {
|
||||||
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
return next(new Error(ErrorCodes.GENERAL_ERROR));
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {NextFunction, Request, Response} from "express";
|
import {NextFunction, Request, Response} from "express";
|
||||||
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
import {Error, ErrorCodes} from "../../../common/entities/Error";
|
||||||
import {UserRoles} from "../../../common/entities/User";
|
import {UserRoles} from "../../../common/entities/UserDTO";
|
||||||
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
|
||||||
|
|
||||||
export class UserRequestConstrainsMWs {
|
export class UserRequestConstrainsMWs {
|
||||||
|
@ -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<User>) => 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;
|
|
||||||
}
|
|
@ -1,24 +1,39 @@
|
|||||||
import {IUserManager} from "./IUserManager";
|
import {IUserManager} from "./interfaces/IUserManager";
|
||||||
import {IGalleryManager} from "./IGalleryManager";
|
import {IGalleryManager} from "./interfaces/IGalleryManager";
|
||||||
import {ISearchManager} from "./ISearchManager";
|
import {ISearchManager} from "./interfaces/ISearchManager";
|
||||||
|
import {MySQLConnection} from "./mysql/MySQLConnection";
|
||||||
|
|
||||||
export class ObjectManagerRepository {
|
export class ObjectManagerRepository {
|
||||||
|
|
||||||
private _galleryManager:IGalleryManager;
|
private _galleryManager: IGalleryManager;
|
||||||
private _userManager:IUserManager;
|
private _userManager: IUserManager;
|
||||||
private _searchManager:ISearchManager;
|
private _searchManager: ISearchManager;
|
||||||
private static _instance:ObjectManagerRepository = null;
|
private static _instance: ObjectManagerRepository = null;
|
||||||
|
|
||||||
|
|
||||||
public static MemoryMongoManagers() {
|
public static InitMemoryManagers() {
|
||||||
let GalleryManager = require("./memory/GalleryManager").GalleryManager;
|
const GalleryManager = require("./memory/GalleryManager").GalleryManager;
|
||||||
let UserManager = require("./memory/UserManager").UserManager;
|
const UserManager = require("./memory/UserManager").UserManager;
|
||||||
let SearchManager = require("./memory/SearchManager").SearchManager;
|
const SearchManager = require("./memory/SearchManager").SearchManager;
|
||||||
ObjectManagerRepository.getInstance().setGalleryManager(new GalleryManager());
|
ObjectManagerRepository.getInstance().setGalleryManager(new GalleryManager());
|
||||||
ObjectManagerRepository.getInstance().setUserManager(new UserManager());
|
ObjectManagerRepository.getInstance().setUserManager(new UserManager());
|
||||||
ObjectManagerRepository.getInstance().setSearchManager(new SearchManager());
|
ObjectManagerRepository.getInstance().setSearchManager(new SearchManager());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public static InitMySQLManagers(): Promise<boolean> {
|
||||||
|
return new Promise<boolean>((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() {
|
public static getInstance() {
|
||||||
if (this._instance === null) {
|
if (this._instance === null) {
|
||||||
this._instance = new ObjectManagerRepository();
|
this._instance = new ObjectManagerRepository();
|
||||||
@ -31,27 +46,27 @@ export class ObjectManagerRepository {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
getGalleryManager():IGalleryManager {
|
getGalleryManager(): IGalleryManager {
|
||||||
return this._galleryManager;
|
return this._galleryManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
setGalleryManager(value:IGalleryManager) {
|
setGalleryManager(value: IGalleryManager) {
|
||||||
this._galleryManager = value;
|
this._galleryManager = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
getUserManager():IUserManager {
|
getUserManager(): IUserManager {
|
||||||
return this._userManager;
|
return this._userManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
setUserManager(value:IUserManager) {
|
setUserManager(value: IUserManager) {
|
||||||
this._userManager = value;
|
this._userManager = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
getSearchManager():ISearchManager {
|
getSearchManager(): ISearchManager {
|
||||||
return this._searchManager;
|
return this._searchManager;
|
||||||
}
|
}
|
||||||
|
|
||||||
setSearchManager(value:ISearchManager) {
|
setSearchManager(value: ISearchManager) {
|
||||||
this._searchManager = value;
|
this._searchManager = value;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
import {Directory} from "../../common/entities/Directory";
|
import {Directory} from "../../../common/entities/Directory";
|
||||||
export interface IGalleryManager {
|
export interface IGalleryManager {
|
||||||
listDirectory(relativeDirectoryName: string, cb: (error: any, result: Directory) => void): void;
|
listDirectory(relativeDirectoryName: string, cb: (error: any, result: Directory) => void): void;
|
||||||
}
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import {AutoCompleteItem, SearchTypes} from "../../common/entities/AutoCompleteItem";
|
import {AutoCompleteItem, SearchTypes} from "../../../common/entities/AutoCompleteItem";
|
||||||
import {SearchResult} from "../../common/entities/SearchResult";
|
import {SearchResult} from "../../../common/entities/SearchResult";
|
||||||
export interface ISearchManager {
|
export interface ISearchManager {
|
||||||
autocomplete(text: string, cb: (error: any, result: Array<AutoCompleteItem>) => void): void;
|
autocomplete(text: string, cb: (error: any, result: Array<AutoCompleteItem>) => void): void;
|
||||||
search(text: string, searchType: SearchTypes, cb: (error: any, result: SearchResult) => void): void;
|
search(text: string, searchType: SearchTypes, cb: (error: any, result: SearchResult) => void): void;
|
9
backend/model/interfaces/IUserManager.ts
Normal file
9
backend/model/interfaces/IUserManager.ts
Normal file
@ -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<UserDTO>) => 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;
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import {Directory} from "../../../common/entities/Directory";
|
import {Directory} from "../../../common/entities/Directory";
|
||||||
import {IGalleryManager} from "../IGalleryManager";
|
import {IGalleryManager} from "../interfaces/IGalleryManager";
|
||||||
import {DiskManager} from "../DiskManger";
|
import {DiskManager} from "../DiskManger";
|
||||||
|
|
||||||
export class GalleryManager implements IGalleryManager {
|
export class GalleryManager implements IGalleryManager {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {AutoCompleteItem, SearchTypes} from "../../../common/entities/AutoCompleteItem";
|
import {AutoCompleteItem, SearchTypes} from "../../../common/entities/AutoCompleteItem";
|
||||||
import {ISearchManager} from "../ISearchManager";
|
import {ISearchManager} from "../interfaces/ISearchManager";
|
||||||
import {SearchResult} from "../../../common/entities/SearchResult";
|
import {SearchResult} from "../../../common/entities/SearchResult";
|
||||||
|
|
||||||
export class SearchManager implements ISearchManager {
|
export class SearchManager implements ISearchManager {
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
///<reference path="flat-file-db.ts"/>
|
///<reference path="flat-file-db.ts"/>
|
||||||
import {User, UserRoles} from "../../../common/entities/User";
|
import {UserDTO, UserRoles} from "../../../common/entities/UserDTO";
|
||||||
import {IUserManager} from "../IUserManager";
|
import {IUserManager} from "../interfaces/IUserManager";
|
||||||
import {ProjectPath} from "../../ProjectPath";
|
import {ProjectPath} from "../../ProjectPath";
|
||||||
import {Utils} from "../../../common/Utils";
|
import {Utils} from "../../../common/Utils";
|
||||||
import * as flatfile from "flat-file-db";
|
import * as flatfile from "flat-file-db";
|
||||||
@ -31,37 +31,37 @@ export class UserManager implements IUserManager {
|
|||||||
if (!this.db.has("users")) {
|
if (!this.db.has("users")) {
|
||||||
this.db.put("users", []);
|
this.db.put("users", []);
|
||||||
//TODO: remove defaults
|
//TODO: remove defaults
|
||||||
this.createUser(new User("developer", "developer", UserRoles.Developer));
|
this.createUser(<UserDTO>{name: "developer", password: "developer", role: UserRoles.Developer});
|
||||||
this.createUser(new User("admin", "admin", UserRoles.Admin));
|
this.createUser(<UserDTO>{name: "admin", password: "admin", role: UserRoles.Admin});
|
||||||
this.createUser(new User("user", "user", UserRoles.User));
|
this.createUser(<UserDTO>{name: "user", password: "user", role: UserRoles.User});
|
||||||
this.createUser(new User("guest", "guest", UserRoles.Guest));
|
this.createUser(<UserDTO>{name: "guest", password: "guest", role: UserRoles.Guest});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public findOne(filter: any, cb: (error: any, result: User) => void) {
|
public findOne(filter: any, cb: (error: any, result: UserDTO) => void) {
|
||||||
this.find(filter, (error, result: Array<User>) => {
|
this.find(filter, (error, result: Array<UserDTO>) => {
|
||||||
if (error) {
|
if (error) {
|
||||||
return cb(error, null);
|
return cb(error, null);
|
||||||
}
|
}
|
||||||
if (result.length == 0) {
|
if (result.length == 0) {
|
||||||
return cb("User not found", null);
|
return cb("UserDTO not found", null);
|
||||||
}
|
}
|
||||||
return cb(null, result[0]);
|
return cb(null, result[0]);
|
||||||
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
public find(filter: any, cb: (error: any, result: Array<User>) => void) {
|
public find(filter: any, cb: (error: any, result: Array<UserDTO>) => 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);
|
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;
|
user.id = parseInt(this.db.get("idCounter")) + 1;
|
||||||
this.db.put("idCounter", user.id);
|
this.db.put("idCounter", user.id);
|
||||||
@ -73,14 +73,14 @@ export class UserManager implements IUserManager {
|
|||||||
}
|
}
|
||||||
|
|
||||||
public deleteUser(id: number, cb: (error: any) => void) {
|
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);
|
this.db.put("users", users);
|
||||||
return cb(null);
|
return cb(null);
|
||||||
}
|
}
|
||||||
|
|
||||||
public changeRole(id: number, newRole: UserRoles, cb: (error: any, result: string) => void) {
|
public changeRole(id: number, newRole: UserRoles, cb: (error: any, result: string) => void) {
|
||||||
|
|
||||||
let users: Array<User> = this.db.get("users");
|
let users: Array<UserDTO> = this.db.get("users");
|
||||||
|
|
||||||
for (let i = 0; i < users.length; i++) {
|
for (let i = 0; i < users.length; i++) {
|
||||||
if (users[i].id == id) {
|
if (users[i].id == id) {
|
||||||
|
65
backend/model/mysql/MySQLConnection.ts
Normal file
65
backend/model/mysql/MySQLConnection.ts
Normal file
@ -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<Connection> {
|
||||||
|
return new Promise<Connection>((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<void> {
|
||||||
|
return new Promise<void>((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));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
}
|
83
backend/model/mysql/UserManager.ts
Normal file
83
backend/model/mysql/UserManager.ts
Normal file
@ -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<UserDTO>) => 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
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
23
backend/model/mysql/enitites/UserEntity.ts
Normal file
23
backend/model/mysql/enitites/UserEntity.ts
Normal file
@ -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;
|
||||||
|
|
||||||
|
}
|
@ -1,5 +1,5 @@
|
|||||||
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||||
import {UserRoles} from "../../common/entities/User";
|
import {UserRoles} from "../../common/entities/UserDTO";
|
||||||
|
|
||||||
export class AdminRouter {
|
export class AdminRouter {
|
||||||
constructor(private app: any) {
|
constructor(private app: any) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||||
import {UserRoles} from "../../common/entities/User";
|
import {UserRoles} from "../../common/entities/UserDTO";
|
||||||
|
|
||||||
export class SharingRouter {
|
export class SharingRouter {
|
||||||
constructor(private app: any) {
|
constructor(private app: any) {
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {UserMWs} from "../middlewares/user/UserMWs";
|
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 {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
||||||
import {UserRequestConstrainsMWs} from "../middlewares/user/UserRequestConstrainsMWs";
|
import {UserRequestConstrainsMWs} from "../middlewares/user/UserRequestConstrainsMWs";
|
||||||
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
||||||
|
@ -54,11 +54,11 @@ export class Server {
|
|||||||
this.app.use(_bodyParser.json());
|
this.app.use(_bodyParser.json());
|
||||||
|
|
||||||
|
|
||||||
if (Config.Server.databaseType === DatabaseType.memory) {
|
ObjectManagerRepository.InitMySQLManagers().catch(() => {
|
||||||
ObjectManagerRepository.MemoryMongoManagers();
|
console.error("Erro during initailizing mysql falling back to memory DB");
|
||||||
} else {
|
Config.setDatabaseType(DatabaseType.memory);
|
||||||
throw new Error("not implemented alternative mangers");
|
ObjectManagerRepository.InitMemoryManagers();
|
||||||
}
|
});
|
||||||
|
|
||||||
new PublicRouter(this.app);
|
new PublicRouter(this.app);
|
||||||
|
|
||||||
|
@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "es5",
|
|
||||||
"module": "commonjs",
|
|
||||||
"moduleResolution": "node",
|
|
||||||
"sourceMap": true,
|
|
||||||
"emitDecoratorMetadata": true,
|
|
||||||
"experimentalDecorators": true,
|
|
||||||
"lib": [
|
|
||||||
"es2015",
|
|
||||||
"dom"
|
|
||||||
],
|
|
||||||
"suppressImplicitAnyIndexErrors": true
|
|
||||||
}
|
|
||||||
}
|
|
12
common/common-classes.d.ts
vendored
12
common/common-classes.d.ts
vendored
@ -1,12 +0,0 @@
|
|||||||
///<reference path="./event/Event.ts"/>
|
|
||||||
///<reference path="./event/Event2Args.ts"/>
|
|
||||||
///<reference path="./event/EventLimit.ts"/>
|
|
||||||
|
|
||||||
///<reference path="./MessageTypes.ts"/>
|
|
||||||
|
|
||||||
///<reference path="./entities/Error.ts"/>
|
|
||||||
///<reference path="./entities/LoginCredential.ts"/>
|
|
||||||
///<reference path="./entities/User.ts"/>
|
|
||||||
///<reference path="./entities/Message.ts"/>
|
|
||||||
///<reference path="./entities/Directory.ts"/>
|
|
||||||
///<reference path="./entities/Photo.ts"/>
|
|
@ -1,12 +1,23 @@
|
|||||||
export enum DatabaseType{
|
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 {
|
interface ServerConfig {
|
||||||
port:number;
|
port:number;
|
||||||
imagesFolder:string;
|
imagesFolder:string;
|
||||||
thumbnailFolder:string;
|
thumbnailFolder:string;
|
||||||
databaseType:DatabaseType;
|
database: DataBaseConfig;
|
||||||
}
|
}
|
||||||
|
|
||||||
interface SearchConfig {
|
interface SearchConfig {
|
||||||
@ -43,7 +54,7 @@ export class ConfigClass {
|
|||||||
};
|
};
|
||||||
|
|
||||||
public setDatabaseType(type:DatabaseType) {
|
public setDatabaseType(type:DatabaseType) {
|
||||||
this.Server.databaseType = type;
|
this.Server.database.type = type;
|
||||||
if (type === DatabaseType.memory) {
|
if (type === DatabaseType.memory) {
|
||||||
this.Client.Search.searchEnabled = false;
|
this.Client.Search.searchEnabled = false;
|
||||||
this.Client.Search.instantSearchEnabled = false;
|
this.Client.Search.instantSearchEnabled = false;
|
||||||
|
@ -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) {
|
|
||||||
}
|
|
||||||
}
|
|
14
common/entities/UserDTO.ts
Normal file
14
common/entities/UserDTO.ts
Normal file
@ -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;
|
||||||
|
}
|
@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "es5",
|
|
||||||
"module": "commonjs",
|
|
||||||
"moduleResolution": "node",
|
|
||||||
"sourceMap": true,
|
|
||||||
"emitDecoratorMetadata": true,
|
|
||||||
"experimentalDecorators": true,
|
|
||||||
"lib": [
|
|
||||||
"es2015",
|
|
||||||
"dom"
|
|
||||||
],
|
|
||||||
"suppressImplicitAnyIndexErrors": true
|
|
||||||
}
|
|
||||||
}
|
|
@ -1,7 +1,7 @@
|
|||||||
import {Component, OnInit} from "@angular/core";
|
import {Component, OnInit} from "@angular/core";
|
||||||
import {AuthenticationService} from "../model/network/authentication.service";
|
import {AuthenticationService} from "../model/network/authentication.service";
|
||||||
import {Router} from "@angular/router";
|
import {Router} from "@angular/router";
|
||||||
import {UserRoles} from "../../../common/entities/User";
|
import {UserRoles} from "../../../common/entities/UserDTO";
|
||||||
import {Config} from "../config/Config";
|
import {Config} from "../config/Config";
|
||||||
@Component({
|
@Component({
|
||||||
selector: 'admin',
|
selector: 'admin',
|
||||||
@ -17,7 +17,7 @@ export class AdminComponent implements OnInit {
|
|||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) {
|
if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) {
|
||||||
this._router.navigate(['Login']);
|
this._router.navigate(['login']);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {Component, OnInit} from "@angular/core";
|
import {Component, OnInit} from "@angular/core";
|
||||||
import {AuthenticationService} from "./model/network/authentication.service";
|
import {AuthenticationService} from "./model/network/authentication.service";
|
||||||
import {User} from "../../common/entities/User";
|
import {UserDTO} from "../../common/entities/UserDTO";
|
||||||
import {Router} from "@angular/router";
|
import {Router} from "@angular/router";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -14,7 +14,7 @@ export class AppComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
this._authenticationService.OnUserChanged.on((user: User) => {
|
this._authenticationService.OnUserChanged.on((user: UserDTO) => {
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
if (this._router.isActive('login', true)) {
|
if (this._router.isActive('login', true)) {
|
||||||
console.log("routing");
|
console.log("routing");
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import {Component, ViewEncapsulation} from "@angular/core";
|
import {Component, ViewEncapsulation} from "@angular/core";
|
||||||
import {RouterLink} from "@angular/router";
|
import {RouterLink} from "@angular/router";
|
||||||
import {AuthenticationService} from "../model/network/authentication.service";
|
import {AuthenticationService} from "../model/network/authentication.service";
|
||||||
import {User} from "../../../common/entities/User";
|
import {UserDTO} from "../../../common/entities/UserDTO";
|
||||||
import {Config} from "../config/Config";
|
import {Config} from "../config/Config";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -12,7 +12,7 @@ import {Config} from "../config/Config";
|
|||||||
})
|
})
|
||||||
export class FrameComponent {
|
export class FrameComponent {
|
||||||
|
|
||||||
user:User;
|
user: UserDTO;
|
||||||
authenticationRequired:boolean = false;
|
authenticationRequired:boolean = false;
|
||||||
|
|
||||||
constructor(private _authService:AuthenticationService) {
|
constructor(private _authService:AuthenticationService) {
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
///<reference path="../../../../../browser.d.ts"/>
|
|
||||||
|
|
||||||
import {Component, Input} from "@angular/core";
|
import {Component, Input} from "@angular/core";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
///<reference path="../../../../browser.d.ts"/>
|
|
||||||
|
|
||||||
import {Component, OnChanges, Input, ViewChild, ElementRef} from "@angular/core";
|
import {Component, OnChanges, Input, ViewChild, ElementRef} from "@angular/core";
|
||||||
import {GridPhoto} from "../../grid/GridPhoto";
|
import {GridPhoto} from "../../grid/GridPhoto";
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
///<reference path="../../../browser.d.ts"/>
|
|
||||||
|
|
||||||
import {Injectable} from "@angular/core";
|
import {Injectable} from "@angular/core";
|
||||||
import {NetworkService} from "../../model/network/network.service";
|
import {NetworkService} from "../../model/network/network.service";
|
||||||
import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem";
|
import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem";
|
||||||
|
@ -3,7 +3,7 @@ import {LoginCredential} from "../../../common/entities/LoginCredential";
|
|||||||
import {AuthenticationService} from "../model/network/authentication.service";
|
import {AuthenticationService} from "../model/network/authentication.service";
|
||||||
import {Router} from "@angular/router";
|
import {Router} from "@angular/router";
|
||||||
import {Message} from "../../../common/entities/Message";
|
import {Message} from "../../../common/entities/Message";
|
||||||
import {User} from "../../../common/entities/User";
|
import {UserDTO} from "../../../common/entities/UserDTO";
|
||||||
import {ErrorCodes} from "../../../common/entities/Error";
|
import {ErrorCodes} from "../../../common/entities/Error";
|
||||||
|
|
||||||
@Component({
|
@Component({
|
||||||
@ -27,7 +27,7 @@ export class LoginComponent implements OnInit {
|
|||||||
|
|
||||||
onLogin() {
|
onLogin() {
|
||||||
this.loginError = null;
|
this.loginError = null;
|
||||||
this._authService.login(this.loginCredential).then((message: Message<User>) => {
|
this._authService.login(this.loginCredential).then((message: Message<UserDTO>) => {
|
||||||
if (message.error) {
|
if (message.error) {
|
||||||
if (message.error.code === ErrorCodes.CREDENTIAL_NOT_FOUND) {
|
if (message.error.code === ErrorCodes.CREDENTIAL_NOT_FOUND) {
|
||||||
this.loginError = "Wrong username or password";
|
this.loginError = "Wrong username or password";
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
import {inject, TestBed} from "@angular/core/testing";
|
import {inject, TestBed} from "@angular/core/testing";
|
||||||
import {UserService} from "./user.service";
|
import {UserService} from "./user.service";
|
||||||
import {User} from "../../../../common/entities/User";
|
import {UserDTO} from "../../../../common/entities/UserDTO";
|
||||||
import {Message} from "../../../../common/entities/Message";
|
import {Message} from "../../../../common/entities/Message";
|
||||||
import "rxjs/Rx";
|
import "rxjs/Rx";
|
||||||
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
||||||
@ -8,7 +8,7 @@ import {AuthenticationService} from "./authentication.service";
|
|||||||
|
|
||||||
class MockUserService {
|
class MockUserService {
|
||||||
public login(credential: LoginCredential) {
|
public login(credential: LoginCredential) {
|
||||||
return Promise.resolve(new Message<User>(null, new User("testUserName")))
|
return Promise.resolve(new Message<UserDTO>(null, <UserDTO>{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();
|
spyOn(userService, "login").and.callThrough();
|
||||||
|
|
||||||
expect(userService.login).not.toHaveBeenCalled();
|
expect(userService.login).not.toHaveBeenCalled();
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {Injectable} from "@angular/core";
|
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 {Event} from "../../../../common/event/Event";
|
||||||
import {UserService} from "./user.service";
|
import {UserService} from "./user.service";
|
||||||
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
||||||
@ -9,14 +9,14 @@ import {ErrorCodes} from "../../../../common/entities/Error";
|
|||||||
import {Config} from "../../config/Config";
|
import {Config} from "../../config/Config";
|
||||||
|
|
||||||
declare module ServerInject {
|
declare module ServerInject {
|
||||||
export let user: User;
|
export let user: UserDTO;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
export class AuthenticationService {
|
export class AuthenticationService {
|
||||||
|
|
||||||
private _user: User = null;
|
private _user: UserDTO = null;
|
||||||
public OnUserChanged: Event<User>;
|
public OnUserChanged: Event<UserDTO>;
|
||||||
|
|
||||||
constructor(private _userService: UserService) {
|
constructor(private _userService: UserService) {
|
||||||
this.OnUserChanged = new Event();
|
this.OnUserChanged = new Event();
|
||||||
@ -34,7 +34,7 @@ export class AuthenticationService {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getSessionUser() {
|
private getSessionUser() {
|
||||||
this._userService.getSessionUser().then((message: Message<User>) => {
|
this._userService.getSessionUser().then((message: Message<UserDTO>) => {
|
||||||
if (message.error) {
|
if (message.error) {
|
||||||
console.log(message.error);
|
console.log(message.error);
|
||||||
} else {
|
} else {
|
||||||
@ -44,13 +44,13 @@ export class AuthenticationService {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
private setUser(user: User) {
|
private setUser(user: UserDTO) {
|
||||||
this._user = user;
|
this._user = user;
|
||||||
this.OnUserChanged.trigger(this._user);
|
this.OnUserChanged.trigger(this._user);
|
||||||
}
|
}
|
||||||
|
|
||||||
public login(credential: LoginCredential) {
|
public login(credential: LoginCredential) {
|
||||||
return this._userService.login(credential).then((message: Message<User>) => {
|
return this._userService.login(credential).then((message: Message<UserDTO>) => {
|
||||||
if (message.error) {
|
if (message.error) {
|
||||||
console.log(ErrorCodes[message.error.code] + ", message: ", message.error.message);
|
console.log(ErrorCodes[message.error.code] + ", message: ", message.error.message);
|
||||||
} else {
|
} else {
|
||||||
@ -70,7 +70,7 @@ export class AuthenticationService {
|
|||||||
|
|
||||||
public getUser() {
|
public getUser() {
|
||||||
if (Config.Client.authenticationRequired === false) {
|
if (Config.Client.authenticationRequired === false) {
|
||||||
return new User("", "", UserRoles.Admin);
|
return <UserDTO>{name: "", password: "", role: UserRoles.Admin};
|
||||||
}
|
}
|
||||||
return this._user;
|
return this._user;
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import {Injectable} from "@angular/core";
|
import {Injectable} from "@angular/core";
|
||||||
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
import {LoginCredential} from "../../../../common/entities/LoginCredential";
|
||||||
import {NetworkService} from "./network.service";
|
import {NetworkService} from "./network.service";
|
||||||
import {User} from "../../../../common/entities/User";
|
import {UserDTO} from "../../../../common/entities/UserDTO";
|
||||||
import {Message} from "../../../../common/entities/Message";
|
import {Message} from "../../../../common/entities/Message";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
@ -16,11 +16,11 @@ export class UserService {
|
|||||||
return this._networkService.postJson("/user/logout");
|
return this._networkService.postJson("/user/logout");
|
||||||
}
|
}
|
||||||
|
|
||||||
public login(credential: LoginCredential): Promise<Message<User>> {
|
public login(credential: LoginCredential): Promise<Message<UserDTO>> {
|
||||||
return this._networkService.postJson("/user/login", {"loginCredential": credential});
|
return this._networkService.postJson("/user/login", {"loginCredential": credential});
|
||||||
}
|
}
|
||||||
|
|
||||||
public getSessionUser(): Promise<Message<User>> {
|
public getSessionUser(): Promise<Message<UserDTO>> {
|
||||||
return this._networkService.getJson("/user/login");
|
return this._networkService.getJson("/user/login");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,5 +1,3 @@
|
|||||||
///<reference path="../browser.d.ts"/>
|
|
||||||
|
|
||||||
import {Injectable} from "@angular/core";
|
import {Injectable} from "@angular/core";
|
||||||
|
|
||||||
@Injectable()
|
@Injectable()
|
||||||
|
@ -1,5 +1,5 @@
|
|||||||
import {Pipe, PipeTransform} from "@angular/core";
|
import {Pipe, PipeTransform} from "@angular/core";
|
||||||
import {UserRoles} from "../../../common/entities/User";
|
import {UserRoles} from "../../../common/entities/UserDTO";
|
||||||
|
|
||||||
|
|
||||||
@Pipe({name: 'stringifyRole'})
|
@Pipe({name: 'stringifyRole'})
|
||||||
|
@ -15,7 +15,7 @@
|
|||||||
<tr *ngFor="let user of users">
|
<tr *ngFor="let user of users">
|
||||||
<td>{{user.name}}</td>
|
<td>{{user.name}}</td>
|
||||||
<td *ngIf="canModifyUser(user)">
|
<td *ngIf="canModifyUser(user)">
|
||||||
<select class="form-control" [(ngModel)]="user.role" (ngModelChange)="updateRole(user)" required>
|
<select class="form-control" [(ngModel)]="user.role" (change)="updateRole(user)" required>
|
||||||
<option *ngFor="let repository of userRoles" [value]="repository.key">
|
<option *ngFor="let repository of userRoles" [value]="repository.key">
|
||||||
{{repository.value}}
|
{{repository.value}}
|
||||||
</option>
|
</option>
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import {Component, OnInit} from "@angular/core";
|
import {Component, OnInit} from "@angular/core";
|
||||||
import {AuthenticationService} from "../../model/network/authentication.service";
|
import {AuthenticationService} from "../../model/network/authentication.service";
|
||||||
import {Router} from "@angular/router";
|
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 {Utils} from "../../../../common/Utils";
|
||||||
import {Message} from "../../../../common/entities/Message";
|
import {Message} from "../../../../common/entities/Message";
|
||||||
import {UserManagerSettingsService} from "./usermanager.settings.service";
|
import {UserManagerSettingsService} from "./usermanager.settings.service";
|
||||||
@ -14,16 +14,16 @@ import {UserManagerSettingsService} from "./usermanager.settings.service";
|
|||||||
})
|
})
|
||||||
export class UserMangerSettingsComponent implements OnInit {
|
export class UserMangerSettingsComponent implements OnInit {
|
||||||
|
|
||||||
private newUser = new User();
|
private newUser = <UserDTO>{};
|
||||||
private userRoles: Array<any> = [];
|
private userRoles: Array<any> = [];
|
||||||
private users: Array<User> = [];
|
private users: Array<UserDTO> = [];
|
||||||
|
|
||||||
constructor(private _authService: AuthenticationService, private _router: Router, private _userSettings: UserManagerSettingsService) {
|
constructor(private _authService: AuthenticationService, private _router: Router, private _userSettings: UserManagerSettingsService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
ngOnInit() {
|
ngOnInit() {
|
||||||
if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) {
|
if (!this._authService.isAuthenticated() || this._authService.getUser().role < UserRoles.Admin) {
|
||||||
this._router.navigate(['Login']);
|
this._router.navigate(['login']);
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role);
|
this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role);
|
||||||
@ -31,13 +31,13 @@ export class UserMangerSettingsComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private getUsersList() {
|
private getUsersList() {
|
||||||
this._userSettings.getUsers().then((result: Message<Array<User>>) => {
|
this._userSettings.getUsers().then((result: Message<Array<UserDTO>>) => {
|
||||||
this.users = result.result;
|
this.users = result.result;
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
canModifyUser(user: User): boolean {
|
canModifyUser(user: UserDTO): boolean {
|
||||||
let currentUser = this._authService.getUser();
|
let currentUser = this._authService.getUser();
|
||||||
if (!currentUser) {
|
if (!currentUser) {
|
||||||
return false;
|
return false;
|
||||||
@ -47,8 +47,7 @@ export class UserMangerSettingsComponent implements OnInit {
|
|||||||
}
|
}
|
||||||
|
|
||||||
initNewUser() {
|
initNewUser() {
|
||||||
this.newUser = new User();
|
this.newUser = <UserDTO>{role: UserRoles.User};
|
||||||
this.newUser.role = UserRoles.User;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
addNewUser() {
|
addNewUser() {
|
||||||
@ -57,13 +56,13 @@ export class UserMangerSettingsComponent implements OnInit {
|
|||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
updateRole(user: User) {
|
updateRole(user: UserDTO) {
|
||||||
this._userSettings.updateRole(user).then(() => {
|
this._userSettings.updateRole(user).then(() => {
|
||||||
this.getUsersList();
|
this.getUsersList();
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
deleteUser(user: User) {
|
deleteUser(user: UserDTO) {
|
||||||
this._userSettings.deleteUser(user).then(() => {
|
this._userSettings.deleteUser(user).then(() => {
|
||||||
this.getUsersList();
|
this.getUsersList();
|
||||||
});
|
});
|
||||||
|
@ -1,7 +1,5 @@
|
|||||||
///<reference path="../../../browser.d.ts"/>
|
|
||||||
|
|
||||||
import {Injectable} from "@angular/core";
|
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 {NetworkService} from "../../model/network/network.service";
|
||||||
import {Message} from "../../../../common/entities/Message";
|
import {Message} from "../../../../common/entities/Message";
|
||||||
|
|
||||||
@ -12,21 +10,21 @@ export class UserManagerSettingsService {
|
|||||||
constructor(private _networkService:NetworkService) {
|
constructor(private _networkService:NetworkService) {
|
||||||
}
|
}
|
||||||
|
|
||||||
public createUser(user:User):Promise<Message<string>> {
|
public createUser(user: UserDTO): Promise<Message<string>> {
|
||||||
return this._networkService.putJson("/user", {newUser: user});
|
return this._networkService.putJson("/user", {newUser: user});
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public getUsers():Promise<Message<Array<User>>> {
|
public getUsers(): Promise<Message<Array<UserDTO>>> {
|
||||||
return this._networkService.getJson("/user/list");
|
return this._networkService.getJson("/user/list");
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
public deleteUser(user:User) {
|
public deleteUser(user: UserDTO) {
|
||||||
return this._networkService.deleteJson("/user/" + user.id);
|
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});
|
return this._networkService.postJson("/user/" + user.id + "/role", {newRole: user.role});
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
6
frontend/browser.d.ts
vendored
6
frontend/browser.d.ts
vendored
@ -1,6 +0,0 @@
|
|||||||
/// <reference path="../common/common-classes.d.ts" />
|
|
||||||
/// <reference path="../node_modules/@angular/core/index.d.ts" />
|
|
||||||
/// <reference path="../node_modules/@angular/common/index.d.ts" />
|
|
||||||
/// <reference path="../node_modules/@angular/router/index.d.ts" />
|
|
||||||
/// <reference path="../node_modules/zone.js/dist/zone.js.d.ts"/>
|
|
||||||
|
|
@ -25,6 +25,7 @@
|
|||||||
</script>
|
</script>
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
<script src="systemjs.config.js"></script>
|
<script src="systemjs.config.js"></script>
|
||||||
<script>
|
<script>
|
||||||
System.import('').catch(function (err) {
|
System.import('').catch(function (err) {
|
||||||
@ -35,5 +36,13 @@
|
|||||||
|
|
||||||
<body style="overflow-y: scroll">
|
<body style="overflow-y: scroll">
|
||||||
<pi-gallery2-app>Loading...</pi-gallery2-app>
|
<pi-gallery2-app>Loading...</pi-gallery2-app>
|
||||||
|
|
||||||
|
<script
|
||||||
|
src="https://code.jquery.com/jquery-2.2.3.min.js"
|
||||||
|
integrity="sha256-a23g1Nt4dtEYOj7bR+vTu7+T8VP13humZFBJNIYoEJo="
|
||||||
|
crossorigin="anonymous"></script>
|
||||||
|
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.6/js/bootstrap.min.js"
|
||||||
|
integrity="sha384-0mSbJDEHialfmuBBQP6A4Qrprq5OVfW37PRR3j5ELqxss1yVqOtnepnHVP9aJ7xS"
|
||||||
|
crossorigin="anonymous"></script>
|
||||||
</body>
|
</body>
|
||||||
</html>
|
</html>
|
@ -1,15 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "es5",
|
|
||||||
"module": "commonjs",
|
|
||||||
"moduleResolution": "node",
|
|
||||||
"sourceMap": true,
|
|
||||||
"emitDecoratorMetadata": true,
|
|
||||||
"experimentalDecorators": true,
|
|
||||||
"lib": [
|
|
||||||
"es2015",
|
|
||||||
"dom"
|
|
||||||
],
|
|
||||||
"suppressImplicitAnyIndexErrors": true
|
|
||||||
}
|
|
||||||
}
|
|
23
package.json
23
package.json
@ -1,6 +1,6 @@
|
|||||||
{
|
{
|
||||||
"name": "PiGallery2",
|
"name": "PiGallery2",
|
||||||
"version": "0.0.5",
|
"version": "0.1.0",
|
||||||
"private": true,
|
"private": true,
|
||||||
"description": "This is a photo gallery optimised for running low resource servers (especially on raspberry pi)",
|
"description": "This is a photo gallery optimised for running low resource servers (especially on raspberry pi)",
|
||||||
"author": "Braun Patrik",
|
"author": "Braun Patrik",
|
||||||
@ -30,12 +30,8 @@
|
|||||||
"@angular/platform-browser-dynamic": "~2.4.1",
|
"@angular/platform-browser-dynamic": "~2.4.1",
|
||||||
"@angular/platform-server": "~2.4.1",
|
"@angular/platform-server": "~2.4.1",
|
||||||
"@angular/router": "~3.4.1",
|
"@angular/router": "~3.4.1",
|
||||||
"systemjs": "0.19.41",
|
|
||||||
"core-js": "^2.4.1",
|
|
||||||
"reflect-metadata": "^0.1.9",
|
|
||||||
"rxjs": "^5.0.2",
|
|
||||||
"zone.js": "^0.7.4",
|
|
||||||
"body-parser": "^1.15.2",
|
"body-parser": "^1.15.2",
|
||||||
|
"core-js": "^2.4.1",
|
||||||
"debug": "^2.5.2",
|
"debug": "^2.5.2",
|
||||||
"ejs": "^2.5.5",
|
"ejs": "^2.5.5",
|
||||||
"exif-parser": "^0.1.9",
|
"exif-parser": "^0.1.9",
|
||||||
@ -46,25 +42,32 @@
|
|||||||
"mime": "^1.3.4",
|
"mime": "^1.3.4",
|
||||||
"mocha": "^3.2.0",
|
"mocha": "^3.2.0",
|
||||||
"morgan": "^1.7.0",
|
"morgan": "^1.7.0",
|
||||||
|
"mysql": "^2.12.0",
|
||||||
"ng2-cookies": "^1.0.4",
|
"ng2-cookies": "^1.0.4",
|
||||||
"node-iptc": "^1.0.4",
|
"node-iptc": "^1.0.4",
|
||||||
"optimist": "^0.6.1"
|
"optimist": "^0.6.1",
|
||||||
|
"reflect-metadata": "^0.1.9",
|
||||||
|
"rxjs": "^5.0.2",
|
||||||
|
"systemjs": "0.19.41",
|
||||||
|
"typeorm": "0.0.5",
|
||||||
|
"zone.js": "^0.7.4"
|
||||||
},
|
},
|
||||||
"devDependencies": {
|
"devDependencies": {
|
||||||
|
"@types/core-js": "^0.9.35",
|
||||||
"@types/express": "^4.0.34",
|
"@types/express": "^4.0.34",
|
||||||
"@types/express-session": "0.0.32",
|
"@types/express-session": "0.0.32",
|
||||||
"@types/jasmine": "^2.5.38",
|
"@types/jasmine": "^2.5.38",
|
||||||
"@types/node": "^6.0.46",
|
"@types/node": "^6.0.54",
|
||||||
"@types/optimist": "0.0.29",
|
"@types/optimist": "0.0.29",
|
||||||
"angular-cli": "^1.0.0-beta.24",
|
"angular-cli": "^1.0.0-beta.24",
|
||||||
"chai": "^3.5.0",
|
"chai": "^3.5.0",
|
||||||
"jasmine-core": "^2.5.2",
|
"jasmine-core": "^2.5.2",
|
||||||
"karma": "1.2.0",
|
"karma": "^1.2.0",
|
||||||
"karma-cli": "^1.0.1",
|
"karma-cli": "^1.0.1",
|
||||||
"karma-jasmine": "^1.0.2",
|
"karma-jasmine": "^1.0.2",
|
||||||
"karma-jasmine-html-reporter": "^0.2.2",
|
"karma-jasmine-html-reporter": "^0.2.2",
|
||||||
"karma-phantomjs-launcher": "^1.0.2",
|
"karma-phantomjs-launcher": "^1.0.2",
|
||||||
"karma-remap-istanbul": "^0.2.1",
|
"karma-remap-istanbul": "^0.2.2",
|
||||||
"karma-systemjs": "^0.16.0",
|
"karma-systemjs": "^0.16.0",
|
||||||
"mocha": "^3.2.0",
|
"mocha": "^3.2.0",
|
||||||
"phantomjs-prebuilt": "^2.1.14",
|
"phantomjs-prebuilt": "^2.1.14",
|
||||||
|
@ -1,7 +1,7 @@
|
|||||||
import {expect} from "chai";
|
import {expect} from "chai";
|
||||||
import {AuthenticationMWs} from "../../../../../backend/middlewares/user/AuthenticationMWs";
|
import {AuthenticationMWs} from "../../../../../backend/middlewares/user/AuthenticationMWs";
|
||||||
import {Error, ErrorCodes} from "../../../../../common/entities/Error";
|
import {Error, ErrorCodes} from "../../../../../common/entities/Error";
|
||||||
import {UserRoles} from "../../../../../common/entities/User";
|
import {UserRoles} from "../../../../../common/entities/UserDTO";
|
||||||
import {ObjectManagerRepository} from "../../../../../backend/model/ObjectManagerRepository";
|
import {ObjectManagerRepository} from "../../../../../backend/model/ObjectManagerRepository";
|
||||||
import {UserManager} from "../../../../../backend/model/memory/UserManager";
|
import {UserManager} from "../../../../../backend/model/memory/UserManager";
|
||||||
|
|
||||||
|
@ -1,14 +0,0 @@
|
|||||||
{
|
|
||||||
"compilerOptions": {
|
|
||||||
"target": "es5",
|
|
||||||
"sourceMap": true,
|
|
||||||
"module": "commonjs",
|
|
||||||
"emitDecoratorMetadata": true,
|
|
||||||
"experimentalDecorators": true
|
|
||||||
},
|
|
||||||
"exclude": [
|
|
||||||
"node_modules",
|
|
||||||
"typings/main.d.ts",
|
|
||||||
"typings/main"
|
|
||||||
]
|
|
||||||
}
|
|
@ -10,6 +10,13 @@
|
|||||||
"es2015",
|
"es2015",
|
||||||
"dom"
|
"dom"
|
||||||
],
|
],
|
||||||
"suppressImplicitAnyIndexErrors": true
|
"suppressImplicitAnyIndexErrors": false,
|
||||||
}
|
"typeRoots": [
|
||||||
|
"./node_modules/@types"
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"exclude": [
|
||||||
|
"node_modules",
|
||||||
|
"node_modules/@types"
|
||||||
|
]
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user