Fork to maintain patches against the official gitea for https://code.ceondo.com https://github.com/go-gitea/gitea

lfs_lock.go 3.4KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2017 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. package models
  5. import (
  6. "fmt"
  7. "path"
  8. "strconv"
  9. "strings"
  10. "time"
  11. api "code.gitea.io/sdk/gitea"
  12. )
  13. // LFSLock represents a git lfs lock of repository.
  14. type LFSLock struct {
  15. ID int64 `xorm:"pk autoincr"`
  16. RepoID int64 `xorm:"INDEX NOT NULL"`
  17. Owner *User `xorm:"-"`
  18. OwnerID int64 `xorm:"INDEX NOT NULL"`
  19. Path string `xorm:"TEXT"`
  20. Created time.Time `xorm:"created"`
  21. }
  22. // BeforeInsert is invoked from XORM before inserting an object of this type.
  23. func (l *LFSLock) BeforeInsert() {
  24. l.OwnerID = l.Owner.ID
  25. l.Path = cleanPath(l.Path)
  26. }
  27. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  28. func (l *LFSLock) AfterLoad() {
  29. l.Owner, _ = GetUserByID(l.OwnerID)
  30. }
  31. func cleanPath(p string) string {
  32. return strings.ToLower(path.Clean(p))
  33. }
  34. // APIFormat convert a Release to lfs.LFSLock
  35. func (l *LFSLock) APIFormat() *api.LFSLock {
  36. return &api.LFSLock{
  37. ID: strconv.FormatInt(l.ID, 10),
  38. Path: l.Path,
  39. LockedAt: l.Created,
  40. Owner: &api.LFSLockOwner{
  41. Name: l.Owner.DisplayName(),
  42. },
  43. }
  44. }
  45. // CreateLFSLock creates a new lock.
  46. func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
  47. err := CheckLFSAccessForRepo(lock.Owner, lock.RepoID, "create")
  48. if err != nil {
  49. return nil, err
  50. }
  51. l, err := GetLFSLock(lock.RepoID, lock.Path)
  52. if err == nil {
  53. return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path}
  54. }
  55. if !IsErrLFSLockNotExist(err) {
  56. return nil, err
  57. }
  58. _, err = x.InsertOne(lock)
  59. return lock, err
  60. }
  61. // GetLFSLock returns release by given path.
  62. func GetLFSLock(repoID int64, path string) (*LFSLock, error) {
  63. path = cleanPath(path)
  64. rel := &LFSLock{RepoID: repoID, Path: path}
  65. has, err := x.Get(rel)
  66. if err != nil {
  67. return nil, err
  68. }
  69. if !has {
  70. return nil, ErrLFSLockNotExist{0, repoID, path}
  71. }
  72. return rel, nil
  73. }
  74. // GetLFSLockByID returns release by given id.
  75. func GetLFSLockByID(id int64) (*LFSLock, error) {
  76. lock := new(LFSLock)
  77. has, err := x.ID(id).Get(lock)
  78. if err != nil {
  79. return nil, err
  80. } else if !has {
  81. return nil, ErrLFSLockNotExist{id, 0, ""}
  82. }
  83. return lock, nil
  84. }
  85. // GetLFSLockByRepoID returns a list of locks of repository.
  86. func GetLFSLockByRepoID(repoID int64) (locks []*LFSLock, err error) {
  87. err = x.Where("repo_id = ?", repoID).Find(&locks)
  88. return
  89. }
  90. // DeleteLFSLockByID deletes a lock by given ID.
  91. func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) {
  92. lock, err := GetLFSLockByID(id)
  93. if err != nil {
  94. return nil, err
  95. }
  96. err = CheckLFSAccessForRepo(u, lock.RepoID, "delete")
  97. if err != nil {
  98. return nil, err
  99. }
  100. if !force && u.ID != lock.OwnerID {
  101. return nil, fmt.Errorf("user doesn't own lock and force flag is not set")
  102. }
  103. _, err = x.ID(id).Delete(new(LFSLock))
  104. return lock, err
  105. }
  106. //CheckLFSAccessForRepo check needed access mode base on action
  107. func CheckLFSAccessForRepo(u *User, repoID int64, action string) error {
  108. if u == nil {
  109. return ErrLFSLockUnauthorizedAction{repoID, "undefined", action}
  110. }
  111. mode := AccessModeRead
  112. if action == "create" || action == "delete" || action == "verify" {
  113. mode = AccessModeWrite
  114. }
  115. repo, err := GetRepositoryByID(repoID)
  116. if err != nil {
  117. return err
  118. }
  119. has, err := HasAccess(u.ID, repo, mode)
  120. if err != nil {
  121. return err
  122. } else if !has {
  123. return ErrLFSLockUnauthorizedAction{repo.ID, u.DisplayName(), action}
  124. }
  125. return nil
  126. }