2016-03-19 04:36:58 +08:00
|
|
|
///<reference path="../../typings/main.d.ts"/>
|
|
|
|
|
|
|
|
import {UserMWs} from "../middlewares/UserMWs";
|
2016-03-20 00:31:42 +08:00
|
|
|
import {UserRoles} from "../../common/entities/User";
|
|
|
|
import {AuthenticationMWs} from "../middlewares/AuthenticationMWs";
|
|
|
|
import {UserRequestConstrainsMWs} from "../middlewares/UserRequestConstrainsMWs";
|
2016-03-26 18:19:10 +08:00
|
|
|
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2016-03-19 04:36:58 +08:00
|
|
|
export class UserRouter{
|
|
|
|
constructor(private app){
|
2016-03-20 00:31:42 +08:00
|
|
|
this.addLogin();
|
2016-03-21 03:06:14 +08:00
|
|
|
this.addGetSessionUser();
|
2016-03-20 00:31:42 +08:00
|
|
|
this.addChangePassword();
|
|
|
|
|
|
|
|
|
|
|
|
this.addCreateUser();
|
|
|
|
this.addDeleteUser();
|
|
|
|
this.addListUsers();
|
|
|
|
this.addChangeRole();
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
private addLogin() {
|
|
|
|
this.app.post("/api/user/login",
|
|
|
|
AuthenticationMWs.inverseAuthenticate,
|
|
|
|
AuthenticationMWs.login,
|
2016-03-26 18:19:10 +08:00
|
|
|
RenderingMWs.renderSessionUser
|
2016-03-20 00:31:42 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-03-21 03:06:14 +08:00
|
|
|
private addGetSessionUser() {
|
|
|
|
this.app.get("/api/user/login",
|
|
|
|
AuthenticationMWs.authenticate,
|
2016-03-26 18:19:10 +08:00
|
|
|
RenderingMWs.renderSessionUser
|
2016-03-21 03:06:14 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
private addChangePassword() {
|
2016-03-20 17:49:49 +08:00
|
|
|
this.app.post("/api/user/:id/password",
|
2016-03-20 00:31:42 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
UserRequestConstrainsMWs.forceSelfRequest,
|
|
|
|
UserMWs.changePassword,
|
2016-03-26 18:19:10 +08:00
|
|
|
RenderingMWs.renderOK
|
2016-03-20 00:31:42 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private addCreateUser() {
|
|
|
|
this.app.put("/api/user",
|
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
|
|
|
UserMWs.createUser,
|
2016-03-26 18:19:10 +08:00
|
|
|
RenderingMWs.renderOK
|
2016-03-19 04:36:58 +08:00
|
|
|
);
|
|
|
|
};
|
2016-03-20 00:31:42 +08:00
|
|
|
|
|
|
|
private addDeleteUser() {
|
|
|
|
this.app.delete("/api/user/:id",
|
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
|
|
|
UserRequestConstrainsMWs.notSelfRequest,
|
|
|
|
UserMWs.deleteUser,
|
2016-03-26 18:19:10 +08:00
|
|
|
RenderingMWs.renderOK
|
2016-03-20 00:31:42 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
private addListUsers() {
|
|
|
|
this.app.post("/api/user/list",
|
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
2016-03-26 18:19:10 +08:00
|
|
|
UserMWs.listUsers,
|
|
|
|
RenderingMWs.renderResult
|
2016-03-20 00:31:42 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
private addChangeRole() {
|
2016-03-20 17:49:49 +08:00
|
|
|
this.app.post("/api/user/:id/role",
|
2016-03-20 00:31:42 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
|
|
|
UserRequestConstrainsMWs.notSelfRequestOr2Admins,
|
|
|
|
UserMWs.changeRole,
|
2016-03-26 18:19:10 +08:00
|
|
|
RenderingMWs.renderOK
|
2016-03-20 00:31:42 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|