|
| 1 | +// Copyright 2021 The Gitea Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a MIT-style |
| 3 | +// license that can be found in the LICENSE file. |
| 4 | + |
| 5 | +package models |
| 6 | + |
| 7 | +import ( |
| 8 | + "regexp" |
| 9 | + "strings" |
| 10 | + |
| 11 | + "code.gitea.io/gitea/modules/base" |
| 12 | + "code.gitea.io/gitea/modules/timeutil" |
| 13 | + |
| 14 | + "github.com/gobwas/glob" |
| 15 | +) |
| 16 | + |
| 17 | +// ProtectedTag struct |
| 18 | +type ProtectedTag struct { |
| 19 | + ID int64 `xorm:"pk autoincr"` |
| 20 | + RepoID int64 |
| 21 | + NamePattern string |
| 22 | + RegexPattern *regexp.Regexp `xorm:"-"` |
| 23 | + GlobPattern glob.Glob `xorm:"-"` |
| 24 | + AllowlistUserIDs []int64 `xorm:"JSON TEXT"` |
| 25 | + AllowlistTeamIDs []int64 `xorm:"JSON TEXT"` |
| 26 | + |
| 27 | + CreatedUnix timeutil.TimeStamp `xorm:"created"` |
| 28 | + UpdatedUnix timeutil.TimeStamp `xorm:"updated"` |
| 29 | +} |
| 30 | + |
| 31 | +// InsertProtectedTag inserts a protected tag to database |
| 32 | +func InsertProtectedTag(pt *ProtectedTag) error { |
| 33 | + _, err := x.Insert(pt) |
| 34 | + return err |
| 35 | +} |
| 36 | + |
| 37 | +// UpdateProtectedTag updates the protected tag |
| 38 | +func UpdateProtectedTag(pt *ProtectedTag) error { |
| 39 | + _, err := x.ID(pt.ID).AllCols().Update(pt) |
| 40 | + return err |
| 41 | +} |
| 42 | + |
| 43 | +// DeleteProtectedTag deletes a protected tag by ID |
| 44 | +func DeleteProtectedTag(pt *ProtectedTag) error { |
| 45 | + _, err := x.ID(pt.ID).Delete(&ProtectedTag{}) |
| 46 | + return err |
| 47 | +} |
| 48 | + |
| 49 | +// EnsureCompiledPattern ensures the glob pattern is compiled |
| 50 | +func (pt *ProtectedTag) EnsureCompiledPattern() error { |
| 51 | + if pt.RegexPattern != nil || pt.GlobPattern != nil { |
| 52 | + return nil |
| 53 | + } |
| 54 | + |
| 55 | + var err error |
| 56 | + if len(pt.NamePattern) >= 2 && strings.HasPrefix(pt.NamePattern, "/") && strings.HasSuffix(pt.NamePattern, "/") { |
| 57 | + pt.RegexPattern, err = regexp.Compile(pt.NamePattern[1 : len(pt.NamePattern)-1]) |
| 58 | + } else { |
| 59 | + pt.GlobPattern, err = glob.Compile(pt.NamePattern) |
| 60 | + } |
| 61 | + return err |
| 62 | +} |
| 63 | + |
| 64 | +// IsUserAllowed returns true if the user is allowed to modify the tag |
| 65 | +func (pt *ProtectedTag) IsUserAllowed(userID int64) (bool, error) { |
| 66 | + if base.Int64sContains(pt.AllowlistUserIDs, userID) { |
| 67 | + return true, nil |
| 68 | + } |
| 69 | + |
| 70 | + if len(pt.AllowlistTeamIDs) == 0 { |
| 71 | + return false, nil |
| 72 | + } |
| 73 | + |
| 74 | + in, err := IsUserInTeams(userID, pt.AllowlistTeamIDs) |
| 75 | + if err != nil { |
| 76 | + return false, err |
| 77 | + } |
| 78 | + return in, nil |
| 79 | +} |
| 80 | + |
| 81 | +// GetProtectedTags gets all protected tags of the repository |
| 82 | +func (repo *Repository) GetProtectedTags() ([]*ProtectedTag, error) { |
| 83 | + tags := make([]*ProtectedTag, 0) |
| 84 | + return tags, x.Find(&tags, &ProtectedTag{RepoID: repo.ID}) |
| 85 | +} |
| 86 | + |
| 87 | +// GetProtectedTagByID gets the protected tag with the specific id |
| 88 | +func GetProtectedTagByID(id int64) (*ProtectedTag, error) { |
| 89 | + tag := new(ProtectedTag) |
| 90 | + has, err := x.ID(id).Get(tag) |
| 91 | + if err != nil { |
| 92 | + return nil, err |
| 93 | + } |
| 94 | + if !has { |
| 95 | + return nil, nil |
| 96 | + } |
| 97 | + return tag, nil |
| 98 | +} |
| 99 | + |
| 100 | +// IsUserAllowedToControlTag checks if a user can control the specific tag. |
| 101 | +// It returns true if the tag name is not protected or the user is allowed to control it. |
| 102 | +func IsUserAllowedToControlTag(tags []*ProtectedTag, tagName string, userID int64) (bool, error) { |
| 103 | + isAllowed := true |
| 104 | + for _, tag := range tags { |
| 105 | + err := tag.EnsureCompiledPattern() |
| 106 | + if err != nil { |
| 107 | + return false, err |
| 108 | + } |
| 109 | + |
| 110 | + if !tag.matchString(tagName) { |
| 111 | + continue |
| 112 | + } |
| 113 | + |
| 114 | + isAllowed, err = tag.IsUserAllowed(userID) |
| 115 | + if err != nil { |
| 116 | + return false, err |
| 117 | + } |
| 118 | + if isAllowed { |
| 119 | + break |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + return isAllowed, nil |
| 124 | +} |
| 125 | + |
| 126 | +func (pt *ProtectedTag) matchString(name string) bool { |
| 127 | + if pt.RegexPattern != nil { |
| 128 | + return pt.RegexPattern.MatchString(name) |
| 129 | + } |
| 130 | + return pt.GlobPattern.Match(name) |
| 131 | +} |
0 commit comments