github.com/marinho/drone@v0.2.1-0.20140504195434-d3ba962e89a7/pkg/handler/teams.go (about)

     1  package handler
     2  
     3  import (
     4  	"fmt"
     5  	"net/http"
     6  
     7  	"github.com/drone/drone/pkg/database"
     8  	. "github.com/drone/drone/pkg/model"
     9  )
    10  
    11  // Display a specific Team.
    12  func TeamShow(w http.ResponseWriter, r *http.Request, u *User) error {
    13  	teamParam := r.FormValue(":team")
    14  	team, err := database.GetTeamSlug(teamParam)
    15  	if err != nil {
    16  		return err
    17  	}
    18  	if member, _ := database.IsMember(u.ID, team.ID); !member {
    19  		return fmt.Errorf("Forbidden")
    20  	}
    21  	// list of repositories owned by Team
    22  	repos, err := database.ListReposTeam(team.ID)
    23  	if err != nil {
    24  		return err
    25  	}
    26  	// list all user teams
    27  	teams, err := database.ListTeams(u.ID)
    28  	if err != nil {
    29  		return err
    30  	}
    31  	// list of recent commits
    32  	commits, err := database.ListCommitsTeam(team.ID)
    33  	if err != nil {
    34  		return err
    35  	}
    36  	data := struct {
    37  		User    *User
    38  		Team    *Team
    39  		Teams   []*Team
    40  		Repos   []*Repo
    41  		Commits []*RepoCommit
    42  	}{u, team, teams, repos, commits}
    43  	return RenderTemplate(w, "team_dashboard.html", &data)
    44  }
    45  
    46  // Return an HTML form for editing a Team.
    47  func TeamEdit(w http.ResponseWriter, r *http.Request, u *User) error {
    48  	teamParam := r.FormValue(":team")
    49  	team, err := database.GetTeamSlug(teamParam)
    50  	if err != nil {
    51  		return err
    52  	}
    53  	if member, _ := database.IsMemberAdmin(u.ID, team.ID); !member {
    54  		return fmt.Errorf("Forbidden")
    55  	}
    56  	data := struct {
    57  		User *User
    58  		Team *Team
    59  	}{u, team}
    60  	return RenderTemplate(w, "team_profile.html", &data)
    61  }
    62  
    63  // Return an HTML form for creating a Team.
    64  func TeamAdd(w http.ResponseWriter, r *http.Request, u *User) error {
    65  	return RenderTemplate(w, "user_teams_add.html", struct{ User *User }{u})
    66  }
    67  
    68  // Create a new Team.
    69  func TeamCreate(w http.ResponseWriter, r *http.Request, u *User) error {
    70  	// set the name and email from the form data
    71  	team := Team{}
    72  	team.SetName(r.FormValue("name"))
    73  	team.SetEmail(r.FormValue("email"))
    74  
    75  	if err := team.Validate(); err != nil {
    76  		return RenderError(w, err, http.StatusBadRequest)
    77  	}
    78  	if err := database.SaveTeam(&team); err != nil {
    79  		return RenderError(w, err, http.StatusBadRequest)
    80  	}
    81  
    82  	// add default member to the team (me)
    83  	if err := database.SaveMember(u.ID, team.ID, RoleOwner); err != nil {
    84  		return RenderError(w, err, http.StatusInternalServerError)
    85  	}
    86  
    87  	return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK)
    88  }
    89  
    90  // Update a specific Team.
    91  func TeamUpdate(w http.ResponseWriter, r *http.Request, u *User) error {
    92  	// get team from the database
    93  	teamName := r.FormValue(":team")
    94  	team, err := database.GetTeamSlug(teamName)
    95  	if err != nil {
    96  		return fmt.Errorf("Forbidden")
    97  	}
    98  	if member, _ := database.IsMemberAdmin(u.ID, team.ID); !member {
    99  		return fmt.Errorf("Forbidden")
   100  	}
   101  
   102  	team.Name = r.FormValue("name")
   103  	team.SetEmail(r.FormValue("email"))
   104  
   105  	if err := team.Validate(); err != nil {
   106  		return RenderError(w, err, http.StatusBadRequest)
   107  	}
   108  	if err := database.SaveTeam(team); err != nil {
   109  		return RenderError(w, err, http.StatusBadRequest)
   110  	}
   111  
   112  	return RenderText(w, http.StatusText(http.StatusOK), http.StatusOK)
   113  }
   114  
   115  // Delete Confirmation Page
   116  func TeamDeleteConfirm(w http.ResponseWriter, r *http.Request, u *User) error {
   117  	teamParam := r.FormValue(":team")
   118  	team, err := database.GetTeamSlug(teamParam)
   119  	if err != nil {
   120  		return err
   121  	}
   122  	if member, _ := database.IsMemberAdmin(u.ID, team.ID); !member {
   123  		return fmt.Errorf("Forbidden")
   124  	}
   125  	data := struct {
   126  		User *User
   127  		Team *Team
   128  	}{u, team}
   129  	return RenderTemplate(w, "team_delete.html", &data)
   130  }
   131  
   132  // Delete a specific Team.
   133  func TeamDelete(w http.ResponseWriter, r *http.Request, u *User) error {
   134  	// get the team from the database
   135  	teamParam := r.FormValue(":team")
   136  	team, err := database.GetTeamSlug(teamParam)
   137  	if err != nil {
   138  		return RenderNotFound(w)
   139  	}
   140  	if member, _ := database.IsMemberAdmin(u.ID, team.ID); !member {
   141  		return fmt.Errorf("Forbidden")
   142  	}
   143  	// the user must confirm their password before deleting
   144  	password := r.FormValue("password")
   145  	if err := u.ComparePassword(password); err != nil {
   146  		return RenderError(w, err, http.StatusBadRequest)
   147  	}
   148  
   149  	database.DeleteTeam(team.ID)
   150  	http.Redirect(w, r, "/account/user/teams", http.StatusSeeOther)
   151  	return nil
   152  }