Commit cf63e0c9 authored by ibuler's avatar ibuler

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

parent 8ff84f5b
......@@ -86,6 +86,11 @@ export class HttpService {
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) {
const url = `/api/v1/perms/users/assets/${assetId}/system-users/`;
return this.http.get<Array<SystemUser>>(url);
......@@ -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()
.set('user_id', userId)
.set('remote_app_id', remoteAppId)
.set('system_user_id', sysUserId)
.set('token', DataStore.guacamoleToken);
let body = new HttpParams();
if (systemUserUsername && systemUserPassword) {
......
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 {AppService, HttpService, LogService, NavService} from '@app/app.service';
import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from '@angular/material';
......@@ -7,6 +9,7 @@ import {ActivatedRoute} from '@angular/router';
import {SystemUser, TreeNode, Asset} from '@app/model';
import {View} from '@app/model';
@Component({
selector: 'elements-connect',
templateUrl: './connect.component.html',
......@@ -70,45 +73,87 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
}
}
connectAsset(node: TreeNode) {
async connectAsset(node: TreeNode) {
const host = node.meta.asset as Asset;
this._http.getMyAssetSystemUsers(host.id).subscribe(systemUsers => {
const systemUsers = await this._http.getMyAssetSystemUsers(host.id).toPromise();
let sysUser = await this.selectLoginSystemUsers(systemUsers);
sysUser = await this.manualSetUserAuthLoginIfNeed(sysUser);
if (sysUser && sysUser.id) {
this.loginAsset(host, sysUser);
} else {
alert('该主机没有授权系统用户');
}
}
selectLoginSystemUsers(systemUsers: Array<SystemUser>): Promise<SystemUser> {
const systemUserMaxPriority = this.filterMaxPrioritySystemUsers(systemUsers);
let user: SystemUser;
if (systemUsers.length > 1) {
// 检查系统用户优先级,获取最高优先级的
user = this.checkPriority(systemUsers);
if (user) {
return this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset.bind(this));
}
return new Promise<SystemUser>(resolve => {
const dialogRef = this._dialog.open(AssetTreeDialogComponent, {
height: '200px',
width: '300px',
data: {users: systemUsers}
data: {users: systemUserMaxPriority}
});
dialogRef.afterClosed().subscribe(result => {
if (result) {
for (const i of systemUsers) {
for (const i of systemUserMaxPriority) {
if (i.id.toString() === result.toString()) {
user = i;
break;
resolve(user);
}
}
return this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset.bind(this));
}
return null;
});
} else if (systemUsers.length === 1) {
user = systemUsers[0];
this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset.bind(this));
});
} else if (systemUserMaxPriority.length === 1) {
user = systemUserMaxPriority[0];
return Promise.resolve(user);
} else {
alert('该主机没有授权登录用户');
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;
return this.manualSetUserAuthLoginIfNeed(node, user, this.loginRemoteApp.bind(this));
}
async connectRemoteApp(node: TreeNode) {
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) {
......@@ -144,28 +189,6 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
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) {
if (user) {
......@@ -185,18 +208,10 @@ export class ElementConnectComponent implements OnInit, OnDestroy {
}
}
checkPriority(sysUsers: Array<SystemUser>) {
let priority = -1;
let user: any;
for (const u of sysUsers) {
if (u.priority > priority) {
user = u;
priority = u.priority;
} else if (u.priority === priority) {
return null;
}
}
return user;
filterMaxPrioritySystemUsers(sysUsers: Array<SystemUser>): Array<SystemUser> {
const priorityAll: Array<number> = sysUsers.map(s => s.priority);
const maxPriority = Math.max(...priorityAll);
return sysUsers.filter(s => s.priority === maxPriority);
}
}
......
......@@ -28,8 +28,9 @@ export class ElementGuacamoleComponent implements OnInit {
registerHost() {
let action: any;
console.log(this.sysUser);
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 {
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 {
const now = new Date();
const nowTime = now.getTime() / 1000;
this.registered = true;
this._logger.debug('Userid is', User.id);
this._logger.debug('User id is', User.id);
this._http.getGuacamoleToken(User.id, '').subscribe(
data => {
// /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