fix(core-connections): credentials saving

This commit is contained in:
Wroud
2020-09-07 16:17:22 +03:00
parent 55b235783e
commit 858778a226
4 changed files with 50 additions and 63 deletions
@@ -173,11 +173,8 @@ export const ConnectionForm = observer(function ConnectionForm({
<>
<TabPanel tabId='options'>
<Options
connection={model.connection}
model={model}
type={controller.connectionType}
credentials={model.credentials}
availableDrivers={model.availableDrivers}
editing={model.editing}
disabled={controller.isDisabled}
onTypeChange={controller.setType}
onSave={controller.save}
@@ -143,8 +143,8 @@ implements IInitializableController, IDestructibleController {
} else {
config.url = this.model.connection.url;
}
if (this.model.connection.authModel) {
config.authModelId = this.model.connection.authModel;
if (this.model.connection.authModel || this.driver!.defaultAuthModel) {
config.authModelId = this.model.connection.authModel || this.driver!.defaultAuthModel;
config.saveCredentials = this.isCredentialsChanged();
if (config.saveCredentials) {
config.credentials = this.model.credentials;
@@ -25,22 +25,19 @@ import {
} from '@cloudbeaver/core-blocks';
import { useController } from '@cloudbeaver/core-di';
import { useTranslate } from '@cloudbeaver/core-localization';
import { ConnectionInfo } from '@cloudbeaver/core-sdk';
import { useStyles } from '@cloudbeaver/core-theming';
import { EConnectionType } from '../EConnectionType';
import { IConnectionFormModel } from '../IConnectionFormModel';
import { formStyles } from './formStyles';
import { OptionsController } from './OptionsController';
import { ParametersForm } from './ParametersForm';
type Props = {
connection: ConnectionInfo;
model: IConnectionFormModel;
type: EConnectionType;
credentials: Record<string, string | number>;
availableDrivers: string[];
saving?: boolean;
disabled?: boolean;
editing?: boolean;
onTypeChange(type: EConnectionType): void;
onSave?(): void;
}
@@ -65,17 +62,14 @@ const styles = css`
`;
export const Options = observer(function Options({
connection,
model,
type,
availableDrivers,
credentials,
disabled,
saving,
editing,
onTypeChange,
onSave,
}: Props) {
const controller = useController(OptionsController, connection, credentials, availableDrivers);
const controller = useController(OptionsController, model);
const translate = useTranslate();
const [focusedRef] = useFocus<HTMLFormElement>({ focusFirstChild: true });
@@ -86,22 +80,22 @@ export const Options = observer(function Options({
<group as="div">
<Checkbox
name="template"
value={connection.id}
state={connection}
value={model.connection.id}
state={model.connection}
checkboxLabel={translate('connections_connection_template')}
disabled={editing || disabled}
disabled={model.editing || disabled}
mod='surface'
/>
</group>
<group as="div">
<Combobox
name='driverId'
state={connection}
state={model.connection}
items={controller.drivers}
keySelector={driver => driver.id}
valueSelector={driver => driver?.name!}
onSelect={controller.onSelectDriver}
readOnly={editing || controller.drivers.length < 2}
readOnly={model.editing || controller.drivers.length < 2}
mod={'surface'}
disabled={disabled}
>
@@ -112,7 +106,7 @@ export const Options = observer(function Options({
<InputField
type="text"
name="name"
state={connection}
state={model.connection}
disabled={disabled}
mod='surface'
>
@@ -123,7 +117,7 @@ export const Options = observer(function Options({
<Textarea
name="description"
rows={3}
state={connection}
state={model.connection}
disabled={disabled}
mod='surface'
>
@@ -145,7 +139,7 @@ export const Options = observer(function Options({
<TabsState currentTabId={type}>
<TabPanel tabId={EConnectionType.Parameters}>
<ParametersForm
connection={connection}
connection={model.connection}
embedded={controller.driver?.embedded}
disabled={disabled || saving}
/>
@@ -155,7 +149,7 @@ export const Options = observer(function Options({
<InputField
type="text"
name="url"
state={connection}
state={model.connection}
disabled={disabled}
autoComplete={`section-${controller.driver?.id || 'driver'} section-jdbc`}
mod='surface'
@@ -173,7 +167,7 @@ export const Options = observer(function Options({
<ObjectPropertyInfoForm
autofillToken='new-password'
properties={controller.authModel.properties}
credentials={credentials}
credentials={model.credentials}
disabled={disabled}
/>
</>
@@ -6,42 +6,40 @@
* you may not use this file except in compliance with the License.
*/
import { observable, action, computed } from 'mobx';
import { action, computed } from 'mobx';
import { injectable, IInitializableController } from '@cloudbeaver/core-di';
import { NotificationService } from '@cloudbeaver/core-events';
import { ConnectionInfo } from '@cloudbeaver/core-sdk';
import { DatabaseAuthModelsResource } from '../../../../DatabaseAuthModelsResource';
import { DBDriverResource } from '../../../../DBDriverResource';
import { IConnectionFormModel } from '../IConnectionFormModel';
@injectable()
export class OptionsController
implements IInitializableController {
@observable credentials!: Record<string, number | string>;
@observable availableDrivers!: string[];
@computed get drivers() {
return Array.from(this.dbDriverResource.data.values())
.filter(({ id }) => this.availableDrivers.includes(id));
.filter(({ id }) => this.model.availableDrivers.includes(id));
}
@computed get driver() {
return this.dbDriverResource.get(this.connectionInfo.driverId);
return this.dbDriverResource.get(this.model.connection.driverId);
}
@computed get authModel() {
if (!this.connectionInfo?.authModel && !this.driver) {
if (!this.model.connection?.authModel && !this.driver) {
return null;
}
return this.dbAuthModelsResource.get(this.connectionInfo?.authModel || this.driver!.defaultAuthModel) || null;
return this.dbAuthModelsResource.get(this.model.connection?.authModel || this.driver!.defaultAuthModel) || null;
}
@computed get authModelLoading() {
return this.dbAuthModelsResource.isLoading();
}
private connectionInfo!: ConnectionInfo;
private model!: IConnectionFormModel;
private nameTemplate = /^.*?\s(|\(.*?\)\s)connection$/
constructor(
@@ -50,10 +48,8 @@ implements IInitializableController {
private dbDriverResource: DBDriverResource,
) { }
init(connection: ConnectionInfo, credentials: Record<string, number | string>, availableDrivers: string[]) {
this.connectionInfo = connection;
this.credentials = credentials;
this.availableDrivers = availableDrivers;
init(model: IConnectionFormModel) {
this.model = model;
this.loadDrivers();
}
@@ -63,34 +59,34 @@ implements IInitializableController {
@action
private setDefaults(prevDriverId: string | null) {
this.setDefaultParameters(prevDriverId);
this.connectionInfo.properties = {};
this.connectionInfo.authModel = this.driver?.defaultAuthModel;
this.model.connection.properties = {};
this.model.connection.authModel = this.driver?.defaultAuthModel;
this.cleanCredentials();
}
private cleanCredentials() {
for (const property of Object.keys(this.credentials)) {
delete this.credentials[property];
for (const property of Object.keys(this.model.credentials)) {
delete this.model.credentials[property];
}
}
private setDefaultParameters(prevDriverId?: string | null) {
const prevDriver = this.dbDriverResource.get(prevDriverId || '');
if (this.connectionInfo.host === prevDriver?.defaultServer) {
this.connectionInfo.host = this.driver?.defaultServer;
if (this.model.connection.host === prevDriver?.defaultServer) {
this.model.connection.host = this.driver?.defaultServer;
}
if (this.connectionInfo.port === prevDriver?.defaultPort) {
this.connectionInfo.port = this.driver?.defaultPort;
if (this.model.connection.port === prevDriver?.defaultPort) {
this.model.connection.port = this.driver?.defaultPort;
}
if (this.connectionInfo.databaseName === prevDriver?.defaultDatabase) {
this.connectionInfo.databaseName = this.driver?.defaultDatabase;
if (this.model.connection.databaseName === prevDriver?.defaultDatabase) {
this.model.connection.databaseName = this.driver?.defaultDatabase;
}
if (this.connectionInfo.url === prevDriver?.sampleURL) {
this.connectionInfo.url = this.driver?.sampleURL;
if (this.model.connection.url === prevDriver?.sampleURL) {
this.model.connection.url = this.driver?.sampleURL;
}
this.updateName();
@@ -100,17 +96,17 @@ implements IInitializableController {
const databaseNames = ['New', ...this.drivers.map(driver => driver.name!)]
.filter(Boolean);
if (!this.connectionInfo.name
|| (this.nameTemplate.test(this.connectionInfo.name)
&& databaseNames.some(driver => this.connectionInfo.name.startsWith(driver)))
if (!this.model.connection.name
|| (this.nameTemplate.test(this.model.connection.name)
&& databaseNames.some(driver => this.model.connection.name.startsWith(driver)))
) {
this.connectionInfo.name = this.getNameTemplate();
this.model.connection.name = this.getNameTemplate();
}
}
private getNameTemplate() {
if (this.driver) {
let address = [this.connectionInfo.host, this.connectionInfo.host && this.connectionInfo.port]
let address = [this.model.connection.host, this.model.connection.host && this.model.connection.port]
.filter(Boolean)
.join(':');
@@ -135,12 +131,12 @@ implements IInitializableController {
try {
await this.dbAuthModelsResource.load(
this.connectionInfo?.authModel || this.driver.defaultAuthModel
this.model.connection?.authModel || this.driver.defaultAuthModel
);
if (this.authModel) {
for (const property of this.connectionInfo.authProperties) {
this.credentials[property.id!] = property.value;
for (const property of this.model.connection.authProperties) {
this.model.credentials[property.id!] = property.value;
}
}
} catch (exception) {
@@ -153,7 +149,7 @@ implements IInitializableController {
private async loadDriver(driverId: string | null, prev: string | null) {
if (!driverId) {
this.connectionInfo.authModel = undefined;
this.model.connection.authModel = undefined;
this.cleanCredentials();
return;
}
@@ -171,12 +167,12 @@ implements IInitializableController {
try {
await this.dbAuthModelsResource.load(
this.connectionInfo?.authModel || this.driver.defaultAuthModel
this.model.connection?.authModel || this.driver.defaultAuthModel
);
if (this.authModel) {
for (const property of this.connectionInfo.authProperties) {
this.credentials[property.id!] = property.value;
for (const property of this.model.connection.authProperties) {
this.model.credentials[property.id!] = property.value;
}
}
} catch (exception) {