-
-
Notifications
You must be signed in to change notification settings - Fork 270
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 deal with conditions on ref field (mongoose) #383
Comments
Hello, The question is quite interesting. But this is a question not a bug and not a feature. Please, move it to stackoverflow - https://stackoverflow.com/questions/tagged/casl, so others can find my answer on it |
Close as the issue doesn’t follow suggested templates |
Ok thanks for your answer and sorry for the bad template. I follow your advice and post a question on stackoverflow https://stackoverflow.com/questions/63660271/how-to-deal-with-conditions-on-ref-field-mongoose-with-casl |
Ok, I’ll give a detailed answer a bit later. Meanwhile you can read my answer to the similar question in past: #220 |
Hi the post was removed from stackoverflow, is it possible to get a summary here? Short answer is "not possible" right? |
Here is my post from stackoverflow After using Casl for some simple project, I am trying to implement something more complicated. I am trying to mix Roles with persisted permissions with JWT described on the website. For this basic example, I try to give read action permissions to User subject but only on users entries that are part of an organization: My User model interface UserAttrs { const userSchema = new mongoose.Schema( userSchema.pre('save', async function (done) { userSchema.statics.build = (attrs: UserAttrs) => { const User = mongoose.model<UserDoc, UserModel>('User', userSchema); export { User }; Organization Model interface OrganizationAttrs { export interface OrganizationDoc extends mongoose.Document { const organizationSchema = new mongoose.Schema( organizationSchema.set('versionKey', 'version'); organizationSchema.statics.findByEvent = (event: { organizationSchema.statics.build = (attrs: OrganizationAttrs) => { const Organization = mongoose.model<OrganizationDoc, OrganizationModel>( export { Organization }; Role model interface RoleAttrs { interface RoleModel extends mongoose.Model { export interface RoleDoc extends mongoose.Document { const roleSchema = new mongoose.Schema( roleSchema.pre('save', async function (done) { roleSchema.statics.build = (attrs: RoleAttrs) => { const Role = mongoose.model<RoleDoc, RoleModel>('Role', roleSchema); export { Role }; The permissions field from role is stored as string. As soon as a user logged in, I add the permissions to the JWT token const existingUser = await User.findOne({ email, active: true }) // Check if user is valid... // userPermissions = [{ action: 'read', subject: 'User', conditions: { organization: '{{organization.id}}' }, }, ](as a string) Then in a middleware, I create the abilities similar to here const { id, email, organizationId, userRolePermissions } = jwt.verify( The result of createAbility is i { if I execute const organizationId = req.params.organizationId as Object; const users = await User.find({ organization: organizationId }); // req.currentUser contain the user with the userRolePermissions above I get message: 'Cannot execute "read" on "User"'. How ca we deal with ref fields ? I don't know if it can help, but if I change the permissions to :
It works. How casl works in case I populate the organization field on user (I will then have an object) ? Should I make two rules in permissions field in role (one if populated one if not) ? |
After using Casl for some simple project, I am trying to implement something more complicated. I am trying to mix Roles with persisted permissions with JWT described on the website.
For this basic example, I try to give read action permissions to User subject but only on users entries that are part of an organization:
My User model
Organization Model
Role model
The permissions field from role is stored as string. As soon as a user logged in, I add the permissions to the JWT token
The result of createAbility is
if I execute
I get message: 'Cannot execute "read" on "User"'. How ca we deal with ref fields ?
How casl works in case I populate the organization field on user (I will then have an object) ? Should I make two rules in permissions field in role (one if populated one if not) ?
The text was updated successfully, but these errors were encountered: