aboutsummaryrefslogtreecommitdiff
path: root/routes/api/v1/admin
diff options
context:
space:
mode:
authorUnknwon <u@gogs.io>2017-06-11 00:34:14 -0400
committerUnknwon <u@gogs.io>2017-06-11 00:34:14 -0400
commit4400d2fdd933204044aeb18ce7d8613c53aa87c0 (patch)
tree841e91d5294c49b7335170fbc4b9ff79e882f91a /routes/api/v1/admin
parent6197a7639a88f7fb0fee8927e1d501504ae770ff (diff)
Refactoring: rename package routers -> routes
Diffstat (limited to 'routes/api/v1/admin')
-rw-r--r--routes/api/v1/admin/org.go44
-rw-r--r--routes/api/v1/admin/org_repo.go50
-rw-r--r--routes/api/v1/admin/org_team.go60
-rw-r--r--routes/api/v1/admin/repo.go23
-rw-r--r--routes/api/v1/admin/user.go160
5 files changed, 337 insertions, 0 deletions
diff --git a/routes/api/v1/admin/org.go b/routes/api/v1/admin/org.go
new file mode 100644
index 00000000..0f84ed2e
--- /dev/null
+++ b/routes/api/v1/admin/org.go
@@ -0,0 +1,44 @@
+// Copyright 2015 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 (
+ api "github.com/gogits/go-gogs-client"
+
+ "github.com/gogits/gogs/models"
+ "github.com/gogits/gogs/pkg/context"
+ "github.com/gogits/gogs/routes/api/v1/convert"
+ "github.com/gogits/gogs/routes/api/v1/user"
+)
+
+// https://github.com/gogits/go-gogs-client/wiki/Administration-Organizations#create-a-new-organization
+func CreateOrg(c *context.APIContext, form api.CreateOrgOption) {
+ u := user.GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+
+ org := &models.User{
+ Name: form.UserName,
+ FullName: form.FullName,
+ Description: form.Description,
+ Website: form.Website,
+ Location: form.Location,
+ IsActive: true,
+ Type: models.USER_TYPE_ORGANIZATION,
+ }
+ if err := models.CreateOrganization(org, u); err != nil {
+ if models.IsErrUserAlreadyExist(err) ||
+ models.IsErrNameReserved(err) ||
+ models.IsErrNamePatternNotAllowed(err) {
+ c.Error(422, "", err)
+ } else {
+ c.Error(500, "CreateOrganization", err)
+ }
+ return
+ }
+
+ c.JSON(201, convert.ToOrganization(org))
+}
diff --git a/routes/api/v1/admin/org_repo.go b/routes/api/v1/admin/org_repo.go
new file mode 100644
index 00000000..7abad1a8
--- /dev/null
+++ b/routes/api/v1/admin/org_repo.go
@@ -0,0 +1,50 @@
+// 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 (
+ "github.com/gogits/gogs/models"
+ "github.com/gogits/gogs/models/errors"
+ "github.com/gogits/gogs/pkg/context"
+)
+
+func GetRepositoryByParams(c *context.APIContext) *models.Repository {
+ repo, err := models.GetRepositoryByName(c.Org.Team.OrgID, c.Params(":reponame"))
+ if err != nil {
+ if errors.IsRepoNotExist(err) {
+ c.Status(404)
+ } else {
+ c.Error(500, "GetRepositoryByName", err)
+ }
+ return nil
+ }
+ return repo
+}
+
+func AddTeamRepository(c *context.APIContext) {
+ repo := GetRepositoryByParams(c)
+ if c.Written() {
+ return
+ }
+ if err := c.Org.Team.AddRepository(repo); err != nil {
+ c.Error(500, "AddRepository", err)
+ return
+ }
+
+ c.Status(204)
+}
+
+func RemoveTeamRepository(c *context.APIContext) {
+ repo := GetRepositoryByParams(c)
+ if c.Written() {
+ return
+ }
+ if err := c.Org.Team.RemoveRepository(repo.ID); err != nil {
+ c.Error(500, "RemoveRepository", err)
+ return
+ }
+
+ c.Status(204)
+}
diff --git a/routes/api/v1/admin/org_team.go b/routes/api/v1/admin/org_team.go
new file mode 100644
index 00000000..ae748504
--- /dev/null
+++ b/routes/api/v1/admin/org_team.go
@@ -0,0 +1,60 @@
+// 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 (
+ api "github.com/gogits/go-gogs-client"
+
+ "github.com/gogits/gogs/models"
+ "github.com/gogits/gogs/pkg/context"
+ "github.com/gogits/gogs/routes/api/v1/convert"
+ "github.com/gogits/gogs/routes/api/v1/user"
+)
+
+func CreateTeam(c *context.APIContext, form api.CreateTeamOption) {
+ team := &models.Team{
+ OrgID: c.Org.Organization.ID,
+ Name: form.Name,
+ Description: form.Description,
+ Authorize: models.ParseAccessMode(form.Permission),
+ }
+ if err := models.NewTeam(team); err != nil {
+ if models.IsErrTeamAlreadyExist(err) {
+ c.Error(422, "", err)
+ } else {
+ c.Error(500, "NewTeam", err)
+ }
+ return
+ }
+
+ c.JSON(201, convert.ToTeam(team))
+}
+
+func AddTeamMember(c *context.APIContext) {
+ u := user.GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+ if err := c.Org.Team.AddMember(u.ID); err != nil {
+ c.Error(500, "AddMember", err)
+ return
+ }
+
+ c.Status(204)
+}
+
+func RemoveTeamMember(c *context.APIContext) {
+ u := user.GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+
+ if err := c.Org.Team.RemoveMember(u.ID); err != nil {
+ c.Error(500, "RemoveMember", err)
+ return
+ }
+
+ c.Status(204)
+}
diff --git a/routes/api/v1/admin/repo.go b/routes/api/v1/admin/repo.go
new file mode 100644
index 00000000..920bac8d
--- /dev/null
+++ b/routes/api/v1/admin/repo.go
@@ -0,0 +1,23 @@
+// Copyright 2015 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 (
+ api "github.com/gogits/go-gogs-client"
+
+ "github.com/gogits/gogs/pkg/context"
+ "github.com/gogits/gogs/routes/api/v1/repo"
+ "github.com/gogits/gogs/routes/api/v1/user"
+)
+
+// https://github.com/gogits/go-gogs-client/wiki/Administration-Repositories#create-a-new-repository
+func CreateRepo(c *context.APIContext, form api.CreateRepoOption) {
+ owner := user.GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+
+ repo.CreateUserRepo(c, owner, form)
+}
diff --git a/routes/api/v1/admin/user.go b/routes/api/v1/admin/user.go
new file mode 100644
index 00000000..623911fd
--- /dev/null
+++ b/routes/api/v1/admin/user.go
@@ -0,0 +1,160 @@
+// Copyright 2015 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 (
+ log "gopkg.in/clog.v1"
+
+ api "github.com/gogits/go-gogs-client"
+
+ "github.com/gogits/gogs/models"
+ "github.com/gogits/gogs/pkg/context"
+ "github.com/gogits/gogs/pkg/mailer"
+ "github.com/gogits/gogs/pkg/setting"
+ "github.com/gogits/gogs/routes/api/v1/user"
+)
+
+func parseLoginSource(c *context.APIContext, u *models.User, sourceID int64, loginName string) {
+ if sourceID == 0 {
+ return
+ }
+
+ source, err := models.GetLoginSourceByID(sourceID)
+ if err != nil {
+ if models.IsErrLoginSourceNotExist(err) {
+ c.Error(422, "", err)
+ } else {
+ c.Error(500, "GetLoginSourceByID", err)
+ }
+ return
+ }
+
+ u.LoginType = source.Type
+ u.LoginSource = source.ID
+ u.LoginName = loginName
+}
+
+// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-new-user
+func CreateUser(c *context.APIContext, form api.CreateUserOption) {
+ u := &models.User{
+ Name: form.Username,
+ FullName: form.FullName,
+ Email: form.Email,
+ Passwd: form.Password,
+ IsActive: true,
+ LoginType: models.LOGIN_PLAIN,
+ }
+
+ parseLoginSource(c, u, form.SourceID, form.LoginName)
+ if c.Written() {
+ return
+ }
+
+ if err := models.CreateUser(u); err != nil {
+ if models.IsErrUserAlreadyExist(err) ||
+ models.IsErrEmailAlreadyUsed(err) ||
+ models.IsErrNameReserved(err) ||
+ models.IsErrNamePatternNotAllowed(err) {
+ c.Error(422, "", err)
+ } else {
+ c.Error(500, "CreateUser", err)
+ }
+ return
+ }
+ log.Trace("Account created by admin (%s): %s", c.User.Name, u.Name)
+
+ // Send email notification.
+ if form.SendNotify && setting.MailService != nil {
+ mailer.SendRegisterNotifyMail(c.Context.Context, models.NewMailerUser(u))
+ }
+
+ c.JSON(201, u.APIFormat())
+}
+
+// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#edit-an-existing-user
+func EditUser(c *context.APIContext, form api.EditUserOption) {
+ u := user.GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+
+ parseLoginSource(c, u, form.SourceID, form.LoginName)
+ if c.Written() {
+ return
+ }
+
+ if len(form.Password) > 0 {
+ u.Passwd = form.Password
+ var err error
+ if u.Salt, err = models.GetUserSalt(); err != nil {
+ c.Error(500, "UpdateUser", err)
+ return
+ }
+ u.EncodePasswd()
+ }
+
+ u.LoginName = form.LoginName
+ u.FullName = form.FullName
+ u.Email = form.Email
+ u.Website = form.Website
+ u.Location = form.Location
+ if form.Active != nil {
+ u.IsActive = *form.Active
+ }
+ if form.Admin != nil {
+ u.IsAdmin = *form.Admin
+ }
+ if form.AllowGitHook != nil {
+ u.AllowGitHook = *form.AllowGitHook
+ }
+ if form.AllowImportLocal != nil {
+ u.AllowImportLocal = *form.AllowImportLocal
+ }
+ if form.MaxRepoCreation != nil {
+ u.MaxRepoCreation = *form.MaxRepoCreation
+ }
+
+ if err := models.UpdateUser(u); err != nil {
+ if models.IsErrEmailAlreadyUsed(err) {
+ c.Error(422, "", err)
+ } else {
+ c.Error(500, "UpdateUser", err)
+ }
+ return
+ }
+ log.Trace("Account profile updated by admin (%s): %s", c.User.Name, u.Name)
+
+ c.JSON(200, u.APIFormat())
+}
+
+// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#delete-a-user
+func DeleteUser(c *context.APIContext) {
+ u := user.GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+
+ if err := models.DeleteUser(u); err != nil {
+ if models.IsErrUserOwnRepos(err) ||
+ models.IsErrUserHasOrgs(err) {
+ c.Error(422, "", err)
+ } else {
+ c.Error(500, "DeleteUser", err)
+ }
+ return
+ }
+ log.Trace("Account deleted by admin(%s): %s", c.User.Name, u.Name)
+
+ c.Status(204)
+}
+
+// https://github.com/gogits/go-gogs-client/wiki/Administration-Users#create-a-public-key-for-user
+func CreatePublicKey(c *context.APIContext, form api.CreateKeyOption) {
+ u := user.GetUserByParams(c)
+ if c.Written() {
+ return
+ }
+ user.CreateUserPublicKey(c, form, u.ID)
+}