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-03-20 00:31:42 +08:00
|
|
|
|
2016-05-09 23:04:56 +08:00
|
|
|
export class UserRouter {
|
2017-06-11 04:32:56 +08:00
|
|
|
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',
|
2017-06-11 04:32:56 +08:00
|
|
|
AuthenticationMWs.inverseAuthenticate,
|
|
|
|
AuthenticationMWs.login,
|
|
|
|
RenderingMWs.renderSessionUser
|
|
|
|
);
|
2018-03-31 03:30:30 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
private static addLogout(app) {
|
2018-03-31 03:30:30 +08:00
|
|
|
app.post('/api/user/logout',
|
2017-06-11 04:32:56 +08:00
|
|
|
AuthenticationMWs.logout,
|
|
|
|
RenderingMWs.renderOK
|
|
|
|
);
|
2018-03-31 03:30:30 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
private static addGetSessionUser(app) {
|
2018-03-31 03:30:30 +08:00
|
|
|
app.get('/api/user/login',
|
2017-06-11 04:32:56 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
RenderingMWs.renderSessionUser
|
|
|
|
);
|
2018-03-31 03:30:30 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
private static addChangePassword(app) {
|
2018-03-31 03:30:30 +08:00
|
|
|
app.post('/api/user/:id/password',
|
2017-06-11 04:32:56 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
UserRequestConstrainsMWs.forceSelfRequest,
|
|
|
|
UserMWs.changePassword,
|
|
|
|
RenderingMWs.renderOK
|
|
|
|
);
|
2018-03-31 03:30:30 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
private static addCreateUser(app) {
|
2018-03-31 03:30:30 +08:00
|
|
|
app.put('/api/user',
|
2017-06-11 04:32:56 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
|
|
|
UserMWs.createUser,
|
|
|
|
RenderingMWs.renderOK
|
|
|
|
);
|
2018-03-31 03:30:30 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
private static addDeleteUser(app) {
|
2018-03-31 03:30:30 +08:00
|
|
|
app.delete('/api/user/:id',
|
2017-06-11 04:32:56 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
|
|
|
UserRequestConstrainsMWs.notSelfRequest,
|
|
|
|
UserMWs.deleteUser,
|
|
|
|
RenderingMWs.renderOK
|
|
|
|
);
|
2018-03-31 03:30:30 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
private static addListUsers(app) {
|
2018-03-31 03:30:30 +08:00
|
|
|
app.get('/api/user/list',
|
2017-06-11 04:32:56 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
|
|
|
UserMWs.listUsers,
|
|
|
|
RenderingMWs.renderResult
|
|
|
|
);
|
2018-03-31 03:30:30 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
private static addChangeRole(app) {
|
2018-03-31 03:30:30 +08:00
|
|
|
app.post('/api/user/:id/role',
|
2017-06-11 04:32:56 +08:00
|
|
|
AuthenticationMWs.authenticate,
|
|
|
|
AuthenticationMWs.authorise(UserRoles.Admin),
|
|
|
|
UserRequestConstrainsMWs.notSelfRequestOr2Admins,
|
|
|
|
UserMWs.changeRole,
|
|
|
|
RenderingMWs.renderOK
|
|
|
|
);
|
2018-03-31 03:30:30 +08:00
|
|
|
}
|
2017-06-11 04:32:56 +08:00
|
|
|
|
|
|
|
|
|
|
|
}
|