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

Feat : choose homeserver for login #9

Merged
merged 15 commits into from
Mar 23, 2022
Merged
Show file tree
Hide file tree
Changes from 12 commits
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
12 changes: 12 additions & 0 deletions config.sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,18 @@
"base_url": "https://matrix.i.tchap.gouv.fr"
}
},
"homeserver_list": [
{
"base_url": "https://matrix.i.tchap.gouv.fr",
"server_name": "Internes - preprod",
"email_examples": "hakim@beta.gouv.fr, alice@interieur.gouv.fr, ..."
},
{
"base_url": "https://matrix.e.tchap.gouv.fr",
"server_name": "Externes - preprod",
"email_examples": "nina@soprasteria.com, paul@athos.fr, ..."
}
],
"disable_custom_urls": false,
"disable_guests": true,
"disable_login_language_selector": false,
Expand Down
125 changes: 125 additions & 0 deletions src/components/views/dialogs/TchapServerPickerDialog.tsx
Original file line number Diff line number Diff line change
@@ -0,0 +1,125 @@
/*
Copyright 2020-2021 The Matrix.org Foundation C.I.C.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

import React from "react";
import SdkConfig from 'matrix-react-sdk/src/SdkConfig';
import { _t } from "matrix-react-sdk/src/languageHandler";
// We import components from the react-sdk like this to avoid " Attempted to get a component before a skin
// has been loaded"
import * as sdk from 'matrix-react-sdk/src/index';
import AutoDiscoveryUtils, { ValidatedServerConfig } from "matrix-react-sdk/src/utils/AutoDiscoveryUtils";

interface IProps {
title?: string;
serverConfig: ValidatedServerConfig;
onFinished(config?: ValidatedServerConfig): void;
}

interface IState {
selectedHomeServerUrl: string;
}

export default class TchapServerPickerDialog extends React.PureComponent<IProps, IState> {
static replaces = 'ServerPickerDialog';

homeServerList;

constructor(props) {
super(props);
this.homeServerList = SdkConfig.get()['homeserver_list'];
this.state = {
selectedHomeServerUrl: props.serverConfig.hsUrl,
};
}

private findHomeServerInListByUrl = url => {
return this.homeServerList.find(homeServer => homeServer.base_url === url);
};

private onSubmit = async (ev) => {
ev.preventDefault();

const selectedHomeServer = this.findHomeServerInListByUrl(this.state.selectedHomeServerUrl);

// Fake the discovery process, we don't need it since we know our own servers.
const discoveryResult = {
"m.homeserver": {
state: "SUCCESS",
error: null,
base_url: selectedHomeServer.base_url,
server_name: selectedHomeServer.server_name,
},
"m.identity_server": {
state: "PROMPT",
error: null,
base_url: selectedHomeServer.base_url, // On Tchap our Identity server urls and home server urls are the same
server_name: selectedHomeServer.server_name,
},
};
// Then continue the same flow as the original ServerPickerDialog.
const validatedConf = AutoDiscoveryUtils.buildValidatedConfigFromDiscovery(
discoveryResult['m.homeserver'].server_name, discoveryResult);
this.props.onFinished(validatedConf);
};

private onHomeServerSelected = (homeServerUrl) => {
this.setState({
selectedHomeServerUrl: homeServerUrl,
});
};

public render() {
// Imports
const BaseDialog = sdk.getComponent('dialogs.BaseDialog');
const AccessibleButton = sdk.getComponent('elements.AccessibleButton');
const StyledRadioGroup = sdk.getComponent('elements.StyledRadioGroup');

const radioButtonOptions = this.homeServerList.map(homeServer => {
return {
value: homeServer.base_url,
label: <span>{ homeServer.server_name }</span>,
description: <span> { _t("Examples:") } { homeServer.email_examples }</span>,
};
});

return <BaseDialog
title={this.props.title || _t("Sign into your homeserver")}
className="mx_ServerPickerDialog"
contentId="mx_ServerPickerDialog"
onFinished={this.props.onFinished}
fixedWidth={false}
hasCancel={true}
>
<form>
<div>
<label htmlFor="homeservers"> { _t("Choose a homeserver :") }</label>
</div>
<div>
<StyledRadioGroup
name="homeservers"
value={this.state.selectedHomeServerUrl}
onChange={this.onHomeServerSelected}
definitions={radioButtonOptions}
/>
</div>

<AccessibleButton className="mx_ServerPickerDialog_continue" kind="primary" onClick={this.onSubmit}>
{ _t("Continue") }
</AccessibleButton>
</form>
</BaseDialog>;
}
}
4 changes: 3 additions & 1 deletion src/i18n/strings/en_EN.json
Original file line number Diff line number Diff line change
Expand Up @@ -32,5 +32,7 @@
"Explore rooms": "Explore rooms",
"Username": "Email",
"Username_comments": "We use Email to login in Tchap. The Login code accepts both username and email. So the simplest change for us to force login by email is to just rename the Username field to Email.",
"Welcome to Tchap": "Bienvenue sur Tchap"
"Welcome to Tchap": "Bienvenue sur Tchap",
"Examples:": "Examples:",
"Choose a homeserver :": "Choose a homeserver :"
}
4 changes: 3 additions & 1 deletion src/i18n/strings/fr.json
Original file line number Diff line number Diff line change
Expand Up @@ -39,5 +39,7 @@
"Previous recently visited room or community": "Salon ou communauté précédemment visité",
"Username": "E-mail",
"Username_comments": "We use E-mail to login in Tchap. The Login code accepts both username and email. So the simplest change for us to force login by email is to just rename the Username field to Email.",
"Welcome to Tchap": "Bienvenue sur Tchap"
"Welcome to Tchap": "Bienvenue sur Tchap",
"Examples:": "Exemples :",
"Choose a homeserver :": "Choisissez un serveur d'accueil :"
}