-
Notifications
You must be signed in to change notification settings - Fork 11.4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #7513 from Darkneon/fix-dont-save-user-when-custom…
…-field-is-invalid [Fix] Don't save user to DB when a custom field is invalid
- Loading branch information
Showing
7 changed files
with
323 additions
and
170 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 2 additions & 52 deletions
54
packages/rocketchat-lib/server/functions/saveCustomFields.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,56 +1,6 @@ | ||
RocketChat.saveCustomFields = function(userId, formData) { | ||
if (s.trim(RocketChat.settings.get('Accounts_CustomFields')) !== '') { | ||
let customFieldsMeta; | ||
try { | ||
customFieldsMeta = JSON.parse(RocketChat.settings.get('Accounts_CustomFields')); | ||
} catch (e) { | ||
throw new Meteor.Error('error-invalid-customfield-json', 'Invalid JSON for Custom Fields'); | ||
} | ||
|
||
const customFields = {}; | ||
|
||
Object.keys(customFieldsMeta).forEach((fieldName) => { | ||
const field = customFieldsMeta[fieldName]; | ||
|
||
customFields[fieldName] = formData[fieldName]; | ||
if (field.required && !formData[fieldName]) { | ||
throw new Meteor.Error('error-user-registration-custom-field', `Field ${ fieldName } is required`, { method: 'registerUser' }); | ||
} | ||
|
||
if (field.type === 'select' && field.options.indexOf(formData[fieldName]) === -1) { | ||
throw new Meteor.Error('error-user-registration-custom-field', `Value for field ${ fieldName } is invalid`, { method: 'registerUser' }); | ||
} | ||
|
||
if (field.maxLength && formData[fieldName].length > field.maxLength) { | ||
throw new Meteor.Error('error-user-registration-custom-field', `Max length of field ${ fieldName } ${ field.maxLength }`, { method: 'registerUser' }); | ||
} | ||
|
||
if (field.minLength && formData[fieldName].length < field.minLength) { | ||
throw new Meteor.Error('error-user-registration-custom-field', `Min length of field ${ fieldName } ${ field.minLength }`, { method: 'registerUser' }); | ||
} | ||
}); | ||
|
||
// for fieldName, field of customFieldsMeta | ||
RocketChat.models.Users.setCustomFields(userId, customFields); | ||
|
||
Object.keys(customFields).forEach((fieldName) => { | ||
if (!customFieldsMeta[fieldName].modifyRecordField) { | ||
return; | ||
} | ||
|
||
const modifyRecordField = customFieldsMeta[fieldName].modifyRecordField; | ||
const update = {}; | ||
if (modifyRecordField.array) { | ||
update.$addToSet = {}; | ||
update.$addToSet[modifyRecordField.field] = customFields[fieldName]; | ||
} else { | ||
update.$set = {}; | ||
update.$set[modifyRecordField.field] = customFields[fieldName]; | ||
} | ||
|
||
RocketChat.models.Users.update(userId, update); | ||
}); | ||
|
||
return true; | ||
RocketChat.validateCustomFields(formData); | ||
return RocketChat.saveCustomFieldsWithoutValidation(userId, formData); | ||
} | ||
}; |
33 changes: 33 additions & 0 deletions
33
packages/rocketchat-lib/server/functions/saveCustomFieldsWithoutValidation.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
RocketChat.saveCustomFieldsWithoutValidation = function(userId, formData) { | ||
if (s.trim(RocketChat.settings.get('Accounts_CustomFields')) !== '') { | ||
let customFieldsMeta; | ||
try { | ||
customFieldsMeta = JSON.parse(RocketChat.settings.get('Accounts_CustomFields')); | ||
} catch (e) { | ||
throw new Meteor.Error('error-invalid-customfield-json', 'Invalid JSON for Custom Fields'); | ||
} | ||
|
||
const customFields = formData; | ||
|
||
// for fieldName, field of customFieldsMeta | ||
RocketChat.models.Users.setCustomFields(userId, customFields); | ||
|
||
Object.keys(customFields).forEach((fieldName) => { | ||
if (!customFieldsMeta[fieldName].modifyRecordField) { | ||
return; | ||
} | ||
|
||
const modifyRecordField = customFieldsMeta[fieldName].modifyRecordField; | ||
const update = {}; | ||
if (modifyRecordField.array) { | ||
update.$addToSet = {}; | ||
update.$addToSet[modifyRecordField.field] = customFields[fieldName]; | ||
} else { | ||
update.$set = {}; | ||
update.$set[modifyRecordField.field] = customFields[fieldName]; | ||
} | ||
|
||
RocketChat.models.Users.update(userId, update); | ||
}); | ||
} | ||
}; |
39 changes: 39 additions & 0 deletions
39
packages/rocketchat-lib/server/functions/validateCustomFields.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
RocketChat.validateCustomFields = function(fields) { | ||
// Special Case: | ||
// If an admin didn't set any custom fields there's nothing to validate against so consider any customFields valid | ||
if (s.trim(RocketChat.settings.get('Accounts_CustomFields')) === '') { | ||
return; | ||
} | ||
|
||
let customFieldsMeta; | ||
try { | ||
customFieldsMeta = JSON.parse(RocketChat.settings.get('Accounts_CustomFields')); | ||
} catch (e) { | ||
throw new Meteor.Error('error-invalid-customfield-json', 'Invalid JSON for Custom Fields'); | ||
} | ||
|
||
const customFields = {}; | ||
|
||
Object.keys(customFieldsMeta).forEach((fieldName) => { | ||
const field = customFieldsMeta[fieldName]; | ||
|
||
customFields[fieldName] = fields[fieldName]; | ||
const fieldValue = s.trim(fields[fieldName]); | ||
|
||
if (field.required && fieldValue === '') { | ||
throw new Meteor.Error('error-user-registration-custom-field', `Field ${ fieldName } is required`, {method: 'registerUser'}); | ||
} | ||
|
||
if (field.type === 'select' && field.options.indexOf(fields[fieldName]) === -1) { | ||
throw new Meteor.Error('error-user-registration-custom-field', `Value for field ${ fieldName } is invalid`, {method: 'registerUser'}); | ||
} | ||
|
||
if (field.maxLength && fieldValue.length > field.maxLength) { | ||
throw new Meteor.Error('error-user-registration-custom-field', `Max length of field ${ fieldName } ${ field.maxLength }`, {method: 'registerUser'}); | ||
} | ||
|
||
if (field.minLength && fieldValue.length < field.minLength) { | ||
throw new Meteor.Error('error-user-registration-custom-field', `Min length of field ${ fieldName } ${ field.minLength }`, {method: 'registerUser'}); | ||
} | ||
}); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
import {getCredentials, request, api, credentials} from './api-data.js'; | ||
|
||
export const customFieldText = { | ||
type: 'text', | ||
required: true, | ||
minLength: 2, | ||
maxLength: 10 | ||
}; | ||
|
||
export function setCustomFields(customFields, done) { | ||
getCredentials((error) => { | ||
if (error) { | ||
return done(error); | ||
} | ||
|
||
const stringified = customFields ? JSON.stringify(customFields) : ''; | ||
|
||
request.post(api('settings/Accounts_CustomFields')) | ||
.set(credentials) | ||
.send({ 'value': stringified }) | ||
.expect(200) | ||
.end(done); | ||
}); | ||
} | ||
|
||
export function clearCustomFields(done = () => {}) { | ||
setCustomFields(null, done); | ||
} |
Oops, something went wrong.