Skip to content

Commit

Permalink
Aftral[premieroctet#111]: fixed permissions to be computed on user, n…
Browse files Browse the repository at this point in the history
…ow only depends of permission group
  • Loading branch information
SeghirOumo committed Aug 27, 2024
1 parent 4b89b2d commit c874ce7
Show file tree
Hide file tree
Showing 5 changed files with 29 additions and 4 deletions.
2 changes: 2 additions & 0 deletions backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ const { setBlockLiked } = require('./block')
const { setBlockDisliked } = require('./block')
const Permission = require('../../models/Permission')
const Group = require('../../models/Group')
const { getUserPermissions } = require('./user')

const GENERAL_FEED_ID='FFFFFFFFFFFFFFFFFFFFFFFF'

Expand Down Expand Up @@ -141,6 +142,7 @@ USER_MODELS.forEach(model => {
instance: 'ObjectID',
options: {ref: 'ticket'}},
})
declareComputedField({model, field: `permissions`, requires:`permission_groups.permissions`, getterFn: getUserPermissions})
})

// search start
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,11 @@ const PermissionGroupSchema = new Schema({
permissions: [{
type: Schema.Types.ObjectId,
ref: 'permission',
}]
}],
label: {
type: String,
required: false,
}
}, schemaOptions)

/* eslint-disable prefer-arrow-callback */
Expand Down
4 changes: 2 additions & 2 deletions backend/web/server/plugins/aftral-lms/schemas/UserSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,10 +52,10 @@ const UserSchema = new Schema({
type: Schema.Types.ObjectId,
ref: 'permission'
}],
permission_group: {
permission_groups: [{
type: Schema.Types.ObjectId,
ref: 'permissionGroup'
}
}],
}, schemaOptions)

UserSchema.virtual('fullname', DUMMY_REF).get(function() {
Expand Down
6 changes: 5 additions & 1 deletion backend/web/server/plugins/aftral-lms/user.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
const PermissionGroup = require('../../models/PermissionGroup')
const Progress = require('../../models/Progress')
const Resource = require('../../models/Resource')
const { loadFromDb } = require('../../utils/database')
Expand All @@ -13,7 +14,10 @@ const getTraineeCurrentResources = async (userId, params, data, fields) => {
return resources.map(r => new Resource(r))
}

const getUserPermissions = async (userId, params, data) => {
return data.permission_groups.flatMap(group => group.permissions)
}

module.exports = {
getTraineeCurrentResources,getTraineeCurrentResources
getTraineeCurrentResources,getTraineeCurrentResources, getUserPermissions
}
15 changes: 15 additions & 0 deletions backend/web/tests/aftral-lms/user.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@ const ProductCode = require('../../server/models/ProductCode')
const { addChildAction } = require('../../server/plugins/aftral-lms/actions')
const { getBlockResources, getRessourceSession } = require('../../server/plugins/aftral-lms/resources')
const Block = require('../../server/models/Block')
const Permission = require('../../server/models/Permission')
const PermissionGroup = require('../../server/models/PermissionGroup')
require('../../server/models/Certification')
require('../../server/models/Feed')

jest.setTimeout(60000)

Expand All @@ -28,12 +32,18 @@ describe('User', () => {
lastname: `Doe`,
})

const permission = await Permission.findOne({})
const permissionGroup = await PermissionGroup.create({
permissions: [permission._id]
})

trainer = await User.create({
role: ROLE_FORMATEUR,
email: `b@b.com`,
password: `J'enseigne le Python`,
firstname: `Jeanette`,
lastname: `Doe`,
permission_groups: [permissionGroup._id]
})

conceptor = await User.create({
Expand Down Expand Up @@ -162,4 +172,9 @@ describe('User', () => {
const [u] = await loadFromDb({model:'user', fields:['resources'], id:trainee._id})
expect(u.resources.length).toEqual(2)
})

it.only('must return user permissions', async () => {
const [data] = await loadFromDb({model:`user`, id:trainer._id, fields:[`permissions`,`fullname`,`permission_groups`]})
expect(data.permissions.length).toEqual(1)
})
})

0 comments on commit c874ce7

Please sign in to comment.