// RemoveUser removes the user from the database and from gandalf server // // In order to successfuly remove a user, it's need that he/she is not the only one in a team, // otherwise the function will return an error // TODO: improve the team update, if possible func RemoveUser(w http.ResponseWriter, r *http.Request, u *User) error { //_, err := db.Session.Teams().UpdateAll(bson.M{"users": u.Email}, bson.M{"$pull": bson.M{"users": u.Email}}) gUrl := repository.GitServerUri() c := gandalf.Client{Endpoint: gUrl} alwdApps, err := allowedApps(u.Email) if err != nil { return err } if err := c.RevokeAccess(alwdApps, []string{u.Email}); err != nil { return err } teams, err := u.Teams() if err != nil { return err } for _, team := range teams { if len(team.Users) < 2 { msg := fmt.Sprintf(`This user is the last member of the team "%s", so it cannot be removed. Please remove the team, them remove the user.`, team.Name) return &errors.Http{Code: http.StatusForbidden, Message: msg} } err = team.removeUser(u) if err != nil { return err } // this can be done without the loop err = db.Session.Teams().Update(bson.M{"_id": team.Name}, team) if err != nil { return err } } if err := c.RemoveUser(u.Email); err != nil { return &errors.Http{Code: http.StatusInternalServerError, Message: "Could not communicate with git server. Aborting..."} } return db.Session.Users().Remove(bson.M{"email": u.Email}) }