parent
475ed0ae06
commit
1411fbb124
@ -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); |
||||
} |
||||
}; |
||||
|
||||
@ -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); |
||||
}); |
||||
} |
||||
}; |
||||
@ -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'}); |
||||
} |
||||
}); |
||||
}; |
||||
@ -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); |
||||
} |
||||
Loading…
Reference in new issue