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

[FIX] Service Accounts bugs fixed #13

Merged
merged 3 commits into from Jul 23, 2019
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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/lib/server/functions/saveUser.js
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
@@ -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
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
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
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