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.

admin.go 3.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2020 The Gitea Authors. All rights reserved.
  3. // Use of this source code is governed by a MIT-style
  4. // license that can be found in the LICENSE file.
  5. package models
  6. import (
  7. "fmt"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/storage"
  10. "code.gitea.io/gitea/modules/timeutil"
  11. "code.gitea.io/gitea/modules/util"
  12. )
  13. // NoticeType describes the notice type
  14. type NoticeType int
  15. const (
  16. // NoticeRepository type
  17. NoticeRepository NoticeType = iota + 1
  18. // NoticeTask type
  19. NoticeTask
  20. )
  21. // Notice represents a system notice for admin.
  22. type Notice struct {
  23. ID int64 `xorm:"pk autoincr"`
  24. Type NoticeType
  25. Description string `xorm:"TEXT"`
  26. CreatedUnix timeutil.TimeStamp `xorm:"INDEX created"`
  27. }
  28. // TrStr returns a translation format string.
  29. func (n *Notice) TrStr() string {
  30. return fmt.Sprintf("admin.notices.type_%d", n.Type)
  31. }
  32. // CreateNotice creates new system notice.
  33. func CreateNotice(tp NoticeType, desc string, args ...interface{}) error {
  34. return createNotice(x, tp, desc, args...)
  35. }
  36. func createNotice(e Engine, tp NoticeType, desc string, args ...interface{}) error {
  37. if len(args) > 0 {
  38. desc = fmt.Sprintf(desc, args...)
  39. }
  40. n := &Notice{
  41. Type: tp,
  42. Description: desc,
  43. }
  44. _, err := e.Insert(n)
  45. return err
  46. }
  47. // CreateRepositoryNotice creates new system notice with type NoticeRepository.
  48. func CreateRepositoryNotice(desc string, args ...interface{}) error {
  49. return createNotice(x, NoticeRepository, desc, args...)
  50. }
  51. // RemoveAllWithNotice removes all directories in given path and
  52. // creates a system notice when error occurs.
  53. func RemoveAllWithNotice(title, path string) {
  54. removeAllWithNotice(x, title, path)
  55. }
  56. // RemoveStorageWithNotice removes a file from the storage and
  57. // creates a system notice when error occurs.
  58. func RemoveStorageWithNotice(bucket storage.ObjectStorage, title, path string) {
  59. removeStorageWithNotice(x, bucket, title, path)
  60. }
  61. func removeStorageWithNotice(e Engine, bucket storage.ObjectStorage, title, path string) {
  62. if err := bucket.Delete(path); err != nil {
  63. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  64. log.Warn(title+" [%s]: %v", path, err)
  65. if err = createNotice(e, NoticeRepository, desc); err != nil {
  66. log.Error("CreateRepositoryNotice: %v", err)
  67. }
  68. }
  69. }
  70. func removeAllWithNotice(e Engine, title, path string) {
  71. if err := util.RemoveAll(path); err != nil {
  72. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  73. log.Warn(title+" [%s]: %v", path, err)
  74. if err = createNotice(e, NoticeRepository, desc); err != nil {
  75. log.Error("CreateRepositoryNotice: %v", err)
  76. }
  77. }
  78. }
  79. // CountNotices returns number of notices.
  80. func CountNotices() int64 {
  81. count, _ := x.Count(new(Notice))
  82. return count
  83. }
  84. // Notices returns notices in given page.
  85. func Notices(page, pageSize int) ([]*Notice, error) {
  86. notices := make([]*Notice, 0, pageSize)
  87. return notices, x.
  88. Limit(pageSize, (page-1)*pageSize).
  89. Desc("id").
  90. Find(&notices)
  91. }
  92. // DeleteNotice deletes a system notice by given ID.
  93. func DeleteNotice(id int64) error {
  94. _, err := x.ID(id).Delete(new(Notice))
  95. return err
  96. }
  97. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  98. func DeleteNotices(start, end int64) error {
  99. sess := x.Where("id >= ?", start)
  100. if end > 0 {
  101. sess.And("id <= ?", end)
  102. }
  103. _, err := sess.Delete(new(Notice))
  104. return err
  105. }
  106. // DeleteNoticesByIDs deletes notices by given IDs.
  107. func DeleteNoticesByIDs(ids []int64) error {
  108. if len(ids) == 0 {
  109. return nil
  110. }
  111. _, err := x.
  112. In("id", ids).
  113. Delete(new(Notice))
  114. return err
  115. }
  116. // GetAdminUser returns the first administrator
  117. func GetAdminUser() (*User, error) {
  118. var admin User
  119. has, err := x.Where("is_admin=?", true).Get(&admin)
  120. if err != nil {
  121. return nil, err
  122. } else if !has {
  123. return nil, ErrUserNotExist{}
  124. }
  125. return &admin, nil
  126. }