-
-
Notifications
You must be signed in to change notification settings - Fork 907
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Error handling & security updates (#744)
* [Feature Request] New Features for Talawa-admin added (#725) * check authentication feature added for talawa admin * tests for check authentication added * minor fixes * typo fixes * minor fixes * npm err fixes * google recaptcha configured * new dependency added * mailer, forgotPassword endpoint and otp endpoint is configured * graphql endpoints updated * constants updated * Documentation updated * tests for changes added * hard coded value error fixes * lint fixes * lint fixes * failed test cases fixes * Failed tests fixes * minor fixes * marked-teminal dependency updated * Tests fixes * tests updated * tests fixes * tests updated * new resolvers added and schema updated * graphql mutations updated * tests updated for changes * tests updated * tests updated * tests updated * minor fixes * [Feature Request] : Plugin Architecture for Server (#730) * Test : lib/resolvers/group_chat_query/groupChatMessages.js * Add Test : Added Valid JSON Check * Create README.md * Create README.md * Update is-auth.js * AddedDocs Docs from withfilter pending * Update index.js * completed * sample * update/index.js * update/index.js * update/is-auth.js * Update/readme.md * update/image-readme * Add/query-schema * Update/Plugin-graphQL-schema * Add/Plugin-MongoDB-Model * Add/createPlugin-and-refractoring * Add/getPlugins * Add/Mutation/UpdatePluginStatus * Add/Mutation/updateTempPluginInstalledOrgs * Add/Mutation/Schema * Add/import/mutations * Add/plugins * Fix/path-err * Fix/Erros * update/lockfile * Add/plugin-model * Update is-auth.js * Removed extra queries * Documentation added for plugin queries and models * Fix/`delelte cr in prettier/prettier` * updated : mutation * Add/test `getPlugins` * Fix/`lint error` * test `updatePluginInstalledOrgs` * Test/ `updateInstallStatus` * Fix/Erros-1 * Fix/Erros-2 * Fix/Erros-3 * Test/`Queries.js` * Test/`Mutation.js` * remove extra `console.log` * Delete admin-plugin-query.js * Delete super-admin-plugin-query.js * Fix/Package-lock * added delimiter to helper functions * error handling for connection manager * added readme.md for connection manager * error handling for Database module Co-authored-by: Asmit Kumar Sirohi <[email protected]> Co-authored-by: Siddhesh Bhupendra Kuakde <[email protected]>
- Loading branch information
1 parent
dbb8271
commit b4455c1
Showing
58 changed files
with
1,380 additions
and
275 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
{ | ||
"singleQuote": true | ||
"singleQuote": true, | ||
"endOfLine": "auto" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Empty file.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
# Connection Manager Module | ||
|
||
This is the module that is responsible for connecting/disconnecting to each tenant's database. | ||
|
||
### Prototype : | ||
|
||
`addTenantConnection(organizationId)` | ||
|
||
`getTenantConnection(organizationId)` | ||
|
||
`initTenants()` | ||
|
||
`destroyConnections()` | ||
|
||
`addTenantConnection` is responsible for connecting to the database of a specific organization `organizationId` | ||
and returning a reference to the connection object if the organization is not found it will throw an organization not found error. | ||
|
||
`getTenantConnection` is responsible for retrivnig a connection to `organizationId` which already is in memory if it's not the function will throw a connection not found error. | ||
|
||
`initTenants` is usually used on the startup of the api which connects to all the available tenants that are stored. | ||
|
||
`destoryConnections` is used to close the connections and deleting them from memory. | ||
|
||
### Basic usage : | ||
|
||
This is mostly how it's going to be used: | ||
|
||
```javascript | ||
const connection = getTenantConnection(organizationId); | ||
const Post = connection.Post; | ||
|
||
// posts is an array of the posts stored in that specific tenant. | ||
const posts = await Post.find({}); | ||
// to close connections: | ||
await destroyConnections(); | ||
// would throw an error: | ||
const con = getTenantConnection(organizationId); | ||
``` | ||
|
||
### This is the main way to manage the databases that are devided into 2 types: | ||
|
||
##### main database: | ||
|
||
which holds connections, organizations, users, files data (shared data). | ||
|
||
##### tenant databases; | ||
|
||
which holds organization specific data such as posts, events, comments... |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,19 +1,29 @@ | ||
const Tenant = require('../models/Tenant'); | ||
const Database = require('../Database/index'); | ||
const { setConnection, getConnection } = require('./connections'); | ||
const { setConnection } = require('./connections'); | ||
const { NotFoundError } = require('errors'); | ||
const requestContext = require('talawa-request-context'); | ||
const { | ||
ORGANIZATION_NOT_FOUND, | ||
ORGANIZATION_NOT_FOUND_MESSAGE, | ||
ORGANIZATION_NOT_FOUND_PARAM, | ||
ORGANIZATION_NOT_FOUND_CODE, | ||
IN_PRODUCTION, | ||
} = require('../../constants'); | ||
|
||
module.exports = async (organizationId) => { | ||
try { | ||
const alreadyConnected = getConnection(organizationId); | ||
if (alreadyConnected) return alreadyConnected; | ||
const [tenant] = await Tenant.find({ organization: organizationId }); | ||
if (!tenant || !tenant.url) throw new Error('Organization not found!'); | ||
console.log('tenant', tenant); | ||
let connection = new Database(tenant.url); | ||
await connection.connect(); | ||
setConnection(tenant.organization, connection); | ||
return connection; | ||
} catch (e) { | ||
console.log('organization not found!'); | ||
const [tenant] = await Tenant.find({ organization: organizationId }); | ||
if (!tenant || !tenant.url) { | ||
throw new NotFoundError( | ||
!IN_PRODUCTION | ||
? ORGANIZATION_NOT_FOUND | ||
: requestContext.translate(ORGANIZATION_NOT_FOUND_MESSAGE), | ||
ORGANIZATION_NOT_FOUND_CODE, | ||
ORGANIZATION_NOT_FOUND_PARAM | ||
); | ||
} | ||
let connection = new Database(tenant.url); | ||
await connection.connect(); | ||
setConnection(tenant.organization, connection); | ||
return connection; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
const connections = require('./connections'); | ||
|
||
module.exports = async () => { | ||
try { | ||
await connections.destroy(); | ||
} catch (e) { | ||
console.log(e); | ||
} | ||
await connections.destroy(); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,5 @@ | ||
const { getConnection } = require('./connections'); | ||
|
||
module.exports = (organizationId) => { | ||
try { | ||
return getConnection(organizationId); | ||
} catch (e) { | ||
console.log('organization not found!'); | ||
} | ||
return getConnection(organizationId); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
module.exports = (tenantId, id) => { | ||
return tenantId + ' ' + id; | ||
module.exports = (tenantId, id, del = ' ') => { | ||
return tenantId + del + id; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
module.exports = (fullId) => { | ||
const [tenantId, id] = fullId.split(' '); | ||
module.exports = (fullId, del = ' ') => { | ||
const [tenantId, id] = fullId.split(del); | ||
return { tenantId, id }; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
const nodemailer = require('nodemailer'); | ||
|
||
const { ERROR_IN_SENDING_MAIL } = require('../../constants'); | ||
|
||
const mailer = (email, subject, body) => { | ||
//NODEMAILER SPECIFIC STUFF | ||
let transporter = nodemailer.createTransport({ | ||
service: 'gmail', | ||
auth: { | ||
user: process.env.MAIL_USERNAME, | ||
pass: process.env.MAIL_PASSWORD, | ||
}, | ||
}); | ||
|
||
let mailOptions = { | ||
from: 'Talawa<>[email protected]', | ||
to: email, | ||
subject: subject, | ||
html: body, | ||
}; | ||
|
||
return new Promise((resolve, reject) => { | ||
transporter.sendMail(mailOptions, function (err, info) { | ||
if (err) { | ||
reject(ERROR_IN_SENDING_MAIL); | ||
} else { | ||
resolve(info); | ||
} | ||
}); | ||
}); | ||
}; | ||
|
||
module.exports = mailer; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -77,5 +77,4 @@ const isAuth = (req) => { | |
userId, | ||
}; | ||
}; | ||
|
||
module.exports = isAuth; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.