diff --git a/pkg/api/credential.go b/pkg/api/credential.go index bfe4a1d..921ea82 100644 --- a/pkg/api/credential.go +++ b/pkg/api/credential.go @@ -126,7 +126,17 @@ func CredentialDeleteEndpoint(c echo.Context) error { func CredentialGetEndpoint(c echo.Context) error { id := c.Param("id") - item, _ := model.FindCredentialById(id) + item, err := model.FindCredentialById(id) + if err != nil { + return err + } return Success(c, item) } + +func CredentialChangeOwnerEndpoint(c echo.Context) error { + id := c.Param("id") + owner := c.QueryParam("owner") + model.UpdateCredentialById(&model.Credential{Owner: owner}, id) + return Success(c, "") +} diff --git a/pkg/api/routes.go b/pkg/api/routes.go index 0ebec7a..759f925 100644 --- a/pkg/api/routes.go +++ b/pkg/api/routes.go @@ -81,6 +81,7 @@ func SetupRoutes() *echo.Echo { credentials.PUT("/:id", CredentialUpdateEndpoint) credentials.DELETE("/:id", CredentialDeleteEndpoint) credentials.GET("/:id", CredentialGetEndpoint) + credentials.POST("/:id/change-owner", CredentialChangeOwnerEndpoint) } sessions := e.Group("/sessions") diff --git a/pkg/model/asset.go b/pkg/model/asset.go index e211496..a37f661 100644 --- a/pkg/model/asset.go +++ b/pkg/model/asset.go @@ -22,7 +22,20 @@ type Asset struct { Active bool `json:"active"` Created utils.JsonTime `json:"created"` Tags string `json:"tags"` - Creator string `json:"creator"` + Owner string `json:"owner"` +} + +type AssetVo struct { + ID string `json:"id"` + Name string `json:"name"` + IP string `json:"ip"` + Protocol string `json:"protocol"` + Port int `json:"port"` + Active bool `json:"active"` + Created utils.JsonTime `json:"created"` + Tags string `json:"tags"` + Owner string `json:"owner"` + OwnerName string `json:"ownerName"` } func (r *Asset) TableName() string { @@ -44,27 +57,28 @@ func FindAssetByConditions(protocol string) (o []Asset, err error) { return } -func FindPageAsset(pageIndex, pageSize int, name, protocol, tags string) (o []Asset, total int64, err error) { +func FindPageAsset(pageIndex, pageSize int, name, protocol, tags string) (o []AssetVo, total int64, err error) { db := global.DB + db = db.Table("assets").Select("assets.id,assets.name,assets.ip,assets.port,assets.protocol,assets.active,assets.owner,assets.created, users.nickname as creator_name").Joins("left join users on assets.owner = users.id") if len(name) > 0 { - db = db.Where("name like ?", "%"+name+"%") + db = db.Where("assets.name like ?", "%"+name+"%") } if len(protocol) > 0 { - db = db.Where("protocol = ?", protocol) + db = db.Where("assets.protocol = ?", protocol) } if len(tags) > 0 { tagArr := strings.Split(tags, ",") for i := range tagArr { - db = db.Where("find_in_set(?, tags)", tagArr[i]) + db = db.Where("find_in_set(?, assets.tags)", tagArr[i]) } } - err = db.Order("created desc").Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error + err = db.Order("assets.created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Count(&total).Error if o == nil { - o = make([]Asset, 0) + o = make([]AssetVo, 0) } return } diff --git a/pkg/model/command.go b/pkg/model/command.go index ca477fb..0d85f17 100644 --- a/pkg/model/command.go +++ b/pkg/model/command.go @@ -28,7 +28,7 @@ func FindPageCommand(pageIndex, pageSize int, name, content string) (o []Command db = db.Where("content like ?", "%"+content+"%") } - err = db.Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error + err = db.Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Find(&o).Error if o == nil { o = make([]Command, 0) } diff --git a/pkg/model/credential.go b/pkg/model/credential.go index e2b893c..42e7c40 100644 --- a/pkg/model/credential.go +++ b/pkg/model/credential.go @@ -20,7 +20,7 @@ type Credential struct { PrivateKey string `json:"privateKey"` Passphrase string `json:"passphrase"` Created utils.JsonTime `json:"created"` - Creator string `json:"creator"` + Owner string `json:"owner"` } func (r *Credential) TableName() string { @@ -28,13 +28,13 @@ func (r *Credential) TableName() string { } type CredentialVo struct { - ID string `json:"id"` - Name string `json:"name"` - Type string `json:"type"` - Username string `json:"username"` - Created utils.JsonTime `json:"created"` - Creator string `json:"creator"` - CreatorName string `json:"creatorName"` + ID string `json:"id"` + Name string `json:"name"` + Type string `json:"type"` + Username string `json:"username"` + Created utils.JsonTime `json:"created"` + Owner string `json:"owner"` + OwnerName string `json:"ownerName"` } func FindAllCredential() (o []Credential, err error) { @@ -42,17 +42,17 @@ func FindAllCredential() (o []Credential, err error) { return } -func FindPageCredential(pageIndex, pageSize int, name, creator string) (o []CredentialVo, total int64, err error) { +func FindPageCredential(pageIndex, pageSize int, name, owner string) (o []CredentialVo, total int64, err error) { db := global.DB - db = db.Table("credentials").Select("credentials.id,credentials.name,credentials.type,credentials.username,credentials.creator,credentials.created,users.nickname as creator_name").Joins("left join users on credentials.creator = users.id") + db = db.Table("credentials").Select("credentials.id,credentials.name,credentials.type,credentials.username,credentials.owner,credentials.created,users.nickname as owner_name").Joins("left join users on credentials.owner = users.id") if len(name) > 0 { db = db.Where("credentials.name like ?", "%"+name+"%") } - if len(creator) > 0 { - db = db.Where("credentials.creator = ?", creator) + if len(owner) > 0 { + db = db.Where("credentials.owner = ?", owner) } - err = db.Order("credentials.created desc").Find(&o).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Count(&total).Error + err = db.Order("credentials.created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Count(&total).Error if o == nil { o = make([]CredentialVo, 0) } diff --git a/web/src/components/asset/Asset.js b/web/src/components/asset/Asset.js index 1ee3bb9..7445798 100644 --- a/web/src/components/asset/Asset.js +++ b/web/src/components/asset/Asset.js @@ -23,7 +23,7 @@ import qs from "qs"; import AssetModal from "./AssetModal"; import request from "../../common/request"; import {message} from "antd/es"; -import {isEmpty, itemRender} from "../../utils/utils"; +import {itemRender} from "../../utils/utils"; import { @@ -383,22 +383,9 @@ class Asset extends Component { } } }, { - title: '标签', - dataIndex: 'tags', - key: 'tags', - render: tags => { - if (!isEmpty(tags)) { - let tagDocuments = [] - let tagArr = tags.split(','); - for (let i = 0; i < tagArr.length; i++) { - if (tags[i] === '-') { - continue; - } - tagDocuments.push({tagArr[i]}) - } - return tagDocuments; - } - } + title: '所有者', + dataIndex: 'ownerName', + key: 'ownerName' }, { title: '创建日期', dataIndex: 'created', @@ -473,10 +460,10 @@ class Asset extends Component {
- + 资产列表 - + { + + let items = this.state.items; + items[index].updateBtnLoading = true; + this.setState({ + items: items + }); + + let result = await request.get('/credentials/' + id); + if (result['code'] !== 1) { + message.error(result['message']); + items[index].updateBtnLoading = false; + this.setState({ + items: items + }); + return; + } + + items[index].updateBtnLoading = false; this.setState({ modalTitle: title, modalVisible: true, - model: idcard + model: result['data'], + items: items }); }; @@ -270,9 +303,9 @@ class Credential extends Component { dataIndex: 'username', key: 'username', }, { - title: '创建人', - dataIndex: 'creatorName', - key: 'creatorName', + title: '所有者', + dataIndex: 'ownerName', + key: 'ownerName', }, { title: '创建时间', dataIndex: 'created', @@ -281,14 +314,41 @@ class Credential extends Component { { title: '操作', key: 'action', - render: (text, record) => { + render: (text, record, index) => { + + const menu = ( + + + + + + + + + + + + + + + ); return (
- - + + + +
) }, @@ -427,6 +487,28 @@ class Credential extends Component { : null } + { + this.changeOwnerFormRef.current + .validateFields() + .then(values => { + this.changeOwnerFormRef.current.resetFields(); + + }) + .catch(info => { + + }); + }} + onCancel={this.handleCancel}> + +
+ + } placeholder="请选择所有者"/> + +
+
+ );