Fixed a bug where it was possible to add the same user multiple times to a team

This commit is contained in:
kolaente 2018-07-26 10:06:41 +02:00
parent 9c451daf30
commit 80e9021ba6
No known key found for this signature in database
GPG key ID: F40E70337AB24C9B
3 changed files with 26 additions and 0 deletions

View file

@ -450,3 +450,19 @@ func IsErrTeamAlreadyHasAccess(err error) bool {
func (err ErrTeamAlreadyHasAccess) Error() string { func (err ErrTeamAlreadyHasAccess) Error() string {
return fmt.Sprintf("This team already has access. [Team ID: %d, ID: %d]", err.TeamID, err.ID) return fmt.Sprintf("This team already has access. [Team ID: %d, ID: %d]", err.TeamID, err.ID)
} }
// ErrUserIsMemberOfTeam represents an error where a user is already member of a team.
type ErrUserIsMemberOfTeam struct {
TeamID int64
UserID int64
}
// IsErrUserIsMemberOfTeam checks if an error is ErrUserIsMemberOfTeam.
func IsErrUserIsMemberOfTeam(err error) bool {
_, ok := err.(ErrUserIsMemberOfTeam)
return ok
}
func (err ErrUserIsMemberOfTeam) Error() string {
return fmt.Sprintf("This user is already a member of that team. [Team ID: %d, User ID: %d]", err.TeamID, err.UserID)
}

View file

@ -14,6 +14,13 @@ func (tm *TeamMember) Create(doer *User) (err error) {
return return
} }
// Check if that user is already part of the team
exists, err := x.Where("team_id = ? AND user_id = ?", tm.TeamID, tm.UserID).
Get(&TeamMember{})
if exists {
return ErrUserIsMemberOfTeam{tm.TeamID, tm.UserID}
}
// Insert the user // Insert the user
_, err = x.Insert(tm) _, err = x.Insert(tm)
return return

View file

@ -56,6 +56,9 @@ func (c *WebHandler) CreateWeb(ctx echo.Context) error {
if models.IsErrTeamAlreadyHasAccess(err) { if models.IsErrTeamAlreadyHasAccess(err) {
return echo.NewHTTPError(http.StatusBadRequest, "This team already has access.") return echo.NewHTTPError(http.StatusBadRequest, "This team already has access.")
} }
if models.IsErrUserIsMemberOfTeam(err) {
return echo.NewHTTPError(http.StatusBadRequest, "This user is already a member of that team.")
}
return echo.NewHTTPError(http.StatusInternalServerError) return echo.NewHTTPError(http.StatusInternalServerError)
} }