Skip to content

Commit

Permalink
feat: add user editing
Browse files Browse the repository at this point in the history
  • Loading branch information
linuxbandit committed Oct 21, 2020
1 parent 999554d commit 8ce5b6b
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
45 changes: 43 additions & 2 deletions lib/google-suite.js
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,49 @@ const gsuiteOperations = {
},

// Edit account e.g. change pic, change pw (until we have SSO)
editAccount: async function addAccount(jwt, data){
throw GsuiteError;
// Assumption: at least one of pic or anything else MUST be specified
editAccount: async function editAccount(jwt, data){
const admin = google.admin('directory_v1');

const userKey = data.userKey;
delete data.userKey;

const photo = data.photoData || null;
delete data.photoData;

let result = { "data": [], "code_photo": null, "code_update": null };

if(Object.keys(data).length > 0){
const result_update = await admin.users.update({
userKey: userKey,
requestBody: data,
auth: jwt
});

result.data.push(result_update.data);
result.code_update=result_update.status;
}

if(photo){
const result_photo = await admin.users.photos.update({
userKey: userKey,
resource: {
photoData: photo
},
auth: jwt
});

result.data.push(result_photo.data);
result.code_photo=result_photo.status;
}

if (result.code_update && result.code_photo){
result.status = result.code_update != result.code_photo ? '207' : result.code_update;
}else{
result.status = result.code_photo ? result.code_photo : result.code_update;
}

return result;
},

// List user accounts present in the system (only for the initial sync script)
Expand Down
54 changes: 54 additions & 0 deletions lib/gsuite-wrapper.js
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,60 @@ exports.createAccount = async function(req, res , next) {
return res.status(statusCode).json(response);
};

exports.editAccount = async function(req, res , next) {
log.debug(req.headers['test-title']);

const personPK = req.params.userPK;
const data = req.body;

let response = {success: false, message: "Undefined error"};
let statusCode = 500;

if( !personPK || Object.keys(data).length === 0 ){

response.message = "Validation error: the primary key or payload is absent or empty";
statusCode = 400;

}else{

const removeEmpty = (obj) => {
Object.keys(obj).forEach((key) => (obj[key] == null) && delete obj[key]);
return obj;
}

const payload = removeEmpty(Object.assign(
{},
data,
{"hashFunction": data.password ? "SHA-1" : null},
{"emails": data.secondaryEmail ? [
{
"address": data.secondaryEmail,
"type": "home",
"customType": "",
"primary": true
}
] : null},
{"organizations": data.antennae ? [ { "department": data.antennae.toString() } ] : null}
))

if ( Object.keys(payload).length > 0 ){

payload.userKey = personPK;

try{
let result = await runGsuiteOperation(gsuiteOperations.editAccount, payload);
response = {success: result.success, message: result.data[0].primaryEmail+" account has been updated", data: result.data };
statusCode = result.code;
}catch(GsuiteError){
log.warn("GsuiteError");
response = {success: false, errors: GsuiteError.errors, message: GsuiteError.errors[0].message };
statusCode = GsuiteError.code;
}
}
}

return res.status(statusCode).json(response);
};
//Possible values for data.operation: add|remove|upgrade|downgrade
exports.editMembershipToGroup = async function(req, res , next) {
log.debug(req.headers['test-title']);
Expand Down
1 change: 1 addition & 0 deletions lib/server.js
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ GsuiteRouter.delete('/group/:bodyPK', wrapper.deleteGroup); //body is deleted ->
GsuiteRouter.post('/account', wrapper.createAccount); //member is created -> create an account
GsuiteRouter.put('/account/:userPK/alias', wrapper.updateAlias); //user may need an alias (netcom-xxx@aegee.eu)
GsuiteRouter.get('/account/:userPK/alias', wrapper.getAliasFromRedis); //user can read their alias (netcom-xxx@aegee.eu)
GsuiteRouter.put('/account/:userPK', wrapper.editAccount); //change pic, password, suspend (not delete)

GsuiteRouter.post('/calendar', wrapper.createCalEvent); //event is accepted by EQAC -> put in calendar of events

Expand Down

0 comments on commit 8ce5b6b

Please sign in to comment.