@orbiting/backend-modules-auth
Advanced tools
Comparing version 0.6.1 to 0.7.0
@@ -1,6 +0,21 @@ | ||
<a name="0.6.1"></a> | ||
## 0.6.1 (2017-11-30) | ||
<a name="0.7.0"></a> | ||
# 0.7.0 (2017-12-04) | ||
<a name="0.7.0"></a> | ||
# 0.7.0 (2017-12-04) | ||
### Bug Fixes | ||
* **backend-modules-auth:** simplify code ([786b1cb](https://github.com/orbiting/backend-modules/commit/786b1cb)) | ||
### Features | ||
* **backend-modules-auth:** extend lib/Roles by userIsInRoles and ensureUserIsInRoles ([256e65d](https://github.com/orbiting/backend-modules/commit/256e65d)) | ||
<a name="0.6.1"></a> | ||
@@ -7,0 +22,0 @@ ## 0.6.1 (2017-11-30) |
@@ -6,3 +6,2 @@ const t = require('./t') | ||
] | ||
exports.roles = roles | ||
@@ -12,3 +11,2 @@ const userHasRole = (user, role) => { | ||
} | ||
exports.userHasRole = userHasRole | ||
@@ -22,7 +20,34 @@ const ensureUserHasRole = (user, role) => { | ||
console.info('unauthorized', { stack: new Error().stack }) | ||
throw new Error(t('api/unauthorized', {role})) | ||
throw new Error(t.pluralize('api/unauthorized', { | ||
count: 1, | ||
role: `«${role}»` | ||
})) | ||
} | ||
} | ||
exports.ensureUserHasRole = ensureUserHasRole | ||
const userIsInRoles = (user, roles = []) => { | ||
const matches = roles.filter( role => | ||
userHasRole(user, role) | ||
) | ||
return matches.length > 0 | ||
} | ||
const ensureUserIsInRoles = (user, roles) => { | ||
if (!user) { | ||
console.info('signIn', { stack: new Error().stack }) | ||
throw new Error(t('api/signIn')) | ||
} | ||
if(!userIsInRoles(user, roles)) { | ||
console.info('unauthorized', { stack: new Error().stack }) | ||
throw new Error(t.pluralize('api/unauthorized', { | ||
count: roles.length, | ||
role: roles | ||
.map( role => `«${role}»`) | ||
.join(', ') | ||
})) | ||
} | ||
} | ||
const addUserToRole = async (userId, role, pgdb) => { | ||
@@ -43,3 +68,2 @@ await pgdb.query(` | ||
} | ||
exports.addUserToRole = addUserToRole | ||
@@ -60,2 +84,11 @@ const removeUserFromRoll = async (userId, role, pgdb) => { | ||
} | ||
exports.removeUserFromRoll = removeUserFromRoll | ||
module.exports = { | ||
roles, | ||
userHasRole, | ||
ensureUserHasRole, | ||
userIsInRoles, | ||
ensureUserIsInRoles, | ||
addUserToRole, | ||
removeUserFromRoll | ||
} |
{ | ||
"updated": "2017-11-18T18:45:28.151Z", | ||
"updated": "2017-12-04T14:03:33.359Z", | ||
"title": "live", | ||
@@ -14,2 +14,10 @@ "data": [ | ||
{ | ||
"key": "api/unauthorized/1", | ||
"value": "Nicht genehmigt. Sie müssen die Rolle {role} besitzen um die gwünschte Aktion auszuführen." | ||
}, | ||
{ | ||
"key": "api/unauthorized/other", | ||
"value": "Nicht genehmigt. Sie müssen mindestens eine der folgenden Rollen {role} besitzen um die gwünschte Aktion auszuführen." | ||
}, | ||
{ | ||
"key": "api/email/invalid", | ||
@@ -16,0 +24,0 @@ "value": "E-Mail ungültig" |
{ | ||
"name": "@orbiting/backend-modules-auth", | ||
"version": "0.6.1", | ||
"version": "0.7.0", | ||
"description": "auth migrations, seeds, middleware, graphql schema and resolvers", | ||
"main": "index.js", | ||
"scripts": { | ||
"link": "yarn link" | ||
"link": "yarn link", | ||
"translations": "gsheets --key=1mgLz3k2rQ_qcB2ZpmJRZbGLAX5vGcr0P9I2jbWaIgog --title=live --pretty --out lib/translations.json" | ||
}, | ||
@@ -9,0 +10,0 @@ "repository": { |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
26954
732