You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

unit.go 8.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368
  1. // Copyright 2017 The Gitea Authors. All rights reserved.
  2. // SPDX-License-Identifier: MIT
  3. package unit
  4. import (
  5. "errors"
  6. "fmt"
  7. "strings"
  8. "code.gitea.io/gitea/models/perm"
  9. "code.gitea.io/gitea/modules/container"
  10. "code.gitea.io/gitea/modules/log"
  11. "code.gitea.io/gitea/modules/setting"
  12. )
  13. // Type is Unit's Type
  14. type Type int
  15. // Enumerate all the unit types
  16. const (
  17. TypeInvalid Type = iota // 0 invalid
  18. TypeCode // 1 code
  19. TypeIssues // 2 issues
  20. TypePullRequests // 3 PRs
  21. TypeReleases // 4 Releases
  22. TypeWiki // 5 Wiki
  23. TypeExternalWiki // 6 ExternalWiki
  24. TypeExternalTracker // 7 ExternalTracker
  25. TypeProjects // 8 Kanban board
  26. TypePackages // 9 Packages
  27. TypeActions // 10 Actions
  28. )
  29. // Value returns integer value for unit type
  30. func (u Type) Value() int {
  31. return int(u)
  32. }
  33. func (u Type) String() string {
  34. switch u {
  35. case TypeCode:
  36. return "TypeCode"
  37. case TypeIssues:
  38. return "TypeIssues"
  39. case TypePullRequests:
  40. return "TypePullRequests"
  41. case TypeReleases:
  42. return "TypeReleases"
  43. case TypeWiki:
  44. return "TypeWiki"
  45. case TypeExternalWiki:
  46. return "TypeExternalWiki"
  47. case TypeExternalTracker:
  48. return "TypeExternalTracker"
  49. case TypeProjects:
  50. return "TypeProjects"
  51. case TypePackages:
  52. return "TypePackages"
  53. case TypeActions:
  54. return "TypeActions"
  55. }
  56. return fmt.Sprintf("Unknown Type %d", u)
  57. }
  58. func (u Type) LogString() string {
  59. return fmt.Sprintf("<UnitType:%d:%s>", u, u.String())
  60. }
  61. var (
  62. // AllRepoUnitTypes contains all the unit types
  63. AllRepoUnitTypes = []Type{
  64. TypeCode,
  65. TypeIssues,
  66. TypePullRequests,
  67. TypeReleases,
  68. TypeWiki,
  69. TypeExternalWiki,
  70. TypeExternalTracker,
  71. TypeProjects,
  72. TypePackages,
  73. TypeActions,
  74. }
  75. // DefaultRepoUnits contains the default unit types
  76. DefaultRepoUnits = []Type{
  77. TypeCode,
  78. TypeIssues,
  79. TypePullRequests,
  80. TypeReleases,
  81. TypeWiki,
  82. TypeProjects,
  83. TypePackages,
  84. }
  85. // ForkRepoUnits contains the default unit types for forks
  86. DefaultForkRepoUnits = []Type{
  87. TypeCode,
  88. TypePullRequests,
  89. }
  90. // NotAllowedDefaultRepoUnits contains units that can't be default
  91. NotAllowedDefaultRepoUnits = []Type{
  92. TypeExternalWiki,
  93. TypeExternalTracker,
  94. }
  95. // DisabledRepoUnits contains the units that have been globally disabled
  96. DisabledRepoUnits = []Type{}
  97. )
  98. // Get valid set of default repository units from settings
  99. func validateDefaultRepoUnits(defaultUnits, settingDefaultUnits []Type) []Type {
  100. units := defaultUnits
  101. // Use setting if not empty
  102. if len(settingDefaultUnits) > 0 {
  103. units = make([]Type, 0, len(settingDefaultUnits))
  104. for _, settingUnit := range settingDefaultUnits {
  105. if !settingUnit.CanBeDefault() {
  106. log.Warn("Not allowed as default unit: %s", settingUnit.String())
  107. continue
  108. }
  109. units = append(units, settingUnit)
  110. }
  111. }
  112. // Remove disabled units
  113. for _, disabledUnit := range DisabledRepoUnits {
  114. for i, unit := range units {
  115. if unit == disabledUnit {
  116. units = append(units[:i], units[i+1:]...)
  117. }
  118. }
  119. }
  120. return units
  121. }
  122. // LoadUnitConfig load units from settings
  123. func LoadUnitConfig() error {
  124. var invalidKeys []string
  125. DisabledRepoUnits, invalidKeys = FindUnitTypes(setting.Repository.DisabledRepoUnits...)
  126. if len(invalidKeys) > 0 {
  127. log.Warn("Invalid keys in disabled repo units: %s", strings.Join(invalidKeys, ", "))
  128. }
  129. setDefaultRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultRepoUnits...)
  130. if len(invalidKeys) > 0 {
  131. log.Warn("Invalid keys in default repo units: %s", strings.Join(invalidKeys, ", "))
  132. }
  133. DefaultRepoUnits = validateDefaultRepoUnits(DefaultRepoUnits, setDefaultRepoUnits)
  134. if len(DefaultRepoUnits) == 0 {
  135. return errors.New("no default repository units found")
  136. }
  137. setDefaultForkRepoUnits, invalidKeys := FindUnitTypes(setting.Repository.DefaultForkRepoUnits...)
  138. if len(invalidKeys) > 0 {
  139. log.Warn("Invalid keys in default fork repo units: %s", strings.Join(invalidKeys, ", "))
  140. }
  141. DefaultForkRepoUnits = validateDefaultRepoUnits(DefaultForkRepoUnits, setDefaultForkRepoUnits)
  142. if len(DefaultForkRepoUnits) == 0 {
  143. return errors.New("no default fork repository units found")
  144. }
  145. return nil
  146. }
  147. // UnitGlobalDisabled checks if unit type is global disabled
  148. func (u Type) UnitGlobalDisabled() bool {
  149. for _, ud := range DisabledRepoUnits {
  150. if u == ud {
  151. return true
  152. }
  153. }
  154. return false
  155. }
  156. // CanBeDefault checks if the unit type can be a default repo unit
  157. func (u *Type) CanBeDefault() bool {
  158. for _, nadU := range NotAllowedDefaultRepoUnits {
  159. if *u == nadU {
  160. return false
  161. }
  162. }
  163. return true
  164. }
  165. // Unit is a section of one repository
  166. type Unit struct {
  167. Type Type
  168. NameKey string
  169. URI string
  170. DescKey string
  171. Idx int
  172. MaxAccessMode perm.AccessMode // The max access mode of the unit. i.e. Read means this unit can only be read.
  173. }
  174. // IsLessThan compares order of two units
  175. func (u Unit) IsLessThan(unit Unit) bool {
  176. if (u.Type == TypeExternalTracker || u.Type == TypeExternalWiki) && unit.Type != TypeExternalTracker && unit.Type != TypeExternalWiki {
  177. return false
  178. }
  179. return u.Idx < unit.Idx
  180. }
  181. // MaxPerm returns the max perms of this unit
  182. func (u Unit) MaxPerm() perm.AccessMode {
  183. if u.Type == TypeExternalTracker || u.Type == TypeExternalWiki {
  184. return perm.AccessModeRead
  185. }
  186. return perm.AccessModeAdmin
  187. }
  188. // Enumerate all the units
  189. var (
  190. UnitCode = Unit{
  191. TypeCode,
  192. "repo.code",
  193. "/",
  194. "repo.code.desc",
  195. 0,
  196. perm.AccessModeOwner,
  197. }
  198. UnitIssues = Unit{
  199. TypeIssues,
  200. "repo.issues",
  201. "/issues",
  202. "repo.issues.desc",
  203. 1,
  204. perm.AccessModeOwner,
  205. }
  206. UnitExternalTracker = Unit{
  207. TypeExternalTracker,
  208. "repo.ext_issues",
  209. "/issues",
  210. "repo.ext_issues.desc",
  211. 1,
  212. perm.AccessModeRead,
  213. }
  214. UnitPullRequests = Unit{
  215. TypePullRequests,
  216. "repo.pulls",
  217. "/pulls",
  218. "repo.pulls.desc",
  219. 2,
  220. perm.AccessModeOwner,
  221. }
  222. UnitReleases = Unit{
  223. TypeReleases,
  224. "repo.releases",
  225. "/releases",
  226. "repo.releases.desc",
  227. 3,
  228. perm.AccessModeOwner,
  229. }
  230. UnitWiki = Unit{
  231. TypeWiki,
  232. "repo.wiki",
  233. "/wiki",
  234. "repo.wiki.desc",
  235. 4,
  236. perm.AccessModeOwner,
  237. }
  238. UnitExternalWiki = Unit{
  239. TypeExternalWiki,
  240. "repo.ext_wiki",
  241. "/wiki",
  242. "repo.ext_wiki.desc",
  243. 4,
  244. perm.AccessModeRead,
  245. }
  246. UnitProjects = Unit{
  247. TypeProjects,
  248. "repo.projects",
  249. "/projects",
  250. "repo.projects.desc",
  251. 5,
  252. perm.AccessModeOwner,
  253. }
  254. UnitPackages = Unit{
  255. TypePackages,
  256. "repo.packages",
  257. "/packages",
  258. "packages.desc",
  259. 6,
  260. perm.AccessModeRead,
  261. }
  262. UnitActions = Unit{
  263. TypeActions,
  264. "repo.actions",
  265. "/actions",
  266. "actions.unit.desc",
  267. 7,
  268. perm.AccessModeOwner,
  269. }
  270. // Units contains all the units
  271. Units = map[Type]Unit{
  272. TypeCode: UnitCode,
  273. TypeIssues: UnitIssues,
  274. TypeExternalTracker: UnitExternalTracker,
  275. TypePullRequests: UnitPullRequests,
  276. TypeReleases: UnitReleases,
  277. TypeWiki: UnitWiki,
  278. TypeExternalWiki: UnitExternalWiki,
  279. TypeProjects: UnitProjects,
  280. TypePackages: UnitPackages,
  281. TypeActions: UnitActions,
  282. }
  283. )
  284. // FindUnitTypes give the unit key names and return valid unique units and invalid keys
  285. func FindUnitTypes(nameKeys ...string) (res []Type, invalidKeys []string) {
  286. m := make(container.Set[Type])
  287. for _, key := range nameKeys {
  288. t := TypeFromKey(key)
  289. if t == TypeInvalid {
  290. invalidKeys = append(invalidKeys, key)
  291. } else if m.Add(t) {
  292. res = append(res, t)
  293. }
  294. }
  295. return res, invalidKeys
  296. }
  297. // TypeFromKey give the unit key name and return unit
  298. func TypeFromKey(nameKey string) Type {
  299. for t, u := range Units {
  300. if strings.EqualFold(nameKey, u.NameKey) {
  301. return t
  302. }
  303. }
  304. return TypeInvalid
  305. }
  306. // AllUnitKeyNames returns all unit key names
  307. func AllUnitKeyNames() []string {
  308. res := make([]string, 0, len(Units))
  309. for _, u := range Units {
  310. res = append(res, u.NameKey)
  311. }
  312. return res
  313. }
  314. // MinUnitAccessMode returns the minial permission of the permission map
  315. func MinUnitAccessMode(unitsMap map[Type]perm.AccessMode) perm.AccessMode {
  316. res := perm.AccessModeNone
  317. for t, mode := range unitsMap {
  318. // Don't allow `TypeExternal{Tracker,Wiki}` to influence this as they can only be set to READ perms.
  319. if t == TypeExternalTracker || t == TypeExternalWiki {
  320. continue
  321. }
  322. // get the minial permission great than AccessModeNone except all are AccessModeNone
  323. if mode > perm.AccessModeNone && (res == perm.AccessModeNone || mode < res) {
  324. res = mode
  325. }
  326. }
  327. return res
  328. }