diff --git a/backend/config/Config.ts b/backend/config/Config.ts index dc24793..c94badd 100644 --- a/backend/config/Config.ts +++ b/backend/config/Config.ts @@ -1,5 +1,6 @@ import {ConfigLoader} from "./ConfigLoader"; +import * as path from "path"; export enum DatabaseType{ memory, mongoDB @@ -8,7 +9,7 @@ export enum DatabaseType{ export class ConfigClass{ constructor(){ - ConfigLoader.init(this,__dirname+'./../../config.json'); + ConfigLoader.init(this,path.join(__dirname,'./../../config.json')); } public PORT:number = 80; diff --git a/backend/middlewares/UserMWs.ts b/backend/middlewares/UserMWs.ts index 6b09736..6a7a98d 100644 --- a/backend/middlewares/UserMWs.ts +++ b/backend/middlewares/UserMWs.ts @@ -3,6 +3,7 @@ import {UserManager} from "../model/memory/UserManager"; import {NextFunction, Request, Response} from "express"; import {Error, ErrorCodes} from "../../common/entities/Error"; import {ObjectManagerRepository} from "../model/ObjectManagerRepository"; +import {User} from "../../common/entities/User"; export class UserMWs { @@ -40,12 +41,11 @@ export class UserMWs { } public static deleteUser(req:Request, res:Response, next:NextFunction){ - if ((typeof req.body === 'undefined') || (typeof req.body.newUser === 'undefined') - || (typeof req.body.userModReq.id === 'undefined')) { + if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) { return next(); } - ObjectManagerRepository.getInstance().getUserManager().deleteUser(req.body.userModReq.id, (err, result) =>{ + ObjectManagerRepository.getInstance().getUserManager().deleteUser(req.params.id, (err, result) =>{ if ((err) || (!result)) { return next(new Error(ErrorCodes.GENERAL_ERROR)); } @@ -57,13 +57,12 @@ export class UserMWs { } public static changeRole(req:Request, res:Response, next:NextFunction){ - if ((typeof req.body === 'undefined') || (typeof req.body.userModReq === 'undefined') - || (typeof req.body.userModReq.id === 'undefined') - || (typeof req.body.userModReq.newRole === 'undefined')) { + if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined') + || (typeof req.body === 'undefined') || (typeof req.body.newRole === 'undefined')) { return next(); } - ObjectManagerRepository.getInstance().getUserManager().changeRole(req.body.userModReq, (err) =>{ + ObjectManagerRepository.getInstance().getUserManager().changeRole(req.params.id,req.body.newRole, (err) =>{ if (err) { return next(new Error(ErrorCodes.GENERAL_ERROR)); } @@ -74,11 +73,14 @@ export class UserMWs { public static listUsers(req:Request, res:Response, next:NextFunction){ - ObjectManagerRepository.getInstance().getUserManager().find({}, (err, result) =>{ + ObjectManagerRepository.getInstance().getUserManager().find({}, (err, result:Array) =>{ if ((err) || (!result)) { return next(new Error(ErrorCodes.GENERAL_ERROR)); } - + for(let i = 0; i < result.length; i++){ + result[i].password = ""; + } + req.resultPipe = result; return next(); }); diff --git a/backend/middlewares/UserRequestConstrainsMWs.ts b/backend/middlewares/UserRequestConstrainsMWs.ts index 70ed302..7ad658a 100644 --- a/backend/middlewares/UserRequestConstrainsMWs.ts +++ b/backend/middlewares/UserRequestConstrainsMWs.ts @@ -40,7 +40,7 @@ export class UserRequestConstrainsMWs { if(req.session.user.id !== req.params.id){ return next(); } - + //TODO: fix it! ObjectManagerRepository.getInstance().getUserManager().find({minRole:UserRoles.Admin}, (err, result) =>{ if ((err) || (!result)) { return next(new Error(ErrorCodes.GENERAL_ERROR)); diff --git a/backend/model/IUserManager.ts b/backend/model/IUserManager.ts index 15b1c4f..e20d6e5 100644 --- a/backend/model/IUserManager.ts +++ b/backend/model/IUserManager.ts @@ -1,9 +1,9 @@ -import {User} from "../../common/entities/User"; +import {User, UserRoles} from "../../common/entities/User"; export interface IUserManager { findOne(filter,cb:(error: any,result:User) => void); find(filter,cb:(error: any,result:Array) => void); createUser(user,cb:(error: any,result:User) => void); deleteUser(id:number,cb:(error: any,result:string) => void); - changeRole(request:any,cb:(error: any) => void); + changeRole(id:number, newRole:UserRoles,cb:(error: any) => void); changePassword(request:any,cb:(error: any,result:string) => void); } \ No newline at end of file diff --git a/backend/model/memory/UserManager.ts b/backend/model/memory/UserManager.ts index 670e4a8..f6af051 100644 --- a/backend/model/memory/UserManager.ts +++ b/backend/model/memory/UserManager.ts @@ -1,11 +1,14 @@ -import {User} from "../../../common/entities/User"; +import {User, UserRoles} from "../../../common/entities/User"; import {IUserManager} from "../IUserManager"; export class UserManager implements IUserManager{ - private users = [new User(1,"TestUser","test@test.hu","122345")]; + private users = [new User(1,"developer", "developer", UserRoles.Developer), + new User(2,"admin", "admin", UserRoles.Admin), + new User(3,"user", "user", UserRoles.User), + new User(4,"guest", "guest", UserRoles.Guest)]; public findOne(filter,cb:(error: any,result:User) => void){ - return cb(null, this.users[0]); + return cb(null, this.users[1]); } public find(filter,cb:(error: any,result:Array) => void){ @@ -23,8 +26,13 @@ export class UserManager implements IUserManager{ return cb(null); } - public changeRole(request:any,cb:(error: any,result:string) => void){ - throw new Error("not implemented"); //TODO: implement + public changeRole(id:number, newRole:UserRoles, cb:(error: any,result:string) => void){ + for(let i = 0; i < this.users.length; i++){ + if (this.users[i].id === id){ + this.users[i].role = newRole; + return cb(null,"ok"); + } + } } public changePassword(request:any,cb:(error: any,result:string) => void){ throw new Error("not implemented"); //TODO: implement diff --git a/backend/model/mongoose/MongoUserManager.ts b/backend/model/mongoose/MongoUserManager.ts index 66e39e6..ca705ad 100644 --- a/backend/model/mongoose/MongoUserManager.ts +++ b/backend/model/mongoose/MongoUserManager.ts @@ -1,44 +1,51 @@ -import {User} from "../../../common/entities/User"; +import {User, UserRoles} from "../../../common/entities/User"; import {IUserManager} from "../IUserManager"; import {DatabaseManager} from "./DatabaseManager"; -export class MongoUserManager implements IUserManager{ +export class MongoUserManager implements IUserManager { private UserModel; - constructor(){ - this.UserModel = DatabaseManager.getInstance().getModel('user',{ - name:String, - email:{ type: String, index: { unique: true }}, - password:String, - role:Number + constructor() { + this.UserModel = DatabaseManager.getInstance().getModel('user', { + name: {type: String, index: {unique: true}}, + password: String, + role: Number }); } - public findOne(filter,cb:(error: any,result:User) => void){ - return this.UserModel.findOne(filter,function (err, result) { - return cb(err, result); - }); - } - - public find(filter,cb:(error: any,result:Array) => void){ - this.UserModel.find(filter,function (err, result) { + public findOne(filter, cb:(error:any, result:User) => void) { + return this.UserModel.findOne(filter, function (err, result) { return cb(err, result); }); } - public createUser(user,cb:(error: any,result:User) => void){ - this.UserModel.create(user,cb); + public find(filter, cb:(error:any, result:Array) => void) { + this.UserModel.find(filter, function (err, result) { + return cb(err, result); + }); } - public deleteUser(id:number,cb:(error: any) => void){ - this.UserModel.remove({id:id},cb); + public createUser(user, cb:(error:any, result:User) => void) { + this.UserModel.create(user, cb); } - - public changeRole(request:any,cb:(error: any,result:string) => void){ - throw new Error("not implemented"); //TODO: implement + + public deleteUser(id:number, cb:(error:any) => void) { + this.UserModel.remove({id: id}, cb); } - public changePassword(request:any,cb:(error: any,result:string) => void){ + + + public changeRole(id:number, newRole:UserRoles, cb:(error:any, result:string) => void) { + return this.UserModel.update({id: id}, {role: newRole}, function (err) { + if (!err) { + return cb(err, "ok") + } + return cb(err, null); + + }); + } + + public changePassword(request:any, cb:(error:any, result:string) => void) { throw new Error("not implemented"); //TODO: implement } diff --git a/backend/routes/UserRouter.ts b/backend/routes/UserRouter.ts index 7f52035..897681f 100644 --- a/backend/routes/UserRouter.ts +++ b/backend/routes/UserRouter.ts @@ -66,7 +66,7 @@ export class UserRouter{ private addListUsers() { - this.app.post("/api/user/list", + this.app.get("/api/user/list", AuthenticationMWs.authenticate, AuthenticationMWs.authorise(UserRoles.Admin), UserMWs.listUsers, diff --git a/common/entities/User.ts b/common/entities/User.ts index 43d723b..9954e99 100644 --- a/common/entities/User.ts +++ b/common/entities/User.ts @@ -8,5 +8,5 @@ export enum UserRoles{ } export class User { - constructor(public id?:number,public name?:string,public email?:string, public password?:string, public role?:UserRoles){} + constructor(public id?:number,public name?:string, public password?:string, public role:UserRoles = UserRoles.User){} } \ No newline at end of file diff --git a/frontend/app/admin/StringifyRolePipe.ts b/frontend/app/admin/StringifyRolePipe.ts new file mode 100644 index 0000000..df0baf4 --- /dev/null +++ b/frontend/app/admin/StringifyRolePipe.ts @@ -0,0 +1,11 @@ +import {Pipe, PipeTransform} from "angular2/core"; +import {UserRoles} from "../../../common/entities/User"; + + +@Pipe({name: 'stringifyRole'}) +export class StringifyRole implements PipeTransform { + transform(role: string): number { + return UserRoles[role]; + } +} + diff --git a/frontend/app/admin/admin.component.html b/frontend/app/admin/admin.component.html index a0995c3..385ec06 100644 --- a/frontend/app/admin/admin.component.html +++ b/frontend/app/admin/admin.component.html @@ -5,7 +5,38 @@

User management

-
diff --git a/frontend/app/admin/admin.component.ts b/frontend/app/admin/admin.component.ts index b0930ea..d5ee626 100644 --- a/frontend/app/admin/admin.component.ts +++ b/frontend/app/admin/admin.component.ts @@ -1,6 +1,6 @@ /// -import {Component, OnInit} from "angular2/core"; +import {Component, OnInit, Pipe, PipeTransform} from "angular2/core"; import {AuthenticationService} from "../model/network/authentication.service.ts"; import {Router} from "angular2/router"; import {FrameComponent} from "../frame/frame.component"; @@ -8,21 +8,24 @@ import {User, UserRoles} from "../../../common/entities/User"; import {FORM_DIRECTIVES} from "angular2/common"; import {Utils} from "../../../common/Utils"; import {AdminService} from "./admin.service"; +import {Message} from "../../../common/entities/Message"; +import {StringifyRole} from "./StringifyRolePipe"; @Component({ selector: 'admin', templateUrl: 'app/admin/admin.component.html', styleUrls: ['app/admin/admin.component.css'], directives: [FrameComponent, FORM_DIRECTIVES], - providers: [AdminService] + providers: [AdminService], + pipes: [StringifyRole] }) export class AdminComponent implements OnInit { private newUser = new User(); - private userRoles:Array; + private userRoles:Array = []; + private users:Array = []; constructor(private _authService:AuthenticationService, private _router:Router, private _adminService:AdminService) { - this.userRoles = Utils.enumToArray(UserRoles); } ngOnInit() { @@ -30,15 +33,49 @@ export class AdminComponent implements OnInit { this._router.navigate(['Login']); return; } + this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role); + this.getUsersList(); + } + + private getUsersList(){ + this._adminService.getUsers().then((result:Message>) =>{ + this.users = result.result; + }); } + + canModifyUser(user:User):boolean{ + let currentUser = this._authService.getUser(); + if(!currentUser){ + return false; + } + + return currentUser.name != user.name && currentUser.role >= user.role; + } + initNewUser() { this.newUser = new User(); this.newUser.role = UserRoles.User; } addNewUser(){ - this._adminService.createUser(this.newUser); + this._adminService.createUser(this.newUser).then(() =>{ + this.getUsersList(); + }); + } + + updateRole(user:User){ + this._adminService.updateRole(user).then(() =>{ + this.getUsersList(); + }); + } + + deleteUser(user:User){ + this._adminService.deleteUser(user).then(() =>{ + this.getUsersList(); + }); } } + + diff --git a/frontend/app/admin/admin.service.ts b/frontend/app/admin/admin.service.ts index ed6c987..0660fa3 100644 --- a/frontend/app/admin/admin.service.ts +++ b/frontend/app/admin/admin.service.ts @@ -19,7 +19,16 @@ export class AdminService extends NetworkService{ } - + public getUsers():Promise>>{ + return this.getJson("/user/list"); + } + public deleteUser(user:User) { + return this.deleteJson("/user/"+user.id); + } + + public updateRole(user:User) { + return this.postJson("/user/"+user.id+"/role",{newRole:user.role}); + } } diff --git a/frontend/app/gallery/grid/grid.gallery.component.ts b/frontend/app/gallery/grid/grid.gallery.component.ts index cd77cd8..47e2636 100644 --- a/frontend/app/gallery/grid/grid.gallery.component.ts +++ b/frontend/app/gallery/grid/grid.gallery.component.ts @@ -88,9 +88,7 @@ export class GalleryGridComponent implements OnChanges,AfterViewInit{ private getContainerWidth(): number{ if(!this.gridContainer){ return 0; - } - console.log(this.gridContainer); - console.log(this.gridContainer.nativeElement.clientWidth); + } return this.gridContainer.nativeElement.clientWidth; } diff --git a/frontend/app/login/login.component.ts b/frontend/app/login/login.component.ts index 70dffcf..f09d75f 100644 --- a/frontend/app/login/login.component.ts +++ b/frontend/app/login/login.component.ts @@ -20,7 +20,7 @@ export class LoginComponent implements OnInit{ ngOnInit(){ if (this._authService.isAuthenticated()) { - this._router.navigate(['Gallery']); + this._router.navigate(['Gallery',{directory:"/"}]); } } diff --git a/frontend/app/model/network/network.service.ts b/frontend/app/model/network/network.service.ts index 41522c6..aed86d8 100644 --- a/frontend/app/model/network/network.service.ts +++ b/frontend/app/model/network/network.service.ts @@ -17,7 +17,7 @@ export class NetworkService{ let headers = new Headers({ 'Content-Type': 'application/json' }); let options = new RequestOptions({ headers: headers }); - if(method == "get"){ + if(method == "get" || method == "delete"){ return this._http[method](this._baseUrl+url, options) .toPromise() .then(res => > res.json())