mirror of
https://github.com/yu-i-i/overleaf-cep.git
synced 2026-11-12 13:13:45 +01:00
Admin Tools: Fix user name
fix user name is not updated fix first_name/last_name is undefined
This commit is contained in:
@@ -467,15 +467,25 @@ async function updateUser(req, res, next) {
|
|||||||
req.logger.addFields({ actorUserId })
|
req.logger.addFields({ actorUserId })
|
||||||
const { body } = req
|
const { body } = req
|
||||||
|
|
||||||
const projection = Object.fromEntries(Object.keys(body).map(k => [k, 1]))
|
const updatesInput = { ...body }
|
||||||
|
if ('firstName' in updatesInput) {
|
||||||
|
updatesInput.first_name = updatesInput.firstName
|
||||||
|
delete updatesInput.firstName
|
||||||
|
}
|
||||||
|
if ('lastName' in updatesInput) {
|
||||||
|
updatesInput.last_name = updatesInput.lastName
|
||||||
|
delete updatesInput.lastName
|
||||||
|
}
|
||||||
|
|
||||||
|
const projection = Object.fromEntries(Object.keys(updatesInput).map(k => [k, 1]))
|
||||||
const user = await User.findById(userId, projection).exec()
|
const user = await User.findById(userId, projection).exec()
|
||||||
|
|
||||||
if (user == null) {
|
if (!user) {
|
||||||
throw new OError('problem updating user settings', { userId })
|
throw new OError('problem updating user settings', { userId })
|
||||||
}
|
}
|
||||||
|
|
||||||
let emailIsUpdated = false
|
let emailIsUpdated = false
|
||||||
const newEmail = body.email?.trim().toLowerCase()
|
const newEmail = updatesInput.email?.trim().toLowerCase()
|
||||||
if (newEmail != null && newEmail !== user.email) { // email is updated
|
if (newEmail != null && newEmail !== user.email) { // email is updated
|
||||||
if (newEmail.indexOf('@') === -1) {
|
if (newEmail.indexOf('@') === -1) {
|
||||||
const message = req.i18n.translate('email_address_is_invalid')
|
const message = req.i18n.translate('email_address_is_invalid')
|
||||||
@@ -502,31 +512,45 @@ async function updateUser(req, res, next) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (userId == actorUserId) {
|
if (userId == actorUserId) {
|
||||||
SessionManager.setInSessionUser(req.session, {
|
SessionManager.setInSessionUser(req.session, { email: newEmail })
|
||||||
email: newEmail,
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
const update = {}
|
const update = {}
|
||||||
for (const [key, value] of Object.entries(body)) {
|
|
||||||
if (key === "email") continue
|
for (let [key, value] of Object.entries(updatesInput)) {
|
||||||
if (value === user[key]) continue
|
if (key === 'email') continue
|
||||||
update[key] = typeof value === "string" ? value.trim() : value
|
|
||||||
|
const newValue = typeof value === 'string' ? value.trim() : value
|
||||||
|
if (newValue === user[key]) continue
|
||||||
|
|
||||||
|
update[key] = newValue
|
||||||
}
|
}
|
||||||
|
|
||||||
Object.assign(user, update)
|
Object.assign(user, update)
|
||||||
try {
|
try {
|
||||||
await user.save()
|
await user.save()
|
||||||
} catch (err) {
|
} catch {
|
||||||
throw new OError('problem updating user settings', { userId })
|
throw new OError('problem updating user settings', { userId })
|
||||||
}
|
}
|
||||||
|
|
||||||
if (userId == actorUserId) {
|
if (userId == actorUserId) {
|
||||||
const sessionUpdate = {}
|
const sessionUpdate = {}
|
||||||
if (update.first_name != null) sessionUpdate.first_name = update.first_name
|
if (update.first_name != null) sessionUpdate.first_name = update.first_name
|
||||||
if (update.last_name != null) sessionUpdate.last_name = update.last_name
|
if (update.last_name != null) sessionUpdate.last_name = update.last_name
|
||||||
SessionManager.setInSessionUser(req.session, sessionUpdate)
|
SessionManager.setInSessionUser(req.session, sessionUpdate)
|
||||||
}
|
}
|
||||||
if (emailIsUpdated) update["email"] = newEmail
|
|
||||||
|
if (emailIsUpdated) update.email = newEmail
|
||||||
|
|
||||||
|
if (update.first_name != null) {
|
||||||
|
update.firstName = update.first_name
|
||||||
|
delete update.first_name
|
||||||
|
}
|
||||||
|
if (update.last_name != null) {
|
||||||
|
update.lastName = update.last_name
|
||||||
|
delete update.last_name
|
||||||
|
}
|
||||||
|
|
||||||
return res.json(update)
|
return res.json(update)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -7,6 +7,15 @@ const order = (order: SortingOrder, projects: Project[]) => {
|
|||||||
return order === 'asc' ? [...projects] : projects.reverse()
|
return order === 'asc' ? [...projects] : projects.reverse()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function cmp(a, b) {
|
||||||
|
const aEmpty = a == null || a === ""
|
||||||
|
const bEmpty = b == null || b === ""
|
||||||
|
if (aEmpty && bEmpty) return Compare.SORT_KEEP_ORDER
|
||||||
|
if (aEmpty) return Compare.SORT_A_AFTER_B
|
||||||
|
if (bEmpty) return Compare.SORT_A_BEFORE_B
|
||||||
|
return a.localeCompare(b)
|
||||||
|
}
|
||||||
|
|
||||||
export const ownerNameComparator =
|
export const ownerNameComparator =
|
||||||
(getUserById: (userId: string) => User | null) =>
|
(getUserById: (userId: string) => User | null) =>
|
||||||
(v1: Project, v2: Project) => {
|
(v1: Project, v2: Project) => {
|
||||||
@@ -26,11 +35,11 @@ export const ownerNameComparator =
|
|||||||
return Compare.SORT_A_BEFORE_B
|
return Compare.SORT_A_BEFORE_B
|
||||||
}
|
}
|
||||||
|
|
||||||
const lastNameCmp = user1.lastName.localeCompare(user2.lastName)
|
const lastNameCmp = cmp(user1.lastName, user2.lastName)
|
||||||
if (lastNameCmp !== 0) return lastNameCmp
|
if (lastNameCmp !== Compare.SORT_KEEP_ORDER) return lastNameCmp
|
||||||
|
|
||||||
const firstNameCmp = user1.firstName.localeCompare(user2.firstName)
|
const firstNameCmp = cmp(user1.firstName, user2.firstName)
|
||||||
if (firstNameCmp !== 0) return firstNameCmp
|
if (firstNameCmp !== Compare.SORT_KEEP_ORDER) return firstNameCmp
|
||||||
|
|
||||||
return v1.lastUpdated < v2.lastUpdated
|
return v1.lastUpdated < v2.lastUpdated
|
||||||
? Compare.SORT_A_BEFORE_B
|
? Compare.SORT_A_BEFORE_B
|
||||||
|
|||||||
@@ -6,18 +6,18 @@ const order = (order: SortingOrder, users: User[]) => {
|
|||||||
return order === 'asc' ? [...users] : users.reverse()
|
return order === 'asc' ? [...users] : users.reverse()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
function cmp(a, b) {
|
function cmp(a, b) {
|
||||||
const aEmpty = (a === "")
|
const aEmpty = a == null || a === ""
|
||||||
const bEmpty = (b === "")
|
const bEmpty = b == null || b === ""
|
||||||
if (aEmpty && bEmpty) return Compare.SORT_KEEP_ORDER
|
if (aEmpty && bEmpty) return Compare.SORT_KEEP_ORDER
|
||||||
if (aEmpty !== bEmpty) return aEmpty ? Compare.SORT_A_AFTER_B : Compare.SORT_A_BEFORE_B
|
if (aEmpty) return Compare.SORT_A_AFTER_B
|
||||||
|
if (bEmpty) return Compare.SORT_A_BEFORE_B
|
||||||
return a.localeCompare(b)
|
return a.localeCompare(b)
|
||||||
}
|
}
|
||||||
|
|
||||||
export const userNameComparator = (v1: User, v2: User) => {
|
export const userNameComparator = (v1: User, v2: User) => {
|
||||||
const res = cmp(v1.lastName, v2.lastName)
|
const res = cmp(v1.lastName, v2.lastName)
|
||||||
if (res !== 0) return res
|
if (res !== Compare.SORT_KEEP_ORDER) return res
|
||||||
return cmp(v1.firstName, v2.firstName)
|
return cmp(v1.firstName, v2.firstName)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user