fix(xo-server/users): serialize properties on user create as well (#5273)

This didn't break anything because we usually don't assign `groups` and/or
`preferences` (which are the only 2 properties that need serialization) on user
creation.

This also prepares a minimal change to add a `authProviders` object property on
users.
This commit is contained in:
Pierre Donias 2020-09-18 12:11:20 +02:00 committed by GitHub
parent 592feb54b7
commit 626e2fcb12
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -2,7 +2,6 @@ import isEmpty from 'lodash/isEmpty'
import Collection from '../collection/redis'
import Model from '../model'
import { forEach } from '../utils'
import { parseProp } from './utils'
@ -16,6 +15,23 @@ User.prototype.default = {
// -------------------------------------------------------------------
const serialize = user => {
let tmp
return {
...user,
groups: isEmpty((tmp = user.groups)) ? undefined : JSON.stringify(tmp),
preferences: isEmpty((tmp = user.preferences))
? undefined
: JSON.stringify(tmp),
}
}
const deserialize = user => ({
...user,
groups: parseProp('user', user, 'groups', []),
preferences: parseProp('user', user, 'preferences', {}),
})
export class Users extends Collection {
get Model() {
return User
@ -30,32 +46,17 @@ export class Users extends Collection {
}
// Create the user object.
const user = new User(properties)
const user = new User(serialize(properties))
// Adds the user to the collection.
return /* await */ this.add(user)
}
async save(user) {
// Serializes.
let tmp
user.groups = isEmpty((tmp = user.groups)) ? undefined : JSON.stringify(tmp)
user.preferences = isEmpty((tmp = user.preferences))
? undefined
: JSON.stringify(tmp)
return /* await */ this.update(user)
return /* await */ this.update(serialize(user))
}
async get(properties) {
const users = await super.get(properties)
// Deserializes
forEach(users, user => {
user.groups = parseProp('user', user, 'groups', [])
user.preferences = parseProp('user', user, 'preferences', {})
})
return users
return (await super.get(properties)).map(deserialize)
}
}