2016-03-21 12:47:54 -04:00
|
|
|
// Copyright 2016 The Gogs Authors. All rights reserved.
|
|
|
|
|
// Use of this source code is governed by a MIT-style
|
|
|
|
|
// license that can be found in the LICENSE file.
|
|
|
|
|
|
|
|
|
|
package admin
|
|
|
|
|
|
|
|
|
|
import (
|
2019-08-10 13:40:48 -07:00
|
|
|
"net/http"
|
|
|
|
|
|
2018-05-27 08:53:48 +08:00
|
|
|
api "github.com/gogs/go-gogs-client"
|
2016-03-21 12:47:54 -04:00
|
|
|
|
2019-10-24 01:51:46 -07:00
|
|
|
"gogs.io/gogs/internal/context"
|
|
|
|
|
"gogs.io/gogs/internal/db"
|
2020-03-16 01:22:27 +08:00
|
|
|
"gogs.io/gogs/internal/route/api/v1/convert"
|
|
|
|
|
"gogs.io/gogs/internal/route/api/v1/user"
|
2016-03-21 12:47:54 -04:00
|
|
|
)
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func CreateTeam(c *context.APIContext, form api.CreateTeamOption) {
|
2019-10-24 01:51:46 -07:00
|
|
|
team := &db.Team{
|
2017-06-03 07:26:09 -04:00
|
|
|
OrgID: c.Org.Organization.ID,
|
2016-03-21 12:47:54 -04:00
|
|
|
Name: form.Name,
|
|
|
|
|
Description: form.Description,
|
2019-10-24 01:51:46 -07:00
|
|
|
Authorize: db.ParseAccessMode(form.Permission),
|
2016-03-21 12:47:54 -04:00
|
|
|
}
|
2019-10-24 01:51:46 -07:00
|
|
|
if err := db.NewTeam(team); err != nil {
|
|
|
|
|
if db.IsErrTeamAlreadyExist(err) {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.ErrorStatus(http.StatusUnprocessableEntity, err)
|
2016-03-21 12:47:54 -04:00
|
|
|
} else {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "new team")
|
2016-03-21 12:47:54 -04:00
|
|
|
}
|
|
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2020-03-16 01:22:27 +08:00
|
|
|
c.JSON(http.StatusCreated, convert.ToTeam(team))
|
2016-03-21 12:47:54 -04:00
|
|
|
}
|
2016-03-25 18:04:02 -04:00
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func AddTeamMember(c *context.APIContext) {
|
2020-03-16 01:22:27 +08:00
|
|
|
u := user.GetUserByParams(c)
|
2017-06-03 07:26:09 -04:00
|
|
|
if c.Written() {
|
2016-03-25 18:04:02 -04:00
|
|
|
return
|
|
|
|
|
}
|
2017-06-03 07:26:09 -04:00
|
|
|
if err := c.Org.Team.AddMember(u.ID); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "add member")
|
2016-03-25 18:04:02 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NoContent()
|
2016-03-25 18:04:02 -04:00
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
func RemoveTeamMember(c *context.APIContext) {
|
2020-03-16 01:22:27 +08:00
|
|
|
u := user.GetUserByParams(c)
|
2017-06-03 07:26:09 -04:00
|
|
|
if c.Written() {
|
2016-03-25 18:04:02 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2017-06-03 07:26:09 -04:00
|
|
|
if err := c.Org.Team.RemoveMember(u.ID); err != nil {
|
2020-03-16 01:22:27 +08:00
|
|
|
c.Error(err, "remove member")
|
2016-03-25 18:04:02 -04:00
|
|
|
return
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-10 13:40:48 -07:00
|
|
|
c.NoContent()
|
2016-03-25 18:04:02 -04:00
|
|
|
}
|