Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[NEW] Template to show Custom Fields in user info view #7688

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"Accounts_BlockedUsernameList": "Blocked Username List",
"Accounts_BlockedUsernameList_Description": "Comma-separated list of blocked usernames (case-insensitive)",
"Accounts_CustomFields_Description": "Should be a valid JSON where keys are the field names containing a dictionary of field settings. Example:<br/><code>{\n \"role\": {\n  \"type\": \"select\",\n  \"defaultValue\": \"student\",\n  \"options\": [\"teacher\", \"student\"],\n  \"required\": true,\n  \"modifyRecordField\": {\n   \"array\": true,\n   \"field\": \"roles\"\n  }\n },\n \"twitter\": {\n  \"type\": \"text\",\n  \"required\": true,\n  \"minLength\": 2,\n  \"maxLength\": 10\n }\n}</code> ",
"Accounts_CustomFieldsToShowInUserInfo": "Custom Fields to Show in User Info",
"Accounts_DefaultUsernamePrefixSuggestion": "Default username prefix suggestion",
"Accounts_denyUnverifiedEmail": "Deny unverified email",
"Accounts_EmailVerification": "Email Verification",
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-i18n/i18n/pt-BR.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
"Accounts_BlockedUsernameList": "Lista de nomes de usuário bloqueados",
"Accounts_BlockedUsernameList_Description": "Lista de nomes de usuários bloqueados, separada por vírgulas (não diferencia maiúsculas)",
"Accounts_CustomFields_Description": "Deve ser um JSON válido onde as chaves são os nomes de campos contendo um dicionário de configuração de campos. Exemplo:<br/><code>{\n \"role\": {\n  \"type\": \"select\",\n  \"defaultValue\": \"estudante\",\n  \"options\": [\"professor\", \"estudante\"],\n  \"required\": true,\n  \"modifyRecordField\": {\n   \"array\": true,\n   \"field\": \"roles\"\n  }\n },\n \"twitter\": {\n  \"type\": \"text\",\n  \"required\": true,\n  \"minLength\": 2,\n  \"maxLength\": 10\n }\n}</code> ",
"Accounts_CustomFieldsToShowInUserInfo": "Campos personalizados a exibir",
"Accounts_denyUnverifiedEmail": "Proibir e-mail não verificado",
"Accounts_EmailVerification": "Verificação de E-mail",
"Accounts_EmailVerification_Description": "Certifique-se de que as configurações de SMTP estão corretas para usar este recurso",
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-i18n/i18n/pt.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
"Accounts_BlockedUsernameList": "Lista de nomes de usuário bloqueados",
"Accounts_BlockedUsernameList_Description": "Lista de nomes de usuários bloqueados, separada por vírgulas (não diferencia maiúsculas)",
"Accounts_CustomFields_Description": "Deve ser um JSON válido onde as chaves são os nomes de campos contendo um dicionário de configuração de campos. Exemplo:<br/><code>{\n \"role\": {\n  \"type\": \"select\",\n  \"defaultValue\": \"estudante\",\n  \"options\": [\"professor\", \"estudante\"],\n  \"required\": true,\n  \"modifyRecordField\": {\n   \"array\": true,\n   \"field\": \"roles\"\n  }\n },\n \"twitter\": {\n  \"type\": \"text\",\n  \"required\": true,\n  \"minLength\": 2,\n  \"maxLength\": 10\n }\n}</code> ",
"Accounts_CustomFieldsToShowInUserInfo": "Campos personalizados a exibir",
"Accounts_DefaultUsernamePrefixSuggestion": "Sugestão de prefixo de utilizador por defeito",
"Accounts_denyUnverifiedEmail": "Proibir e-mail não verificado",
"Accounts_EmailVerification": "Verificação de E-mail",
Expand Down
30 changes: 2 additions & 28 deletions packages/rocketchat-ldap/server/sync.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,35 +85,9 @@ getDataToSyncUserData = function getDataToSyncUserData(ldapUser, user) {
break;

case 'name':
const templateRegex = /#{(\w+)}/gi;
let match = templateRegex.exec(ldapField);
let tmpLdapField = ldapField;

if (match == null) {
if (!ldapUser.object.hasOwnProperty(ldapField)) {
logger.debug(`user does not have attribute: ${ ldapField }`);
return;
}
tmpLdapField = ldapUser.object[ldapField];
} else {
logger.debug('template found. replacing values');
while (match != null) {
const tmplVar = match[0];
const tmplAttrName = match[1];

if (!ldapUser.object.hasOwnProperty(tmplAttrName)) {
logger.debug(`user does not have attribute: ${ tmplAttrName }`);
return;
}

const attrVal = ldapUser.object[tmplAttrName];
logger.debug(`replacing template var: ${ tmplVar } with value from ldap: ${ attrVal }`);
tmpLdapField = tmpLdapField.replace(tmplVar, attrVal);
match = templateRegex.exec(ldapField);
}
}
const tmpLdapField = RocketChat.templateVarHandler(ldapField, ldapUser.object);

if (user.name !== tmpLdapField) {
if (tmpLdapField && user.name !== tmpLdapField) {
userData.name = tmpLdapField;
logger.debug(`user.name changed to: ${ tmpLdapField }`);
}
Expand Down
37 changes: 37 additions & 0 deletions packages/rocketchat-lib/lib/templateVarHandler.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
let logger;

if (Meteor.isServer) {
logger = new Logger('TemplateVarHandler', {});
}

RocketChat.templateVarHandler = function(variable, object) {

const templateRegex = /#{([\w\-]+)}/gi;
let match = templateRegex.exec(variable);
let tmpVariable = variable;

if (match == null) {
if (!object.hasOwnProperty(variable)) {
logger && logger.debug(`user does not have attribute: ${ variable }`);
return;
}
return object[variable];
} else {
logger && logger.debug('template found. replacing values');
while (match != null) {
const tmplVar = match[0];
const tmplAttrName = match[1];

if (!object.hasOwnProperty(tmplAttrName)) {
logger && logger.debug(`user does not have attribute: ${ tmplAttrName }`);
return;
}

const attrVal = object[tmplAttrName];
logger && logger.debug(`replacing template var: ${ tmplVar } with value: ${ attrVal }`);
tmpVariable = tmpVariable.replace(tmplVar, attrVal);
match = templateRegex.exec(variable);
}
return tmpVariable;
}
};
1 change: 1 addition & 0 deletions packages/rocketchat-lib/package.js
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ Package.onUse(function(api) {
api.addFiles('lib/slashCommand.js');
api.addFiles('lib/Message.js');
api.addFiles('lib/MessageTypes.js');
api.addFiles('lib/templateVarHandler.js');

api.addFiles('server/lib/bugsnag.js', 'server');
api.addFiles('server/lib/metrics.js', 'server');
Expand Down
4 changes: 4 additions & 0 deletions packages/rocketchat-lib/server/startup/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,10 @@ RocketChat.settings.addGroup('Accounts', function() {
type: 'boolean',
'public': true
});
this.add('Accounts_CustomFieldsToShowInUserInfo', '', {
type: 'string',
public: true
});
this.add('Accounts_LoginExpiration', 90, {
type: 'int',
'public': true
Expand Down
1 change: 1 addition & 0 deletions packages/rocketchat-ui-flextab/client/tabs/userInfo.html
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ <h3 title="{{name}}"><i class="status-{{status}}"></i> {{name}}</h3>
{{/if}}
{{#if lastLogin}} <p class="secondary-font-color"><i class="icon-calendar"></i> {{_ "Created_at"}}: {{createdAt}}</p> {{/if}}
{{#if lastLogin}} <p class="secondary-font-color"><i class="icon-calendar"></i> {{_ "Last_login"}}: {{lastLogin}}</p> {{/if}}
{{#each customField}} <p class="secondary-font-color">{{.}}</p> {{/each}}
{{#if services.facebook.id}} <p class="secondary-font-color"><i class="icon-facebook"></i><a href="{{services.facebook.link}}" target="_blank">{{services.facebook.name}}</a></p> {{/if}}
{{#if services.github.id}} <p class="secondary-font-color"><i class="icon-github-circled"></i><a href="https://www.github.com/{{services.github.username}}" target="_blank">{{services.github.username}}</a></p> {{/if}}
{{#if services.gitlab.id}} <p class="secondary-font-color"><i class="icon-gitlab"></i>{{services.gitlab.username}}</p> {{/if}}
Expand Down
33 changes: 33 additions & 0 deletions packages/rocketchat-ui-flextab/client/tabs/userInfo.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,39 @@ import moment from 'moment';
import toastr from 'toastr';

Template.userInfo.helpers({
customField() {
if (!RocketChat.authz.hasAllPermission('view-full-other-user-info')) {
return;
}

const sCustomFieldsToShow = RocketChat.settings.get('Accounts_CustomFieldsToShowInUserInfo').trim();
const customFields = [];

if (sCustomFieldsToShow) {
const user = Template.instance().user.get();
const userCustomFields = user && user.customFields || {};
const listOfCustomFieldsToShow = JSON.parse(sCustomFieldsToShow);

_.map(listOfCustomFieldsToShow, (el) => {
let content = '';
if (_.isObject(el)) {
_.map(el, (key, label) => {
const value = RocketChat.templateVarHandler(key, userCustomFields);
if (value) {
content = `${ label }: ${ value }`;
}
});
} else {
content = RocketChat.templateVarHandler(el, userCustomFields);
}
if (content) {
customFields.push(content);
}
});
}
return customFields;
},

name() {
const user = Template.instance().user.get();
return user && user.name ? user.name : TAPi18n.__('Unnamed');
Expand Down