Commit 65ccd3dd authored by ibuler's avatar ibuler

[Update] 提取Model

parent af908312
...@@ -13,14 +13,12 @@ import 'rxjs/add/operator/catch'; ...@@ -13,14 +13,12 @@ import 'rxjs/add/operator/catch';
import {DataStore, User, Browser, i18n} from './globals'; import {DataStore, User, Browser, i18n} from './globals';
import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http'; import {HttpClient, HttpHeaders, HttpParams} from '@angular/common/http';
import {NGXLogger} from 'ngx-logger'; import {NGXLogger} from 'ngx-logger';
import {SystemUser, GuacObjAddResp, TreeNode} from './model';
import * as UUID from 'uuid-js/lib/uuid.js'; import * as UUID from 'uuid-js/lib/uuid.js';
declare function unescape(s: string): string; declare function unescape(s: string): string;
class GuacObjAddResp {
code: number;
result: string;
}
@Injectable() @Injectable()
export class HttpService { export class HttpService {
...@@ -71,18 +69,23 @@ export class HttpService { ...@@ -71,18 +69,23 @@ export class HttpService {
getMyGrantedNodes(async: boolean, refresh?: boolean) { getMyGrantedNodes(async: boolean, refresh?: boolean) {
const cachePolicy = refresh ? '2' : '1'; const cachePolicy = refresh ? '2' : '1';
const syncUrl = '/api/perms/v1/users/nodes-with-assets/tree/?cache_policy=' + cachePolicy; const syncUrl = `/api/perms/v1/users/nodes-with-assets/tree/?cache_policy=${cachePolicy}`;
const asyncUrl = '/api/perms/v1/users/nodes/children-with-assets/tree/?cache_policy=' + cachePolicy; const asyncUrl = `/api/perms/v1/users/nodes/children-with-assets/tree/?cache_policy=${cachePolicy}`;
const url = async ? asyncUrl : syncUrl; const url = async ? asyncUrl : syncUrl;
return this.http.get<Array<Node>>(url); return this.http.get<Array<TreeNode>>(url);
} }
getMyGrantedRemoteApps() { getMyGrantedRemoteApps() {
return this.http.get<Array<Node>>('/api/perms/v1/user/remote-apps/tree/'); return this.http.get<Array<TreeNode>>('/api/perms/v1/user/remote-apps/tree/');
}
getMyAssetSystemUsers(assetId: string) {
const url = `/api/v1/perms/users/assets/${assetId}/system-users/`;
return this.http.get<Array<SystemUser>>(url);
} }
refreshMyGrantedNodes() { refreshMyGrantedNodes() {
return this.http.get<Array<Node>>('/api/perms/v1/user/nodes-assets/tree/?cache_policy=2'); return this.http.get<Array<TreeNode>>('/api/perms/v1/user/nodes-assets/tree/?cache_policy=2');
} }
getGuacamoleToken(user_id: string, authToken: string) { getGuacamoleToken(user_id: string, authToken: string) {
...@@ -180,8 +183,7 @@ export class HttpService { ...@@ -180,8 +183,7 @@ export class HttpService {
} }
search(q: string) { search(q: string) {
const params = new HttpParams() const params = new HttpParams().set('q', q);
.set('q', q);
return this.http.get('/api/search', {params: params}); return this.http.get('/api/search', {params: params});
} }
......
...@@ -5,6 +5,7 @@ import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from '@angular/material'; ...@@ -5,6 +5,7 @@ import {MAT_DIALOG_DATA, MatDialog, MatDialogRef} from '@angular/material';
import {FormControl, Validators} from '@angular/forms'; import {FormControl, Validators} from '@angular/forms';
import {BehaviorSubject} from 'rxjs/BehaviorSubject'; import {BehaviorSubject} from 'rxjs/BehaviorSubject';
import {ActivatedRoute} from '@angular/router'; import {ActivatedRoute} from '@angular/router';
import {SystemUser, TreeNode, Asset, Node} from '../../model';
import * as jQuery from 'jquery/dist/jquery.min'; import * as jQuery from 'jquery/dist/jquery.min';
declare var $: any; declare var $: any;
...@@ -54,7 +55,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -54,7 +55,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
} }
} }
constructor(private _appService: AppService, constructor(private _appSvc: AppService,
public _dialog: MatDialog, public _dialog: MatDialog,
public _logger: LogService, public _logger: LogService,
private activatedRoute: ActivatedRoute, private activatedRoute: ActivatedRoute,
...@@ -64,19 +65,12 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -64,19 +65,12 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
this.searchEvt$ = new BehaviorSubject<string>(this.query); this.searchEvt$ = new BehaviorSubject<string>(this.query);
} }
refreshGrantedAssetsNodes() {
this._http.refreshMyGrantedNodes()
.subscribe(response => {
this.Data = [...response, ...this.Data];
this.initTree();
});
}
ngOnInit() { ngOnInit() {
this.initAssetsTree(); this.initTree();
this.initRemoteAppsTree();
document.addEventListener('click', this.hideRMenu.bind(this), false); document.addEventListener('click', this.hideRMenu.bind(this), false);
this.loadTreeAsync = this._navSvc.treeLoadAsync; this.loadTreeAsync = this._navSvc.treeLoadAsync;
// Todo: 搜索
this.searchEvt$.asObservable() this.searchEvt$.asObservable()
.debounceTime(300) .debounceTime(300)
.distinctUntilChanged() .distinctUntilChanged()
...@@ -115,12 +109,12 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -115,12 +109,12 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
} }
this._http.getMyGrantedNodes(this.loadTreeAsync, refresh).subscribe(resp => { this._http.getMyGrantedNodes(this.loadTreeAsync, refresh).subscribe(resp => {
const assetsTree = $.fn.zTree.init($('#assetsTree'), setting, resp); const assetsTree = $.fn.zTree.init($('#assetsTree'), setting, resp);
this.assetsTree = assetsTree; this.assetsTree = assetsTree;
this.rootNodeAddDom(assetsTree, () => { this.rootNodeAddDom(assetsTree, () => {
this.refreshAssetsTree(); this.refreshAssetsTree();
});
}); });
});
} }
refreshRemoteAppsTree() { refreshRemoteAppsTree() {
...@@ -144,6 +138,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -144,6 +138,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
this.initAssetsTree(); this.initAssetsTree();
this.initRemoteAppsTree(); this.initRemoteAppsTree();
// Todo: connect to some asset, direct
this.activatedRoute.queryParams.subscribe(params => { this.activatedRoute.queryParams.subscribe(params => {
const login_to = params['login_to']; const login_to = params['login_to'];
if (login_to && !this.hasLoginTo) { if (login_to && !this.hasLoginTo) {
...@@ -215,7 +210,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -215,7 +210,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
} }
} }
Connect(node) { Connect(node: TreeNode) {
switch (node.meta.type) { switch (node.meta.type) {
case 'asset': case 'asset':
this.connectAsset(node); this.connectAsset(node);
...@@ -228,15 +223,16 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -228,15 +223,16 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
} }
} }
connectAsset(node) { connectAsset(node: TreeNode) {
const systemUsers = node.meta.system_users; const host = node.meta.asset as Asset;
const host = node.meta.asset; this._http.getMyAssetSystemUsers(host.id).subscribe(systemUsers => {
let user: any; let user: SystemUser;
if (systemUsers.length > 1) { if (systemUsers.length > 1) {
user = this.checkPriority(systemUsers); // 检查系统用户优先级,获取最高优先级的
if (user) { user = this.checkPriority(systemUsers);
return this.manualSetUserAuthLoginIfNeed(host, user, this.login); if (user) {
} else { return this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset);
}
const dialogRef = this._dialog.open(AssetTreeDialogComponent, { const dialogRef = this._dialog.open(AssetTreeDialogComponent, {
height: '200px', height: '200px',
width: '300px', width: '300px',
...@@ -251,24 +247,24 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -251,24 +247,24 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
break; break;
} }
} }
return this.manualSetUserAuthLoginIfNeed(host, user, this.login); return this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset);
} }
}); });
} else if (systemUsers.length === 1) {
user = systemUsers[0];
this.manualSetUserAuthLoginIfNeed(host, user, this.loginAsset);
} else {
alert('该主机没有授权登录用户');
} }
} else if (systemUsers.length === 1) { });
user = systemUsers[0];
this.manualSetUserAuthLoginIfNeed(host, user, this.login);
} else {
alert('该主机没有授权登录用户');
}
} }
connectRemoteApp(node) { connectRemoteApp(node: TreeNode) {
const user = node.meta.user; const user = node.meta.user as SystemUser;
return this.manualSetUserAuthLoginIfNeed(node, user, this.loginRemoteApp); return this.manualSetUserAuthLoginIfNeed(node, user, this.loginRemoteApp);
} }
loginRemoteApp(node, user) { loginRemoteApp(node: TreeNode, user: SystemUser) {
const id = NavList.List.length - 1; const id = NavList.List.length - 1;
if (node) { if (node) {
NavList.List[id].nick = node.name; NavList.List[id].nick = node.name;
...@@ -305,7 +301,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -305,7 +301,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
this.Connect(host); this.Connect(host);
} }
manualSetUserAuthLoginIfNeed(host, user, callback) { manualSetUserAuthLoginIfNeed(host: Asset, user: SystemUser, callback) {
if (user.login_mode !== 'manual' || user.protocol !== 'rdp') { if (user.login_mode !== 'manual' || user.protocol !== 'rdp') {
return callback(host, user); return callback(host, user);
} }
...@@ -328,7 +324,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -328,7 +324,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
}); });
} }
login(host, user) { loginAsset(host: Asset, user: SystemUser) {
const id = NavList.List.length - 1; const id = NavList.List.length - 1;
if (user) { if (user) {
...@@ -349,7 +345,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges { ...@@ -349,7 +345,7 @@ export class ElementAssetTreeComponent implements OnInit, OnChanges {
} }
} }
checkPriority(sysUsers) { checkPriority(sysUsers: Array<SystemUser>) {
let priority = -1; let priority = -1;
let user: any; let user: any;
for (const u of sysUsers) { for (const u of sysUsers) {
......
export class SystemUser {
id: string;
name: string;
login_mode: string;
username: string;
priority: number;
protocol: string;
password: string;
actions: Array<string>;
}
export class TreeNode {
id: string;
name: string;
comment: string;
title: string;
isParent: boolean;
pId: string;
open: boolean;
iconSkin: string;
meta: any;
}
export class Node {
id: string;
key: string;
value: string;
}
export class Asset {
id: string;
hostname: string;
ip: string;
comment: string;
domain: string;
os: string;
platform: string;
protocols: Array<string>;
}
export class GuacObjAddResp {
code: number;
result: string;
}
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