From c1ba83d52e789c1fc484b410f15b33dca466f1a1 Mon Sep 17 00:00:00 2001 From: xfarrow Date: Mon, 4 Mar 2024 10:37:54 +0100 Subject: [PATCH] Activate Person by its activation link --- backend/apis/nodejs/src/app.js | 1 + .../nodejs/src/models/activation_model.js | 33 +++++++++++++++++++ .../apis/nodejs/src/models/person_model.js | 13 ++++++-- .../apis/nodejs/src/routes/person_routes.js | 26 +++++++++++++-- 4 files changed, 68 insertions(+), 5 deletions(-) create mode 100644 backend/apis/nodejs/src/models/activation_model.js diff --git a/backend/apis/nodejs/src/app.js b/backend/apis/nodejs/src/app.js index 45aaf11..19f50f0 100644 --- a/backend/apis/nodejs/src/app.js +++ b/backend/apis/nodejs/src/app.js @@ -56,6 +56,7 @@ publicRoutes.post('/register', personRoutes.registerPerson); publicRoutes.post('/login', personRoutes.login); publicRoutes.get('/person/:id/details', personRoutes.getPerson); publicRoutes.get('/organization/:id', organizationRoutes.getOrganization); +publicRoutes.post('/person/activation', personRoutes.enablePersonByActivationLink); const protectedRoutes = express.Router(); protectedRoutes.use(jwtUtils.verifyToken); diff --git a/backend/apis/nodejs/src/models/activation_model.js b/backend/apis/nodejs/src/models/activation_model.js new file mode 100644 index 0000000..a0afb5a --- /dev/null +++ b/backend/apis/nodejs/src/models/activation_model.js @@ -0,0 +1,33 @@ +/* + This code is part of Blink + licensed under GPLv3 + + THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR + IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, + FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL + THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER + LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING + FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS + IN THE SOFTWARE. +*/ + +const knex = require('../utils/knex_config'); + +/** + * Get a Person's ID by its activation identifier + * @param {*} identifier + * @returns + */ +async function getPersonIdByIdentifier (identifier){ + const tuple = await knex('ActivationLink') + .where('identifier', identifier) + .first(); + if(tuple){ + return tuple.person_id; + } + return null; +} + +module.exports = { + getPersonIdByIdentifier +}; \ No newline at end of file diff --git a/backend/apis/nodejs/src/models/person_model.js b/backend/apis/nodejs/src/models/person_model.js index 9909780..c9558b4 100644 --- a/backend/apis/nodejs/src/models/person_model.js +++ b/backend/apis/nodejs/src/models/person_model.js @@ -128,12 +128,18 @@ async function updatePerson (person, person_id) { * Deletes a Person specified by its database id. * @param {*} person_id */ -async function deletePerson (person_id) { +async function deletePerson (personId) { await knex('Person') - .where({ id: person_id }) + .where({ id: personId }) .del(); } +async function enablePerson (personId) { + await knex('Person') + .where('id', personId) + .update({enabled: true}); +} + // Exporting a function // means making a JavaScript function defined in one // module available for use in another module. @@ -144,5 +150,6 @@ module.exports = { getPersonByEmailAndPassword, registerPerson, updatePerson, - deletePerson + deletePerson, + enablePerson }; diff --git a/backend/apis/nodejs/src/routes/person_routes.js b/backend/apis/nodejs/src/routes/person_routes.js index cb6cd18..f9688fd 100644 --- a/backend/apis/nodejs/src/routes/person_routes.js +++ b/backend/apis/nodejs/src/routes/person_routes.js @@ -16,7 +16,7 @@ const jwtUtils = require('../utils/middleware_utils'); const bcrypt = require('bcrypt'); const crypto = require('crypto'); const personModel = require('../models/person_model'); - +const activationModel = require('../models/activation_model'); /** * POST Request * @@ -225,6 +225,27 @@ async function deletePerson (req, res) { } } +/** + * POST Request + * + * Enable a Person object by its activation identifier + * + * Required field(s): identifier + */ +async function enablePersonByActivationLink(req, res){ + try { + const personId = await activationModel.getPersonIdByIdentifier(req.body.identifier); + if(!personId){ + return res.status(401).json({error: 'Activation Link either not valid or expired'}); + } + await personModel.enablePerson(personId); + return res.status(200).json({ success: true }); + } catch (error) { + console.error(`Error in function ${enablePersonByActivationLink.name}: ${error}`); + return res.status(500).json({ error: 'Internal server error' }); + } +} + // Exporting a function // means making a JavaScript function defined in one // module available for use in another module. @@ -234,5 +255,6 @@ module.exports = { getPerson, getMyself, updatePerson, - deletePerson + deletePerson, + enablePersonByActivationLink };