Unverified Commit 981f283e authored by Eric_Lee's avatar Eric_Lee Committed by GitHub

[update] select system user info (#119)

parent 4c1125a8
......@@ -126,6 +126,26 @@ msgstr ""
msgid "Refresh done"
msgstr ""
#. i18n.T
#: pkg/handler/banner.go:101
msgid "Tips: Enter system user ID and directly login the asset [ %s(%s) ]"
msgstr ""
#. i18n.T
#: pkg/handler/banner.go:102
msgid "Back: B/b"
msgstr ""
#. i18n.T
#: pkg/handler/banner.go:103
msgid "Name"
msgstr ""
#. i18n.T
#: pkg/handler/banner.go:104
msgid "Username"
msgstr ""
#. i18n.T
#: pkg/proxy/parser.go:131
msgid "Command `%s` is forbidden"
......
......@@ -20,7 +20,7 @@ msgstr "欢迎使用Jumpserver开源堡垒机系统"
#. i18n.T
#: pkg/handler/banner.go:48
msgid "directly login"
msgstr "直接登"
msgstr "直接登"
#. i18n.T
#: pkg/handler/banner.go:49
......@@ -35,12 +35,12 @@ msgstr "进行搜索登录(如果唯一)"
#. i18n.T
#: pkg/handler/banner.go:50
msgid "/ + IP, Hostname, Comment"
msgstr "/ + IP, 主机名 or 备注"
msgstr "/ + IP 主机名 or 备注"
#. i18n.T
#: pkg/handler/banner.go:50
msgid "to search, such as: /192.168"
msgstr "搜索, 如: /192.168"
msgstr "搜索, 如/192.168"
#. i18n.T
#: pkg/handler/banner.go:51
......@@ -90,7 +90,7 @@ msgstr "备注"
#. i18n.T
#: pkg/handler/banner.go:94
msgid "Page: %d, Count: %d, Total Page: %d, Total Count: %d"
msgstr "页码: %d, 每页行数: %d, 总页数: %d, 总数量: %d"
msgstr "页码:%d,每页行数:%d,总页数:%d,总数量:%d"
#. i18n.T
#: pkg/handler/banner.go:95
......@@ -105,7 +105,7 @@ msgid ""
"Tips: Enter the asset ID and directly login the asset.\n"
msgstr ""
"\n"
"提示: 输入资产ID,直接登录资产.\n"
"提示:输入资产ID,登录资产\n"
#. i18n.T
#: pkg/handler/banner.go:97
......@@ -114,23 +114,47 @@ msgid ""
"Page up: P/p\tPage down: Enter|N/n\tBACK: b.\n"
msgstr ""
"\n"
"上一页: P/p 下一页: Enter|N/n 返回: B/b\n"
"上一页:P/p 下一页:Enter|N/n 返回:B/b\n"
#. i18n.T
#: pkg/handler/banner.go:98
msgid "Node: [ ID.Name(Asset amount) ]"
msgstr "节点: [ ID.名称(资产数量) ]"
msgstr "节点[ ID.名称(资产数量) ]"
#. i18n.T
#: pkg/handler/banner.go:99
msgid "Tips: Enter g+NodeID to display the host under the node, such as g1"
msgstr "提示: 输入 g+节点ID 显示节点下主机. 如: g1"
msgstr "提示:输入 g+节点ID 显示节点下主机,如: g1"
#. i18n.T
#: pkg/handler/banner.go:100
msgid "Refresh done"
msgstr "刷新完成"
#. i18n.T
#: pkg/handler/banner.go:101
#, fuzzy
msgid "Tips: Enter system user ID and directly login the asset [ %s(%s) ]"
msgstr ""
"\n"
"提示:输入系统用户ID,登录资产[ %s(%s) ]\n"
#. i18n.T
#: pkg/handler/banner.go:102
msgid "Back: B/b"
msgstr "返回:B/b"
#. i18n.T
#: pkg/handler/banner.go:103
msgid "Name"
msgstr "名称"
#. i18n.T
#: pkg/handler/banner.go:104
#, fuzzy
msgid "Username"
msgstr "用户名"
#. i18n.T
#: pkg/proxy/parser.go:131
msgid "Command `%s` is forbidden"
......
......@@ -98,6 +98,10 @@ func getI18nFromMap(name string) string {
"NodeHeaderTip": i18n.T("Node: [ ID.Name(Asset amount) ]"),
"NodeEndTip": i18n.T("Tips: Enter g+NodeID to display the host under the node, such as g1"),
"RefreshDone": i18n.T("Refresh done"),
"SelectUserTip": i18n.T("Tips: Enter system user ID and directly login the asset [ %s(%s) ]"),
"BackTip": i18n.T("Back: B/b"),
"Name": i18n.T("Name"),
"Username": i18n.T("Username"),
}
})
return i18nMap[name]
......
......@@ -8,10 +8,10 @@ import (
"strings"
"github.com/gliderlabs/ssh"
"github.com/olekukonko/tablewriter"
"github.com/xlab/treeprint"
"github.com/jumpserver/koko/pkg/cctx"
"github.com/jumpserver/koko/pkg/common"
"github.com/jumpserver/koko/pkg/config"
"github.com/jumpserver/koko/pkg/logger"
"github.com/jumpserver/koko/pkg/model"
......@@ -195,45 +195,71 @@ func (h *interactiveHandler) displayAllAssets() {
}
}
func (h *interactiveHandler) chooseSystemUser(systemUsers []model.SystemUser) model.SystemUser {
func (h *interactiveHandler) chooseSystemUser(asset model.Asset,
systemUsers []model.SystemUser) (systemUser model.SystemUser, ok bool) {
length := len(systemUsers)
switch length {
case 0:
return model.SystemUser{}
return model.SystemUser{}, false
case 1:
return systemUsers[0]
return systemUsers[0], true
default:
}
displaySystemUsers := selectHighestPrioritySystemUsers(systemUsers)
if len(displaySystemUsers) == 1 {
return displaySystemUsers[0]
}
table := tablewriter.NewWriter(h.term)
table.SetHeader([]string{"ID", "Name"})
for i := 0; i < len(displaySystemUsers); i++ {
table.Append([]string{strconv.Itoa(i + 1), displaySystemUsers[i].Name})
}
table.SetBorder(false)
table.SetHeaderAlignment(tablewriter.ALIGN_LEFT)
table.SetAlignment(tablewriter.ALIGN_LEFT)
return displaySystemUsers[0], true
}
Labels := []string{getI18nFromMap("ID"), getI18nFromMap("Name"), getI18nFromMap("Username")}
fields := []string{"ID", "Name", "Username"}
data := make([]map[string]string, len(displaySystemUsers))
for i, j := range displaySystemUsers {
row := make(map[string]string)
row["ID"] = strconv.Itoa(i + 1)
row["Name"] = j.Name
row["Username"] = j.Username
data[i] = row
}
w, _ := h.term.GetSize()
table := common.WrapperTable{
Fields: fields,
Labels: Labels,
FieldsSize: map[string][3]int{
"ID": {0, 0, 5},
"Name": {0, 8, 0},
"Username": {0, 10, 0},
},
Data: data,
TotalSize: w,
TruncPolicy: common.TruncMiddle,
}
table.Initial()
h.term.SetPrompt("ID> ")
defer h.term.SetPrompt("Opt> ")
for count := 0; count < 3; count++ {
table.Render()
selectUserTip := fmt.Sprintf(getI18nFromMap("SelectUserTip"), asset.Hostname, asset.IP)
for {
utils.IgnoreErrWriteString(h.term, table.Display())
utils.IgnoreErrWriteString(h.term, selectUserTip)
utils.IgnoreErrWriteString(h.term, getI18nFromMap("BackTip"))
utils.IgnoreErrWriteString(h.term, "\r\n")
line, err := h.term.ReadLine()
if err != nil {
break
return
}
line = strings.TrimSpace(line)
switch strings.ToLower(line) {
case "q", "b", "quit", "exit", "back":
return
}
if num, err := strconv.Atoi(line); err == nil {
if num > 0 && num <= len(displaySystemUsers) {
return displaySystemUsers[num-1]
return displaySystemUsers[num-1], true
}
}
}
return displaySystemUsers[0]
}
func (h *interactiveHandler) displayAssets(assets model.AssetList) {
......@@ -245,7 +271,10 @@ func (h *interactiveHandler) displayAssets(assets model.AssetList) {
selectOneAssets := pag.Start()
if len(selectOneAssets) == 1 {
systemUsers := service.GetUserAssetSystemUsers(h.user.ID, selectOneAssets[0].ID)
systemUser := h.chooseSystemUser(systemUsers)
systemUser, ok := h.chooseSystemUser(selectOneAssets[0], systemUsers)
if !ok {
return
}
h.assetSelect = &selectOneAssets[0]
h.systemUserSelect = &systemUser
h.Proxy(context.TODO())
......@@ -359,7 +388,10 @@ func (h *interactiveHandler) searchNodeAssets(num int) (assets model.AssetList)
func (h *interactiveHandler) ProxyAsset(assetSelect model.Asset) {
systemUsers := service.GetUserAssetSystemUsers(h.user.ID, assetSelect.ID)
systemUserSelect := h.chooseSystemUser(systemUsers)
systemUserSelect, ok := h.chooseSystemUser(assetSelect, systemUsers)
if !ok {
return
}
h.systemUserSelect = &systemUserSelect
h.assetSelect = &assetSelect
h.Proxy(context.Background())
......
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