Skip to content

Commit ca032c2

Browse files
ZhaoJiSenfeng626
authored andcommitted
Fixed: Fixed an issue where the auto-login state could not be saved for certain special account types.
1 parent ca27364 commit ca032c2

2 files changed

Lines changed: 81 additions & 71 deletions

File tree

src/app/elements/connect/connect.component.ts

Lines changed: 40 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
1-
import {Component, EventEmitter, Input, OnDestroy, OnInit, Output} from '@angular/core';
2-
import {connectEvt} from '@app/globals';
1+
import { Component, EventEmitter, Input, OnDestroy, OnInit, Output } from '@angular/core';
2+
import { connectEvt } from '@app/globals';
33
import {
44
AlertService,
55
AppService,
@@ -10,18 +10,17 @@ import {
1010
SettingService,
1111
ViewService
1212
} from '@app/services';
13-
import {Account, Asset, ConnectData, ConnectionToken, View} from '@app/model';
14-
import {launchLocalApp} from '@app/utils/common';
15-
import {ElementConnectDialogComponent} from '@app/elements/connect/connect-dialog/connect-dialog.component';
16-
import {NzModalService} from 'ng-zorro-antd/modal';
17-
import {ElementDownloadDialogComponent} from './download-dialog/download-dialog.component';
18-
import {firstValueFrom} from 'rxjs';
19-
13+
import { Account, Asset, ConnectData, ConnectionToken, View } from '@app/model';
14+
import { launchLocalApp } from '@app/utils/common';
15+
import { ElementConnectDialogComponent } from '@app/elements/connect/connect-dialog/connect-dialog.component';
16+
import { NzModalService } from 'ng-zorro-antd/modal';
17+
import { ElementDownloadDialogComponent } from './download-dialog/download-dialog.component';
18+
import { firstValueFrom } from 'rxjs';
2019

2120
@Component({
2221
standalone: false,
2322
selector: 'elements-connect',
24-
templateUrl: 'connect.component.html',
23+
templateUrl: 'connect.component.html'
2524
})
2625
export class ElementConnectComponent implements OnInit, OnDestroy {
2726
@Output() onNewView: EventEmitter<View> = new EventEmitter<View>();
@@ -32,17 +31,17 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
3231
accounts: Array<Account>;
3332
preConnectData: ConnectData;
3433

35-
constructor(private _appSvc: AppService,
36-
private _alert: AlertService,
37-
private _dialog: NzModalService,
38-
private _logger: LogService,
39-
private _http: HttpService,
40-
private _connectTokenSvc: ConnectTokenService,
41-
private _i18n: I18nService,
42-
private _settingSvc: SettingService,
43-
public viewSrv: ViewService,
44-
) {
45-
}
34+
constructor(
35+
private _appSvc: AppService,
36+
private _alert: AlertService,
37+
private _dialog: NzModalService,
38+
private _logger: LogService,
39+
private _http: HttpService,
40+
private _connectTokenSvc: ConnectTokenService,
41+
private _i18n: I18nService,
42+
private _settingSvc: SettingService,
43+
public viewSrv: ViewService
44+
) {}
4645

4746
ngOnInit(): void {
4847
this.subscribeConnectEvent();
@@ -87,7 +86,7 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
8786
const idObject = {};
8887
const idList = id.split('&');
8988
for (const element of idList) {
90-
idObject[element.split('=')[0]] = (element.split('=')[1]);
89+
idObject[element.split('=')[0]] = element.split('=')[1];
9190
}
9291
return idObject;
9392
}
@@ -123,7 +122,9 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
123122
*/
124123
async connectAsset(asset: Asset, splitConnect = false) {
125124
if (!asset) {
126-
const msg = this._i18n.instant('Asset not found or You have no permission to access it, please refresh asset tree');
125+
const msg = this._i18n.instant(
126+
'Asset not found or You have no permission to access it, please refresh asset tree'
127+
);
127128
const title = await this._i18n.instant('Permission expired');
128129
await this._alert.error(msg, title);
129130
return;
@@ -153,13 +154,6 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
153154
return;
154155
}
155156

156-
// if (connToken.protocol === 'k8s') {
157-
// const url = `${window.location.protocol}//${window.location.host}/luna/k8s/${connToken.id}?asset=${asset.id}`;
158-
// window.open(url);
159-
// return;
160-
// console.log('k8s', this.viewSrv)
161-
// }
162-
163157
// 分屏连接
164158
if (splitConnect) {
165159
return this.currentWebSubView(asset, connectInfo, connToken);
@@ -177,7 +171,11 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
177171
}
178172

179173
if (connectInfo.downloadRDP) {
180-
return this._http.downloadRDPFile(connToken, this._settingSvc.setting, connectInfo.connectOption);
174+
return this._http.downloadRDPFile(
175+
connToken,
176+
this._settingSvc.setting,
177+
connectInfo.connectOption
178+
);
181179
} else if (connectMethod.type === 'native') {
182180
this.callLocalClient(connToken).then();
183181
} else if (connectMethod.type === 'applet' && appletConnectMethod === 'client') {
@@ -192,25 +190,25 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
192190
if (connToken.connect_options.token_reusable) {
193191
await this._connectTokenSvc.setReusable(connToken, true).toPromise();
194192
}
195-
const response = await firstValueFrom(this._http.getLocalClientUrl(connToken, this._settingSvc.setting));
193+
const response = await firstValueFrom(
194+
this._http.getLocalClientUrl(connToken, this._settingSvc.setting)
195+
);
196196
const url = response['url'];
197197
launchLocalApp(url, () => {
198198
const downLoadStatus = localStorage.getItem('hasDownLoadApp');
199199
if (downLoadStatus !== '1') {
200200
this._dialog.create({
201201
nzTitle: this._i18n.instant('DownloadClient'),
202202
nzContent: ElementDownloadDialogComponent,
203-
nzOnOk: (cmp => cmp.onConfirm()),
204-
nzOnCancel: (cmp => cmp.onCancel()),
203+
nzOnOk: cmp => cmp.onConfirm(),
204+
nzOnCancel: cmp => cmp.onCancel()
205205
});
206206
}
207207
});
208208
}
209209

210-
211210
createWebView(asset: Asset, connectInfo: any, connToken: ConnectionToken) {
212211
const view = new View(asset, connectInfo, connToken, 'node');
213-
console.log('createWebView', view);
214212
this.onNewView.emit(view);
215213
}
216214

@@ -224,6 +222,7 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
224222
this._logger.debug('No account or node');
225223
return false;
226224
}
225+
227226
if (!preData.autoLogin) {
228227
this._logger.debug('Not auto login');
229228
return false;
@@ -237,16 +236,20 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
237236
const account = accounts.find(item => {
238237
return item.alias === preAccount.alias;
239238
});
239+
240240
if (!account) {
241241
this._logger.debug('Account may be not valid');
242242
return false;
243243
}
244+
244245
// 验证登录信息
245246
const preAuth = preData.manualAuthInfo;
247+
246248
if (!account.has_secret && (!preAuth || !preAuth.secret)) {
247249
this._logger.debug('Account no manual auth');
248250
return false;
249251
}
252+
250253
// 验证连接方式
251254
const connectMethods = this._appSvc.getProtocolConnectMethods(preData.protocol.name);
252255
if (!connectMethods) {
@@ -286,7 +289,7 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
286289
nzCentered: true,
287290
nzClassName: 'connect-dialog',
288291
nzWrapClassName: 'connect-dialog-wrap',
289-
nzFooter: null,
292+
nzFooter: null
290293
});
291294

292295
return new Promise<ConnectData>(resolve => {

src/app/services/app.ts

Lines changed: 41 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,15 @@
1-
import {Injectable} from '@angular/core';
2-
import {Router} from '@angular/router';
3-
import {CookieService} from 'ngx-cookie-service';
4-
import {environment} from '@src/environments/environment';
5-
import {DataStore, User} from '@app/globals';
6-
import {HttpService} from './http';
7-
import {LocalStorageService, LogService} from './share';
8-
import {SettingService} from '@app/services/setting';
9-
import {Account, Asset, AuthInfo, ConnectData, Endpoint, Organization, View} from '@app/model';
1+
import { Injectable } from '@angular/core';
2+
import { Router } from '@angular/router';
3+
import { CookieService } from 'ngx-cookie-service';
4+
import { environment } from '@src/environments/environment';
5+
import { DataStore, User } from '@app/globals';
6+
import { HttpService } from './http';
7+
import { LocalStorageService, LogService } from './share';
8+
import { SettingService } from '@app/services/setting';
9+
import { Account, Asset, AuthInfo, ConnectData, Endpoint, Organization, View } from '@app/model';
1010
import * as CryptoJS from 'crypto-js';
11-
import {OrganizationService} from './organization';
12-
import {I18nService} from '@app/services/i18n';
11+
import { OrganizationService } from './organization';
12+
import { I18nService } from '@app/services/i18n';
1313

1414
declare function unescape(s: string): string;
1515

@@ -34,14 +34,16 @@ export class AppService {
3434
private newLoginHasOpen = false; // 避免多次打开新登录页
3535
private checkSecond = 120;
3636

37-
constructor(private _http: HttpService,
38-
private _router: Router,
39-
private _cookie: CookieService,
40-
private _i18n: I18nService,
41-
private _logger: LogService,
42-
private _settingSvc: SettingService,
43-
private _localStorage: LocalStorageService,
44-
private _orgSvc: OrganizationService) {
37+
constructor(
38+
private _http: HttpService,
39+
private _router: Router,
40+
private _cookie: CookieService,
41+
private _i18n: I18nService,
42+
private _logger: LogService,
43+
private _settingSvc: SettingService,
44+
private _localStorage: LocalStorageService,
45+
private _orgSvc: OrganizationService
46+
) {
4547
this.setLogLevel();
4648
this.setOrgFromQueryString();
4749
this.checkLogin();
@@ -62,7 +64,7 @@ export class AppService {
6264
setOrgFromQueryString() {
6365
const oid = this.getQueryString('oid');
6466
if (oid) {
65-
const currentOrg: Organization = {id: oid, name: ''};
67+
const currentOrg: Organization = { id: oid, name: '' };
6668
this._orgSvc.switchOrg(currentOrg);
6769
}
6870
}
@@ -127,9 +129,9 @@ export class AppService {
127129

128130
getErrorMsg(status: string) {
129131
const messages = {
130-
'unauthorized': 'LoginExpireMsg',
131-
'badrequest': 'Bad request. The server does not understand the syntax of the request',
132-
'error': 'The server encountered an error while trying to process the request'
132+
unauthorized: 'LoginExpireMsg',
133+
badrequest: 'Bad request. The server does not understand the syntax of the request',
134+
error: 'The server encountered an error while trying to process the request'
133135
};
134136
return messages[status];
135137
}
@@ -168,7 +170,6 @@ export class AppService {
168170

169171
this._http.getUserProfile().then(
170172
user => {
171-
console.log('User is: ', user);
172173
this._orgSvc.setWorkbenchOrgs(user['workbench_orgs']);
173174
Object.assign(User, user);
174175
User.logined = true;
@@ -182,7 +183,7 @@ export class AppService {
182183
gotoLogin();
183184
}
184185
// this._router.navigate(['login']);
185-
},
186+
}
186187
);
187188
}
188189

@@ -237,17 +238,17 @@ export class AppService {
237238
}
238239

239240
setPreConnectData(asset: Asset, connectData: ConnectData) {
240-
const {account, protocol, connectMethod, manualAuthInfo, connectOption} = connectData;
241+
const { account, protocol, connectMethod, manualAuthInfo, connectOption } = connectData;
241242
const key = `JMS_PRE_${asset.id}`;
242243

243244
const saveData = {
244-
account: {alias: account.alias, username: account.username, has_secret: account.has_secret},
245-
connectMethod: {value: connectMethod.value},
246-
protocol: {name: protocol.name},
245+
account: { alias: account.alias, username: account.username, has_secret: account.has_secret },
246+
connectMethod: { value: connectMethod.value },
247+
protocol: { name: protocol.name },
247248
downloadRDP: connectData.downloadRDP,
248249
autoLogin: connectData.autoLogin,
249250
connectOption,
250-
direct: connectData.direct,
251+
direct: connectData.direct
251252
};
252253
this.setAccountLocalAuth(asset, account, manualAuthInfo);
253254
this._localStorage.set(key, saveData);
@@ -330,7 +331,13 @@ export class AppService {
330331

331332
setAccountLocalAuth(asset: Asset, account: Account, auth: AuthInfo) {
332333
const assetId = asset.id;
333-
const newAuth = Object.assign({alias: account.alias, username: account.username}, auth);
334+
const newAuth = Object.assign({ alias: account.alias, username: account.username }, auth);
335+
336+
// 如果 auth.alias 是 undefined,保持使用 account.alias
337+
if (auth.alias === undefined && account.alias !== undefined) {
338+
newAuth.alias = account.alias;
339+
}
340+
334341
if (!auth.secret || !auth.rememberAuth) {
335342
newAuth.secret = '';
336343
} else {
@@ -340,7 +347,7 @@ export class AppService {
340347
let auths = this.getAccountLocalAuth(assetId, false);
341348
const localKey = `JMS_MA_${assetId}`;
342349

343-
auths = auths.filter((item) => item.username !== newAuth.username);
350+
auths = auths.filter(item => item.username !== newAuth.username);
344351
auths.splice(0, 0, newAuth);
345352
this._localStorage.set(localKey, auths);
346353
}
@@ -355,7 +362,7 @@ export class AppService {
355362
if (protocol === 'http') {
356363
protocol = window.location.protocol.replace(':', '');
357364
}
358-
const data = {'assetId': '', 'sessionId': '', 'token': ''};
365+
const data = { assetId: '', sessionId: '', token: '' };
359366
if (view.connectToken) {
360367
data['token'] = view.connectToken.id;
361368
} else {
@@ -369,7 +376,7 @@ export class AppService {
369376
}
370377
}
371378
const res = this._http.getSmartEndpoint(data, protocol);
372-
res.catch((err) => {
379+
res.catch(err => {
373380
alert(err.error.detail);
374381
});
375382
return res;

0 commit comments

Comments
 (0)