Skip to content

Commit

Permalink
Merge pull request #13 from bhardwajaditya/bugs
Browse files Browse the repository at this point in the history
[FIX] Service Accounts bugs fixed
  • Loading branch information
bhardwajaditya committed Jul 23, 2019
2 parents 1b4b75f + a9313ad commit 1579a5a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 16 deletions.
2 changes: 1 addition & 1 deletion app/lib/server/functions/saveUser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down
28 changes: 16 additions & 12 deletions app/service-accounts/client/views/serviceAccountSidebarLogin.js
Original file line number Diff line number Diff line change
@@ -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() {
Expand All @@ -19,35 +19,39 @@ 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;
},
});

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');
}
});
});
});
},
Expand Down
4 changes: 2 additions & 2 deletions app/service-accounts/server/methods/addServiceAccount.js
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,15 @@ Meteor.methods({
}

if (!checkUsernameAvailability(userData.username)) {
throw new Meteor.Error('error-field-unavailable', `<strong>${ _.escape(userData.username) }</strong> 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();
const serviceAccounts = Users.findLinkedServiceAccounts(user._id, {});
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 = {
Expand Down
2 changes: 1 addition & 1 deletion app/ui-sidenav/client/sidebarHeader.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
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 @@ -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",
Expand Down

0 comments on commit 1579a5a

Please sign in to comment.