1
0
mirror of https://github.com/xuthus83/pigallery2.git synced 2024-11-03 21:04:03 +08:00
pigallery2/backend/routes/UserRouter.ts

96 lines
2.7 KiB
TypeScript
Raw Normal View History

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-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",
AuthenticationMWs.inverseAuthenticate,
AuthenticationMWs.login,
RenderingMWs.renderSessionUser
);
};
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",
AuthenticationMWs.authenticate,
RenderingMWs.renderSessionUser
);
};
2017-06-04 21:25:08 +08:00
private static addChangePassword(app) {
app.post("/api/user/:id/password",
AuthenticationMWs.authenticate,
UserRequestConstrainsMWs.forceSelfRequest,
UserMWs.changePassword,
RenderingMWs.renderOK
);
};
2017-06-04 21:25:08 +08:00
private static addCreateUser(app) {
app.put("/api/user",
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
UserMWs.createUser,
RenderingMWs.renderOK
2016-03-19 04:36:58 +08:00
);
};
2017-06-04 21:25:08 +08:00
private static addDeleteUser(app) {
app.delete("/api/user/:id",
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
UserRequestConstrainsMWs.notSelfRequest,
UserMWs.deleteUser,
RenderingMWs.renderOK
);
};
2017-06-04 21:25:08 +08:00
private static addListUsers(app) {
app.get("/api/user/list",
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
UserMWs.listUsers,
RenderingMWs.renderResult
);
};
2017-06-04 21:25:08 +08:00
private static addChangeRole(app) {
app.post("/api/user/:id/role",
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
UserRequestConstrainsMWs.notSelfRequestOr2Admins,
UserMWs.changeRole,
RenderingMWs.renderOK
);
};
2016-03-19 04:36:58 +08:00
}