Skip to content

Commit

Permalink
Aftral[premieroctet#111]: Added permission and permission group schemas
Browse files Browse the repository at this point in the history
  • Loading branch information
SeghirOumo committed Aug 20, 2024
1 parent 0e1b1ef commit 719dfd4
Show file tree
Hide file tree
Showing 7 changed files with 113 additions and 1 deletion.
16 changes: 16 additions & 0 deletions backend/web/server/models/Permission.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose')
const {getDataModel} = require('../../config/config')

let PermissionSchema=null

try {
PermissionSchema=require(`../plugins/${getDataModel()}/schemas/PermissionSchema`)
PermissionSchema.plugin(require('mongoose-lean-virtuals'))
}
catch(err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
}
}

module.exports = PermissionSchema ? mongoose.model('permission', PermissionSchema) : null
16 changes: 16 additions & 0 deletions backend/web/server/models/PermissionGroup.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
const mongoose = require('mongoose')
const {getDataModel} = require('../../config/config')

let PermissionGroupSchema=null

try {
PermissionGroupSchema=require(`../plugins/${getDataModel()}/schemas/PermissionGroupSchema`)
PermissionGroupSchema.plugin(require('mongoose-lean-virtuals'))
}
catch(err) {
if (err.code !== 'MODULE_NOT_FOUND') {
throw err
}
}

module.exports = PermissionGroupSchema ? mongoose.model('permissionGroup', PermissionGroupSchema) : null
15 changes: 15 additions & 0 deletions backend/web/server/plugins/aftral-lms/consts.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,20 @@ const TICKET_STATUS = {
[TICKET_STATUS_NOT_TREATED] : `Non traité`,
}

const PERMISSION_ADD_RESOURCE = `PERM_ADD_RESOURCE`
const PERMISSION_CMS = `PERM_CMS`
const PERMISSION_TICKET = `PERM_TICKET`
const PERMISSION_PERMISSIONS = `PERM_PERMISSIONS`
const PERMISSION_END_RESOURCE = `PERM_END_RESOURCE`

const PERMISSIONS = {
[PERMISSION_ADD_RESOURCE] : `Ajouter une ressource`,
[PERMISSION_CMS] : `Accéder à l'espace gestionnaire`,
[PERMISSION_TICKET] : `Visualiser et traiter les tickets`,
[PERMISSION_PERMISSIONS] : `Gérer les permissions`,
[PERMISSION_END_RESOURCE] : `Achever une ressource`
}

module.exports={
ROLES, ROLE_ADMINISTRATEUR, ROLE_APPRENANT, ROLE_CONCEPTEUR, ROLE_FORMATEUR, ROLE_GESTIONNAIRE, ROLE_HELPDESK, ROLE_SUPERVISEUR,
BLOCK_DISCRIMINATOR, BLOCK_TYPE,
Expand All @@ -196,4 +210,5 @@ module.exports={
ACHIEVEMENT_RULE, ACHIEVEMENT_RULE_SUCCESS, ACHIEVEMENT_RULE_CONSULT, ACHIEVEMENT_RULE_DOWNLOAD,
SCALE, RESOURCE_TYPE_EXT, RESOURCE_TYPE_LINK, DEFAULT_ACHIEVEMENT_RULE, AVAILABLE_ACHIEVEMENT_RULES,
ACHIEVEMENT_RULE_CHECK, DURATION_UNIT, TICKET_TAG, TICKET_STATUS, TICKET_STATUS_NOT_TREATED,
PERMISSIONS,
}
20 changes: 19 additions & 1 deletion backend/web/server/plugins/aftral-lms/functions.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ const lodash=require('lodash')
const {
declareVirtualField, setPreCreateData, setPreprocessGet, setMaxPopulateDepth, setFilterDataUser, declareComputedField, declareEnumField, idEqual, getModel, declareFieldDependencies, setPostPutData, setPreDeleteData, setPrePutData, loadFromDb,
} = require('../../utils/database')
const { RESOURCE_TYPE, PROGRAM_STATUS, ROLES, MAX_POPULATE_DEPTH, BLOCK_STATUS, ROLE_CONCEPTEUR, ROLE_FORMATEUR,ROLE_APPRENANT, FEED_TYPE_GENERAL, FEED_TYPE_SESSION, FEED_TYPE_GROUP, FEED_TYPE, ACHIEVEMENT_RULE, SCALE, RESOURCE_TYPE_LINK, DEFAULT_ACHIEVEMENT_RULE, BLOCK_STATUS_TO_COME, BLOCK_STATUS_CURRENT, TICKET_STATUS, TICKET_TAG } = require('./consts')
const { RESOURCE_TYPE, PROGRAM_STATUS, ROLES, MAX_POPULATE_DEPTH, BLOCK_STATUS, ROLE_CONCEPTEUR, ROLE_FORMATEUR,ROLE_APPRENANT, FEED_TYPE_GENERAL, FEED_TYPE_SESSION, FEED_TYPE_GROUP, FEED_TYPE, ACHIEVEMENT_RULE, SCALE, RESOURCE_TYPE_LINK, DEFAULT_ACHIEVEMENT_RULE, BLOCK_STATUS_TO_COME, BLOCK_STATUS_CURRENT, TICKET_STATUS, TICKET_TAG, PERMISSIONS } = require('./consts')
const mongoose = require('mongoose')
require('../../models/Resource')
const Session = require('../../models/Session')
Expand Down Expand Up @@ -39,6 +39,7 @@ const { getBlockLiked } = require('./block')
const { getBlockDisliked } = require('./block')
const { setBlockLiked } = require('./block')
const { setBlockDisliked } = require('./block')
const Permission = require('../../models/Permission')

const GENERAL_FEED_ID='FFFFFFFFFFFFFFFFFFFFFFFF'

Expand Down Expand Up @@ -171,6 +172,10 @@ declareEnumField({model:'ticket', field: 'status', instance: 'String', enumValue
declareEnumField({model:'ticket', field: 'tag', instance: 'String', enumValues: TICKET_TAG})
// Ticket End

// Permission start
declareEnumField({model:'permission', field: 'value', instance: 'String', enumValues: PERMISSIONS})
// Permission end

const preCreate = async ({model, params, user}) => {
params.creator=params.creator || user._id
params.last_updater=user._id
Expand Down Expand Up @@ -560,6 +565,19 @@ const lockSession = async blockId => {
}
}

//Make sure permissions are upserted
Promise.all(
Object.entries(PERMISSIONS).map(([key, value]) =>
Permission.findOneAndUpdate(
{ value },
{ value, key },
{ upsert: true }
)
)
)
.then(() => console.log('Permission upserts completed successfully.'))
.catch((error) => console.error('An error occurred during permission upserts:', error))

module.exports={
lockSession, setSessionInitialStatus
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
const mongoose = require('mongoose')
const {schemaOptions} = require('../../../utils/schemas')
const { PERMISSIONS } = require('../consts')

const Schema = mongoose.Schema

const PermissionGroupSchema = new Schema({
permissions: [{
type: Schema.Types.ObjectId,
ref: 'permission',
}]
}, schemaOptions)

/* eslint-disable prefer-arrow-callback */
/* eslint-enable prefer-arrow-callback */

module.exports = PermissionGroupSchema
22 changes: 22 additions & 0 deletions backend/web/server/plugins/aftral-lms/schemas/PermissionSchema.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
const mongoose = require('mongoose')
const {schemaOptions} = require('../../../utils/schemas')
const { PERMISSIONS } = require('../consts')

const Schema = mongoose.Schema

const PermissionSchema = new Schema({
value: {
type: String,
enum: Object.keys(PERMISSIONS),
required: true,
},
label: {
type: String,
required: true,
}
}, schemaOptions)

/* eslint-disable prefer-arrow-callback */
/* eslint-enable prefer-arrow-callback */

module.exports = PermissionSchema
8 changes: 8 additions & 0 deletions backend/web/server/plugins/aftral-lms/schemas/UserSchema.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,14 @@ const UserSchema = new Schema({
type: Schema.Types.ObjectId,
ref: 'resource',
}],
permissions: [{
type: Schema.Types.ObjectId,
ref: 'permission'
}],
permission_group: {
type: Schema.Types.ObjectId,
ref: 'permissionGroup'
}
}, schemaOptions)

UserSchema.virtual('fullname', DUMMY_REF).get(function() {
Expand Down

0 comments on commit 719dfd4

Please sign in to comment.