]> source.dussan.org Git - gitea.git/commitdiff
Fix create team, update team missing units (#5188)
authorLunny Xiao <xiaolunwen@gmail.com>
Sat, 10 Nov 2018 19:45:32 +0000 (03:45 +0800)
committertechknowlogick <hello@techknowlogick.com>
Sat, 10 Nov 2018 19:45:32 +0000 (14:45 -0500)
Gopkg.lock
models/org_team.go
models/unit.go
routers/api/v1/convert/convert.go
routers/api/v1/org/team.go
templates/swagger/v1_json.tmpl
vendor/code.gitea.io/sdk/gitea/org_team.go

index 2da402ba741a3cb5b67dc2fc3c0c4c0796dfb26b..1a2b4b5f5bc605d47999a08b4c8faddb320e58c2 100644 (file)
 
 [[projects]]
   branch = "master"
-  digest = "1:bf4f822f636b99ac7d4f8fa5210a7439bacaf8d1f15d5783956bdd5dd2069bdc"
+  digest = "1:b194da40b41ae99546dfeec5a85f1fec2a6c51350d438e511ef90f4293c6dcd7"
   name = "code.gitea.io/sdk"
   packages = ["gitea"]
   pruneopts = "NUT"
-  revision = "11c860c8e49a23be26e6d6c6a039a46ad2ae1d27"
+  revision = "4f96d9ac89886e78c50de8c835ebe87461578a5e"
 
 [[projects]]
   digest = "1:3fcef06a1a6561955c94af6c7757a6fa37605eb653f0d06ab960e5bb80092195"
index ad11431a4954ed0526840b950ab340059186437d..53c1ec34d8bdb68fb0ff31eb5307b5ccdafb4cd7 100644 (file)
@@ -40,6 +40,14 @@ func (t *Team) getUnits(e Engine) (err error) {
        return err
 }
 
+// GetUnitNames returns the team units names
+func (t *Team) GetUnitNames() (res []string) {
+       for _, u := range t.Units {
+               res = append(res, Units[u.Type].NameKey)
+       }
+       return
+}
+
 // HasWriteAccess returns true if team has at least write level access mode.
 func (t *Team) HasWriteAccess() bool {
        return t.Authorize >= AccessModeWrite
@@ -367,6 +375,24 @@ func UpdateTeam(t *Team, authChanged bool) (err error) {
                return fmt.Errorf("update: %v", err)
        }
 
+       // update units for team
+       if len(t.Units) > 0 {
+               for _, unit := range t.Units {
+                       unit.TeamID = t.ID
+               }
+               // Delete team-unit.
+               if _, err := sess.
+                       Where("team_id=?", t.ID).
+                       Delete(new(TeamUnit)); err != nil {
+                       return err
+               }
+
+               if _, err = sess.Insert(&t.Units); err != nil {
+                       sess.Rollback()
+                       return err
+               }
+       }
+
        // Update access for team members if needed.
        if authChanged {
                if err = t.getRepositories(sess); err != nil {
index 1d263595280e23eefefeb58713ce5b9186e8ef81..9619232cf4c1d00442146d7616216638ff00aac6 100644 (file)
@@ -4,6 +4,10 @@
 
 package models
 
+import (
+       "strings"
+)
+
 // UnitType is Unit's Type
 type UnitType int
 
@@ -137,3 +141,16 @@ var (
                UnitTypeExternalWiki:    UnitExternalWiki,
        }
 )
+
+// FindUnitTypes give the unit key name and return unit
+func FindUnitTypes(nameKeys ...string) (res []UnitType) {
+       for _, key := range nameKeys {
+               for t, u := range Units {
+                       if strings.EqualFold(key, u.NameKey) {
+                               res = append(res, t)
+                               break
+                       }
+               }
+       }
+       return
+}
index 1bfeae34bfe39c268001c9b6562a9baaba2456b6..35416dea5181b62a46f9d0e86c318076363b9c74 100644 (file)
@@ -198,5 +198,6 @@ func ToTeam(team *models.Team) *api.Team {
                Name:        team.Name,
                Description: team.Description,
                Permission:  team.Authorize.String(),
+               Units:       team.GetUnitNames(),
        }
 }
index 2e319a18310cea5ed3dfd41de60d8a6d2882b387..8b67eda42fdf5d813eb6096a3c5a3f260b9e3424 100644 (file)
@@ -90,6 +90,20 @@ func CreateTeam(ctx *context.APIContext, form api.CreateTeamOption) {
                Description: form.Description,
                Authorize:   models.ParseAccessMode(form.Permission),
        }
+
+       unitTypes := models.FindUnitTypes(form.Units...)
+
+       if team.Authorize < models.AccessModeOwner {
+               var units = make([]*models.TeamUnit, 0, len(form.Units))
+               for _, tp := range unitTypes {
+                       units = append(units, &models.TeamUnit{
+                               OrgID: ctx.Org.Organization.ID,
+                               Type:  tp,
+                       })
+               }
+               team.Units = units
+       }
+
        if err := models.NewTeam(team); err != nil {
                if models.IsErrTeamAlreadyExist(err) {
                        ctx.Error(422, "", err)
@@ -128,6 +142,19 @@ func EditTeam(ctx *context.APIContext, form api.EditTeamOption) {
        team.Name = form.Name
        team.Description = form.Description
        team.Authorize = models.ParseAccessMode(form.Permission)
+       unitTypes := models.FindUnitTypes(form.Units...)
+
+       if team.Authorize < models.AccessModeOwner {
+               var units = make([]*models.TeamUnit, 0, len(form.Units))
+               for _, tp := range unitTypes {
+                       units = append(units, &models.TeamUnit{
+                               OrgID: ctx.Org.Organization.ID,
+                               Type:  tp,
+                       })
+               }
+               team.Units = units
+       }
+
        if err := models.UpdateTeam(team, true); err != nil {
                ctx.Error(500, "EditTeam", err)
                return
index 56a169c295d52a28e76e59325beae15680d9bc28..5c8c666041f5701fbe1bcf8297afc257c46ebd14 100644 (file)
             "admin"
           ],
           "x-go-name": "Permission"
+        },
+        "units": {
+          "type": "array",
+          "enum": [
+            "repo.code",
+            "repo.issues",
+            "repo.ext_issues",
+            "repo.wiki",
+            "repo.pulls",
+            "repo.releases",
+            "repo.ext_wiki"
+          ],
+          "items": {
+            "type": "string"
+          },
+          "x-go-name": "Units"
         }
       },
       "x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
             "admin"
           ],
           "x-go-name": "Permission"
+        },
+        "units": {
+          "type": "array",
+          "enum": [
+            "repo.code",
+            "repo.issues",
+            "repo.ext_issues",
+            "repo.wiki",
+            "repo.pulls",
+            "repo.releases",
+            "repo.ext_wiki"
+          ],
+          "items": {
+            "type": "string"
+          },
+          "x-go-name": "Units"
         }
       },
       "x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
             "owner"
           ],
           "x-go-name": "Permission"
+        },
+        "units": {
+          "type": "array",
+          "enum": [
+            "repo.code",
+            "repo.issues",
+            "repo.ext_issues",
+            "repo.wiki",
+            "repo.pulls",
+            "repo.releases",
+            "repo.ext_wiki"
+          ],
+          "items": {
+            "type": "string"
+          },
+          "x-go-name": "Units"
         }
       },
       "x-go-package": "code.gitea.io/gitea/vendor/code.gitea.io/sdk/gitea"
index 2fc6796d43ff94d4f75affaf2aa10884062f0a82..f3e98b932e91c55712fd9daf6ffa1815a2f74fe2 100644 (file)
@@ -1,4 +1,5 @@
 // Copyright 2016 The Gogs Authors. All rights reserved.
+// Copyright 2018 The Gitea Authors. All rights reserved.
 // Use of this source code is governed by a MIT-style
 // license that can be found in the LICENSE file.
 
@@ -11,6 +12,8 @@ type Team struct {
        Description string `json:"description"`
        // enum: none,read,write,admin,owner
        Permission string `json:"permission"`
+       // enum: repo.code,repo.issues,repo.ext_issues,repo.wiki,repo.pulls,repo.releases,repo.ext_wiki
+       Units []string `json:"units"`
 }
 
 // CreateTeamOption options for creating a team
@@ -20,6 +23,8 @@ type CreateTeamOption struct {
        Description string `json:"description" binding:"MaxSize(255)"`
        // enum: read,write,admin
        Permission string `json:"permission"`
+       // enum: repo.code,repo.issues,repo.ext_issues,repo.wiki,repo.pulls,repo.releases,repo.ext_wiki
+       Units []string `json:"units"`
 }
 
 // EditTeamOption options for editing a team
@@ -29,4 +34,6 @@ type EditTeamOption struct {
        Description string `json:"description" binding:"MaxSize(255)"`
        // enum: read,write,admin
        Permission string `json:"permission"`
+       // enum: repo.code,repo.issues,repo.ext_issues,repo.wiki,repo.pulls,repo.releases,repo.ext_wiki
+       Units []string `json:"units"`
 }