Skip to content

Commit

Permalink
Merge pull request #1265 from EcrituresNumeriques/fix/1264
Browse files Browse the repository at this point in the history
Supprime le champ `user.admin`
  • Loading branch information
thom4parisot authored Feb 13, 2025
2 parents 5d807f2 + 60a5308 commit 72479bd
Show file tree
Hide file tree
Showing 20 changed files with 271 additions and 167 deletions.
1 change: 1 addition & 0 deletions front/.eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ module.exports = {
'plugin:react/recommended',
'plugin:jsonc/recommended-with-json',
'plugin:jsdoc/recommended-typescript-flavor',
'prettier',
],
parserOptions: {
ecmaFeatures: {
Expand Down
36 changes: 26 additions & 10 deletions front/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 4 additions & 3 deletions front/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
"@babel/preset-react": "^7.25.7",
"@geist-ui/core": "^2.3.8",
"@monaco-editor/react": "~4.6",
"@rjsf/core": "~5.22",
"@rjsf/validator-ajv8": "~5.22",
"@rjsf/core": "^5.24.3",
"@rjsf/validator-ajv8": "^5.24.3",
"@rollup/plugin-graphql": "^2.0.2",
"@sentry/react": "^8.38.0",
"@sentry/vite-plugin": "^3.0.0",
Expand Down Expand Up @@ -100,6 +100,7 @@
"@vitest/coverage-v8": "^3.0.3",
"@welldone-software/why-did-you-render": "~8.0",
"eslint": "^8.2.0",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-jsdoc": "^50.6.2",
"eslint-plugin-jsonc": "^2.5.0",
"eslint-plugin-react": "^7.27.0",
Expand All @@ -113,4 +114,4 @@
"node": "22.13.1",
"npm": "10.9.2"
}
}
}
3 changes: 0 additions & 3 deletions front/src/components/Credentials.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ query getUserDetails($user: ID!) {
displayName
_id
email
admin
createdAt
updatedAt
firstName
Expand All @@ -23,7 +22,6 @@ query getFullUserProfile {
lastName
institution
email
admin
createdAt
updatedAt
apiToken
Expand Down Expand Up @@ -64,7 +62,6 @@ mutation updateUser($user: ID!, $details: UserProfileInput!) {
displayName
_id
email
admin
createdAt
updatedAt
firstName
Expand Down
1 change: 0 additions & 1 deletion front/src/components/UserInfos.jsx
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,6 @@ export default function UserInfos() {
<Field label={t('user.account.id')}>
<code>{activeUser._id}</code>
</Field>
{activeUser.admin && <Field label="Admin">✔️</Field>}
<Field label={t('user.account.createdAt')}>
<TimeAgo date={activeUser.createdAt} />
</Field>
Expand Down
16 changes: 9 additions & 7 deletions graphql/eslint.config.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,19 +2,20 @@ const globals = require('globals')
const pluginSecurity = require('eslint-plugin-security')
const pluginJest = require('eslint-plugin-jest')
const eslint = require('@eslint/js')
const eslintConfigPrettier = require('eslint-config-prettier')

module.exports = [
{
languageOptions: {
ecmaVersion: 2022,
sourceType: 'commonjs',
globals: {
...globals.node
}
}
...globals.node,
},
},
},
{
files: ["**/*.test.js", "**/tests/**.js"],
files: ['**/*.test.js', '**/tests/**.js'],
plugins: {
jest: pluginJest,
},
Expand All @@ -23,10 +24,11 @@ module.exports = [
},

rules: {
"security/detect-object-injection": ["off"],
"security/detect-non-literal-fs-filename": ["off"],
'security/detect-object-injection': ['off'],
'security/detect-non-literal-fs-filename': ['off'],
},
},
eslint.configs.recommended,
pluginSecurity.configs.recommended
pluginSecurity.configs.recommended,
eslintConfigPrettier,
]
1 change: 0 additions & 1 deletion graphql/helpers/token.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@ module.exports.createJWTToken = async function createJWTToken({
email: user.email,
_id: user._id,
authType: user.authType,
admin: Boolean(user.admin),
session: true,
}

Expand Down
17 changes: 17 additions & 0 deletions graphql/migrations/20250212122400-user-delete-admin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
exports.up = async function (db) {
const mongo = await db._run('getDbInstance', true)
try {
await mongo.collection('users').updateMany({}, { $unset: { admin: '' } })
} finally {
await mongo.close()
}
}

exports.down = async function (db) {
const mongo = await db._run('getDbInstance', true)
try {
await mongo.collection('users').updateMany({}, { $set: { admin: false } })
} finally {
await mongo.close()
}
}
7 changes: 3 additions & 4 deletions graphql/models/user.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,10 +46,6 @@ const userSchema = new Schema(
return bcrypt.hashSync(password, 10)
},
},
admin: {
type: Boolean,
default: false,
},
firstName: String,
lastName: String,
institution: String,
Expand Down Expand Up @@ -87,6 +83,9 @@ userSchema.methods.createDefaultArticle =
})

await newArticle.createNewVersion({ mode: 'MINOR', user: this })

this.articles.push(newArticle)
return this.save()
}

userSchema.statics.assessLogin = async function assessLogin(query) {
Expand Down
14 changes: 14 additions & 0 deletions graphql/package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion graphql/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,7 @@
"@shelf/jest-mongodb": "~4.3",
"@types/jest": "~29.5",
"eslint": "~8.57",
"eslint-config-prettier": "^10.0.1",
"eslint-plugin-jest": "~28.9",
"eslint-plugin-security": "~3.0",
"jest": "~29.7",
Expand All @@ -68,4 +69,4 @@
"node": "22.13.1",
"npm": "10.9.2"
}
}
}
27 changes: 20 additions & 7 deletions graphql/policies/isUser.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,20 @@ const UserModel = require('../models/user.js')

const user = '63977de2f83aa77c5f92cb1c'
const sameUserObject = new UserModel({ _id: user })
const sameUserToken = { _id: user, email: '[email protected]', admin: false, session: true, authType: 'oidc' }
const sameUserToken = {
_id: user,
email: '[email protected]',
session: true,
authType: 'oidc',
}

const differentUserObject = new UserModel({ _id: '00000de2f83aa77c5f92dc2f'})
const differentUserObject = new UserModel({ _id: '00000de2f83aa77c5f92dc2f' })

const adminToken = { admin: true, roles: ['read'], readonly: true }


describe('isUser', () => {
test('without token, no args.user', () => {
expect(() => isUser({ }, { token: {} })).toThrow(/Unauthorized/)
expect(() => isUser({}, { token: {} })).toThrow(/Unauthorized/)
})

test('without token, explicit args.user', () => {
Expand All @@ -28,14 +32,23 @@ describe('isUser', () => {
})

test('with token, implicit user is token user', () => {
expect(isUser({}, { token: sameUserToken, user: sameUserObject })).toEqual({ userId: sameUserToken._id })
expect(isUser({}, { token: sameUserToken, user: sameUserObject })).toEqual({
userId: sameUserToken._id,
})
})

test('with token, explicit user is same as user token', () => {
expect(isUser({ user }, { token: sameUserToken, user: sameUserObject })).toEqual({ userId: user })
expect(
isUser({ user }, { token: sameUserToken, user: sameUserObject })
).toEqual({ userId: user })
})

test('with token, explicit user is different than user token', () => {
expect(() => isUser({ user: differentUserObject.id }, { token: sameUserToken, user: sameUserObject })).toThrow(/Forbidden/)
expect(() =>
isUser(
{ user: differentUserObject.id },
{ token: sameUserToken, user: sameUserObject }
)
).toThrow(/Forbidden/)
})
})
4 changes: 2 additions & 2 deletions graphql/resolvers/articleResolver.js
Original file line number Diff line number Diff line change
Expand Up @@ -336,8 +336,8 @@ module.exports = {
},

Article: {
async workspaces(article, _, { user }) {
if (user.admin) {
async workspaces(article, _, { user, token }) {
if (token.admin) {
return Workspace.find({ articles: article._id })
}
return Workspace.find({
Expand Down
Loading

0 comments on commit 72479bd

Please sign in to comment.