feat: 添加数据库资产、命令拦截器、授权资产等功能,修复GitHub Actions工作流
This commit is contained in:
@@ -0,0 +1,389 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var AuthorisedAssetRepository = new(authorisedAssetRepository)
|
||||
|
||||
type authorisedAssetRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r authorisedAssetRepository) Find(c context.Context, pageIndex, pageSize int, userId, departmentId, assetGroupId, assetId string) (o []model.AuthorisedAsset, total int64, err error) {
|
||||
db := r.GetDB(c).Model(&model.AuthorisedAsset{})
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("user_id = ?", userId)
|
||||
}
|
||||
if departmentId != "" {
|
||||
db = db.Where("department_id = ?", departmentId)
|
||||
}
|
||||
if assetGroupId != "" {
|
||||
db = db.Where("asset_group_id = ?", assetGroupId)
|
||||
}
|
||||
if assetId != "" {
|
||||
db = db.Where("asset_id = ?", assetId)
|
||||
}
|
||||
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r authorisedAssetRepository) FindById(c context.Context, id string) (o model.AuthorisedAsset, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r authorisedAssetRepository) Create(c context.Context, o *model.AuthorisedAsset) error {
|
||||
o.Created = common.NowJsonTime()
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r authorisedAssetRepository) CreateInBatches(c context.Context, items []model.AuthorisedAsset) error {
|
||||
for i := range items {
|
||||
items[i].Created = common.NowJsonTime()
|
||||
}
|
||||
return r.GetDB(c).CreateInBatches(items, 100).Error
|
||||
}
|
||||
|
||||
func (r authorisedAssetRepository) UpdateById(c context.Context, o *model.AuthorisedAsset) error {
|
||||
return r.GetDB(c).Updates(o).Error
|
||||
}
|
||||
|
||||
func (r authorisedAssetRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.AuthorisedAsset{}).Error
|
||||
}
|
||||
|
||||
func (r authorisedAssetRepository) Selected(c context.Context, expect, userId, departmentId, assetId string) (result []string, err error) {
|
||||
var items []model.AuthorisedAsset
|
||||
db := r.GetDB(c)
|
||||
switch expect {
|
||||
case "userId":
|
||||
db = db.Select("user_id")
|
||||
case "departmentId":
|
||||
db = db.Select("department_id")
|
||||
case "assetId":
|
||||
db = db.Select("asset_id")
|
||||
case "assetGroupId":
|
||||
db = db.Select("asset_group_id")
|
||||
}
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("user_id = ?", userId)
|
||||
}
|
||||
if departmentId != "" {
|
||||
db = db.Where("department_id = ?", departmentId)
|
||||
}
|
||||
if assetId != "" {
|
||||
db = db.Where("asset_id = ?", assetId)
|
||||
}
|
||||
|
||||
err = db.Find(&items).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
switch expect {
|
||||
case "userId":
|
||||
if item.UserId != "" {
|
||||
result = append(result, item.UserId)
|
||||
}
|
||||
case "departmentId":
|
||||
if item.DepartmentId != "" {
|
||||
result = append(result, item.DepartmentId)
|
||||
}
|
||||
case "assetId":
|
||||
if item.AssetId != "" {
|
||||
result = append(result, item.AssetId)
|
||||
}
|
||||
case "assetGroupId":
|
||||
if item.AssetGroupId != "" {
|
||||
result = append(result, item.AssetGroupId)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r authorisedAssetRepository) FindWithDetails(c context.Context, pageIndex, pageSize int, userId, departmentId, assetGroupId, assetId string) (o []map[string]interface{}, total int64, err error) {
|
||||
db := r.GetDB(c).Table("authorised_assets").
|
||||
Select(`authorised_assets.id,
|
||||
strftime('%s', authorised_assets.created) * 1000 as "createdAt",
|
||||
authorised_assets.expired_at as "expiredAt",
|
||||
authorised_assets.user_id as "userId", users.nickname as "userName",
|
||||
authorised_assets.department_id as "departmentId", departments.name as "departmentName",
|
||||
authorised_assets.asset_id as "assetId", assets.name as "assetName",
|
||||
authorised_assets.asset_group_id as "assetGroupId", asset_groups.name as "assetGroupName",
|
||||
authorised_assets.strategy_id as "strategyId", strategies.name as "strategyName"`).
|
||||
Joins("left join users on users.id = authorised_assets.user_id").
|
||||
Joins("left join departments on departments.id = authorised_assets.department_id").
|
||||
Joins("left join assets on assets.id = authorised_assets.asset_id").
|
||||
Joins("left join asset_groups on asset_groups.id = authorised_assets.asset_group_id").
|
||||
Joins("left join strategies on strategies.id = authorised_assets.strategy_id")
|
||||
|
||||
dbCounter := r.GetDB(c).Model(&model.AuthorisedAsset{})
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("authorised_assets.user_id = ?", userId)
|
||||
dbCounter = dbCounter.Where("user_id = ?", userId)
|
||||
}
|
||||
if departmentId != "" {
|
||||
db = db.Where("authorised_assets.department_id = ?", departmentId)
|
||||
dbCounter = dbCounter.Where("department_id = ?", departmentId)
|
||||
}
|
||||
if assetGroupId != "" {
|
||||
db = db.Where("authorised_assets.asset_group_id = ?", assetGroupId)
|
||||
dbCounter = dbCounter.Where("asset_group_id = ?", assetGroupId)
|
||||
}
|
||||
if assetId != "" {
|
||||
db = db.Where("authorised_assets.asset_id = ?", assetId)
|
||||
dbCounter = dbCounter.Where("asset_id = ?", assetId)
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Order("authorised_assets.created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
var AuthorisedDatabaseAssetRepository = new(authorisedDatabaseAssetRepository)
|
||||
|
||||
type authorisedDatabaseAssetRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r authorisedDatabaseAssetRepository) Find(c context.Context, pageIndex, pageSize int, userId, departmentId, databaseAssetId string) (o []model.AuthorisedDatabaseAsset, total int64, err error) {
|
||||
db := r.GetDB(c).Model(&model.AuthorisedDatabaseAsset{})
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("user_id = ?", userId)
|
||||
}
|
||||
if departmentId != "" {
|
||||
db = db.Where("department_id = ?", departmentId)
|
||||
}
|
||||
if databaseAssetId != "" {
|
||||
db = db.Where("database_asset_id = ?", databaseAssetId)
|
||||
}
|
||||
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r authorisedDatabaseAssetRepository) FindById(c context.Context, id string) (o model.AuthorisedDatabaseAsset, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r authorisedDatabaseAssetRepository) Create(c context.Context, o *model.AuthorisedDatabaseAsset) error {
|
||||
o.Created = common.NowJsonTime()
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r authorisedDatabaseAssetRepository) CreateInBatches(c context.Context, items []model.AuthorisedDatabaseAsset) error {
|
||||
for i := range items {
|
||||
items[i].Created = common.NowJsonTime()
|
||||
}
|
||||
return r.GetDB(c).CreateInBatches(items, 100).Error
|
||||
}
|
||||
|
||||
func (r authorisedDatabaseAssetRepository) UpdateById(c context.Context, o *model.AuthorisedDatabaseAsset) error {
|
||||
return r.GetDB(c).Updates(o).Error
|
||||
}
|
||||
|
||||
func (r authorisedDatabaseAssetRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.AuthorisedDatabaseAsset{}).Error
|
||||
}
|
||||
|
||||
func (r authorisedDatabaseAssetRepository) Selected(c context.Context, expect, userId, departmentId, databaseAssetId string) (result []string, err error) {
|
||||
var items []model.AuthorisedDatabaseAsset
|
||||
db := r.GetDB(c)
|
||||
switch expect {
|
||||
case "userId":
|
||||
db = db.Select("user_id")
|
||||
case "departmentId":
|
||||
db = db.Select("department_id")
|
||||
case "databaseAssetId":
|
||||
db = db.Select("database_asset_id")
|
||||
}
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("user_id = ?", userId)
|
||||
}
|
||||
if departmentId != "" {
|
||||
db = db.Where("department_id = ?", departmentId)
|
||||
}
|
||||
if databaseAssetId != "" {
|
||||
db = db.Where("database_asset_id = ?", databaseAssetId)
|
||||
}
|
||||
|
||||
err = db.Find(&items).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
for _, item := range items {
|
||||
switch expect {
|
||||
case "userId":
|
||||
if item.UserId != "" {
|
||||
result = append(result, item.UserId)
|
||||
}
|
||||
case "departmentId":
|
||||
if item.DepartmentId != "" {
|
||||
result = append(result, item.DepartmentId)
|
||||
}
|
||||
case "databaseAssetId":
|
||||
if item.DatabaseAssetId != "" {
|
||||
result = append(result, item.DatabaseAssetId)
|
||||
}
|
||||
}
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r authorisedDatabaseAssetRepository) FindWithDetails(c context.Context, pageIndex, pageSize int, userId, departmentId, databaseAssetId string) (o []map[string]interface{}, total int64, err error) {
|
||||
db := r.GetDB(c).Table("authorised_database_assets").
|
||||
Select(`authorised_database_assets.id,
|
||||
strftime('%s', authorised_database_assets.created) * 1000 as "createdAt",
|
||||
authorised_database_assets.expired_at as "expiredAt",
|
||||
authorised_database_assets.user_id as "userId", users.nickname as "userName",
|
||||
authorised_database_assets.department_id as "departmentId", departments.name as "departmentName",
|
||||
authorised_database_assets.database_asset_id as "databaseAssetId", database_assets.name as "databaseAssetName",
|
||||
authorised_database_assets.strategy_id as "strategyId", strategies.name as "strategyName"`).
|
||||
Joins("left join users on users.id = authorised_database_assets.user_id").
|
||||
Joins("left join departments on departments.id = authorised_database_assets.department_id").
|
||||
Joins("left join database_assets on database_assets.id = authorised_database_assets.database_asset_id").
|
||||
Joins("left join strategies on strategies.id = authorised_database_assets.strategy_id")
|
||||
|
||||
dbCounter := r.GetDB(c).Model(&model.AuthorisedDatabaseAsset{})
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("authorised_database_assets.user_id = ?", userId)
|
||||
dbCounter = dbCounter.Where("user_id = ?", userId)
|
||||
}
|
||||
if departmentId != "" {
|
||||
db = db.Where("authorised_database_assets.department_id = ?", departmentId)
|
||||
dbCounter = dbCounter.Where("department_id = ?", departmentId)
|
||||
}
|
||||
if databaseAssetId != "" {
|
||||
db = db.Where("authorised_database_assets.database_asset_id = ?", databaseAssetId)
|
||||
dbCounter = dbCounter.Where("database_asset_id = ?", databaseAssetId)
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Order("authorised_database_assets.created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
var AuthorisedWebsiteRepository = new(authorisedWebsiteRepository)
|
||||
|
||||
type authorisedWebsiteRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r authorisedWebsiteRepository) Find(c context.Context, pageIndex, pageSize int, userId, departmentId, websiteId string) (o []model.AuthorisedWebsite, total int64, err error) {
|
||||
db := r.GetDB(c).Model(&model.AuthorisedWebsite{})
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("user_id = ?", userId)
|
||||
}
|
||||
if departmentId != "" {
|
||||
db = db.Where("department_id = ?", departmentId)
|
||||
}
|
||||
if websiteId != "" {
|
||||
db = db.Where("website_id = ?", websiteId)
|
||||
}
|
||||
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Order("created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r authorisedWebsiteRepository) FindById(c context.Context, id string) (o model.AuthorisedWebsite, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r authorisedWebsiteRepository) Create(c context.Context, o *model.AuthorisedWebsite) error {
|
||||
o.Created = common.NowJsonTime()
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r authorisedWebsiteRepository) CreateInBatches(c context.Context, items []model.AuthorisedWebsite) error {
|
||||
for i := range items {
|
||||
items[i].Created = common.NowJsonTime()
|
||||
}
|
||||
return r.GetDB(c).CreateInBatches(items, 100).Error
|
||||
}
|
||||
|
||||
func (r authorisedWebsiteRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.AuthorisedWebsite{}).Error
|
||||
}
|
||||
|
||||
func (r authorisedWebsiteRepository) FindWithDetails(c context.Context, pageIndex, pageSize int, userId, departmentId, websiteId string) (o []map[string]interface{}, total int64, err error) {
|
||||
db := r.GetDB(c).Table("authorised_websites").
|
||||
Select(`authorised_websites.id,
|
||||
strftime('%s', authorised_websites.created) * 1000 as "createdAt",
|
||||
authorised_websites.expired_at as "expiredAt",
|
||||
authorised_websites.user_id as "userId", users.nickname as "userName",
|
||||
authorised_websites.department_id as "departmentId", departments.name as "departmentName",
|
||||
authorised_websites.website_id as "websiteId", websites.name as "websiteName",
|
||||
authorised_websites.strategy_id as "strategyId", strategies.name as "strategyName"`).
|
||||
Joins("left join users on users.id = authorised_websites.user_id").
|
||||
Joins("left join departments on departments.id = authorised_websites.department_id").
|
||||
Joins("left join websites on websites.id = authorised_websites.website_id").
|
||||
Joins("left join strategies on strategies.id = authorised_websites.strategy_id")
|
||||
|
||||
dbCounter := r.GetDB(c).Model(&model.AuthorisedWebsite{})
|
||||
|
||||
if userId != "" {
|
||||
db = db.Where("authorised_websites.user_id = ?", userId)
|
||||
dbCounter = dbCounter.Where("user_id = ?", userId)
|
||||
}
|
||||
if departmentId != "" {
|
||||
db = db.Where("authorised_websites.department_id = ?", departmentId)
|
||||
dbCounter = dbCounter.Where("department_id = ?", departmentId)
|
||||
}
|
||||
if websiteId != "" {
|
||||
db = db.Where("authorised_websites.website_id = ?", websiteId)
|
||||
dbCounter = dbCounter.Where("website_id = ?", websiteId)
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
err = db.Order("authorised_websites.created desc").Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func init() {
|
||||
_ = strconv.Itoa(0)
|
||||
}
|
||||
@@ -0,0 +1,110 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var CommandFilterRepository = new(commandFilterRepository)
|
||||
|
||||
type commandFilterRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r commandFilterRepository) FindAll(c context.Context) (o []model.CommandFilter, err error) {
|
||||
err = r.GetDB(c).Order("name asc").Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r commandFilterRepository) Find(c context.Context, pageIndex, pageSize int, name, order, field string) (o []model.CommandFilter, total int64, err error) {
|
||||
db := r.GetDB(c).Model(&model.CommandFilter{})
|
||||
|
||||
if len(name) > 0 {
|
||||
db = db.Where("name like ?", "%"+name+"%")
|
||||
}
|
||||
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if order == "" {
|
||||
order = "asc"
|
||||
}
|
||||
if field == "" {
|
||||
field = "name"
|
||||
}
|
||||
|
||||
err = db.Order(field + " " + order).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r commandFilterRepository) FindById(c context.Context, id string) (o model.CommandFilter, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r commandFilterRepository) Create(c context.Context, o *model.CommandFilter) error {
|
||||
o.Created = common.NowJsonTime()
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r commandFilterRepository) UpdateById(c context.Context, o *model.CommandFilter) error {
|
||||
return r.GetDB(c).Updates(o).Error
|
||||
}
|
||||
|
||||
func (r commandFilterRepository) DeleteById(c context.Context, id string) error {
|
||||
tx := r.GetDB(c).Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
if err := tx.Where("command_filter_id = ?", id).Delete(&model.CommandFilterRule{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
if err := tx.Where("id = ?", id).Delete(&model.CommandFilter{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
||||
var CommandFilterRuleRepository = new(commandFilterRuleRepository)
|
||||
|
||||
type commandFilterRuleRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r commandFilterRuleRepository) FindByCommandFilterId(c context.Context, commandFilterId string) (o []model.CommandFilterRule, err error) {
|
||||
err = r.GetDB(c).Where("command_filter_id = ?", commandFilterId).Order("priority asc").Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r commandFilterRuleRepository) FindById(c context.Context, id string) (o model.CommandFilterRule, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r commandFilterRuleRepository) Create(c context.Context, o *model.CommandFilterRule) error {
|
||||
o.Created = common.NowJsonTime()
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r commandFilterRuleRepository) UpdateById(c context.Context, o *model.CommandFilterRule) error {
|
||||
return r.GetDB(c).Updates(o).Error
|
||||
}
|
||||
|
||||
func (r commandFilterRuleRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.CommandFilterRule{}).Error
|
||||
}
|
||||
|
||||
func (r commandFilterRuleRepository) DeleteByCommandFilterId(c context.Context, commandFilterId string) error {
|
||||
return r.GetDB(c).Where("command_filter_id = ?", commandFilterId).Delete(&model.CommandFilterRule{}).Error
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
"strconv"
|
||||
"strings"
|
||||
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var DatabaseAssetRepository = new(databaseAssetRepository)
|
||||
|
||||
type databaseAssetRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) FindAll(c context.Context) (o []model.DatabaseAsset, err error) {
|
||||
err = r.GetDB(c).Order("name asc").Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) FindByType(c context.Context, dbType string) (o []model.DatabaseAsset, err error) {
|
||||
db := r.GetDB(c)
|
||||
if dbType != "" {
|
||||
db = db.Where("type = ?", dbType)
|
||||
}
|
||||
err = db.Order("name asc").Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) Find(c context.Context, pageIndex, pageSize int, name, dbType, order, field string) (o []model.DatabaseAsset, total int64, err error) {
|
||||
db := r.GetDB(c).Model(&model.DatabaseAsset{})
|
||||
|
||||
if len(name) > 0 {
|
||||
db = db.Where("name like ?", "%"+name+"%")
|
||||
}
|
||||
|
||||
if len(dbType) > 0 {
|
||||
db = db.Where("type = ?", dbType)
|
||||
}
|
||||
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if order == "" {
|
||||
order = "asc"
|
||||
}
|
||||
if field == "" {
|
||||
field = "name"
|
||||
}
|
||||
|
||||
orderBy := field + " " + order
|
||||
if field == "created" {
|
||||
orderBy = "created " + order
|
||||
}
|
||||
|
||||
err = db.Order(orderBy).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) FindById(c context.Context, id string) (o model.DatabaseAsset, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) Create(c context.Context, o *model.DatabaseAsset) error {
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) UpdateById(c context.Context, o *model.DatabaseAsset) error {
|
||||
return r.GetDB(c).Updates(o).Error
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.DatabaseAsset{}).Error
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) Count(c context.Context) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.DatabaseAsset{}).Count(&total).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) FindAllTags(c context.Context) (o []string, err error) {
|
||||
var assets []model.DatabaseAsset
|
||||
err = r.GetDB(c).Select("tags").Find(&assets).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
tagSet := make(map[string]bool)
|
||||
for _, asset := range assets {
|
||||
if asset.Tags == "" {
|
||||
continue
|
||||
}
|
||||
tags := strings.Split(asset.Tags, ",")
|
||||
for _, tag := range tags {
|
||||
tag = strings.TrimSpace(tag)
|
||||
if tag != "" {
|
||||
tagSet[tag] = true
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
for tag := range tagSet {
|
||||
o = append(o, tag)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) ParseTags(tags string) []string {
|
||||
if tags == "" {
|
||||
return nil
|
||||
}
|
||||
return strings.Split(tags, ",")
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) FormatTags(tags []string) string {
|
||||
if len(tags) == 0 {
|
||||
return ""
|
||||
}
|
||||
return strings.Join(tags, ",")
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) GetDBPort(dbType string) int {
|
||||
switch dbType {
|
||||
case "mysql":
|
||||
return 3306
|
||||
case "pg":
|
||||
return 5432
|
||||
case "sqlserver":
|
||||
return 1433
|
||||
case "oracle":
|
||||
return 1521
|
||||
default:
|
||||
return 3306
|
||||
}
|
||||
}
|
||||
|
||||
func (r databaseAssetRepository) UpdateOwner(c context.Context, ownerId string, newOwnerId string) error {
|
||||
return r.GetDB(c).Model(&model.DatabaseAsset{}).Where("owner = ?", ownerId).Update("owner", newOwnerId).Error
|
||||
}
|
||||
|
||||
func init() {
|
||||
_ = strconv.Itoa(0)
|
||||
_ = strings.Join(nil, "")
|
||||
}
|
||||
@@ -0,0 +1,109 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var DepartmentRepository = new(departmentRepository)
|
||||
|
||||
type departmentRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r departmentRepository) FindAll(c context.Context) (o []model.Department, err error) {
|
||||
err = r.GetDB(c).Order("sort asc, name asc").Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r departmentRepository) Find(c context.Context, pageIndex, pageSize int, name, order, field string) (o []model.Department, total int64, err error) {
|
||||
db := r.GetDB(c).Model(&model.Department{})
|
||||
|
||||
if len(name) > 0 {
|
||||
db = db.Where("name like ?", "%"+name+"%")
|
||||
}
|
||||
|
||||
err = db.Count(&total).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
if order == "" {
|
||||
order = "asc"
|
||||
}
|
||||
if field == "" {
|
||||
field = "sort"
|
||||
}
|
||||
|
||||
err = db.Order(field + " " + order).Offset((pageIndex - 1) * pageSize).Limit(pageSize).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r departmentRepository) FindById(c context.Context, id string) (o model.Department, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r departmentRepository) FindByParentId(c context.Context, parentId string) (o []model.Department, err error) {
|
||||
db := r.GetDB(c)
|
||||
if parentId == "" {
|
||||
db = db.Where("parent_id = '' or parent_id is null")
|
||||
} else {
|
||||
db = db.Where("parent_id = ?", parentId)
|
||||
}
|
||||
err = db.Order("sort asc, name asc").Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r departmentRepository) Create(c context.Context, o *model.Department) error {
|
||||
o.Created = common.NowJsonTime()
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r departmentRepository) UpdateById(c context.Context, o *model.Department) error {
|
||||
return r.GetDB(c).Updates(o).Error
|
||||
}
|
||||
|
||||
func (r departmentRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.Department{}).Error
|
||||
}
|
||||
|
||||
func (r departmentRepository) CountByParentId(c context.Context, parentId string) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.Department{}).Where("parent_id = ?", parentId).Count(&total).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r departmentRepository) BuildTree(departments []model.Department) []map[string]interface{} {
|
||||
departmentMap := make(map[string][]map[string]interface{})
|
||||
var roots []map[string]interface{}
|
||||
|
||||
for _, dept := range departments {
|
||||
node := map[string]interface{}{
|
||||
"title": dept.Name,
|
||||
"key": dept.ID,
|
||||
"value": dept.ID,
|
||||
"children": []map[string]interface{}{},
|
||||
}
|
||||
if dept.ParentId == "" {
|
||||
roots = append(roots, node)
|
||||
} else {
|
||||
departmentMap[dept.ParentId] = append(departmentMap[dept.ParentId], node)
|
||||
}
|
||||
}
|
||||
|
||||
var buildChildren func(nodes []map[string]interface{})
|
||||
buildChildren = func(nodes []map[string]interface{}) {
|
||||
for i := range nodes {
|
||||
id := nodes[i]["key"].(string)
|
||||
if children, ok := departmentMap[id]; ok {
|
||||
nodes[i]["children"] = children
|
||||
buildChildren(children)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
buildChildren(roots)
|
||||
return roots
|
||||
}
|
||||
@@ -7,6 +7,8 @@ import (
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
const SuperAdminID = `abcdefghijklmnopqrstuvwxyz`
|
||||
|
||||
var UserRepository = new(userRepository)
|
||||
|
||||
type userRepository struct {
|
||||
@@ -18,7 +20,7 @@ func (r userRepository) FindAll(c context.Context) (o []model.User, err error) {
|
||||
return
|
||||
}
|
||||
|
||||
func (r userRepository) Find(c context.Context, pageIndex, pageSize int, username, nickname, mail, online, loginPolicyId, order, field string) (o []model.UserForPage, total int64, err error) {
|
||||
func (r userRepository) Find(c context.Context, pageIndex, pageSize int, username, nickname, mail, online, userType, loginPolicyId, order, field string) (o []model.UserForPage, total int64, err error) {
|
||||
db := r.GetDB(c).Table("users").Select("users.id,users.username,users.nickname,users.mail,users.phone,users.online,users.created,users.type,users.status,users.source,users.recording,users.watermark, users.totp_secret")
|
||||
dbCounter := r.GetDB(c).Table("users")
|
||||
|
||||
@@ -54,6 +56,17 @@ func (r userRepository) Find(c context.Context, pageIndex, pageSize int, usernam
|
||||
dbCounter = dbCounter.Where("users.online = ?", _online)
|
||||
}
|
||||
|
||||
if userType == "super-admin" {
|
||||
db = db.Where("users.id = ?", SuperAdminID)
|
||||
dbCounter = dbCounter.Where("id = ?", SuperAdminID)
|
||||
} else if userType == "admin" {
|
||||
db = db.Where("users.type = ? and users.id != ?", "admin", SuperAdminID)
|
||||
dbCounter = dbCounter.Where("type = ? and id != ?", "admin", SuperAdminID)
|
||||
} else if userType == "user" {
|
||||
db = db.Where("users.type = ?", "user")
|
||||
dbCounter = dbCounter.Where("type = ?", "user")
|
||||
}
|
||||
|
||||
err = dbCounter.Count(&total).Error
|
||||
if err != nil {
|
||||
return nil, 0, err
|
||||
|
||||
@@ -0,0 +1,106 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var UserDepartmentRepository = new(userDepartmentRepository)
|
||||
|
||||
type userDepartmentRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) FindByUserId(c context.Context, userId string) (o []model.UserDepartmentRef, err error) {
|
||||
err = r.GetDB(c).Where("user_id = ?", userId).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) FindByDepartmentId(c context.Context, departmentId string) (o []model.UserDepartmentRef, err error) {
|
||||
err = r.GetDB(c).Where("department_id = ?", departmentId).Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) FindUsersByDepartmentId(c context.Context, departmentId string) (userIds []string, err error) {
|
||||
var refs []model.UserDepartmentRef
|
||||
err = r.GetDB(c).Where("department_id = ?", departmentId).Find(&refs).Error
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
for _, ref := range refs {
|
||||
userIds = append(userIds, ref.UserId)
|
||||
}
|
||||
return
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) SaveUserDepartments(c context.Context, userId string, departmentIds []string) error {
|
||||
tx := r.GetDB(c).Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
if err := tx.Where("user_id = ?", userId).Delete(&model.UserDepartmentRef{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
for _, deptId := range departmentIds {
|
||||
ref := model.UserDepartmentRef{
|
||||
UserId: userId,
|
||||
DepartmentId: deptId,
|
||||
}
|
||||
if err := tx.Create(&ref).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) SaveDepartmentUsers(c context.Context, departmentId string, userIds []string) error {
|
||||
tx := r.GetDB(c).Begin()
|
||||
defer func() {
|
||||
if r := recover(); r != nil {
|
||||
tx.Rollback()
|
||||
}
|
||||
}()
|
||||
|
||||
if err := tx.Where("department_id = ?", departmentId).Delete(&model.UserDepartmentRef{}).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
|
||||
for _, userId := range userIds {
|
||||
ref := model.UserDepartmentRef{
|
||||
UserId: userId,
|
||||
DepartmentId: departmentId,
|
||||
}
|
||||
if err := tx.Create(&ref).Error; err != nil {
|
||||
tx.Rollback()
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return tx.Commit().Error
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) RemoveUsersFromDepartment(c context.Context, departmentId string, userIds []string) error {
|
||||
return r.GetDB(c).Where("department_id = ? AND user_id IN ?", departmentId, userIds).Delete(&model.UserDepartmentRef{}).Error
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) DeleteByUserId(c context.Context, userId string) error {
|
||||
return r.GetDB(c).Where("user_id = ?", userId).Delete(&model.UserDepartmentRef{}).Error
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) DeleteByDepartmentId(c context.Context, departmentId string) error {
|
||||
return r.GetDB(c).Where("department_id = ?", departmentId).Delete(&model.UserDepartmentRef{}).Error
|
||||
}
|
||||
|
||||
func (r userDepartmentRepository) CountByDepartmentId(c context.Context, departmentId string) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.UserDepartmentRef{}).Where("department_id = ?", departmentId).Count(&total).Error
|
||||
return
|
||||
}
|
||||
@@ -3,6 +3,7 @@ package repository
|
||||
import (
|
||||
"context"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
@@ -18,7 +19,8 @@ func (r websiteRepository) FindAll(c context.Context) (o []model.Website, err er
|
||||
}
|
||||
|
||||
func (r websiteRepository) Find(c context.Context, pageIndex, pageSize int, keyword string) (o []model.WebsiteForPage, total int64, err error) {
|
||||
db := r.GetDB(c).Table("websites").Select("id,name,enabled,target_url,target_host,target_port,domain,status,status_text,created,group_id,sort")
|
||||
db := r.GetDB(c).Table("websites").Select(`id,name,enabled,target_url,target_host,target_port,domain,status,status_text,
|
||||
strftime('%s', created) * 1000 as created,group_id,sort`)
|
||||
dbCounter := r.GetDB(c).Table("websites")
|
||||
|
||||
if len(keyword) > 0 {
|
||||
@@ -39,6 +41,7 @@ func (r websiteRepository) Find(c context.Context, pageIndex, pageSize int, keyw
|
||||
}
|
||||
|
||||
func (r websiteRepository) Create(c context.Context, o *model.Website) error {
|
||||
o.Created = common.NowJsonTime()
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
@@ -55,3 +58,8 @@ func (r websiteRepository) FindById(c context.Context, id string) (o model.Websi
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r websiteRepository) Count(c context.Context) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.Website{}).Count(&total).Error
|
||||
return
|
||||
}
|
||||
|
||||
@@ -0,0 +1,50 @@
|
||||
package repository
|
||||
|
||||
import (
|
||||
"context"
|
||||
|
||||
"next-terminal/server/common"
|
||||
"next-terminal/server/model"
|
||||
)
|
||||
|
||||
var WebsiteGroupRepository = new(websiteGroupRepository)
|
||||
|
||||
type websiteGroupRepository struct {
|
||||
baseRepository
|
||||
}
|
||||
|
||||
func (r websiteGroupRepository) FindAll(c context.Context) (o []model.WebsiteGroup, err error) {
|
||||
err = r.GetDB(c).Order("sort asc, name asc").Find(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r websiteGroupRepository) FindById(c context.Context, id string) (o model.WebsiteGroup, err error) {
|
||||
err = r.GetDB(c).Where("id = ?", id).First(&o).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r websiteGroupRepository) Create(c context.Context, o *model.WebsiteGroup) error {
|
||||
o.Created = common.NowJsonTime()
|
||||
return r.GetDB(c).Create(o).Error
|
||||
}
|
||||
|
||||
func (r websiteGroupRepository) UpdateById(c context.Context, o *model.WebsiteGroup) error {
|
||||
return r.GetDB(c).Updates(o).Error
|
||||
}
|
||||
|
||||
func (r websiteGroupRepository) DeleteById(c context.Context, id string) error {
|
||||
return r.GetDB(c).Where("id = ?", id).Delete(&model.WebsiteGroup{}).Error
|
||||
}
|
||||
|
||||
func (r websiteGroupRepository) DeleteByParentId(c context.Context, parentId string) error {
|
||||
return r.GetDB(c).Where("parent_id = ?", parentId).Delete(&model.WebsiteGroup{}).Error
|
||||
}
|
||||
|
||||
func (r websiteGroupRepository) CountByParentId(c context.Context, parentId string) (total int64, err error) {
|
||||
err = r.GetDB(c).Model(&model.WebsiteGroup{}).Where("parent_id = ?", parentId).Count(&total).Error
|
||||
return
|
||||
}
|
||||
|
||||
func (r websiteGroupRepository) DeleteAll(c context.Context) error {
|
||||
return r.GetDB(c).Where("1 = 1").Delete(&model.WebsiteGroup{}).Error
|
||||
}
|
||||
Reference in New Issue
Block a user