mirror of https://github.com/go-gitea/gitea
Git with a cup of tea, painless self-hosted git service
Mirror for internal git.with.parts use
https://git.with.parts
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.
441 lines
13 KiB
441 lines
13 KiB
6 years ago
|
// 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.
|
||
|
|
||
3 years ago
|
package access
|
||
6 years ago
|
|
||
6 years ago
|
import (
|
||
3 years ago
|
"context"
|
||
6 years ago
|
"fmt"
|
||
|
|
||
3 years ago
|
"code.gitea.io/gitea/models/db"
|
||
3 years ago
|
"code.gitea.io/gitea/models/organization"
|
||
3 years ago
|
perm_model "code.gitea.io/gitea/models/perm"
|
||
3 years ago
|
repo_model "code.gitea.io/gitea/models/repo"
|
||
3 years ago
|
"code.gitea.io/gitea/models/unit"
|
||
3 years ago
|
user_model "code.gitea.io/gitea/models/user"
|
||
6 years ago
|
"code.gitea.io/gitea/modules/log"
|
||
|
)
|
||
|
|
||
6 years ago
|
// Permission contains all the permissions related variables to a repository for a user
|
||
|
type Permission struct {
|
||
3 years ago
|
AccessMode perm_model.AccessMode
|
||
3 years ago
|
Units []*repo_model.RepoUnit
|
||
3 years ago
|
UnitsMode map[unit.Type]perm_model.AccessMode
|
||
6 years ago
|
}
|
||
|
|
||
|
// IsOwner returns true if current user is the owner of repository.
|
||
|
func (p *Permission) IsOwner() bool {
|
||
3 years ago
|
return p.AccessMode >= perm_model.AccessModeOwner
|
||
6 years ago
|
}
|
||
|
|
||
|
// IsAdmin returns true if current user has admin or higher access of repository.
|
||
|
func (p *Permission) IsAdmin() bool {
|
||
3 years ago
|
return p.AccessMode >= perm_model.AccessModeAdmin
|
||
6 years ago
|
}
|
||
|
|
||
|
// HasAccess returns true if the current user has at least read access to any unit of this repository
|
||
|
func (p *Permission) HasAccess() bool {
|
||
|
if p.UnitsMode == nil {
|
||
3 years ago
|
return p.AccessMode >= perm_model.AccessModeRead
|
||
6 years ago
|
}
|
||
|
return len(p.UnitsMode) > 0
|
||
|
}
|
||
|
|
||
|
// UnitAccessMode returns current user accessmode to the specify unit of the repository
|
||
3 years ago
|
func (p *Permission) UnitAccessMode(unitType unit.Type) perm_model.AccessMode {
|
||
6 years ago
|
if p.UnitsMode == nil {
|
||
|
for _, u := range p.Units {
|
||
|
if u.Type == unitType {
|
||
|
return p.AccessMode
|
||
|
}
|
||
|
}
|
||
3 years ago
|
return perm_model.AccessModeNone
|
||
6 years ago
|
}
|
||
|
return p.UnitsMode[unitType]
|
||
|
}
|
||
|
|
||
|
// CanAccess returns true if user has mode access to the unit of the repository
|
||
3 years ago
|
func (p *Permission) CanAccess(mode perm_model.AccessMode, unitType unit.Type) bool {
|
||
6 years ago
|
return p.UnitAccessMode(unitType) >= mode
|
||
|
}
|
||
|
|
||
|
// CanAccessAny returns true if user has mode access to any of the units of the repository
|
||
3 years ago
|
func (p *Permission) CanAccessAny(mode perm_model.AccessMode, unitTypes ...unit.Type) bool {
|
||
6 years ago
|
for _, u := range unitTypes {
|
||
|
if p.CanAccess(mode, u) {
|
||
|
return true
|
||
|
}
|
||
|
}
|
||
|
return false
|
||
|
}
|
||
|
|
||
|
// CanRead returns true if user could read to this unit
|
||
3 years ago
|
func (p *Permission) CanRead(unitType unit.Type) bool {
|
||
3 years ago
|
return p.CanAccess(perm_model.AccessModeRead, unitType)
|
||
6 years ago
|
}
|
||
|
|
||
|
// CanReadAny returns true if user has read access to any of the units of the repository
|
||
3 years ago
|
func (p *Permission) CanReadAny(unitTypes ...unit.Type) bool {
|
||
3 years ago
|
return p.CanAccessAny(perm_model.AccessModeRead, unitTypes...)
|
||
6 years ago
|
}
|
||
|
|
||
|
// CanReadIssuesOrPulls returns true if isPull is true and user could read pull requests and
|
||
|
// returns true if isPull is false and user could read to issues
|
||
|
func (p *Permission) CanReadIssuesOrPulls(isPull bool) bool {
|
||
|
if isPull {
|
||
3 years ago
|
return p.CanRead(unit.TypePullRequests)
|
||
6 years ago
|
}
|
||
3 years ago
|
return p.CanRead(unit.TypeIssues)
|
||
6 years ago
|
}
|
||
|
|
||
|
// CanWrite returns true if user could write to this unit
|
||
3 years ago
|
func (p *Permission) CanWrite(unitType unit.Type) bool {
|
||
3 years ago
|
return p.CanAccess(perm_model.AccessModeWrite, unitType)
|
||
6 years ago
|
}
|
||
|
|
||
|
// CanWriteIssuesOrPulls returns true if isPull is true and user could write to pull requests and
|
||
|
// returns true if isPull is false and user could write to issues
|
||
|
func (p *Permission) CanWriteIssuesOrPulls(isPull bool) bool {
|
||
|
if isPull {
|
||
3 years ago
|
return p.CanWrite(unit.TypePullRequests)
|
||
6 years ago
|
}
|
||
3 years ago
|
return p.CanWrite(unit.TypeIssues)
|
||
6 years ago
|
}
|
||
|
|
||
6 years ago
|
// ColorFormat writes a colored string for these Permissions
|
||
|
func (p *Permission) ColorFormat(s fmt.State) {
|
||
|
noColor := log.ColorBytes(log.Reset)
|
||
|
|
||
3 years ago
|
format := "perm_model.AccessMode: %-v, %d Units, %d UnitsMode(s): [ "
|
||
6 years ago
|
args := []interface{}{
|
||
|
p.AccessMode,
|
||
|
log.NewColoredValueBytes(len(p.Units), &noColor),
|
||
|
log.NewColoredValueBytes(len(p.UnitsMode), &noColor),
|
||
|
}
|
||
|
if s.Flag('+') {
|
||
|
for i, unit := range p.Units {
|
||
|
config := ""
|
||
|
if unit.Config != nil {
|
||
|
configBytes, err := unit.Config.ToDB()
|
||
|
config = string(configBytes)
|
||
|
if err != nil {
|
||
5 years ago
|
config = err.Error()
|
||
6 years ago
|
}
|
||
|
}
|
||
|
format += "\nUnits[%d]: ID: %d RepoID: %d Type: %-v Config: %s"
|
||
|
args = append(args,
|
||
|
log.NewColoredValueBytes(i, &noColor),
|
||
|
log.NewColoredIDValue(unit.ID),
|
||
|
log.NewColoredIDValue(unit.RepoID),
|
||
|
unit.Type,
|
||
|
config)
|
||
|
}
|
||
|
for key, value := range p.UnitsMode {
|
||
|
format += "\nUnitMode[%-v]: %-v"
|
||
|
args = append(args,
|
||
|
key,
|
||
|
value)
|
||
|
}
|
||
|
} else {
|
||
|
format += "..."
|
||
|
}
|
||
|
format += " ]"
|
||
|
log.ColorFprintf(s, format, args...)
|
||
|
}
|
||
|
|
||
6 years ago
|
// GetUserRepoPermission returns the user permissions to the repository
|
||
3 years ago
|
func GetUserRepoPermission(ctx context.Context, repo *repo_model.Repository, user *user_model.User) (perm Permission, err error) {
|
||
6 years ago
|
if log.IsTrace() {
|
||
|
defer func() {
|
||
|
if user == nil {
|
||
|
log.Trace("Permission Loaded for anonymous user in %-v:\nPermissions: %-+v",
|
||
|
repo,
|
||
|
perm)
|
||
|
return
|
||
|
}
|
||
|
log.Trace("Permission Loaded for %-v in %-v:\nPermissions: %-+v",
|
||
|
user,
|
||
|
repo,
|
||
|
perm)
|
||
|
}()
|
||
|
}
|
||
3 years ago
|
|
||
6 years ago
|
// anonymous user visit private repo.
|
||
|
// TODO: anonymous user visit public unit of private repo???
|
||
|
if user == nil && repo.IsPrivate {
|
||
3 years ago
|
perm.AccessMode = perm_model.AccessModeNone
|
||
6 years ago
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
e := db.GetEngine(ctx)
|
||
|
|
||
|
var is bool
|
||
6 years ago
|
if user != nil {
|
||
3 years ago
|
is, err = repo_model.IsCollaborator(ctx, repo.ID, user.ID)
|
||
6 years ago
|
if err != nil {
|
||
|
return perm, err
|
||
|
}
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = repo.GetOwner(ctx); err != nil {
|
||
5 years ago
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
// Prevent strangers from checking out public repo of private organization/users
|
||
3 years ago
|
// Allow user if they are collaborator of a repo within a private user or a private organization but not a member of the organization itself
|
||
3 years ago
|
if !organization.HasOrgOrUserVisible(ctx, repo.Owner, user) && !is {
|
||
3 years ago
|
perm.AccessMode = perm_model.AccessModeNone
|
||
6 years ago
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = repo.LoadUnits(ctx); err != nil {
|
||
6 years ago
|
return
|
||
|
}
|
||
|
|
||
|
perm.Units = repo.Units
|
||
|
|
||
|
// anonymous visit public repo
|
||
|
if user == nil {
|
||
3 years ago
|
perm.AccessMode = perm_model.AccessModeRead
|
||
6 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// Admin or the owner has super access to the repository
|
||
|
if user.IsAdmin || user.ID == repo.OwnerID {
|
||
3 years ago
|
perm.AccessMode = perm_model.AccessModeOwner
|
||
6 years ago
|
return
|
||
|
}
|
||
|
|
||
|
// plain user
|
||
5 years ago
|
perm.AccessMode, err = accessLevel(e, user, repo)
|
||
6 years ago
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
if err = repo.GetOwner(ctx); err != nil {
|
||
6 years ago
|
return
|
||
|
}
|
||
|
if !repo.Owner.IsOrganization() {
|
||
|
return
|
||
|
}
|
||
|
|
||
3 years ago
|
perm.UnitsMode = make(map[unit.Type]perm_model.AccessMode)
|
||
6 years ago
|
|
||
|
// Collaborators on organization
|
||
3 years ago
|
if is {
|
||
6 years ago
|
for _, u := range repo.Units {
|
||
|
perm.UnitsMode[u.Type] = perm.AccessMode
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// get units mode from teams
|
||
3 years ago
|
teams, err := organization.GetUserRepoTeams(ctx, repo.OwnerID, user.ID, repo.ID)
|
||
6 years ago
|
if err != nil {
|
||
|
return
|
||
|
}
|
||
|
|
||
6 years ago
|
// if user in an owner team
|
||
|
for _, team := range teams {
|
||
3 years ago
|
if team.AccessMode >= perm_model.AccessModeAdmin {
|
||
3 years ago
|
perm.AccessMode = perm_model.AccessModeOwner
|
||
6 years ago
|
perm.UnitsMode = nil
|
||
|
return
|
||
|
}
|
||
|
}
|
||
|
|
||
6 years ago
|
for _, u := range repo.Units {
|
||
|
var found bool
|
||
|
for _, team := range teams {
|
||
3 years ago
|
teamMode := team.UnitAccessModeCtx(ctx, u.Type)
|
||
3 years ago
|
if teamMode > perm_model.AccessModeNone {
|
||
6 years ago
|
m := perm.UnitsMode[u.Type]
|
||
3 years ago
|
if m < teamMode {
|
||
|
perm.UnitsMode[u.Type] = teamMode
|
||
6 years ago
|
}
|
||
|
found = true
|
||
|
}
|
||
|
}
|
||
|
|
||
5 years ago
|
// for a public repo on an organization, a non-restricted user has read permission on non-team defined units.
|
||
|
if !found && !repo.IsPrivate && !user.IsRestricted {
|
||
6 years ago
|
if _, ok := perm.UnitsMode[u.Type]; !ok {
|
||
3 years ago
|
perm.UnitsMode[u.Type] = perm_model.AccessModeRead
|
||
6 years ago
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
// remove no permission units
|
||
3 years ago
|
perm.Units = make([]*repo_model.RepoUnit, 0, len(repo.Units))
|
||
6 years ago
|
for t := range perm.UnitsMode {
|
||
|
for _, u := range repo.Units {
|
||
|
if u.Type == t {
|
||
|
perm.Units = append(perm.Units, u)
|
||
|
}
|
||
|
}
|
||
|
}
|
||
|
|
||
|
return
|
||
|
}
|
||
|
|
||
4 years ago
|
// IsUserRealRepoAdmin check if this user is real repo admin
|
||
3 years ago
|
func IsUserRealRepoAdmin(repo *repo_model.Repository, user *user_model.User) (bool, error) {
|
||
4 years ago
|
if repo.OwnerID == user.ID {
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
if err := repo.GetOwner(db.DefaultContext); err != nil {
|
||
4 years ago
|
return false, err
|
||
|
}
|
||
|
|
||
3 years ago
|
accessMode, err := accessLevel(db.GetEngine(db.DefaultContext), user, repo)
|
||
4 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
3 years ago
|
return accessMode >= perm_model.AccessModeAdmin, nil
|
||
4 years ago
|
}
|
||
|
|
||
5 years ago
|
// IsUserRepoAdmin return true if user has admin right of a repo
|
||
3 years ago
|
func IsUserRepoAdmin(repo *repo_model.Repository, user *user_model.User) (bool, error) {
|
||
3 years ago
|
return IsUserRepoAdminCtx(db.DefaultContext, repo, user)
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
// IsUserRepoAdminCtx return true if user has admin right of a repo
|
||
|
func IsUserRepoAdminCtx(ctx context.Context, repo *repo_model.Repository, user *user_model.User) (bool, error) {
|
||
6 years ago
|
if user == nil || repo == nil {
|
||
|
return false, nil
|
||
|
}
|
||
|
if user.IsAdmin {
|
||
|
return true, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
e := db.GetEngine(ctx)
|
||
5 years ago
|
mode, err := accessLevel(e, user, repo)
|
||
6 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
3 years ago
|
if mode >= perm_model.AccessModeAdmin {
|
||
6 years ago
|
return true, nil
|
||
|
}
|
||
|
|
||
3 years ago
|
teams, err := organization.GetUserRepoTeams(ctx, repo.OwnerID, user.ID, repo.ID)
|
||
6 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
|
||
|
for _, team := range teams {
|
||
3 years ago
|
if team.AccessMode >= perm_model.AccessModeAdmin {
|
||
6 years ago
|
return true, nil
|
||
|
}
|
||
|
}
|
||
|
return false, nil
|
||
|
}
|
||
|
|
||
|
// AccessLevel returns the Access a user has to a repository. Will return NoneAccess if the
|
||
|
// user does not have access.
|
||
3 years ago
|
func AccessLevel(user *user_model.User, repo *repo_model.Repository) (perm_model.AccessMode, error) { //nolint
|
||
|
return AccessLevelUnit(user, repo, unit.TypeCode)
|
||
6 years ago
|
}
|
||
|
|
||
5 years ago
|
// AccessLevelUnit returns the Access a user has to a repository's. Will return NoneAccess if the
|
||
|
// user does not have access.
|
||
3 years ago
|
func AccessLevelUnit(user *user_model.User, repo *repo_model.Repository, unitType unit.Type) (perm_model.AccessMode, error) { //nolint
|
||
3 years ago
|
return accessLevelUnit(db.DefaultContext, user, repo, unitType)
|
||
5 years ago
|
}
|
||
|
|
||
3 years ago
|
func accessLevelUnit(ctx context.Context, user *user_model.User, repo *repo_model.Repository, unitType unit.Type) (perm_model.AccessMode, error) {
|
||
3 years ago
|
perm, err := GetUserRepoPermission(ctx, repo, user)
|
||
6 years ago
|
if err != nil {
|
||
3 years ago
|
return perm_model.AccessModeNone, err
|
||
6 years ago
|
}
|
||
6 years ago
|
return perm.UnitAccessMode(unitType), nil
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
// HasAccessUnit returns true if user has testMode to the unit of the repository
|
||
|
func HasAccessUnit(ctx context.Context, user *user_model.User, repo *repo_model.Repository, unitType unit.Type, testMode perm_model.AccessMode) (bool, error) {
|
||
3 years ago
|
mode, err := accessLevelUnit(ctx, user, repo, unitType)
|
||
6 years ago
|
return testMode <= mode, err
|
||
|
}
|
||
|
|
||
5 years ago
|
// CanBeAssigned return true if user can be assigned to issue or pull requests in repo
|
||
|
// Currently any write access (code, issues or pr's) is assignable, to match assignee list in user interface.
|
||
6 years ago
|
// FIXME: user could send PullRequest also could be assigned???
|
||
3 years ago
|
func CanBeAssigned(ctx context.Context, user *user_model.User, repo *repo_model.Repository, _ bool) (bool, error) {
|
||
5 years ago
|
if user.IsOrganization() {
|
||
|
return false, fmt.Errorf("Organization can't be added as assignee [user_id: %d, repo_id: %d]", user.ID, repo.ID)
|
||
|
}
|
||
3 years ago
|
perm, err := GetUserRepoPermission(ctx, repo, user)
|
||
5 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
3 years ago
|
return perm.CanAccessAny(perm_model.AccessModeWrite, unit.TypeCode, unit.TypeIssues, unit.TypePullRequests), nil
|
||
6 years ago
|
}
|
||
|
|
||
3 years ago
|
// HasAccess returns true if user has access to repo
|
||
|
func HasAccess(ctx context.Context, userID int64, repo *repo_model.Repository) (bool, error) {
|
||
3 years ago
|
var user *user_model.User
|
||
6 years ago
|
var err error
|
||
|
if userID > 0 {
|
||
3 years ago
|
user, err = user_model.GetUserByIDEngine(db.GetEngine(ctx), userID)
|
||
6 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
}
|
||
3 years ago
|
perm, err := GetUserRepoPermission(ctx, repo, user)
|
||
6 years ago
|
if err != nil {
|
||
|
return false, err
|
||
|
}
|
||
|
return perm.HasAccess(), nil
|
||
|
}
|
||
|
|
||
3 years ago
|
// getUsersWithAccessMode returns users that have at least given access mode to the repository.
|
||
|
func getUsersWithAccessMode(ctx context.Context, repo *repo_model.Repository, mode perm_model.AccessMode) (_ []*user_model.User, err error) {
|
||
|
if err = repo.GetOwner(ctx); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
e := db.GetEngine(ctx)
|
||
|
accesses := make([]*Access, 0, 10)
|
||
|
if err = e.Where("repo_id = ? AND mode >= ?", repo.ID, mode).Find(&accesses); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
|
||
|
// Leave a seat for owner itself to append later, but if owner is an organization
|
||
|
// and just waste 1 unit is cheaper than re-allocate memory once.
|
||
|
users := make([]*user_model.User, 0, len(accesses)+1)
|
||
|
if len(accesses) > 0 {
|
||
|
userIDs := make([]int64, len(accesses))
|
||
|
for i := 0; i < len(accesses); i++ {
|
||
|
userIDs[i] = accesses[i].UserID
|
||
|
}
|
||
|
|
||
|
if err = e.In("id", userIDs).Find(&users); err != nil {
|
||
|
return nil, err
|
||
|
}
|
||
|
}
|
||
|
if !repo.Owner.IsOrganization() {
|
||
|
users = append(users, repo.Owner)
|
||
|
}
|
||
|
|
||
|
return users, nil
|
||
6 years ago
|
}
|
||
5 years ago
|
|
||
3 years ago
|
// GetRepoReaders returns all users that have explicit read access or higher to the repository.
|
||
|
func GetRepoReaders(repo *repo_model.Repository) (_ []*user_model.User, err error) {
|
||
|
return getUsersWithAccessMode(db.DefaultContext, repo, perm_model.AccessModeRead)
|
||
|
}
|
||
|
|
||
|
// GetRepoWriters returns all users that have write access to the repository.
|
||
|
func GetRepoWriters(repo *repo_model.Repository) (_ []*user_model.User, err error) {
|
||
|
return getUsersWithAccessMode(db.DefaultContext, repo, perm_model.AccessModeWrite)
|
||
|
}
|
||
|
|
||
|
// IsRepoReader returns true if user has explicit read access or higher to the repository.
|
||
3 years ago
|
func IsRepoReader(ctx context.Context, repo *repo_model.Repository, userID int64) (bool, error) {
|
||
3 years ago
|
if repo.OwnerID == userID {
|
||
|
return true, nil
|
||
|
}
|
||
3 years ago
|
return db.GetEngine(ctx).Where("repo_id = ? AND user_id = ? AND mode >= ?", repo.ID, userID, perm_model.AccessModeRead).Get(&Access{})
|
||
3 years ago
|
}
|