本站源代码
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.

152 lines
3.7KB

  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. "code.gitea.io/gitea/modules/log"
  12. api "code.gitea.io/gitea/modules/structs"
  13. "xorm.io/xorm"
  14. )
  15. // LFSLock represents a git lfs lock of repository.
  16. type LFSLock struct {
  17. ID int64 `xorm:"pk autoincr"`
  18. Repo *Repository `xorm:"-"`
  19. RepoID int64 `xorm:"INDEX NOT NULL"`
  20. Owner *User `xorm:"-"`
  21. OwnerID int64 `xorm:"INDEX NOT NULL"`
  22. Path string `xorm:"TEXT"`
  23. Created time.Time `xorm:"created"`
  24. }
  25. // BeforeInsert is invoked from XORM before inserting an object of this type.
  26. func (l *LFSLock) BeforeInsert() {
  27. l.OwnerID = l.Owner.ID
  28. l.RepoID = l.Repo.ID
  29. l.Path = cleanPath(l.Path)
  30. }
  31. // AfterLoad is invoked from XORM after setting the values of all fields of this object.
  32. func (l *LFSLock) AfterLoad(session *xorm.Session) {
  33. var err error
  34. l.Owner, err = getUserByID(session, l.OwnerID)
  35. if err != nil {
  36. log.Error("LFS lock AfterLoad failed OwnerId[%d] not found: %v", l.OwnerID, err)
  37. }
  38. l.Repo, err = getRepositoryByID(session, l.RepoID)
  39. if err != nil {
  40. log.Error("LFS lock AfterLoad failed RepoId[%d] not found: %v", l.RepoID, err)
  41. }
  42. }
  43. func cleanPath(p string) string {
  44. return path.Clean(p)
  45. }
  46. // APIFormat convert a Release to lfs.LFSLock
  47. func (l *LFSLock) APIFormat() *api.LFSLock {
  48. return &api.LFSLock{
  49. ID: strconv.FormatInt(l.ID, 10),
  50. Path: l.Path,
  51. LockedAt: l.Created.Round(time.Second),
  52. Owner: &api.LFSLockOwner{
  53. Name: l.Owner.DisplayName(),
  54. },
  55. }
  56. }
  57. // CreateLFSLock creates a new lock.
  58. func CreateLFSLock(lock *LFSLock) (*LFSLock, error) {
  59. err := CheckLFSAccessForRepo(lock.Owner, lock.Repo, AccessModeWrite)
  60. if err != nil {
  61. return nil, err
  62. }
  63. l, err := GetLFSLock(lock.Repo, lock.Path)
  64. if err == nil {
  65. return l, ErrLFSLockAlreadyExist{lock.RepoID, lock.Path}
  66. }
  67. if !IsErrLFSLockNotExist(err) {
  68. return nil, err
  69. }
  70. _, err = x.InsertOne(lock)
  71. return lock, err
  72. }
  73. // GetLFSLock returns release by given path.
  74. func GetLFSLock(repo *Repository, path string) (*LFSLock, error) {
  75. path = cleanPath(path)
  76. rel := &LFSLock{RepoID: repo.ID}
  77. has, err := x.Where("lower(path) = ?", strings.ToLower(path)).Get(rel)
  78. if err != nil {
  79. return nil, err
  80. }
  81. if !has {
  82. return nil, ErrLFSLockNotExist{0, repo.ID, path}
  83. }
  84. return rel, nil
  85. }
  86. // GetLFSLockByID returns release by given id.
  87. func GetLFSLockByID(id int64) (*LFSLock, error) {
  88. lock := new(LFSLock)
  89. has, err := x.ID(id).Get(lock)
  90. if err != nil {
  91. return nil, err
  92. } else if !has {
  93. return nil, ErrLFSLockNotExist{id, 0, ""}
  94. }
  95. return lock, nil
  96. }
  97. // GetLFSLockByRepoID returns a list of locks of repository.
  98. func GetLFSLockByRepoID(repoID int64) (locks []*LFSLock, err error) {
  99. err = x.Where("repo_id = ?", repoID).Find(&locks)
  100. return
  101. }
  102. // DeleteLFSLockByID deletes a lock by given ID.
  103. func DeleteLFSLockByID(id int64, u *User, force bool) (*LFSLock, error) {
  104. lock, err := GetLFSLockByID(id)
  105. if err != nil {
  106. return nil, err
  107. }
  108. err = CheckLFSAccessForRepo(u, lock.Repo, AccessModeWrite)
  109. if err != nil {
  110. return nil, err
  111. }
  112. if !force && u.ID != lock.OwnerID {
  113. return nil, fmt.Errorf("user doesn't own lock and force flag is not set")
  114. }
  115. _, err = x.ID(id).Delete(new(LFSLock))
  116. return lock, err
  117. }
  118. //CheckLFSAccessForRepo check needed access mode base on action
  119. func CheckLFSAccessForRepo(u *User, repo *Repository, mode AccessMode) error {
  120. if u == nil {
  121. return ErrLFSUnauthorizedAction{repo.ID, "undefined", mode}
  122. }
  123. perm, err := GetUserRepoPermission(repo, u)
  124. if err != nil {
  125. return err
  126. }
  127. if !perm.CanAccess(mode, UnitTypeCode) {
  128. return ErrLFSUnauthorizedAction{repo.ID, u.DisplayName(), mode}
  129. }
  130. return nil
  131. }
上海开阖软件有限公司 沪ICP备12045867号-1