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 2.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. // Copyright 2014 The Gogs 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. "time"
  8. "code.gitea.io/gitea/modules/log"
  9. "code.gitea.io/gitea/modules/util"
  10. "github.com/Unknwon/com"
  11. "github.com/go-xorm/xorm"
  12. )
  13. //NoticeType describes the notice type
  14. type NoticeType int
  15. const (
  16. //NoticeRepository type
  17. NoticeRepository NoticeType = iota + 1
  18. )
  19. // Notice represents a system notice for admin.
  20. type Notice struct {
  21. ID int64 `xorm:"pk autoincr"`
  22. Type NoticeType
  23. Description string `xorm:"TEXT"`
  24. Created time.Time `xorm:"-"`
  25. CreatedUnix int64 `xorm:"INDEX created"`
  26. }
  27. // AfterSet is invoked from XORM after setting the value of a field of this object.
  28. func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
  29. switch colName {
  30. case "created_unix":
  31. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  32. }
  33. }
  34. // TrStr returns a translation format string.
  35. func (n *Notice) TrStr() string {
  36. return "admin.notices.type_" + com.ToStr(n.Type)
  37. }
  38. // CreateNotice creates new system notice.
  39. func CreateNotice(tp NoticeType, desc string) error {
  40. return createNotice(x, tp, desc)
  41. }
  42. func createNotice(e Engine, tp NoticeType, desc string) error {
  43. n := &Notice{
  44. Type: tp,
  45. Description: desc,
  46. }
  47. _, err := e.Insert(n)
  48. return err
  49. }
  50. // CreateRepositoryNotice creates new system notice with type NoticeRepository.
  51. func CreateRepositoryNotice(desc string) error {
  52. return createNotice(x, NoticeRepository, desc)
  53. }
  54. // RemoveAllWithNotice removes all directories in given path and
  55. // creates a system notice when error occurs.
  56. func RemoveAllWithNotice(title, path string) {
  57. removeAllWithNotice(x, title, path)
  58. }
  59. func removeAllWithNotice(e Engine, title, path string) {
  60. if err := util.RemoveAll(path); err != nil {
  61. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  62. log.Warn(desc)
  63. if err = createNotice(e, NoticeRepository, desc); err != nil {
  64. log.Error(4, "CreateRepositoryNotice: %v", err)
  65. }
  66. }
  67. }
  68. // CountNotices returns number of notices.
  69. func CountNotices() int64 {
  70. count, _ := x.Count(new(Notice))
  71. return count
  72. }
  73. // Notices returns notices in given page.
  74. func Notices(page, pageSize int) ([]*Notice, error) {
  75. notices := make([]*Notice, 0, pageSize)
  76. return notices, x.
  77. Limit(pageSize, (page-1)*pageSize).
  78. Desc("id").
  79. Find(&notices)
  80. }
  81. // DeleteNotice deletes a system notice by given ID.
  82. func DeleteNotice(id int64) error {
  83. _, err := x.Id(id).Delete(new(Notice))
  84. return err
  85. }
  86. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  87. func DeleteNotices(start, end int64) error {
  88. sess := x.Where("id >= ?", start)
  89. if end > 0 {
  90. sess.And("id <= ?", end)
  91. }
  92. _, err := sess.Delete(new(Notice))
  93. return err
  94. }
  95. // DeleteNoticesByIDs deletes notices by given IDs.
  96. func DeleteNoticesByIDs(ids []int64) error {
  97. if len(ids) == 0 {
  98. return nil
  99. }
  100. _, err := x.
  101. In("id", ids).
  102. Delete(new(Notice))
  103. return err
  104. }