Skip to content
4 changes: 2 additions & 2 deletions internal/testing/fake/election.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,8 @@ func NewForm(formID string) types.Form {
MainTitle: "dummyTitle",
},
FormID: formID,
Status: types.Closed,
Pubkey: pubKey,
Status: types.Closed,
Pubkey: pubKey,
Suffragia: types.Suffragia{
Ciphervotes: []types.Ciphervote{},
},
Expand Down
31 changes: 27 additions & 4 deletions web/backend/src/Server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -216,10 +216,33 @@ app.post('/api/add_role', (req, res) => {
const { sciper } = req.body;
const { role } = req.body;

usersDB.put(sciper, role).catch((error) => {
res.status(500).send('Failed to add role');
console.log(error);
});
// The sciper has to contain 6 numbers
if (sciper > 999999 || sciper < 100000) {
res.status(400).send('Sciper length is incorrect');
return;
}

// Call https://search-api.epfl.ch/api/ldap?q=228271, if the answer is
// empty then sciper unknown, otherwise add it in userDB
axios
.get(`https://search-api.epfl.ch/api/ldap?q=${sciper}`)
.then((response) => {
if (response.data.length === 0) {
res.status(400).send('Unknown Sciper');
return;
}

usersDB.put(sciper, role).catch((error) => {
res.status(500).send('Failed to add role');
console.log(error);
});

res.status(200).send('Role added');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this will also execute in the case of the catch above, don't you want to put in a then (before the catch) ?

})
.catch((error) => {
res.status(500).send('Failed to check Sciper');
console.log(error);
});
});

// This call (only for admins) allow an admin to remove a role to a user.
Expand Down