summaryrefslogtreecommitdiffstats
path: root/models/unit/unit.go
diff options
context:
space:
mode:
Diffstat (limited to 'models/unit/unit.go')
-rw-r--r--models/unit/unit.go75
1 files changed, 61 insertions, 14 deletions
diff --git a/models/unit/unit.go b/models/unit/unit.go
index 0af4640b7a..b05f34b64c 100644
--- a/models/unit/unit.go
+++ b/models/unit/unit.go
@@ -8,6 +8,7 @@ import (
"fmt"
"strings"
+ "code.gitea.io/gitea/models/perm"
"code.gitea.io/gitea/modules/log"
"code.gitea.io/gitea/modules/setting"
)
@@ -17,14 +18,15 @@ type Type int
// Enumerate all the unit types
const (
- TypeCode Type = iota + 1 // 1 code
- TypeIssues // 2 issues
- TypePullRequests // 3 PRs
- TypeReleases // 4 Releases
- TypeWiki // 5 Wiki
- TypeExternalWiki // 6 ExternalWiki
- TypeExternalTracker // 7 ExternalTracker
- TypeProjects // 8 Kanban board
+ TypeInvalid Type = iota // 0 invalid
+ TypeCode // 1 code
+ TypeIssues // 2 issues
+ TypePullRequests // 3 PRs
+ TypeReleases // 4 Releases
+ TypeWiki // 5 Wiki
+ TypeExternalWiki // 6 ExternalWiki
+ TypeExternalTracker // 7 ExternalTracker
+ TypeProjects // 8 Kanban board
)
// Value returns integer value for unit type
@@ -170,11 +172,12 @@ func (u *Type) CanBeDefault() bool {
// Unit is a section of one repository
type Unit struct {
- Type Type
- NameKey string
- URI string
- DescKey string
- Idx int
+ Type Type
+ NameKey string
+ URI string
+ DescKey string
+ Idx int
+ MaxAccessMode perm.AccessMode // The max access mode of the unit. i.e. Read means this unit can only be read.
}
// CanDisable returns if this unit could be disabled.
@@ -198,6 +201,7 @@ var (
"/",
"repo.code.desc",
0,
+ perm.AccessModeOwner,
}
UnitIssues = Unit{
@@ -206,6 +210,7 @@ var (
"/issues",
"repo.issues.desc",
1,
+ perm.AccessModeOwner,
}
UnitExternalTracker = Unit{
@@ -214,6 +219,7 @@ var (
"/issues",
"repo.ext_issues.desc",
1,
+ perm.AccessModeRead,
}
UnitPullRequests = Unit{
@@ -222,6 +228,7 @@ var (
"/pulls",
"repo.pulls.desc",
2,
+ perm.AccessModeOwner,
}
UnitReleases = Unit{
@@ -230,6 +237,7 @@ var (
"/releases",
"repo.releases.desc",
3,
+ perm.AccessModeOwner,
}
UnitWiki = Unit{
@@ -238,6 +246,7 @@ var (
"/wiki",
"repo.wiki.desc",
4,
+ perm.AccessModeOwner,
}
UnitExternalWiki = Unit{
@@ -246,6 +255,7 @@ var (
"/wiki",
"repo.ext_wiki.desc",
4,
+ perm.AccessModeRead,
}
UnitProjects = Unit{
@@ -254,6 +264,7 @@ var (
"/projects",
"repo.projects.desc",
5,
+ perm.AccessModeOwner,
}
// Units contains all the units
@@ -269,15 +280,51 @@ var (
}
)
-// FindUnitTypes give the unit key name and return unit
+// FindUnitTypes give the unit key names and return unit
func FindUnitTypes(nameKeys ...string) (res []Type) {
for _, key := range nameKeys {
+ var found bool
for t, u := range Units {
if strings.EqualFold(key, u.NameKey) {
res = append(res, t)
+ found = true
break
}
}
+ if !found {
+ res = append(res, TypeInvalid)
+ }
}
return
}
+
+// TypeFromKey give the unit key name and return unit
+func TypeFromKey(nameKey string) Type {
+ for t, u := range Units {
+ if strings.EqualFold(nameKey, u.NameKey) {
+ return t
+ }
+ }
+ return TypeInvalid
+}
+
+// AllUnitKeyNames returns all unit key names
+func AllUnitKeyNames() []string {
+ res := make([]string, 0, len(Units))
+ for _, u := range Units {
+ res = append(res, u.NameKey)
+ }
+ return res
+}
+
+// MinUnitAccessMode returns the minial permission of the permission map
+func MinUnitAccessMode(unitsMap map[Type]perm.AccessMode) perm.AccessMode {
+ res := perm.AccessModeNone
+ for _, mode := range unitsMap {
+ // get the minial permission great than AccessModeNone except all are AccessModeNone
+ if mode > perm.AccessModeNone && (res == perm.AccessModeNone || mode < res) {
+ res = mode
+ }
+ }
+ return res
+}