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] Import SAML language and auto join SAML channels #14203

Merged
merged 8 commits into from
Oct 18, 2019
43 changes: 42 additions & 1 deletion app/meteor-accounts-saml/server/saml_server.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,13 @@ import { Accounts } from 'meteor/accounts-base';
import { Random } from 'meteor/random';
import { WebApp } from 'meteor/webapp';
import { RoutePolicy } from 'meteor/routepolicy';
import { TAPi18n } from 'meteor/rocketchat:tap-i18n';
import bodyParser from 'body-parser';
import fiber from 'fibers';
import _ from 'underscore';

import { SAML } from './saml_utils';
import { CredentialTokens } from '../../models';
import { Rooms, Subscriptions, CredentialTokens } from '../../models';
import { generateUsernameSuggestion } from '../../lib';
import { _setUsername } from '../../lib/server/functions';

Expand Down Expand Up @@ -177,6 +178,7 @@ Accounts.registerLoginHandler(function(loginRequest) {
eppn: eduPersonPrincipalName,
globalRoles: ['user'],
emails,
services: {},
};

if (Accounts.saml.settings.generateUsername === true) {
Expand All @@ -187,8 +189,18 @@ Accounts.registerLoginHandler(function(loginRequest) {
newUser.username = username;
}

const languages = TAPi18n.getLanguages();
if (languages[loginResult.profile.language]) {
newUser.language = loginResult.profile.language;
}

const userId = Accounts.insertUserDoc({}, newUser);
user = Meteor.users.findOne(userId);

if (loginResult.profile.channels) {
const channels = loginResult.profile.channels.split(',');
Accounts.saml.subscribeToSAMLChannels(channels, user);
}
}

// If eppn is not exist then update
Expand Down Expand Up @@ -272,6 +284,35 @@ Accounts.registerLoginHandler(function(loginRequest) {
throw new Error('SAML Profile did not contain an email address');
});

Accounts.saml.subscribeToSAMLChannels = function(channels, user) {
try {
for (let roomName of channels) {
roomName = roomName.trim();
if (!roomName) {
continue;
}

let room = Rooms.findOneByNameAndType(roomName, 'c');
if (!room) {
room = Rooms.createWithIdTypeAndName(Random.id(), 'c', roomName);
}

if (!Subscriptions.findOneByRoomIdAndUserId(room._id, user._id)) {
Subscriptions.createWithRoomAndUser(room, user, {
ts: new Date(),
open: true,
alert: true,
unread: 1,
userMentions: 1,
groupMentions: 0,
});
}
}
} catch (err) {
console.error(err);
}
};

Accounts.saml.hasCredential = function(credentialToken) {
return CredentialTokens.findOneById(credentialToken) != null;
};
Expand Down