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.3 KiB
TypeScript
Raw Normal View History

2018-03-31 03:30:30 +08:00
import {UserMWs} from '../middlewares/user/UserMWs';
import {UserRoles} from '../../common/entities/UserDTO';
import {AuthenticationMWs} from '../middlewares/user/AuthenticationMWs';
import {UserRequestConstrainsMWs} from '../middlewares/user/UserRequestConstrainsMWs';
import {RenderingMWs} from '../middlewares/RenderingMWs';
2016-05-09 23:04:56 +08:00
export class UserRouter {
public static route(app) {
this.addLogin(app);
this.addLogout(app);
this.addGetSessionUser(app);
this.addChangePassword(app);
this.addCreateUser(app);
this.addDeleteUser(app);
this.addListUsers(app);
this.addChangeRole(app);
}
private static addLogin(app) {
2018-03-31 03:30:30 +08:00
app.post('/api/user/login',
AuthenticationMWs.inverseAuthenticate,
AuthenticationMWs.login,
RenderingMWs.renderSessionUser
);
2018-03-31 03:30:30 +08:00
}
private static addLogout(app) {
2018-03-31 03:30:30 +08:00
app.post('/api/user/logout',
AuthenticationMWs.logout,
RenderingMWs.renderOK
);
2018-03-31 03:30:30 +08:00
}
private static addGetSessionUser(app) {
2018-03-31 03:30:30 +08:00
app.get('/api/user/login',
AuthenticationMWs.authenticate,
RenderingMWs.renderSessionUser
);
2018-03-31 03:30:30 +08:00
}
private static addChangePassword(app) {
2018-03-31 03:30:30 +08:00
app.post('/api/user/:id/password',
AuthenticationMWs.authenticate,
UserRequestConstrainsMWs.forceSelfRequest,
UserMWs.changePassword,
RenderingMWs.renderOK
);
2018-03-31 03:30:30 +08:00
}
private static addCreateUser(app) {
2018-03-31 03:30:30 +08:00
app.put('/api/user',
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
UserMWs.createUser,
RenderingMWs.renderOK
);
2018-03-31 03:30:30 +08:00
}
private static addDeleteUser(app) {
2018-03-31 03:30:30 +08:00
app.delete('/api/user/:id',
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
UserRequestConstrainsMWs.notSelfRequest,
UserMWs.deleteUser,
RenderingMWs.renderOK
);
2018-03-31 03:30:30 +08:00
}
private static addListUsers(app) {
2018-03-31 03:30:30 +08:00
app.get('/api/user/list',
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
UserMWs.listUsers,
RenderingMWs.renderResult
);
2018-03-31 03:30:30 +08:00
}
private static addChangeRole(app) {
2018-03-31 03:30:30 +08:00
app.post('/api/user/:id/role',
AuthenticationMWs.authenticate,
AuthenticationMWs.authorise(UserRoles.Admin),
UserRequestConstrainsMWs.notSelfRequestOr2Admins,
UserMWs.changeRole,
RenderingMWs.renderOK
);
2018-03-31 03:30:30 +08:00
}
}