mirror of
https://github.com/xfarrow/blink
synced 2025-06-27 09:03:02 +02:00
@ -2,116 +2,116 @@
|
||||
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 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
|
||||
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')
|
||||
const knex = require('../utils/knex_config');
|
||||
|
||||
/**
|
||||
* Create Organization object
|
||||
* @param {*} name
|
||||
* @param {*} location
|
||||
* @param {*} description
|
||||
* @param {*} is_hiring
|
||||
* @returns
|
||||
* @param {*} name
|
||||
* @param {*} location
|
||||
* @param {*} description
|
||||
* @param {*} is_hiring
|
||||
* @returns
|
||||
*/
|
||||
function organization (name, location, description, is_hiring) {
|
||||
const organization = {
|
||||
name,
|
||||
location,
|
||||
description,
|
||||
is_hiring
|
||||
}
|
||||
return organization
|
||||
function organization(name, location, description, is_hiring){
|
||||
const organization = {
|
||||
name: name,
|
||||
location: location,
|
||||
description: description,
|
||||
is_hiring: is_hiring
|
||||
};
|
||||
return organization;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets an Organization by its identifier
|
||||
* @param {*} id
|
||||
* @returns
|
||||
* @param {*} id
|
||||
* @returns
|
||||
*/
|
||||
async function getOrganizationById (id) {
|
||||
async function getOrganizationById(id){
|
||||
const organization = await knex('Organization')
|
||||
.where('id', id)
|
||||
.select('*')
|
||||
.first()
|
||||
return organization
|
||||
.first();
|
||||
return organization;
|
||||
}
|
||||
|
||||
/**
|
||||
* Insert an Organization and its relative Administrator
|
||||
* @param {*} organization
|
||||
* @param {*} organization
|
||||
*/
|
||||
async function insertOrganization (organization, organizationAdministratorId) {
|
||||
await knex.transaction(async (trx) => {
|
||||
// We have to insert either both in Organization and in OrganizationAdministrator
|
||||
// or in neither
|
||||
const organizationResult = await trx('Organization')
|
||||
.insert(organization, '*')
|
||||
|
||||
// Inserting in the "OrganizationAdministrator" table
|
||||
await trx('OrganizationAdministrator')
|
||||
.insert({
|
||||
id_person: organizationAdministratorId,
|
||||
id_organization: organizationResult[0].id
|
||||
})
|
||||
})
|
||||
async function insertOrganization(organization, organizationAdministratorId){
|
||||
await knex.transaction(async (trx) => {
|
||||
// We have to insert either both in Organization and in OrganizationAdministrator
|
||||
// or in neither
|
||||
const organizationResult = await trx('Organization')
|
||||
.insert(organization, '*');
|
||||
|
||||
// Inserting in the "OrganizationAdministrator" table
|
||||
await trx('OrganizationAdministrator')
|
||||
.insert({
|
||||
id_person: organizationAdministratorId,
|
||||
id_organization: organizationResult[0].id,
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
* Updates an Organization specified by the OrganizationId, if and
|
||||
* only if the specified personId is one of its Administrator
|
||||
* @param {*} organization
|
||||
* @param {*} organizationId
|
||||
* @param {*} personId
|
||||
* @param {*} organization
|
||||
* @param {*} organizationId
|
||||
* @param {*} personId
|
||||
* @returns true if the row was updated, false otherwise
|
||||
*/
|
||||
async function updateOrganizationIfAdministrator (organization, organizationId, personId) {
|
||||
// // const isOrganizationAdmin = await knex('OrganizationAdministrator')
|
||||
// // .where('id_person', req.jwt.person_id)
|
||||
// // .where('id_organization', req.params.id)
|
||||
// // .select('*')
|
||||
// // .first();
|
||||
|
||||
// // // This introduces a Time of check Time of use weakeness
|
||||
// // // which could'have been fixed by either
|
||||
// // // 1) Using "whereExists", thanks to the "it's easier to ask for
|
||||
// // // forgiveness than for permission" padarigm. Or,
|
||||
// // // 2) Using a serializable transaction.
|
||||
// // //
|
||||
// // // The undersigned chose not to follow these approaches because
|
||||
// // // this does not introduces any serious vulnerability. In this
|
||||
// // // way it seems more readable.
|
||||
|
||||
// // if(!isOrganizationAdmin){
|
||||
// // return res.status(403).json({error : "Forbidden"});
|
||||
// // }
|
||||
|
||||
// // await knex('Organization')
|
||||
// // .where('id', req.params.id)
|
||||
// // .update({
|
||||
// // name: req.body.name,
|
||||
// // location: req.body.location,
|
||||
// // description: req.body.description,
|
||||
// // is_hiring: req.body.is_hiring
|
||||
// // });
|
||||
async function updateOrganizationIfAdministrator(organization, organizationId, personId){
|
||||
// // const isOrganizationAdmin = await knex('OrganizationAdministrator')
|
||||
// // .where('id_person', req.jwt.person_id)
|
||||
// // .where('id_organization', req.params.id)
|
||||
// // .select('*')
|
||||
// // .first();
|
||||
|
||||
// // // This introduces a Time of check Time of use weakeness
|
||||
// // // which could'have been fixed by either
|
||||
// // // 1) Using "whereExists", thanks to the "it's easier to ask for
|
||||
// // // forgiveness than for permission" padarigm. Or,
|
||||
// // // 2) Using a serializable transaction.
|
||||
// // //
|
||||
// // // The undersigned chose not to follow these approaches because
|
||||
// // // this does not introduces any serious vulnerability. In this
|
||||
// // // way it seems more readable.
|
||||
|
||||
// // if(!isOrganizationAdmin){
|
||||
// // return res.status(403).json({error : "Forbidden"});
|
||||
// // }
|
||||
|
||||
// // await knex('Organization')
|
||||
// // .where('id', req.params.id)
|
||||
// // .update({
|
||||
// // name: req.body.name,
|
||||
// // location: req.body.location,
|
||||
// // description: req.body.description,
|
||||
// // is_hiring: req.body.is_hiring
|
||||
// // });
|
||||
|
||||
const numberOfUpdatedRows = await knex('Organization')
|
||||
.where('id', organizationId)
|
||||
.whereExists(function () {
|
||||
.whereExists(function(){
|
||||
this.select('*')
|
||||
.from('OrganizationAdministrator')
|
||||
.where('id_person', personId)
|
||||
.where('id_organization', organizationId)
|
||||
})
|
||||
.update(organization)
|
||||
return numberOfUpdatedRows == 1
|
||||
.update(organization);
|
||||
return numberOfUpdatedRows == 1;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -121,17 +121,17 @@ async function updateOrganizationIfAdministrator (organization, organizationId,
|
||||
* @param {*} personId PersonId of the supposedly administrator
|
||||
* @returns true if the Organization was successfully deleted, false otherwise
|
||||
*/
|
||||
async function deleteOrganizationIfAdmin (organizationId, personId) {
|
||||
async function deleteOrganizationIfAdmin(organizationId, personId){
|
||||
const numberOfDeletedRows = await knex('Organization')
|
||||
.where({ id: organizationId })
|
||||
.whereExists(function () {
|
||||
.whereExists(function(){
|
||||
this.select('*')
|
||||
.from('OrganizationAdministrator')
|
||||
.where('id_person', personId)
|
||||
.where('id_organization', organizationId)
|
||||
})
|
||||
.del()
|
||||
return numberOfDeletedRows == 1
|
||||
.del();
|
||||
return numberOfDeletedRows == 1;
|
||||
}
|
||||
|
||||
// Exporting a function
|
||||
@ -144,4 +144,4 @@ module.exports = {
|
||||
updateOrganizationIfAdministrator,
|
||||
updateOrganizationIfAdministrator,
|
||||
deleteOrganizationIfAdmin
|
||||
}
|
||||
};
|
@ -2,40 +2,40 @@
|
||||
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 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
|
||||
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')
|
||||
const bcrypt = require('bcrypt')
|
||||
const knex = require('../utils/knex_config');
|
||||
const bcrypt = require('bcrypt');
|
||||
|
||||
/**
|
||||
* Creates Person object by the specified fields
|
||||
* @param {*} email
|
||||
* @param {*} password
|
||||
* @param {*} display_name
|
||||
* @param {*} date_of_birth
|
||||
* @param {*} available
|
||||
* @param {*} enabled
|
||||
* @param {*} place_of_living
|
||||
* @returns
|
||||
* @param {*} email
|
||||
* @param {*} password
|
||||
* @param {*} display_name
|
||||
* @param {*} date_of_birth
|
||||
* @param {*} available
|
||||
* @param {*} enabled
|
||||
* @param {*} place_of_living
|
||||
* @returns
|
||||
*/
|
||||
function person (email, password, display_name, date_of_birth, available, enabled, place_of_living) {
|
||||
const person = {
|
||||
email: email.toLowerCase(),
|
||||
password,
|
||||
display_name,
|
||||
date_of_birth,
|
||||
available,
|
||||
enabled,
|
||||
place_of_living
|
||||
}
|
||||
return person
|
||||
function person(email, password, display_name, date_of_birth, available, enabled, place_of_living) {
|
||||
const person = {
|
||||
email: email.toLowerCase(),
|
||||
password: password,
|
||||
display_name: display_name,
|
||||
date_of_birth: date_of_birth,
|
||||
available: available,
|
||||
enabled: enabled,
|
||||
place_of_living: place_of_living
|
||||
};
|
||||
return person;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -43,22 +43,22 @@ function person (email, password, display_name, date_of_birth, available, enable
|
||||
* @param {*} email email to look the Person for
|
||||
* @returns the Person object
|
||||
*/
|
||||
async function getPersonByEmail (email) {
|
||||
return await knex('Person')
|
||||
.where('email', email.toLowerCase())
|
||||
.first()
|
||||
async function getPersonByEmail(email){
|
||||
return await knex('Person')
|
||||
.where('email', email.toLowerCase())
|
||||
.first();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get Person by Id
|
||||
* @param {*} id - The id to look the person for
|
||||
* @returns
|
||||
* @returns
|
||||
*/
|
||||
async function getPersonById (id) {
|
||||
return await knex('Person')
|
||||
.select('*')
|
||||
.where({ id })
|
||||
.first()
|
||||
async function getPersonById(id){
|
||||
return await knex('Person')
|
||||
.select('*')
|
||||
.where({ id: id })
|
||||
.first();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -67,27 +67,27 @@ async function getPersonById (id) {
|
||||
* @param {*} person A Person object
|
||||
* @param {*} activationLink the activationLink identifier
|
||||
*/
|
||||
async function registerPerson (person, activationLink) {
|
||||
// We need to insert either both in the "Person" table
|
||||
// and in the "ActivationLink" one, or in neither
|
||||
await knex.transaction(async (tr) => {
|
||||
const personIdResult = await tr('Person')
|
||||
.insert({
|
||||
email: person.email.toLowerCase(),
|
||||
password: person.password,
|
||||
display_name: person.display_name,
|
||||
date_of_birth: person.date_of_birth,
|
||||
available: person.available,
|
||||
enabled: person.enabled,
|
||||
place_of_living: person.place_of_living
|
||||
})
|
||||
.returning('id')
|
||||
await tr('ActivationLink')
|
||||
.insert({
|
||||
person_id: personIdResult[0].id,
|
||||
identifier: activationLink
|
||||
})
|
||||
})
|
||||
async function registerPerson(person, activationLink){
|
||||
// We need to insert either both in the "Person" table
|
||||
// and in the "ActivationLink" one, or in neither
|
||||
await knex.transaction(async (tr) => {
|
||||
const personIdResult = await tr('Person')
|
||||
.insert({
|
||||
email: person.email.toLowerCase(),
|
||||
password: person.password,
|
||||
display_name: person.display_name,
|
||||
date_of_birth: person.date_of_birth,
|
||||
available: person.available,
|
||||
enabled: person.enabled,
|
||||
place_of_living: person.place_of_living
|
||||
})
|
||||
.returning("id");
|
||||
await tr('ActivationLink')
|
||||
.insert({
|
||||
person_id: personIdResult[0].id,
|
||||
identifier: activationLink
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
/**
|
||||
@ -95,22 +95,22 @@ async function registerPerson (person, activationLink) {
|
||||
* Used for log-in
|
||||
* @param {*} email
|
||||
* @param {*} password
|
||||
* @returns
|
||||
* @returns
|
||||
*/
|
||||
async function getPersonByEmailAndPassword (email, password) {
|
||||
const person = await knex('Person')
|
||||
.where('email', email.toLowerCase())
|
||||
.where('enabled', true)
|
||||
.select('*')
|
||||
.first()
|
||||
async function getPersonByEmailAndPassword(email, password){
|
||||
const person = await knex('Person')
|
||||
.where('email', email.toLowerCase())
|
||||
.where('enabled', true)
|
||||
.select('*')
|
||||
.first();
|
||||
|
||||
if (person) {
|
||||
const passwordMatches = await bcrypt.compare(password, person.password)
|
||||
if (passwordMatches) {
|
||||
return person
|
||||
if(person){
|
||||
const passwordMatches = await bcrypt.compare(password, person.password);
|
||||
if (passwordMatches) {
|
||||
return person;
|
||||
}
|
||||
}
|
||||
}
|
||||
return null
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -118,31 +118,32 @@ async function getPersonByEmailAndPassword (email, password) {
|
||||
* @param {*} person The Person to update
|
||||
* @param {*} person_id The database id of the Person to update
|
||||
*/
|
||||
async function updatePerson (person, person_id) {
|
||||
await knex('Person')
|
||||
.where('id', person_id)
|
||||
.update(person)
|
||||
async function updatePerson(person, person_id){
|
||||
await knex('Person')
|
||||
.where('id', person_id)
|
||||
.update(person);
|
||||
}
|
||||
|
||||
/**
|
||||
* Deletes a Person specified by its database id.
|
||||
* @param {*} person_id
|
||||
* @param {*} person_id
|
||||
*/
|
||||
async function deletePerson (person_id) {
|
||||
await knex('Person')
|
||||
.where({ id: person_id })
|
||||
.del()
|
||||
async function deletePerson(person_id){
|
||||
await knex('Person')
|
||||
.where({id : person_id})
|
||||
.del();
|
||||
}
|
||||
|
||||
|
||||
// Exporting a function
|
||||
// means making a JavaScript function defined in one
|
||||
// module available for use in another module.
|
||||
module.exports = {
|
||||
person,
|
||||
getPersonByEmail,
|
||||
getPersonById,
|
||||
getPersonByEmailAndPassword,
|
||||
registerPerson,
|
||||
updatePerson,
|
||||
deletePerson
|
||||
}
|
||||
person,
|
||||
getPersonByEmail,
|
||||
getPersonById,
|
||||
getPersonByEmailAndPassword,
|
||||
registerPerson,
|
||||
updatePerson,
|
||||
deletePerson
|
||||
};
|
Reference in New Issue
Block a user