Skip to content

Commit

Permalink
[NEW] Settings to further customize GitLab OAuth (#15014)
Browse files Browse the repository at this point in the history
Co-Authored-By: Hajo Möller <dasjoe@gmail.com>
  • Loading branch information
2 people authored and sampaiodiego committed Jul 21, 2019
1 parent ac7ac5b commit 49feadc
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 5 deletions.
31 changes: 27 additions & 4 deletions app/gitlab/lib/common.js
Original file line number Diff line number Diff line change
@@ -1,13 +1,15 @@
import { Meteor } from 'meteor/meteor';
import { Tracker } from 'meteor/tracker';
import _ from 'underscore';

import { settings } from '../../settings';
import { CustomOAuth } from '../../custom-oauth';

const config = {
serverURL: 'https://gitlab.com',
identityPath: '/api/v3/user',
identityPath: '/api/v4/user',
scope: 'read_user',
mergeUsers: false,
addAutopublishFields: {
forLoggedInUser: ['services.gitlab'],
forOtherUsers: ['services.gitlab.username'],
Expand All @@ -19,16 +21,37 @@ const Gitlab = new CustomOAuth('gitlab', config);

if (Meteor.isServer) {
Meteor.startup(function() {
settings.get('API_Gitlab_URL', function(key, value) {
config.serverURL = value.trim().replace(/\/*$/, '');
const updateConfig = _.debounce(() => {
config.serverURL = settings.get('API_Gitlab_URL').trim().replace(/\/*$/, '') || config.serverURL;
config.identityPath = settings.get('Accounts_OAuth_Gitlab_identity_path') || config.identityPath;
config.mergeUsers = Boolean(settings.get('Accounts_OAuth_Gitlab_merge_users'));
Gitlab.configure(config);
});
}, 300);

settings.get('API_Gitlab_URL', updateConfig);
settings.get('Accounts_OAuth_Gitlab_identity_path', updateConfig);
settings.get('Accounts_OAuth_Gitlab_merge_users', updateConfig);
});
} else {
Meteor.startup(function() {
Tracker.autorun(function() {
let anyChange = false;
if (settings.get('API_Gitlab_URL')) {
config.serverURL = settings.get('API_Gitlab_URL').trim().replace(/\/*$/, '');
anyChange = true;
}

if (settings.get('Accounts_OAuth_Gitlab_identity_path')) {
config.identityPath = settings.get('Accounts_OAuth_Gitlab_identity_path').trim() || config.identityPath;
anyChange = true;
}

if (settings.get('Accounts_OAuth_Gitlab_merge_users')) {
config.mergeUsers = true;
anyChange = true;
}

if (anyChange) {
Gitlab.configure(config);
}
});
Expand Down
2 changes: 2 additions & 0 deletions app/gitlab/server/startup.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ settings.addGroup('OAuth', function() {
this.add('API_Gitlab_URL', '', { type: 'string', enableQuery, public: true, secret: true });
this.add('Accounts_OAuth_Gitlab_id', '', { type: 'string', enableQuery });
this.add('Accounts_OAuth_Gitlab_secret', '', { type: 'string', enableQuery, secret: true });
this.add('Accounts_OAuth_Gitlab_identity_path', '/api/v4/user', { type: 'string', public: true, enableQuery });
this.add('Accounts_OAuth_Gitlab_merge_users', false, { type: 'boolean', public: true, enableQuery });
this.add('Accounts_OAuth_Gitlab_callback_url', '_oauth/gitlab', { type: 'relativeUrl', readonly: true, force: true, enableQuery });
});
});
2 changes: 2 additions & 0 deletions packages/rocketchat-i18n/i18n/en.i18n.json
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,8 @@
"Accounts_OAuth_Gitlab": "OAuth Enabled",
"Accounts_OAuth_Gitlab_callback_url": "GitLab Callback URL",
"Accounts_OAuth_Gitlab_id": "GitLab Id",
"Accounts_OAuth_Gitlab_identity_path": "Identity Path",
"Accounts_OAuth_Gitlab_merge_users": "Merge Users",
"Accounts_OAuth_Gitlab_secret": "Client Secret",
"Accounts_OAuth_Google": "Google Login",
"Accounts_OAuth_Google_callback_url": "Google Callback URL",
Expand Down
1 change: 1 addition & 0 deletions server/startup/migrations/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -146,4 +146,5 @@ import './v145';
import './v146';
import './v147';
import './v148';
import './v149';
import './xrun';
22 changes: 22 additions & 0 deletions server/startup/migrations/v149.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
import { Migrations } from '../../../app/migrations/server';
import { settings } from '../../../app/settings/server';
import { Settings } from '../../../app/models/server';

Migrations.add({
version: 149,
up() {
if (settings.get('Accounts_OAuth_Gitlab')) {
const setting = Settings.findById('Accounts_OAuth_Gitlab_identity_path');
const newValue = '/api/v3/user';

if (setting) {
Settings.updateValueById('Accounts_OAuth_Gitlab_identity_path', newValue);
} else {
Settings.createWithIdAndValue('Accounts_OAuth_Gitlab_identity_path', newValue);
}
}
},
down() {
// Down migration does not apply in this case
},
});
2 changes: 1 addition & 1 deletion tests/end-to-end/ui/11-admin.js
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ describe('[Administration]', () => {
before(() => {
admin.infoLink.waitForVisible(5000);
admin.infoLink.click();
admin.infoRocketChatTable.waitForVisible(5000);
admin.infoRocketChatTable.waitForVisible(10000);
});
it('the first title should be Rocket.Chat', () => {
admin.infoRocketChatTableTitle.getText().should.equal('Rocket.Chat');
Expand Down

0 comments on commit 49feadc

Please sign in to comment.