diff --git a/app/lib/server/functions/saveUser.js b/app/lib/server/functions/saveUser.js index 5d1083a15fce..95f8b841b637 100644 --- a/app/lib/server/functions/saveUser.js +++ b/app/lib/server/functions/saveUser.js @@ -31,7 +31,7 @@ function validateUserData(userId, userData) { }); } - if (!userData._id && !hasPermission(userId, 'create-user')) { + if (!userData._id && !hasPermission(userId, 'create-user') && !userData.u) { throw new Meteor.Error('error-action-not-allowed', 'Adding user is not allowed', { method: 'insertOrUpdateUser', action: 'Adding_user', diff --git a/app/service-accounts/client/views/serviceAccountSidebarLogin.js b/app/service-accounts/client/views/serviceAccountSidebarLogin.js index ee652351b48b..c0f48151f8d4 100644 --- a/app/service-accounts/client/views/serviceAccountSidebarLogin.js +++ b/app/service-accounts/client/views/serviceAccountSidebarLogin.js @@ -1,11 +1,11 @@ import { Meteor } from 'meteor/meteor'; import { ReactiveVar } from 'meteor/reactive-var'; import { Template } from 'meteor/templating'; -import { FlowRouter } from 'meteor/kadira:flow-router'; import { handleError } from '../../../utils'; import FullUser from '../../../models/client/models/FullUser'; import './serviceAccountSidebarLogin.html'; +import { popover } from '../../../ui-utils/client'; Template.serviceAccountSidebarLogin.helpers({ isReady() { @@ -19,10 +19,10 @@ Template.serviceAccountSidebarLogin.helpers({ return Template.instance().users() && Template.instance().users().length > 0; }, owner() { - return Meteor.user().u; + return Meteor.user() && Meteor.user().u; }, showOwnerAccountLink() { - return localStorage.getItem('serviceAccountForceLogin') && !!Meteor.user().u; + return localStorage.getItem('serviceAccountForceLogin') && Meteor.user() && !!Meteor.user().u; }, }); @@ -30,24 +30,28 @@ Template.serviceAccountSidebarLogin.events({ 'click .js-login'(e) { e.preventDefault(); let { username } = this; - if (Meteor.user().u) { + if (Meteor.user() && Meteor.user().u) { username = Meteor.user().u.username; } Meteor.call('getLoginToken', username, function(error, token) { if (error) { return handleError(error); } - FlowRouter.go('/home'); - Meteor.loginWithToken(token.token, (err) => { + popover.close(); + Meteor.logout((err) => { if (err) { return handleError(err); } - document.location.reload(true); - if (Meteor.user().u) { - localStorage.setItem('serviceAccountForceLogin', true); - } else { - localStorage.removeItem('serviceAccountForceLogin'); - } + Meteor.loginWithToken(token.token, (err) => { + if (err) { + return handleError(err); + } + if (Meteor.user() && Meteor.user().u) { + localStorage.setItem('serviceAccountForceLogin', true); + } else { + localStorage.removeItem('serviceAccountForceLogin'); + } + }); }); }); }, diff --git a/app/service-accounts/server/methods/addServiceAccount.js b/app/service-accounts/server/methods/addServiceAccount.js index ee763c73068d..4e5ad4de45f3 100644 --- a/app/service-accounts/server/methods/addServiceAccount.js +++ b/app/service-accounts/server/methods/addServiceAccount.js @@ -31,7 +31,7 @@ Meteor.methods({ } if (!checkUsernameAvailability(userData.username)) { - throw new Meteor.Error('error-field-unavailable', `${ _.escape(userData.username) } is already in use :(`, { method: 'addServiceAccount' }); + throw new Meteor.Error('Username_already_exist', `${ _.escape(userData.username) } is already in use :(`, { method: 'addServiceAccount' }); } const user = Meteor.user(); @@ -39,7 +39,7 @@ Meteor.methods({ const limit = settings.get('Service_account_limit'); if (serviceAccounts.count() >= limit) { - throw new Meteor.Error('error-not-allowed', 'Max service account limit reached', { method: 'addServiceAccount' }); + throw new Meteor.Error('service-account-limit-reached', 'Max service account limit reached', { method: 'addServiceAccount' }); } userData.u = { diff --git a/app/ui-sidenav/client/sidebarHeader.js b/app/ui-sidenav/client/sidebarHeader.js index ed2de621991f..cb1d846773ce 100644 --- a/app/ui-sidenav/client/sidebarHeader.js +++ b/app/ui-sidenav/client/sidebarHeader.js @@ -74,7 +74,7 @@ const toolbarButtons = (user) => [{ { name: t('Service_account_login'), icon: 'reload', - condition: () => !Meteor.user().u || (Meteor.user().u && localStorage.getItem('serviceAccountForceLogin')), + condition: () => (Meteor.user() && !Meteor.user().u) || (Meteor.user() && Meteor.user().u && localStorage.getItem('serviceAccountForceLogin')), action: (e) => { const options = []; const config = { diff --git a/packages/rocketchat-i18n/i18n/en.i18n.json b/packages/rocketchat-i18n/i18n/en.i18n.json index 4dce615a4e7f..6228a181998f 100644 --- a/packages/rocketchat-i18n/i18n/en.i18n.json +++ b/packages/rocketchat-i18n/i18n/en.i18n.json @@ -2671,6 +2671,7 @@ "Service_account_dashboard": "Service Account Dashboard", "Service_account_description": "Service Accounts are an upgrade to existing user accounts. You can connect to a large number of users using service account with exclusive features such as broadcast message to all your subscribers at once", "Service_account_limit": "Service Accounts per user", + "service-account-limit-reached": "Max service account limit reached", "Service_account_login": "Service Account login", "Service_Accounts_SearchFields": "Fields to consider for service account search", "Service_account_name_placeholder": "Service Account name",