Fixed a bug where it was possible to add a team multiple times to a list/namespace

This commit is contained in:
kolaente 2018-07-24 17:46:32 +02:00 committed by konrad
parent 9638f36788
commit 26c2ad078f
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
5 changed files with 43 additions and 1 deletions

View file

@ -434,3 +434,19 @@ func IsErrInvalidTeamRight(err error) bool {
func (err ErrInvalidTeamRight) Error() string { func (err ErrInvalidTeamRight) Error() string {
return fmt.Sprintf("The right is invalid [Right: %d]", err.Right) return fmt.Sprintf("The right is invalid [Right: %d]", err.Right)
} }
// ErrTeamAlreadyHasAccess represents an error where a team already has access to a list/namespace
type ErrTeamAlreadyHasAccess struct {
TeamID int64
ID int64
}
// IsErrTeamAlreadyHasAccess checks if an error is ErrTeamAlreadyHasAccess.
func IsErrTeamAlreadyHasAccess(err error) bool {
_, ok := err.(ErrTeamAlreadyHasAccess)
return ok
}
func (err ErrTeamAlreadyHasAccess) Error() string {
return fmt.Sprintf("This team already has access. [Team ID: %d, ID: %d]", err.TeamID, err.ID)
}

View file

@ -20,6 +20,17 @@ func (tl *TeamList) Create(doer *User) (err error) {
return return
} }
// Check if the team is already on the list
exists, err := x.Where("team_id = ?", tl.TeamID).
And("list_id = ?", tl.ListID).
Get(&TeamList{})
if err != nil {
return
}
if exists {
return ErrTeamAlreadyHasAccess{tl.TeamID, tl.ListID}
}
// Insert the new team // Insert the new team
_, err = x.Insert(tl) _, err = x.Insert(tl)
return return

View file

@ -2,7 +2,7 @@ package models
// Create implements the create method to assign a user to a team // Create implements the create method to assign a user to a team
func (tm *TeamMember) Create(doer *User) (err error) { func (tm *TeamMember) Create(doer *User) (err error) {
//tm.TeamID = id // TODO: Check if it exists etc
_, err = x.Insert(tm) _, err = x.Insert(tm)
return return
} }

View file

@ -20,6 +20,17 @@ func (tn *TeamNamespace) Create(doer *User) (err error) {
return return
} }
// Check if the team already has access to the namespace
exists, err := x.Where("team_id = ?", tn.TeamID).
And("namespace_id = ?", tn.NamespaceID).
Get(&TeamNamespace{})
if err != nil {
return
}
if exists {
return ErrTeamAlreadyHasAccess{tn.TeamID, tn.NamespaceID}
}
// Insert the new team // Insert the new team
_, err = x.Insert(tn) _, err = x.Insert(tn)
return return

View file

@ -53,6 +53,10 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
return echo.NewHTTPError(http.StatusBadRequest, "The team name cannot be empty.") return echo.NewHTTPError(http.StatusBadRequest, "The team name cannot be empty.")
} }
if models.IsErrTeamAlreadyHasAccess(err) {
return echo.NewHTTPError(http.StatusBadRequest, "This team already has access.")
}
return echo.NewHTTPError(http.StatusInternalServerError) return echo.NewHTTPError(http.StatusInternalServerError)
} }