-
Notifications
You must be signed in to change notification settings - Fork 35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
How to implement access control? #88
Comments
AHH! I'm terribly sorry, but I only just saw #56 and the access control example folder. But, I still don't understand what exactly needs to be done here to achieve such access control described above. But from what I understand, the objectives (according to #56) are:
Is this correct, or is there a better solution to access control? |
I finally managed to figure something out myself: // app.js - Main file, graphql route
app.use('/graphql', isLoggedIn, (req, res, next) => graphql({
schema,
context: loggedUser,
graphiql: true,
rootValue: {
onQuery: async (qb) => {
await qb.mergeContext({ loggedUser, isGraphQLQuery: true });
},
},
})(req, res, next));
// user.js - Model file
const { Model } = require('objection');
class User extends Model {
static get tableName() {
return 'user';
}
static get jsonSchema() {
return {
type: 'object',
required: ['name'],
properties: {
id: { type: 'integer' },
name: { type: 'string' },
},
};
}
static query(...args) {
const query = super.query(...args);
return query.onBuild((qb) => {
const ctx = qb.context();
const { loggedUser: user, isGraphQLQuery } = ctx;
if (user.id !== 1 && isGraphQLQuery) return qb.where('id', user.id);
return qb;
});
}
}
module.exports = User; What this basically does is if the authenticated user's id is not 1 (let's say this one is an admin), then the user cannot query for the other user info. Though, I really feel this is a really hacky solution as this adds another SELECT id, name FROM user WHERE id = 2 AND user.id = 3 So, would anybody share an improvement to this solution or even provide a better solution? Thanks! |
Hi there!
I am trying to implement a GraphQL server with Objection.js to handle database queries, and of course this library to build the GraphQL schema. And for security measures, some access control needs to be implemented, as it is undesirable to have let's say a user querying another user's data in
Users
table. Assuming there are common (normal user privilege) and admin user roles in the table, the access controls should be implemented is as follows:How can we achieve such access control with this library?
Thanks in advance!
The text was updated successfully, but these errors were encountered: