Skip to content
This repository has been archived by the owner on Jun 6, 2024. It is now read-only.

API to get groups's userlist and add or remove an user from a group #3003

Merged
merged 6 commits into from
Jun 26, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions src/rest-server/src/config/v2/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,14 @@ const userAdminPermissionUpdateInputSchema = Joi.object().keys({
admin: Joi.boolean().required(),
});

// define the input schema for the 'add or remove group from grouplist' api
const addOrRemoveGroupInputSchema = Joi.object().keys({
groupname: Joi.string()
.token()
.required(),
});


// define the input schema for the 'create user' api
const userCreateInputSchema = Joi.object().keys({
username: Joi.string()
Expand Down Expand Up @@ -79,4 +87,5 @@ module.exports = {
userEmailUpdateInputSchema,
userCreateInputSchema,
userAdminPermissionUpdateInputSchema,
addOrRemoveGroupInputSchema,
};
25 changes: 25 additions & 0 deletions src/rest-server/src/controllers/v2/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
// module dependencies
const createError = require('@pai/utils/error');
const groupModel = require('@pai/models/v2/group');
const userModel = require('@pai/models/v2/user');
const authConfig = require('@pai/config/authn');

const getGroup = async (req, res, next) => {
try {
Expand All @@ -38,6 +40,28 @@ const getAllGroup = async (req, res, next) => {
}
};

const getGroupUserList = async (req, res, next) => {
try {
if (!req.user.admin) {
next(createError('Forbidden', 'ForbiddenUserError', `Non-admin is not allow to do this operation.`));
}
const groupname = req.params.groupname;
const allUserInfoList = await userModel.getAllUser();
let userlist = [];
for (const userInfo of allUserInfoList) {
if (userInfo.grouplist.includes(groupname)) {
userlist.push({
username: userInfo.username,
clusterAdmin: userInfo.grouplist.includes(authConfig.groupConfig.adminGroup.groupname),
});
}
}
return res.status(200).json(userlist);
} catch (error) {
return next(createError.unknown(error));
}
};

const createGroup = async (req, res, next) => {
try {
const groupname = req.body.groupname;
Expand Down Expand Up @@ -136,6 +160,7 @@ module.exports = {
getGroup,
getAllGroup,
createGroup,
getGroupUserList,
updateGroupExtension,
updateGroupDescription,
updateGroupExternalName,
Expand Down
54 changes: 54 additions & 0 deletions src/rest-server/src/controllers/v2/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,58 @@ const updateUserGroupList = async (req, res, next) => {
}
};

const addGroupIntoUserGrouplist = async (req, res, next) => {
try {
if (!req.user.admin) {
next(createError('Forbidden', 'ForbiddenUserError', `Non-admin is not allow to do this operation.`));
}
const username = req.params.username;
const groupname = req.body.groupname;
let userInfo = await userModel.getUser(username);
if (userInfo.grouplist.includes(authConfig.groupConfig.adminGroup.groupname)) {
return next(createError('Forbidden', 'ForbiddenUserError', 'Admin\'s grouplist cannot be updated.'));
}
if (!userInfo.grouplist.includes(groupname)) {
userInfo.grouplist.push(groupname);
}
await userModel.updateUser(username, userInfo);
return res.status(201).json({
message: `User ${username} is added into group ${groupname}`,
});
} catch (error) {
if (error.status === 404) {
return next(createError('Not found', 'NoUserError', `User ${req.params.username} not found.`));
}
return next(createError.unknown(error));
}
};

const removeGroupIntoUserGrouplist = async (req, res, next) => {
try {
if (!req.user.admin) {
next(createError('Forbidden', 'ForbiddenUserError', `Non-admin is not allow to do this operation.`));
}
const username = req.params.username;
const groupname = req.body.groupname;
let userInfo = await userModel.getUser(username);
if (userInfo.grouplist.includes(authConfig.groupConfig.adminGroup.groupname)) {
return next(createError('Forbidden', 'ForbiddenUserError', 'Admin\'s grouplist cannot be updated.'));
}
if (userInfo.grouplist.includes(groupname)) {
userInfo.grouplist.splice(userInfo.grouplist.indexOf(groupname), 1);
}
await userModel.updateUser(username, userInfo);
return res.status(201).json({
message: `User ${username} is removed from group ${groupname}`,
});
} catch (error) {
if (error.status === 404) {
return next(createError('Not found', 'NoUserError', `User ${req.params.username} not found.`));
}
return next(createError.unknown(error));
}
};

const updateUserPassword = async (req, res, next) => {
try {
const username = req.params.username;
Expand Down Expand Up @@ -396,6 +448,8 @@ module.exports = {
updateUserGroupList,
updateUserEmail,
updateUserAdminPermission,
addGroupIntoUserGrouplist,
removeGroupIntoUserGrouplist,
deleteUser,
updateUserPassword,
createUser,
Expand Down
4 changes: 4 additions & 0 deletions src/rest-server/src/routes/v2/group.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,10 @@ router.route('/update/:groupname/externalname')
/** put /api/v2/group/update/:groupname/external' */
.put(token.check, param.validate(groupInputSchema.groupExternalNameUpdateInputSchema), groupController.updateGroupExternalName);

router.route('/get/:groupname/userlist')
/** get /api/v2/group/get/:groupname/userlist */
.get(token.check, groupController.getGroupUserList);

module.exports = router;


6 changes: 6 additions & 0 deletions src/rest-server/src/routes/v2/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,12 @@ if (authnConfig.authnMethod === 'basic') {

router.route('/:username/grouplist')
.put(token.check, param.validate(userInputSchema.userGrouplistUpdateInputSchema), userController.updateUserGroupList);

router.route('/:username/group')
.put(token.check, param.validate(userInputSchema.addOrRemoveGroupInputSchema), userController.addGroupIntoUserGrouplist);

router.route('/:username/group')
.delete(token.check, param.validate(userInputSchema.addOrRemoveGroupInputSchema), userController.removeGroupIntoUserGrouplist);
}

router.use('/:username/jobs', jobRouter);
Expand Down
2 changes: 1 addition & 1 deletion src/webportal/src/app/home/home/virtual-cluster-list.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ VirtualClusterItem.propTypes = {
};

const VirtualCluster = ({style, userInfo, virtualClusters}) => {
const vcNames = userInfo.virtualCluster.split(',').filter((name) => !isNil(virtualClusters[name]));
const vcNames = userInfo.virtualCluster.filter((name) => !isNil(virtualClusters[name]));
const {spacing} = getTheme();
return (
<Card style={{paddingRight: spacing.m, ...style}}>
Expand Down
4 changes: 3 additions & 1 deletion src/webportal/src/app/vc/vc.component.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,10 +171,12 @@ const addGroup = () => {
return false;
}
$.ajax({
url: `${webportalConfig.restServerUri}/api/v2/user/create/${vcName}`,
url: `${webportalConfig.restServerUri}/api/v2/group/create`,
data: JSON.stringify({
'groupname': vcName,
'description': `This group of the same name is created by creating a Virtual Cluster named ${vcName}`,
'externalName': ``,
'extension': `{"groupType": "vc"}`,
}),
headers: {
Authorization: `Bearer ${token}`,
Expand Down