增加用户组的资产授权

This commit is contained in:
dushixiang 2021-01-21 21:43:00 +08:00
parent 26058476b3
commit cdbb7b567a
13 changed files with 138 additions and 57 deletions

View File

@ -10,6 +10,7 @@ import (
"gorm.io/driver/mysql"
"gorm.io/driver/sqlite"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"io"
"next-terminal/pkg/api"
"next-terminal/pkg/config"
@ -36,7 +37,7 @@ func Run() error {
/ | \_/ __ \\ \/ /\ __\ | |_/ __ \_ __ \/ \| |/ \\__ \ | |
/ | \ ___/ > < | | | |\ ___/| | \/ Y Y \ | | \/ __ \| |__
\____|__ /\___ >__/\_ \ |__| |____| \___ >__| |__|_| /__|___| (____ /____/
\/ \/ \/ \/ \/ \/ \/ ` + Version + "\n")
\/ \/ \/ \/ \/ \/ \/ ` + Version + "\n\n")
var err error
//logrus.SetReportCaller(true)
@ -70,7 +71,7 @@ func Run() error {
global.Config.Mysql.Database,
)
global.DB, err = gorm.Open(mysql.Open(dsn), &gorm.Config{
//Logger: logger.Default.LogMode(logger.Info),
Logger: logger.Default.LogMode(logger.Info),
})
} else {
global.DB, err = gorm.Open(sqlite.Open(global.Config.Sqlite.File), &gorm.Config{})

View File

@ -41,9 +41,10 @@ func AssetPagingEndpoint(c echo.Context) error {
tags := c.QueryParam("tags")
owner := c.QueryParam("owner")
sharer := c.QueryParam("sharer")
userGroupId := c.QueryParam("userGroupId")
account, _ := GetCurrentAccount(c)
items, total, _ := model.FindPageAsset(pageIndex, pageSize, name, protocol, tags, account, owner, sharer)
items, total, _ := model.FindPageAsset(pageIndex, pageSize, name, protocol, tags, account, owner, sharer, userGroupId)
return Success(c, H{
"total": total,

View File

@ -6,6 +6,7 @@ import (
)
type RU struct {
UserGroupId string `json:"userGroupId"`
UserId string `json:"userId"`
ResourceType string `json:"resourceType"`
ResourceIds []string `json:"resourceIds"`
@ -45,7 +46,7 @@ func ResourceRemoveByUserIdAssignEndPoint(c echo.Context) error {
return err
}
if err := model.DeleteByUserIdAndResourceTypeAndResourceIdIn(ru.UserId, ru.ResourceType, ru.ResourceIds); err != nil {
if err := model.DeleteByUserIdAndResourceTypeAndResourceIdIn(ru.UserGroupId, ru.UserId, ru.ResourceType, ru.ResourceIds); err != nil {
return err
}
@ -58,7 +59,7 @@ func ResourceAddByUserIdAssignEndPoint(c echo.Context) error {
return err
}
if err := model.AddSharerResources(ru.UserId, ru.ResourceType, ru.ResourceIds); err != nil {
if err := model.AddSharerResources(ru.UserGroupId, ru.UserId, ru.ResourceType, ru.ResourceIds); err != nil {
return err
}

View File

@ -63,7 +63,7 @@ func FindAssetByConditions(protocol string, account User) (o []Asset, err error)
return
}
func FindPageAsset(pageIndex, pageSize int, name, protocol, tags string, account User, owner, sharer string) (o []AssetVo, total int64, err error) {
func FindPageAsset(pageIndex, pageSize int, name, protocol, tags string, account User, owner, sharer, userGroupId string) (o []AssetVo, total int64, err error) {
db := global.DB.Table("assets").Select("assets.id,assets.name,assets.ip,assets.port,assets.protocol,assets.active,assets.owner,assets.created, users.nickname as owner_name,COUNT(resource_sharers.user_id) as sharer_count").Joins("left join users on assets.owner = users.id").Joins("left join resource_sharers on assets.id = resource_sharers.resource_id").Group("assets.id")
dbCounter := global.DB.Table("assets").Select("DISTINCT assets.id").Joins("left join resource_sharers on assets.id = resource_sharers.resource_id").Group("assets.id")
@ -71,6 +71,17 @@ func FindPageAsset(pageIndex, pageSize int, name, protocol, tags string, account
owner := account.ID
db = db.Where("assets.owner = ? or resource_sharers.user_id = ?", owner, owner)
dbCounter = dbCounter.Where("assets.owner = ? or resource_sharers.user_id = ?", owner, owner)
// 查询用户所在用户组列表
userGroupIds, err := FindUserGroupIdsByUserId(account.ID)
if err != nil {
return nil, 0, err
}
if userGroupIds != nil && len(userGroupIds) > 0 {
db = db.Or("resource_sharers.user_group_id in ?", userGroupIds)
dbCounter = dbCounter.Or("resource_sharers.user_group_id in ?", userGroupIds)
}
} else {
if len(owner) > 0 {
db = db.Where("assets.owner = ?", owner)
@ -80,6 +91,11 @@ func FindPageAsset(pageIndex, pageSize int, name, protocol, tags string, account
db = db.Where("resource_sharers.user_id = ?", sharer)
dbCounter = dbCounter.Where("resource_sharers.user_id = ?", sharer)
}
if len(userGroupId) > 0 {
db = db.Where("resource_sharers.user_group_id = ?", userGroupId)
dbCounter = dbCounter.Where("resource_sharers.user_group_id = ?", userGroupId)
}
}
if len(name) > 0 {

View File

@ -81,11 +81,28 @@ func OverwriteUserIdsByResourceId(resourceId, resourceType string, userIds []str
return nil
}
func DeleteByUserIdAndResourceTypeAndResourceIdIn(userId, resourceType string, resourceIds []string) error {
return global.DB.Where("user_id = ? and resource_type = ? and resource_id in ?", userId, resourceType, resourceIds).Delete(&ResourceSharer{}).Error
func DeleteByUserIdAndResourceTypeAndResourceIdIn(userGroupId, userId, resourceType string, resourceIds []string) error {
db := global.DB
if userGroupId != "" {
db = db.Where("user_group_id = ?", userGroupId)
}
if userId != "" {
db = db.Where("user_id = ?", userId)
}
if resourceType != "" {
db = db.Where("resource_type = ?", resourceType)
}
if resourceIds != nil {
db = db.Where("resource_id in ?", resourceIds)
}
return db.Delete(&ResourceSharer{}).Error
}
func AddSharerResources(userId, resourceType string, resourceIds []string) error {
func AddSharerResources(userGroupId, userId, resourceType string, resourceIds []string) error {
return global.DB.Transaction(func(tx *gorm.DB) (err error) {
for i := range resourceIds {
@ -112,12 +129,13 @@ func AddSharerResources(userId, resourceType string, resourceIds []string) error
return echo.NewHTTPError(400, "参数错误")
}
id := utils.Sign([]string{resourceId, resourceType, userId})
id := utils.Sign([]string{resourceId, resourceType, userId, userGroupId})
resource := &ResourceSharer{
ID: id,
ResourceId: resourceId,
ResourceType: resourceType,
UserId: userId,
UserGroupId: userGroupId,
}
err = tx.Create(resource).Error
if err != nil {

View File

@ -13,10 +13,10 @@ type UserGroup struct {
}
type UserGroupVo struct {
ID string `json:"id"`
Name string `json:"name"`
Created utils.JsonTime `json:"created"`
MemberCount int64 `json:"memberCount"`
ID string `json:"id"`
Name string `json:"name"`
Created utils.JsonTime `json:"created"`
AssetCount int64 `json:"assetCount"`
}
func (r *UserGroup) TableName() string {
@ -24,7 +24,7 @@ func (r *UserGroup) TableName() string {
}
func FindPageUserGroup(pageIndex, pageSize int, name string) (o []UserGroupVo, total int64, err error) {
db := global.DB.Table("user_groups").Select("user_groups.id, user_groups.name, user_groups.created, count(user_group_members.user_id) as member_count").Joins("left join user_group_members on user_groups.id = user_group_members.user_group_id").Group("user_groups.id")
db := global.DB.Table("user_groups").Select("user_groups.id, user_groups.name, user_groups.created, count(resource_sharers.user_group_id) as asset_count").Joins("left join resource_sharers on user_groups.id = resource_sharers.user_group_id and resource_sharers.resource_type = 'asset'").Group("user_groups.id")
dbCounter := global.DB.Table("user_groups")
if len(name) > 0 {
db = db.Where("user_groups.name like ?", "%"+name+"%")
@ -86,6 +86,11 @@ func FindUserGroupById(id string) (o UserGroup, err error) {
return
}
func FindUserGroupIdsByUserId(userId string) (o []string, err error) {
err = global.DB.Table("user_groups").Select("user_groups.id").Joins("right join user_group_members on user_groups.id = user_group_members.user_group_id").Where("user_group_members.user_id = ?", userId).Find(&o).Error
return
}
func UpdateUserGroupById(o *UserGroup, members []string, id string) error {
return global.DB.Transaction(func(tx *gorm.DB) error {
o.ID = id

23
web/package-lock.json generated
View File

@ -1,6 +1,6 @@
{
"name": "next-terminal",
"version": "0.0.8",
"version": "0.0.9",
"lockfileVersion": 1,
"requires": true,
"dependencies": {
@ -3464,11 +3464,18 @@
"integrity": "sha512-5Kgy8Cz6LPC9DJcNb3yjAXTu3XihQgEdnIg50c//zOC/MyLP0Clg+Y8Sh9ZjjnvBrDZU4DgXS9C3T9r4/scGZQ=="
},
"axios": {
"version": "0.19.2",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.19.2.tgz",
"integrity": "sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==",
"version": "0.21.1",
"resolved": "https://registry.npmjs.org/axios/-/axios-0.21.1.tgz",
"integrity": "sha512-dKQiRHxGD9PPRIUNIWvZhPTPpl1rf/OxTYKsqKUDjBwYylTvV7SjSHJb9ratfyzM6wCdLCOYLzs73qpg5c4iGA==",
"requires": {
"follow-redirects": "1.5.10"
"follow-redirects": "^1.10.0"
},
"dependencies": {
"follow-redirects": {
"version": "1.13.1",
"resolved": "https://registry.npmjs.org/follow-redirects/-/follow-redirects-1.13.1.tgz",
"integrity": "sha512-SSG5xmZh1mkPGyKzjZP8zLjltIfpW32Y5QpdNJyjcfGxK3qo3NDDkZOZSFiGn1A6SclQxY9GzEwAHQ3dmYRWpg=="
}
}
},
"axobject-query": {
@ -8127,9 +8134,9 @@
"integrity": "sha512-k/vGaX4/Yla3WzyMCvTQOXYeIHvqOKtnqBduzTHpzpQZzAskKMhZ2K+EnBiSM9zGSoIFeMpXKxa4dYeZIQqewQ=="
},
"ini": {
"version": "1.3.5",
"resolved": "https://registry.npmjs.org/ini/-/ini-1.3.5.tgz",
"integrity": "sha512-RZY5huIKCMRWDUqZlEi72f/lmXKMvuszcMBduliQ3nnWbx9X/ZBQO7DijMEYS9EhHBb2qacRUMtC7svLwe0lcw=="
"version": "1.3.8",
"resolved": "https://registry.npmjs.org/ini/-/ini-1.3.8.tgz",
"integrity": "sha512-JV/yugV2uzW5iMRSiZAyDtQd+nxtUnjeLt0acNdw98kKLrvuRVyB80tsREOE7yvGVgalhZ6RNXCmEHkUKBKxew=="
},
"inquirer": {
"version": "7.3.3",

View File

@ -5,9 +5,8 @@
"dependencies": {
"@ant-design/icons": "^4.3.0",
"antd": "^4.8.4",
"axios": "^0.19.2",
"axios": "^0.21.1",
"guacamole-common-js": "^1.2.0",
"http-proxy-middleware": "^1.0.6",
"qs": "^6.9.4",
"react": "^16.14.0",
"react-contexify": "^4.1.1",

View File

@ -1,19 +1,19 @@
// prod
let wsPrefix;
if (window.location.protocol === 'https:') {
wsPrefix = 'wss:'
} else {
wsPrefix = 'ws:'
}
export const server = '';
export const wsServer = wsPrefix + window.location.host;
export const prefix = window.location.protocol + '//' + window.location.host;
// let wsPrefix;
// if (window.location.protocol === 'https:') {
// wsPrefix = 'wss:'
// } else {
// wsPrefix = 'ws:'
// }
//
// export const server = '';
// export const wsServer = wsPrefix + window.location.host;
// export const prefix = window.location.protocol + '//' + window.location.host;
// dev
// export const server = '//127.0.0.1:8088';
// export const wsServer = 'ws://127.0.0.1:8088';
// export const prefix = '';
export const server = '//127.0.0.1:8088';
export const wsServer = 'ws://127.0.0.1:8088';
export const prefix = '';
export const PROTOCOL_COLORS = {
'rdp': 'red',

View File

@ -602,7 +602,6 @@ class User extends Component {
>
<UserShareAsset
sharer={this.state.sharer}
owner={this.state.owner}
/>
</Modal>

View File

@ -8,6 +8,7 @@ import {message} from "antd/es";
import {DeleteOutlined, ExclamationCircleOutlined, PlusOutlined, SyncOutlined, UndoOutlined} from '@ant-design/icons';
import Logout from "./Logout";
import UserGroupModal from "./UserGroupModal";
import UserShareAsset from "./UserShareAsset";
const confirm = Modal.confirm;
const {Search} = Input;
@ -250,6 +251,13 @@ class UserGroup extends Component {
})
}
handleAssetCancel = () => {
this.loadTableData()
this.setState({
assetVisible: false
})
}
render() {
const columns = [{
@ -263,11 +271,16 @@ class UserGroup extends Component {
title: '名称',
dataIndex: 'name',
}, {
title: '成员人数',
dataIndex: 'memberCount',
key: 'memberCount',
title: '授权资产',
dataIndex: 'assetCount',
key: 'assetCount',
render: (text, record, index) => {
return <Button type='link' onClick={() => this.showModal('更新用户组', record['id'], index)}>{text}</Button>
return <Button type='link' onClick={async () => {
this.setState({
assetVisible: true,
userGroupId: record['id']
})
}}>{text}</Button>
}
}, {
title: '创建日期',
@ -417,6 +430,24 @@ class UserGroup extends Component {
</UserGroupModal> : undefined
}
<Modal
width={window.innerWidth * 0.8}
title='已授权资产'
visible={this.state.assetVisible}
maskClosable={false}
destroyOnClose={true}
onOk={() => {
}}
onCancel={this.handleAssetCancel}
okText='确定'
cancelText='取消'
footer={null}
>
<UserShareAsset
userGroupId={this.state.userGroupId}
/>
</Modal>
</Content>
</>

View File

@ -24,7 +24,7 @@ import {message} from "antd/es";
import {DeleteOutlined, ExclamationCircleOutlined, PlusOutlined, SyncOutlined, UndoOutlined} from '@ant-design/icons';
import {PROTOCOL_COLORS} from "../../common/constants";
import UserShareChooseAsset from "./UserShareSelectAsset";
import UserShareSelectedAsset from "./UserShareSelectedAsset";
const confirm = Modal.confirm;
const {Search} = Input;
@ -61,7 +61,8 @@ class UserShareAsset extends Component {
async componentDidMount() {
let sharer = this.props.sharer;
this.loadTableData({sharer: sharer});
let userGroupId = this.props.userGroupId;
this.loadTableData({sharer: sharer, userGroupId: userGroupId});
let result = await request.get('/tags');
if (result['code'] === 1) {
@ -132,10 +133,6 @@ class UserShareAsset extends Component {
};
handleTagsChange = tags => {
console.log(tags)
// this.setState({
// tags: tags
// })
let query = {
...this.state.queryParams,
'pageIndex': 1,
@ -375,11 +372,12 @@ class UserShareAsset extends Component {
visible={this.state.chooseAssetVisible}
width={window.innerWidth * 0.8}
>
<UserShareChooseAsset
<UserShareSelectedAsset
sharer={this.state.queryParams.sharer}
userGroupId={this.state.queryParams.userGroupId}
>
</UserShareChooseAsset>
</UserShareSelectedAsset>
</Drawer> : undefined
}

View File

@ -13,7 +13,7 @@ const {Search} = Input;
const {Content} = Layout;
const {Title} = Typography;
class UserShareAsset extends Component {
class UserShareSelectedAsset extends Component {
inputRefOfName = React.createRef();
changeOwnerFormRef = React.createRef();
@ -45,16 +45,17 @@ class UserShareAsset extends Component {
async componentDidMount() {
this.setState({
sharer: this.props.sharer
sharer: this.props.sharer,
userGroupId: this.props.userGroupId
})
this.loadTableData();
this.init(this.props.sharer)
this.init(this.props.sharer, this.props.userGroupId)
}
async init(sharer) {
async init(sharer, userGroupId) {
let q1 = request.get('/tags');
let q2 = request.get('/assets/paging?pageIndex=1&pageSize=1000&sharer=' + sharer);
let q2 = request.get(`/assets/paging?pageIndex=1&pageSize=1000&sharer=${sharer}&userGroupId=${userGroupId}`);
let r1 = await q1;
let r2 = await q2;
@ -168,7 +169,9 @@ class UserShareAsset extends Component {
unSelectRow = async (assetId) => {
let userId = this.state.sharer;
let userGroupId = this.state.userGroupId;
let result = await request.post(`/resource-sharers/remove-resources`, {
userGroupId: userGroupId,
userId: userId,
resourceType: 'asset',
resourceIds: [assetId]
@ -368,7 +371,9 @@ class UserShareAsset extends Component {
}
let userId = this.state.sharer;
let userGroupId = this.state.userGroupId;
let result = await request.post(`/resource-sharers/add-resources`, {
userGroupId: userGroupId,
userId: userId,
resourceType: 'asset',
resourceIds: newRowKeys
@ -412,4 +417,4 @@ class UserShareAsset extends Component {
}
}
export default UserShareAsset;
export default UserShareSelectedAsset;