func removeUserFromTeam(email, teamName string, u *auth.User) error { conn, err := db.Conn() if err != nil { return err } defer conn.Close() team := new(auth.Team) err = conn.Teams().FindId(teamName).One(team) if err != nil { return &errors.Http{Code: http.StatusNotFound, Message: "Team not found"} } if !team.ContainsUser(u) { msg := fmt.Sprintf("You are not authorized to remove a member from the team %s", team.Name) return &errors.Http{Code: http.StatusUnauthorized, Message: msg} } if len(team.Users) == 1 { msg := "You can not remove this user from this team, because it is the last user within the team, and a team can not be orphaned" return &errors.Http{Code: http.StatusForbidden, Message: msg} } user := auth.User{Email: email} err = user.Get() if err != nil { return &errors.Http{Code: http.StatusNotFound, Message: err.Error()} } err = removeUserFromTeamInGandalf(&user, team.Name) if err != nil { return nil } return removeUserFromTeamInDatabase(&user, team) }
func removeUserFromTeamInGandalf(u *auth.User, team *auth.Team) error { gURL := repository.ServerURL() teamApps, err := team.AllowedApps() if err != nil { return err } userApps, err := u.AllowedApps() if err != nil { return err } appsToRemove := make([]string, 0, len(teamApps)) for _, teamApp := range teamApps { found := false for _, userApp := range userApps { if userApp == teamApp { found = true break } } if !found { appsToRemove = append(appsToRemove, teamApp) } } client := gandalf.Client{Endpoint: gURL} if err := client.RevokeAccess(appsToRemove, []string{u.Email}); err != nil { return fmt.Errorf("Failed to revoke access from git repositories: %s", err) } return nil }
func removeUserFromTeamInDatabase(u *auth.User, team *auth.Team) error { conn, err := db.Conn() if err != nil { return err } defer conn.Close() if err = team.RemoveUser(u); err != nil { return &errors.HTTP{Code: http.StatusNotFound, Message: err.Error()} } return conn.Teams().UpdateId(team.Name, team) }
func addUserToTeamInDatabase(user *auth.User, team *auth.Team) error { if err := team.AddUser(user); err != nil { return &errors.HTTP{Code: http.StatusConflict, Message: err.Error()} } conn, err := db.Conn() if err != nil { return err } defer conn.Close() return conn.Teams().UpdateId(team.Name, team) }
func addUserToTeamInGandalf(user *auth.User, t *auth.Team) error { gURL := repository.ServerURL() alwdApps, err := t.AllowedApps() if err != nil { return fmt.Errorf("Failed to obtain allowed apps to grant: %s", err) } if err := (&gandalf.Client{Endpoint: gURL}).GrantAccess(alwdApps, []string{user.Email}); err != nil { return fmt.Errorf("Failed to grant access to git repositories: %s", err) } return nil }
func removeUserFromTeam(w http.ResponseWriter, r *http.Request, t *auth.Token) error { email := r.URL.Query().Get(":user") teamName := r.URL.Query().Get(":team") u, err := t.User() if err != nil { return err } rec.Log(u.Email, "remove-user-from-team", "team="+teamName, "user="******"Team not found"} } if !team.ContainsUser(u) { msg := fmt.Sprintf("You are not authorized to remove a member from the team %s", team.Name) return &errors.Http{Code: http.StatusUnauthorized, Message: msg} } if len(team.Users) == 1 { msg := "You can not remove this user from this team, because it is the last user within the team, and a team can not be orphaned" return &errors.Http{Code: http.StatusForbidden, Message: msg} } user, err := auth.GetUserByEmail(email) if err != nil { return &errors.Http{Code: http.StatusNotFound, Message: err.Error()} } err = removeUserFromTeamInGandalf(user, team.Name) if err != nil { return nil } return removeUserFromTeamInDatabase(user, team) }