1
0
mirror of https://github.com/bpatrik/pigallery2.git synced 2024-12-25 02:04:15 +02:00

implementing mysql user management

This commit is contained in:
Braun Patrik 2016-12-27 16:09:47 +01:00
parent 338e9bbb0e
commit bb139b8159
47 changed files with 360 additions and 222 deletions

View File

@ -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"]]);

View File

@ -1,26 +1,26 @@
///<reference path="../customtypings/ExtendedRequest.d.ts"/>
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 = <UserDTO>{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();
}

View File

@ -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<User>) => {
ObjectManagerRepository.getInstance().getUserManager().find({}, (err, result: Array<UserDTO>) => {
if ((err) || (!result)) {
return next(new Error(ErrorCodes.GENERAL_ERROR));
}

View File

@ -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 {

View File

@ -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;
}

View File

@ -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<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() {
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;
}

View File

@ -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;
}

View File

@ -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<AutoCompleteItem>) => void): void;
search(text: string, searchType: SearchTypes, cb: (error: any, result: SearchResult) => void): void;

View 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;
}

View File

@ -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 {

View File

@ -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 {

View File

@ -1,6 +1,6 @@
///<reference path="flat-file-db.ts"/>
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(<UserDTO>{name: "developer", password: "developer", role: UserRoles.Developer});
this.createUser(<UserDTO>{name: "admin", password: "admin", role: UserRoles.Admin});
this.createUser(<UserDTO>{name: "user", password: "user", role: UserRoles.User});
this.createUser(<UserDTO>{name: "guest", password: "guest", role: UserRoles.Guest});
}
}
public findOne(filter: any, cb: (error: any, result: User) => void) {
this.find(filter, (error, result: Array<User>) => {
public findOne(filter: any, cb: (error: any, result: UserDTO) => void) {
this.find(filter, (error, result: Array<UserDTO>) => {
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<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);
}
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<User> = this.db.get("users");
let users: Array<UserDTO> = this.db.get("users");
for (let i = 0; i < users.length; i++) {
if (users[i].id == id) {

View 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));
});
}
}

View 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
}
}

View 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;
}

View File

@ -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) {

View File

@ -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) {

View File

@ -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";

View File

@ -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);

View File

@ -1,15 +0,0 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2015",
"dom"
],
"suppressImplicitAnyIndexErrors": true
}
}

View File

@ -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"/>

View File

@ -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;

View File

@ -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) {
}
}

View 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;
}

View File

@ -1,15 +0,0 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2015",
"dom"
],
"suppressImplicitAnyIndexErrors": true
}
}

View File

@ -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;
}
}

View File

@ -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");

View File

@ -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) {

View File

@ -1,5 +1,3 @@
///<reference path="../../../../../browser.d.ts"/>
import {Component, Input} from "@angular/core";
@Component({

View File

@ -1,5 +1,3 @@
///<reference path="../../../../browser.d.ts"/>
import {Component, OnChanges, Input, ViewChild, ElementRef} from "@angular/core";
import {GridPhoto} from "../../grid/GridPhoto";

View File

@ -1,5 +1,3 @@
///<reference path="../../../browser.d.ts"/>
import {Injectable} from "@angular/core";
import {NetworkService} from "../../model/network/network.service";
import {AutoCompleteItem} from "../../../../common/entities/AutoCompleteItem";

View File

@ -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<User>) => {
this._authService.login(this.loginCredential).then((message: Message<UserDTO>) => {
if (message.error) {
if (message.error.code === ErrorCodes.CREDENTIAL_NOT_FOUND) {
this.loginError = "Wrong username or password";

View File

@ -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<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();
expect(userService.login).not.toHaveBeenCalled();

View File

@ -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<User>;
private _user: UserDTO = null;
public OnUserChanged: Event<UserDTO>;
constructor(private _userService: UserService) {
this.OnUserChanged = new Event();
@ -34,7 +34,7 @@ export class AuthenticationService {
}
private getSessionUser() {
this._userService.getSessionUser().then((message: Message<User>) => {
this._userService.getSessionUser().then((message: Message<UserDTO>) => {
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<User>) => {
return this._userService.login(credential).then((message: Message<UserDTO>) => {
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 <UserDTO>{name: "", password: "", role: UserRoles.Admin};
}
return this._user;
}

View File

@ -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<Message<User>> {
public login(credential: LoginCredential): Promise<Message<UserDTO>> {
return this._networkService.postJson("/user/login", {"loginCredential": credential});
}
public getSessionUser(): Promise<Message<User>> {
public getSessionUser(): Promise<Message<UserDTO>> {
return this._networkService.getJson("/user/login");
}

View File

@ -1,5 +1,3 @@
///<reference path="../browser.d.ts"/>
import {Injectable} from "@angular/core";
@Injectable()

View File

@ -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'})

View File

@ -15,7 +15,7 @@
<tr *ngFor="let user of users">
<td>{{user.name}}</td>
<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">
{{repository.value}}
</option>

View File

@ -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 = <UserDTO>{};
private userRoles: Array<any> = [];
private users: Array<User> = [];
private users: Array<UserDTO> = [];
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<Array<User>>) => {
this._userSettings.getUsers().then((result: Message<Array<UserDTO>>) => {
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 = <UserDTO>{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();
});

View File

@ -1,7 +1,5 @@
///<reference path="../../../browser.d.ts"/>
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<Message<string>> {
public createUser(user: UserDTO): Promise<Message<string>> {
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");
}
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});
}
}

View File

@ -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"/>

View File

@ -25,6 +25,7 @@
</script>
<script src="systemjs.config.js"></script>
<script>
System.import('').catch(function (err) {
@ -35,5 +36,13 @@
<body style="overflow-y: scroll">
<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>
</html>

View File

@ -1,15 +0,0 @@
{
"compilerOptions": {
"target": "es5",
"module": "commonjs",
"moduleResolution": "node",
"sourceMap": true,
"emitDecoratorMetadata": true,
"experimentalDecorators": true,
"lib": [
"es2015",
"dom"
],
"suppressImplicitAnyIndexErrors": true
}
}

View File

@ -1,6 +1,6 @@
{
"name": "PiGallery2",
"version": "0.0.5",
"version": "0.1.0",
"private": true,
"description": "This is a photo gallery optimised for running low resource servers (especially on raspberry pi)",
"author": "Braun Patrik",
@ -30,12 +30,8 @@
"@angular/platform-browser-dynamic": "~2.4.1",
"@angular/platform-server": "~2.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",
"core-js": "^2.4.1",
"debug": "^2.5.2",
"ejs": "^2.5.5",
"exif-parser": "^0.1.9",
@ -46,25 +42,32 @@
"mime": "^1.3.4",
"mocha": "^3.2.0",
"morgan": "^1.7.0",
"mysql": "^2.12.0",
"ng2-cookies": "^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": {
"@types/core-js": "^0.9.35",
"@types/express": "^4.0.34",
"@types/express-session": "0.0.32",
"@types/jasmine": "^2.5.38",
"@types/node": "^6.0.46",
"@types/node": "^6.0.54",
"@types/optimist": "0.0.29",
"angular-cli": "^1.0.0-beta.24",
"chai": "^3.5.0",
"jasmine-core": "^2.5.2",
"karma": "1.2.0",
"karma": "^1.2.0",
"karma-cli": "^1.0.1",
"karma-jasmine": "^1.0.2",
"karma-jasmine-html-reporter": "^0.2.2",
"karma-phantomjs-launcher": "^1.0.2",
"karma-remap-istanbul": "^0.2.1",
"karma-remap-istanbul": "^0.2.2",
"karma-systemjs": "^0.16.0",
"mocha": "^3.2.0",
"phantomjs-prebuilt": "^2.1.14",

View File

@ -1,7 +1,7 @@
import {expect} from "chai";
import {AuthenticationMWs} from "../../../../../backend/middlewares/user/AuthenticationMWs";
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 {UserManager} from "../../../../../backend/model/memory/UserManager";

View File

@ -1,14 +0,0 @@
{
"compilerOptions": {
"target": "es5",
"sourceMap": true,
"module": "commonjs",
"emitDecoratorMetadata": true,
"experimentalDecorators": true
},
"exclude": [
"node_modules",
"typings/main.d.ts",
"typings/main"
]
}

View File

@ -10,6 +10,13 @@
"es2015",
"dom"
],
"suppressImplicitAnyIndexErrors": true
}
"suppressImplicitAnyIndexErrors": false,
"typeRoots": [
"./node_modules/@types"
]
},
"exclude": [
"node_modules",
"node_modules/@types"
]
}