Commit cf63e0c9 authored by ibuler's avatar ibuler

[Update] 修改选择系统用户和remote app登陆

parent 8ff84f5b
...@@ -86,6 +86,11 @@ export class HttpService { ...@@ -86,6 +86,11 @@ export class HttpService {
return this.http.get<Array<TreeNode>>(url); return this.http.get<Array<TreeNode>>(url);
} }
getMyRemoteAppSystemUsers(remoteAppId: string) {
const url = `/api/v1/perms/users/remote-apps/${remoteAppId}/system-users/`;
return this.http.get<Array<SystemUser>>(url);
}
getMyAssetSystemUsers(assetId: string) { getMyAssetSystemUsers(assetId: string) {
const url = `/api/v1/perms/users/assets/${assetId}/system-users/`; const url = `/api/v1/perms/users/assets/${assetId}/system-users/`;
return this.http.get<Array<SystemUser>>(url); return this.http.get<Array<SystemUser>>(url);
...@@ -138,10 +143,11 @@ export class HttpService { ...@@ -138,10 +143,11 @@ export class HttpService {
); );
} }
guacamoleAddRemoteApp(userId: string, remoteAppId: string, systemUserUsername?: string, systemUserPassword?: string) { guacamoleAddRemoteApp(userId: string, remoteAppId: string, sysUserId: string, systemUserUsername?: string, systemUserPassword?: string) {
let params = new HttpParams() let params = new HttpParams()
.set('user_id', userId) .set('user_id', userId)
.set('remote_app_id', remoteAppId) .set('remote_app_id', remoteAppId)
.set('system_user_id', sysUserId)
.set('token', DataStore.guacamoleToken); .set('token', DataStore.guacamoleToken);
let body = new HttpParams(); let body = new HttpParams();
if (systemUserUsername && systemUserPassword) { if (systemUserUsername && systemUserPassword) {
......
import {Component, OnInit, Output, Inject, OnDestroy, EventEmitter} from '@angular/core'; import {Component, OnInit, Output, Inject, OnDestroy, EventEmitter} from '@angular/core';
import {Observable, Subject} from 'rxjs';
import 'rxjs/add/operator/toPromise';
import {connectEvt} from '@app/globals'; import {connectEvt} from '@app/globals';
import {AppService, HttpService, LogService, NavService} from '@app/app.service'; import {AppService, HttpService, LogService, NavService} from '@app/app.service';
import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from '@angular/material'; import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from '@angular/material';
...@@ -7,6 +9,7 @@ import {ActivatedRoute} from '@angular/router'; ...@@ -7,6 +9,7 @@ import {ActivatedRoute} from '@angular/router';
import {SystemUser, TreeNode, Asset} from '@app/model'; import {SystemUser, TreeNode, Asset} from '@app/model';
import {View} from '@app/model'; import {View} from '@app/model';
@Component({ @Component({
selector: 'elements-connect', selector: 'elements-connect',
templateUrl: './connect.component.html', templateUrl: './connect.component.html',
...@@ -70,45 +73,87 @@ export class ElementConnectComponent implements OnInit, OnDestroy { ...@@ -70,45 +73,87 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
} }
} }
connectAsset(node: TreeNode) { async connectAsset(node: TreeNode) {
const host = node.meta.asset as Asset; const host = node.meta.asset as Asset;
this._http.getMyAssetSystemUsers(host.id).subscribe(systemUsers => { const systemUsers = await this._http.getMyAssetSystemUsers(host.id).toPromise();
let user: SystemUser; let sysUser = await this.selectLoginSystemUsers(systemUsers);
if (systemUsers.length > 1) { sysUser = await this.manualSetUserAuthLoginIfNeed(sysUser);
// 检查系统用户优先级,获取最高优先级的 if (sysUser && sysUser.id) {
user = this.checkPriority(systemUsers); this.loginAsset(host, sysUser);
if (user) { } else {
return this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset.bind(this)); alert('该主机没有授权系统用户');
} }
}
selectLoginSystemUsers(systemUsers: Array<SystemUser>): Promise<SystemUser> {
const systemUserMaxPriority = this.filterMaxPrioritySystemUsers(systemUsers);
let user: SystemUser;
if (systemUsers.length > 1) {
return new Promise<SystemUser>(resolve => {
const dialogRef = this._dialog.open(AssetTreeDialogComponent, { const dialogRef = this._dialog.open(AssetTreeDialogComponent, {
height: '200px', height: '200px',
width: '300px', width: '300px',
data: {users: systemUsers} data: {users: systemUserMaxPriority}
}); });
dialogRef.afterClosed().subscribe(result => { dialogRef.afterClosed().subscribe(result => {
if (result) { if (result) {
for (const i of systemUsers) { for (const i of systemUserMaxPriority) {
if (i.id.toString() === result.toString()) { if (i.id.toString() === result.toString()) {
user = i; user = i;
break; resolve(user);
} }
} }
return this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset.bind(this));
} }
return null;
}); });
} else if (systemUsers.length === 1) { });
user = systemUsers[0]; } else if (systemUserMaxPriority.length === 1) {
this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset.bind(this)); user = systemUserMaxPriority[0];
} else { return Promise.resolve(user);
alert('该主机没有授权登录用户'); } else {
} return Promise.resolve(null);
}
}
manualSetUserAuthLoginIfNeed(user: SystemUser): Promise<SystemUser> {
if (!user || user.login_mode !== 'manual' || user.protocol !== 'rdp' || this._navSrv.skipAllManualPassword) {
return Promise.resolve(user);
}
user = Object.assign({}, user);
return new Promise(resolve => {
const dialogRef = this._dialog.open(ManualPasswordDialogComponent, {
height: '250px',
width: '500px',
data: {username: user.username}
});
dialogRef.afterClosed().subscribe(result => {
if (!result) {
return resolve(null);
}
if (result.skip) {
return resolve(user);
}
user.username = result.username;
user.password = result.password;
return resolve(user);
});
}); });
} }
connectRemoteApp(node: TreeNode) {
const user = node.meta.user as SystemUser; async connectRemoteApp(node: TreeNode) {
return this.manualSetUserAuthLoginIfNeed(node, user, this.loginRemoteApp.bind(this)); this._logger.debug('Connect remote app: ', node.id);
const systemUsers = await this._http.getMyRemoteAppSystemUsers(node.id).toPromise();
let sysUser = await this.selectLoginSystemUsers(systemUsers);
sysUser = await this.manualSetUserAuthLoginIfNeed(sysUser);
if (sysUser && sysUser.id) {
return this.loginRemoteApp(node, sysUser);
} else {
alert('该应用没有授权系统用户');
}
} }
loginRemoteApp(node: TreeNode, user: SystemUser) { loginRemoteApp(node: TreeNode, user: SystemUser) {
...@@ -144,28 +189,6 @@ export class ElementConnectComponent implements OnInit, OnDestroy { ...@@ -144,28 +189,6 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
this.Connect(node); this.Connect(node);
} }
manualSetUserAuthLoginIfNeed(node: any, user: SystemUser, callback) {
if (user.login_mode !== 'manual' || user.protocol !== 'rdp' || this._navSrv.skipAllManualPassword) {
return callback(node, user);
}
user = Object.assign({}, user);
const dialogRef = this._dialog.open(ManualPasswordDialogComponent, {
height: '250px',
width: '500px',
data: {username: user.username}
});
dialogRef.afterClosed().subscribe(result => {
if (!result) {
return;
}
if (result.skip) {
return callback(node, user);
}
user.username = result.username;
user.password = result.password;
return callback(node, user);
});
}
loginAsset(host: Asset, user: SystemUser) { loginAsset(host: Asset, user: SystemUser) {
if (user) { if (user) {
...@@ -185,18 +208,10 @@ export class ElementConnectComponent implements OnInit, OnDestroy { ...@@ -185,18 +208,10 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
} }
} }
checkPriority(sysUsers: Array<SystemUser>) { filterMaxPrioritySystemUsers(sysUsers: Array<SystemUser>): Array<SystemUser> {
let priority = -1; const priorityAll: Array<number> = sysUsers.map(s => s.priority);
let user: any; const maxPriority = Math.max(...priorityAll);
for (const u of sysUsers) { return sysUsers.filter(s => s.priority === maxPriority);
if (u.priority > priority) {
user = u;
priority = u.priority;
} else if (u.priority === priority) {
return null;
}
}
return user;
} }
} }
......
...@@ -28,8 +28,9 @@ export class ElementGuacamoleComponent implements OnInit { ...@@ -28,8 +28,9 @@ export class ElementGuacamoleComponent implements OnInit {
registerHost() { registerHost() {
let action: any; let action: any;
console.log(this.sysUser);
if (this.remoteAppId) { if (this.remoteAppId) {
action = this._http.guacamoleAddRemoteApp(User.id, this.remoteAppId, this.sysUser.username, this.sysUser.password); action = this._http.guacamoleAddRemoteApp(User.id, this.remoteAppId, this.sysUser.id, this.sysUser.username, this.sysUser.password);
} else { } else {
action = this._http.guacamoleAddAsset(User.id, this.host.id, this.sysUser.id, this.sysUser.username, this.sysUser.password); action = this._http.guacamoleAddAsset(User.id, this.host.id, this.sysUser.id, this.sysUser.username, this.sysUser.password);
} }
...@@ -51,7 +52,7 @@ export class ElementGuacamoleComponent implements OnInit { ...@@ -51,7 +52,7 @@ export class ElementGuacamoleComponent implements OnInit {
const now = new Date(); const now = new Date();
const nowTime = now.getTime() / 1000; const nowTime = now.getTime() / 1000;
this.registered = true; this.registered = true;
this._logger.debug('Userid is', User.id); this._logger.debug('User id is', User.id);
this._http.getGuacamoleToken(User.id, '').subscribe( this._http.getGuacamoleToken(User.id, '').subscribe(
data => { data => {
// /guacamole/client will redirect to http://guacamole/#/client // /guacamole/client will redirect to http://guacamole/#/client
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment