2016-05-26 02:17:42 +08:00
|
|
|
import {UserMWs} from "../middlewares/user/UserMWs";
|
2016-12-27 23:09:47 +08:00
|
|
|
import {UserRoles} from "../../common/entities/UserDTO";
|
2016-05-26 02:17:42 +08:00
|
|
|
import {AuthenticationMWs} from "../middlewares/user/AuthenticationMWs";
|
|
|
|
import {UserRequestConstrainsMWs} from "../middlewares/user/UserRequestConstrainsMWs";
|
2016-05-09 23:04:56 +08:00
|
|
|
import {RenderingMWs} from "../middlewares/RenderingMWs";
|
2016-03-20 00:31:42 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class UserRouter {
|
2017-06-04 21:25:08 +08:00
|
|
|
public static route(app) {
|
|
|
|
this.addLogin(app);
|
|
|
|
this.addLogout(app);
|
|
|
|
this.addGetSessionUser(app);
|
|
|
|
this.addChangePassword(app);
|
2016-05-09 23:04:56 +08:00
|
|
|
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
this.addCreateUser(app);
|
|
|
|
this.addDeleteUser(app);
|
|
|
|
this.addListUsers(app);
|
|
|
|
this.addChangeRole(app);
|
2016-03-19 04:36:58 +08:00
|
|
|
}
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static addLogin(app) {
|
|
|
|
app.post("/api/user/login",
|
2016-03-20 00:31:42 +08:00
|
|
|
AuthenticationMWs.inverseAuthenticate,
|
|
|
|
AuthenticationMWs.login,
|
2016-03-26 18:19:10 +08:00
|
|
|
RenderingMWs.renderSessionUser
|
2016-03-20 00:31:42 +08:00
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static addLogout(app) {
|
|
|
|
app.post("/api/user/logout",
|
2016-05-17 05:15:03 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.logout,
|
|
|
|
RenderingMWs.renderOK
|
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static addGetSessionUser(app) {
|
|
|
|
app.get("/api/user/login",
|
2016-03-21 03:06:14 +08:00
|
|
|
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
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static addChangePassword(app) {
|
|
|
|
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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static addCreateUser(app) {
|
|
|
|
app.put("/api/user",
|
2016-03-20 00:31:42 +08:00
|
|
|
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
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static addDeleteUser(app) {
|
|
|
|
app.delete("/api/user/:id",
|
2016-03-20 00:31:42 +08:00
|
|
|
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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static addListUsers(app) {
|
|
|
|
app.get("/api/user/list",
|
2016-03-20 00:31:42 +08:00
|
|
|
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
|
|
|
);
|
|
|
|
};
|
|
|
|
|
2017-06-04 21:25:08 +08:00
|
|
|
private static addChangeRole(app) {
|
|
|
|
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
|
|
|
}
|