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] Settings to further customize GitLab OAuth #15014

Merged
merged 6 commits into from
Jul 21, 2019
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
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 @@ -145,4 +145,5 @@ import './v144';
import './v145';
import './v146';
import './v147';
import './v148';
import './xrun';
24 changes: 24 additions & 0 deletions server/startup/migrations/v148.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Migrations } from '../../../app/migrations/server';
import { settings } from '../../../app/settings';
Hudell marked this conversation as resolved.
Show resolved Hide resolved
import { Settings } from '../../../app/models';
Hudell marked this conversation as resolved.
Show resolved Hide resolved

Migrations.add({
version: 148,
up() {
settings.get('Accounts_OAuth_Gitlab', function(key, value) {
Hudell marked this conversation as resolved.
Show resolved Hide resolved
if (value) {
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
},
});