1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2025-01-14 14:43:17 +08:00

implementing switchable userManagement

This commit is contained in:
Braun Patrik 2016-07-07 12:26:36 +02:00
parent ca0de09881
commit a68159cfea
6 changed files with 35 additions and 7 deletions

View File

@ -3,13 +3,17 @@
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, User} from "../../../common/entities/User";
import {ObjectManagerRepository} from "../../model/ObjectManagerRepository"; import {ObjectManagerRepository} from "../../model/ObjectManagerRepository";
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) {
req.session.user = new User("", "", UserRoles.Admin);
return next();
}
if (typeof req.session.user === 'undefined') { if (typeof req.session.user === 'undefined') {
return next(new Error(ErrorCodes.NOT_AUTHENTICATED)); return next(new Error(ErrorCodes.NOT_AUTHENTICATED));
} }

View File

@ -2,10 +2,14 @@ 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 {User} from "../../../common/entities/User";
import {Config} from "../../config/Config";
export class UserMWs { export class UserMWs {
public static changePassword(req:Request, res:Response, next:NextFunction) { public static changePassword(req:Request, res:Response, next:NextFunction) {
if (Config.Client.authenticationRequired === false) {
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
}
if ((typeof req.body === 'undefined') || (typeof req.body.userModReq === 'undefined') if ((typeof req.body === 'undefined') || (typeof req.body.userModReq === 'undefined')
|| (typeof req.body.userModReq.id === 'undefined') || (typeof req.body.userModReq.id === 'undefined')
|| (typeof req.body.userModReq.oldPassword === 'undefined') || (typeof req.body.userModReq.oldPassword === 'undefined')
@ -24,6 +28,9 @@ export class UserMWs {
public static createUser(req:Request, res:Response, next:NextFunction) { public static createUser(req:Request, res:Response, next:NextFunction) {
if (Config.Client.authenticationRequired === false) {
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
}
if ((typeof req.body === 'undefined') || (typeof req.body.newUser === 'undefined')) { if ((typeof req.body === 'undefined') || (typeof req.body.newUser === 'undefined')) {
return next(); return next();
} }
@ -39,6 +46,9 @@ export class UserMWs {
} }
public static deleteUser(req:Request, res:Response, next:NextFunction) { public static deleteUser(req:Request, res:Response, next:NextFunction) {
if (Config.Client.authenticationRequired === false) {
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
}
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) { if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')) {
return next(); return next();
} }
@ -55,6 +65,9 @@ export class UserMWs {
} }
public static changeRole(req:Request, res:Response, next:NextFunction) { public static changeRole(req:Request, res:Response, next:NextFunction) {
if (Config.Client.authenticationRequired === false) {
return next(new Error(ErrorCodes.USER_MANAGEMENT_DISABLED));
}
if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined') if ((typeof req.params === 'undefined') || (typeof req.params.id === 'undefined')
|| (typeof req.body === 'undefined') || (typeof req.body.newRole === 'undefined')) { || (typeof req.body === 'undefined') || (typeof req.body.newRole === 'undefined')) {
return next(); return next();
@ -71,6 +84,9 @@ export class UserMWs {
public static listUsers(req:Request, res:Response, next:NextFunction) { public static listUsers(req:Request, res:Response, next:NextFunction) {
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<User>) => {
if ((err) || (!result)) { if ((err) || (!result)) {
return next(new Error(ErrorCodes.GENERAL_ERROR)); return next(new Error(ErrorCodes.GENERAL_ERROR));

View File

@ -39,7 +39,7 @@ export class ConfigClass {
enableCache: false, enableCache: false,
enableOnScrollRendering: true, enableOnScrollRendering: true,
enableOnScrollThumbnailPrioritising: true, enableOnScrollThumbnailPrioritising: true,
authenticationRequired: true authenticationRequired: false
}; };
public setDatabaseType(type:DatabaseType) { public setDatabaseType(type:DatabaseType) {

View File

@ -9,7 +9,9 @@ export enum ErrorCodes{
GENERAL_ERROR, GENERAL_ERROR,
SERVER_ERROR SERVER_ERROR,
USER_MANAGEMENT_DISABLED
} }

View File

@ -1,6 +1,6 @@
<app-frame> <app-frame>
<div body class="container"> <div body class="container">
<div class="panel panel-default"> <div class="panel panel-default" *ngIf="userManagementEnable">
<div class="panel-heading"> <div class="panel-heading">
<h3 class="panel-title">User management</h3> <h3 class="panel-title">User management</h3>
</div> </div>

View File

@ -10,6 +10,7 @@ import {Utils} from "../../../common/Utils";
import {AdminService} from "./admin.service"; import {AdminService} from "./admin.service";
import {Message} from "../../../common/entities/Message"; import {Message} from "../../../common/entities/Message";
import {StringifyRole} from "./../pipes/StringifyRolePipe"; import {StringifyRole} from "./../pipes/StringifyRolePipe";
import {Config} from "../config/Config";
@Component({ @Component({
selector: 'admin', selector: 'admin',
@ -24,8 +25,11 @@ export class AdminComponent implements OnInit {
private newUser = new User(); private newUser = new User();
private userRoles:Array<any> = []; private userRoles:Array<any> = [];
private users:Array<User> = []; private users:Array<User> = [];
userManagementEnable:boolean = false;
constructor(private _authService:AuthenticationService, private _router:Router, private _adminService:AdminService) { constructor(private _authService:AuthenticationService, private _router:Router, private _adminService:AdminService) {
this.userManagementEnable = Config.Client.authenticationRequired;
} }
ngOnInit() { ngOnInit() {
@ -33,8 +37,10 @@ export class AdminComponent implements OnInit {
this._router.navigate(['Login']); this._router.navigate(['Login']);
return; return;
} }
this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role); if (Config.Client.authenticationRequired === true) {
this.getUsersList(); this.userRoles = Utils.enumToArray(UserRoles).filter(r => r.key <= this._authService.getUser().role);
this.getUsersList();
}
} }
private getUsersList() { private getUsersList() {