From 4a9ef647621861a29bb64bad6a86782e47eb62fc Mon Sep 17 00:00:00 2001 From: yystopf Date: Wed, 30 Nov 2022 12:41:56 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=EF=BC=9A=E7=BB=84=E7=BB=87?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3=E8=BF=94=E5=9B=9Eowner=E5=9B=A2=E9=98=9F?= =?UTF-8?q?=E4=BF=A1=E6=81=AF?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- modules/convert/convert.go | 21 ++++++++++++ modules/structs/org.go | 15 +++++++++ routers/hat/hat.go | 19 +++++++++++ routers/hat/org/org.go | 66 ++++++++++++++++++++++++++++++++++++++ 4 files changed, 121 insertions(+) create mode 100644 modules/structs/org.go create mode 100644 routers/hat/org/org.go diff --git a/modules/convert/convert.go b/modules/convert/convert.go index 029d323..1379a47 100644 --- a/modules/convert/convert.go +++ b/modules/convert/convert.go @@ -3,6 +3,7 @@ package convert import ( "strings" + "code.gitea.io/gitea/models/organization" "code.gitea.io/gitea/models/repo" gitea_convert "code.gitea.io/gitea/modules/convert" "code.gitea.io/gitea/modules/git" @@ -39,3 +40,23 @@ func ToTagCommit(repo *repo.Repository, gitRepo *git.Repository, t *git.Tag) (re Message: commit.CommitMessage, }, nil } + +func ToOrganization(org *organization.Organization, team *organization.Team) (*api.Organization, error) { + apiTeam, err := gitea_convert.ToTeam(team) + if err != nil { + return &api.Organization{}, err + } + return &api.Organization{ + ID: org.ID, + Name: org.Name, + AvatarURL: org.AvatarLink(), + UserName: org.Name, + FullName: org.FullName, + Description: org.Description, + Website: org.Website, + Location: org.Location, + Visibility: org.Visibility.String(), + RepoAdminChangeTeamAccess: org.RepoAdminChangeTeamAccess, + OwnerTeam: apiTeam, + }, nil +} diff --git a/modules/structs/org.go b/modules/structs/org.go new file mode 100644 index 0000000..b78916b --- /dev/null +++ b/modules/structs/org.go @@ -0,0 +1,15 @@ +package structs + +type Organization struct { + ID int64 `json:"id"` + Name string `json:"name"` + UserName string `json:"username"` + FullName string `json:"full_name"` + AvatarURL string `json:"avatar_url"` + Description string `json:"description"` + Website string `json:"website"` + Location string `json:"location"` + Visibility string `json:"visibility"` + RepoAdminChangeTeamAccess bool `json:"repo_admin_change_team_access"` + OwnerTeam interface{} `json:"owner_team"` //团队关系; +} diff --git a/routers/hat/hat.go b/routers/hat/hat.go index e3fd9bd..e24c7f9 100644 --- a/routers/hat/hat.go +++ b/routers/hat/hat.go @@ -6,6 +6,7 @@ import ( "reflect" "strings" + gitea_api "code.gitea.io/gitea/modules/structs" hat_api "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/structs" "gitea.com/go-chi/binding" @@ -20,6 +21,7 @@ import ( "code.gitea.io/gitea/modules/web" "code.gitea.io/gitea/routers/api/v1/misc" "code.gitea.io/gitea/services/auth" + "code.gitlink.org.cn/Gitlink/gitea_hat.git/routers/hat/org" "code.gitlink.org.cn/Gitlink/gitea_hat.git/routers/hat/repo" "github.com/go-chi/cors" ) @@ -86,6 +88,7 @@ func Routers() *web.Route { }, mustAllowPulls, reqRepoReader(unit_model.TypeCode), context.ReferencesGitRepo()) }, repoAssignment()) }) + m.Post("/orgs", reqToken(), bind(gitea_api.CreateOrgOption{}), org.Create) }) return m @@ -231,3 +234,19 @@ func mustAllowPulls(ctx *context.APIContext) { return } } + +func reqToken() func(ctx *context.APIContext) { + return func(ctx *context.APIContext) { + if true == ctx.Data["IsApiToken"] { + return + } + if ctx.Context.IsBasicAuth { + ctx.CheckForOTP() + return + } + if ctx.IsSigned { + return + } + ctx.Error(http.StatusUnauthorized, "reqToken", "token is required") + } +} diff --git a/routers/hat/org/org.go b/routers/hat/org/org.go new file mode 100644 index 0000000..4d7c36a --- /dev/null +++ b/routers/hat/org/org.go @@ -0,0 +1,66 @@ +package org + +import ( + "net/http" + + "code.gitea.io/gitea/models/db" + org_model "code.gitea.io/gitea/models/organization" + + user_model "code.gitea.io/gitea/models/user" + "code.gitea.io/gitea/modules/context" + api "code.gitea.io/gitea/modules/structs" + "code.gitea.io/gitea/modules/web" + hat_convert "code.gitlink.org.cn/Gitlink/gitea_hat.git/modules/convert" +) + +func Create(ctx *context.APIContext) { + form := web.GetForm(ctx).(*api.CreateOrgOption) + if !ctx.Doer.CanCreateOrganization() { + ctx.Error(http.StatusForbidden, "create organization not allowed", nil) + return + } + visibility := api.VisibleTypePublic + if form.Visibility != "" { + visibility = api.VisibilityModes[form.Visibility] + } + + org := &org_model.Organization{ + Name: form.UserName, + FullName: form.FullName, + Description: form.Description, + Website: form.Website, + Location: form.Location, + IsActive: true, + Type: user_model.UserTypeOrganization, + Visibility: visibility, + RepoAdminChangeTeamAccess: form.RepoAdminChangeTeamAccess, + } + + if err := org_model.CreateOrganization(org, ctx.Doer); err != nil { + if user_model.IsErrUserAlreadyExist(err) || + db.IsErrNameReserved(err) || + db.IsErrNameCharsNotAllowed(err) || + db.IsErrNamePatternNotAllowed(err) { + ctx.Error(http.StatusUnprocessableEntity, "", err) + } else { + ctx.Error(http.StatusInternalServerError, "CreateOrganization", err) + } + return + } + + team, err := org_model.GetTeam(ctx, org.ID, "") + if err != nil { + if user_model.IsErrUserNotExist(err) { + ctx.NotFound() + } else { + ctx.Error(http.StatusInternalServerError, "GetTeam", err) + } + return + } + + if apiOrg, err := hat_convert.ToOrganization(org, team); err != nil { + ctx.Error(http.StatusInternalServerError, "ToOrganization", err) + } else { + ctx.JSON(http.StatusCreated, apiOrg) + } +}