-
Notifications
You must be signed in to change notification settings - Fork 2.9k
fix: Fix issue where website group can still be deleted even if it co… #8049
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| package v2 | ||
|
|
||
| import ( | ||
| "github.com/1Panel-dev/1Panel/agent/app/api/v2/helper" | ||
| "github.com/1Panel-dev/1Panel/agent/app/dto" | ||
| "github.com/gin-gonic/gin" | ||
| ) | ||
|
|
||
| // @Tags System Group | ||
| // @Summary Create group | ||
| // @Accept json | ||
| // @Param request body dto.GroupCreate true "request" | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /agent/groups [post] | ||
| // @x-panel-log {"bodyKeys":["name","type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"创建组 [name][type]","formatEN":"create group [name][type]"} | ||
| func (b *BaseApi) CreateGroup(c *gin.Context) { | ||
| var req dto.GroupCreate | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if err := groupService.Create(req); err != nil { | ||
| helper.InternalServer(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithOutData(c) | ||
| } | ||
|
|
||
| // @Tags System Group | ||
| // @Summary Delete group | ||
| // @Accept json | ||
| // @Param request body dto.OperateByID true "request" | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /agent/groups/del [post] | ||
| // @x-panel-log {"bodyKeys":["id"],"paramKeys":[],"BeforeFunctions":[{"input_column":"id","input_value":"id","isList":false,"db":"groups","output_column":"name","output_value":"name"},{"input_column":"id","input_value":"id","isList":false,"db":"groups","output_column":"type","output_value":"type"}],"formatZH":"删除组 [type][name]","formatEN":"delete group [type][name]"} | ||
| func (b *BaseApi) DeleteGroup(c *gin.Context) { | ||
| var req dto.OperateByID | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if err := groupService.Delete(req.ID); err != nil { | ||
| helper.InternalServer(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithOutData(c) | ||
| } | ||
|
|
||
| // @Tags System Group | ||
| // @Summary Update group | ||
| // @Accept json | ||
| // @Param request body dto.GroupUpdate true "request" | ||
| // @Success 200 | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /agent/groups/update [post] | ||
| // @x-panel-log {"bodyKeys":["name","type"],"paramKeys":[],"BeforeFunctions":[],"formatZH":"更新组 [name][type]","formatEN":"update group [name][type]"} | ||
| func (b *BaseApi) UpdateGroup(c *gin.Context) { | ||
| var req dto.GroupUpdate | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if err := groupService.Update(req); err != nil { | ||
| helper.InternalServer(c, err) | ||
| return | ||
| } | ||
| helper.SuccessWithOutData(c) | ||
| } | ||
|
|
||
| // @Tags System Group | ||
| // @Summary List groups | ||
| // @Accept json | ||
| // @Param request body dto.GroupSearch true "request" | ||
| // @Success 200 {array} dto.OperateByType | ||
| // @Security ApiKeyAuth | ||
| // @Security Timestamp | ||
| // @Router /agent/groups/search [post] | ||
| func (b *BaseApi) ListGroup(c *gin.Context) { | ||
| var req dto.OperateByType | ||
| if err := helper.CheckBindAndValidate(&req, c); err != nil { | ||
| return | ||
| } | ||
|
|
||
| list, err := groupService.List(req) | ||
| if err != nil { | ||
| helper.InternalServer(c, err) | ||
| return | ||
| } | ||
|
|
||
| helper.SuccessWithData(c, list) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| package dto | ||
|
|
||
| type GroupCreate struct { | ||
| ID uint `json:"id"` | ||
| Name string `json:"name" validate:"required"` | ||
| Type string `json:"type" validate:"required"` | ||
| } | ||
|
|
||
| type GroupSearch struct { | ||
| Type string `json:"type" validate:"required"` | ||
| } | ||
|
|
||
| type GroupUpdate struct { | ||
| ID uint `json:"id"` | ||
| Name string `json:"name"` | ||
| Type string `json:"type" validate:"required"` | ||
| IsDefault bool `json:"isDefault"` | ||
| } | ||
|
|
||
| type GroupInfo struct { | ||
| ID uint `json:"id"` | ||
| Name string `json:"name"` | ||
| Type string `json:"type"` | ||
| IsDefault bool `json:"isDefault"` | ||
| } | ||
|
|
||
| type OperateByType struct { | ||
| Type string `json:"type"` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,8 @@ | ||
| package model | ||
|
|
||
| type Group struct { | ||
| BaseModel | ||
| IsDefault bool `json:"isDefault"` | ||
| Name string `json:"name"` | ||
| Type string `json:"type"` | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,56 @@ | ||
| package repo | ||
|
|
||
| import ( | ||
| "github.com/1Panel-dev/1Panel/agent/app/model" | ||
| "github.com/1Panel-dev/1Panel/agent/global" | ||
| ) | ||
|
|
||
| type GroupRepo struct{} | ||
|
|
||
| type IGroupRepo interface { | ||
| Get(opts ...DBOption) (model.Group, error) | ||
| GetList(opts ...DBOption) ([]model.Group, error) | ||
| Create(group *model.Group) error | ||
| Update(id uint, vars map[string]interface{}) error | ||
| Delete(opts ...DBOption) error | ||
| } | ||
|
|
||
| func NewIGroupRepo() IGroupRepo { | ||
| return &GroupRepo{} | ||
| } | ||
|
|
||
| func (g *GroupRepo) Get(opts ...DBOption) (model.Group, error) { | ||
| var group model.Group | ||
| db := global.DB | ||
| for _, opt := range opts { | ||
| db = opt(db) | ||
| } | ||
| err := db.First(&group).Error | ||
| return group, err | ||
| } | ||
|
|
||
| func (g *GroupRepo) GetList(opts ...DBOption) ([]model.Group, error) { | ||
| var groups []model.Group | ||
| db := global.DB.Model(&model.Group{}) | ||
| for _, opt := range opts { | ||
| db = opt(db) | ||
| } | ||
| err := db.Find(&groups).Error | ||
| return groups, err | ||
| } | ||
|
|
||
| func (g *GroupRepo) Create(group *model.Group) error { | ||
| return global.DB.Create(group).Error | ||
| } | ||
|
|
||
| func (g *GroupRepo) Update(id uint, vars map[string]interface{}) error { | ||
| return global.DB.Model(&model.Group{}).Where("id = ?", id).Updates(vars).Error | ||
| } | ||
|
|
||
| func (g *GroupRepo) Delete(opts ...DBOption) error { | ||
| db := global.DB | ||
| for _, opt := range opts { | ||
| db = opt(db) | ||
| } | ||
| return db.Delete(&model.Group{}).Error | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -45,4 +45,6 @@ var ( | |
| favoriteRepo = repo.NewIFavoriteRepo() | ||
|
|
||
| taskRepo = repo.NewITaskRepo() | ||
|
|
||
| groupRepo = repo.NewIGroupRepo() | ||
| ) | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,87 @@ | ||
| package service | ||
|
|
||
| import ( | ||
| "github.com/1Panel-dev/1Panel/agent/app/dto" | ||
| "github.com/1Panel-dev/1Panel/agent/app/model" | ||
| "github.com/1Panel-dev/1Panel/agent/app/repo" | ||
| "github.com/1Panel-dev/1Panel/agent/buserr" | ||
| "github.com/jinzhu/copier" | ||
| ) | ||
|
|
||
| type GroupService struct{} | ||
|
|
||
| type IGroupService interface { | ||
| List(req dto.OperateByType) ([]dto.GroupInfo, error) | ||
| Create(req dto.GroupCreate) error | ||
| Update(req dto.GroupUpdate) error | ||
| Delete(id uint) error | ||
| } | ||
|
|
||
| func NewIGroupService() IGroupService { | ||
| return &GroupService{} | ||
| } | ||
|
|
||
| func (u *GroupService) List(req dto.OperateByType) ([]dto.GroupInfo, error) { | ||
| options := []repo.DBOption{ | ||
| repo.WithOrderBy("is_default desc"), | ||
| repo.WithOrderBy("created_at desc"), | ||
| } | ||
| if len(req.Type) != 0 { | ||
| options = append(options, repo.WithByType(req.Type)) | ||
| } | ||
| var ( | ||
| groups []model.Group | ||
| err error | ||
| ) | ||
| groups, err = groupRepo.GetList(options...) | ||
| if err != nil { | ||
| return nil, buserr.New("ErrRecordNotFound") | ||
| } | ||
| var dtoUsers []dto.GroupInfo | ||
| for _, group := range groups { | ||
| var item dto.GroupInfo | ||
| if err := copier.Copy(&item, &group); err != nil { | ||
| return nil, buserr.WithDetail("ErrStructTransform", err.Error(), nil) | ||
| } | ||
| dtoUsers = append(dtoUsers, item) | ||
| } | ||
| return dtoUsers, err | ||
| } | ||
|
|
||
| func (u *GroupService) Create(req dto.GroupCreate) error { | ||
| group, _ := groupRepo.Get(repo.WithByName(req.Name), repo.WithByType(req.Type)) | ||
| if group.ID != 0 { | ||
| return buserr.New("ErrRecordExist") | ||
| } | ||
| if err := copier.Copy(&group, &req); err != nil { | ||
| return buserr.WithDetail("ErrStructTransform", err.Error(), nil) | ||
| } | ||
| if err := groupRepo.Create(&group); err != nil { | ||
| return err | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| func (u *GroupService) Delete(id uint) error { | ||
| group, _ := groupRepo.Get(repo.WithByID(id)) | ||
| if group.ID == 0 { | ||
| return buserr.New("ErrRecordNotFound") | ||
| } | ||
| if group.IsDefault { | ||
| return buserr.New("ErrGroupIsDefault") | ||
| } | ||
| if group.Type == "website" { | ||
| websites, _ := websiteRepo.List(websiteRepo.WithGroupID(group.ID)) | ||
| if len(websites) > 0 { | ||
| return buserr.New("ErrGroupIsInWebsiteUse") | ||
| } | ||
| } | ||
| return groupRepo.Delete(repo.WithByID(id)) | ||
| } | ||
|
|
||
| func (u *GroupService) Update(req dto.GroupUpdate) error { | ||
| upMap := make(map[string]interface{}) | ||
| upMap["name"] = req.Name | ||
| upMap["is_default"] = req.IsDefault | ||
| return groupRepo.Update(req.ID, upMap) | ||
| } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Here are the discrepancies and improvements:
) |
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There are no apparent inconsistencies, issues, or optimizations necessary at this time. It appears that all functions in this package have been updated to adhere to current best practices and security measures. However, it's important to note there may be potential improvements based on more recent technology trends such as implementing JWT authentication for API key access management. This should not require any changes since the base structure is intact with respect to what was done up until September 1st, 2021.