summaryrefslogtreecommitdiffstats
path: root/backend/src/utils.js
diff options
context:
space:
mode:
authorWes Bos <wesbos@gmail.com>2018-03-28 15:22:34 -0400
committerWes Bos <wesbos@gmail.com>2018-03-28 15:22:34 -0400
commit65c524251d49ade9d44a62337f3c1c1fed6eedd0 (patch)
treed504ba9cea8d89bbde4b2e809fddb048bd660548 /backend/src/utils.js
parenta1fd7eb33bb08610aaf381a64b7f963cd98bfdaa (diff)
Permissions
Diffstat (limited to 'backend/src/utils.js')
-rw-r--r--backend/src/utils.js19
1 files changed, 18 insertions, 1 deletions
diff --git a/backend/src/utils.js b/backend/src/utils.js
index 642c04a..2988b3d 100644
--- a/backend/src/utils.js
+++ b/backend/src/utils.js
@@ -7,8 +7,24 @@ function getUserId(ctx) {
const { userId } = jwt.verify(token, process.env.APP_SECRET);
return userId;
}
+ // TODO: Don't throw when they aren't logged in
+ // throw new Error('Sorry, you must be logged in to do that!');
+}
+
+function hasPermission(user, permissionsNeeded) {
+ const matchedPermissions = user.permissions.filter(permissionTheyHave =>
+ permissionsNeeded.includes(permissionTheyHave)
+ );
+ if (!matchedPermissions.length) {
+ throw new Error(`You do not have sufficient permissions
+
+ : ${permissionsNeeded}
- throw new AuthError();
+ You Have:
+
+ ${user.permissions}
+ `);
+ }
}
function checkForUserId(ctx) {
@@ -30,4 +46,5 @@ module.exports = {
getUserId,
AuthError,
checkForUserId,
+ hasPermission,
};