Fixed a bug where it was possible to add a team multiple times to a list/namespace
This commit is contained in:
parent
9638f36788
commit
26c2ad078f
5 changed files with 43 additions and 1 deletions
|
@ -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)
|
||||||
|
}
|
|
@ -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
|
||||||
|
|
|
@ -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
|
||||||
}
|
}
|
||||||
|
|
|
@ -19,6 +19,17 @@ func (tn *TeamNamespace) Create(doer *User) (err error) {
|
||||||
if err != nil {
|
if err != nil {
|
||||||
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)
|
||||||
|
|
|
@ -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)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in a new issue