admin.go 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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. "os"
  8. "os/exec"
  9. "strings"
  10. "time"
  11. "github.com/Unknwon/com"
  12. "github.com/go-xorm/xorm"
  13. "github.com/gogits/gogs/modules/base"
  14. "github.com/gogits/gogs/modules/log"
  15. "github.com/gogits/gogs/modules/setting"
  16. )
  17. type NoticeType int
  18. const (
  19. NOTICE_REPOSITORY NoticeType = iota + 1
  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. Created time.Time `xorm:"-"`
  27. CreatedUnix int64
  28. }
  29. func (n *Notice) BeforeInsert() {
  30. n.CreatedUnix = time.Now().UTC().Unix()
  31. }
  32. func (n *Notice) AfterSet(colName string, _ xorm.Cell) {
  33. switch colName {
  34. case "created_unix":
  35. n.Created = time.Unix(n.CreatedUnix, 0).Local()
  36. }
  37. }
  38. // TrStr returns a translation format string.
  39. func (n *Notice) TrStr() string {
  40. return "admin.notices.type_" + com.ToStr(n.Type)
  41. }
  42. // CreateNotice creates new system notice.
  43. func CreateNotice(tp NoticeType, desc string) error {
  44. n := &Notice{
  45. Type: tp,
  46. Description: desc,
  47. }
  48. _, err := x.Insert(n)
  49. return err
  50. }
  51. // CreateRepositoryNotice creates new system notice with type NOTICE_REPOSITORY.
  52. func CreateRepositoryNotice(desc string) error {
  53. return CreateNotice(NOTICE_REPOSITORY, desc)
  54. }
  55. // RemoveAllWithNotice removes all directories in given path and
  56. // creates a system notice when error occurs.
  57. func RemoveAllWithNotice(title, path string) {
  58. var err error
  59. if setting.IsWindows {
  60. err = exec.Command("cmd", "/C", "rmdir", "/S", "/Q", path).Run()
  61. } else {
  62. err = os.RemoveAll(path)
  63. }
  64. if err != nil {
  65. desc := fmt.Sprintf("%s [%s]: %v", title, path, err)
  66. log.Warn(desc)
  67. if err = CreateRepositoryNotice(desc); err != nil {
  68. log.Error(4, "CreateRepositoryNotice: %v", err)
  69. }
  70. }
  71. }
  72. // CountNotices returns number of notices.
  73. func CountNotices() int64 {
  74. count, _ := x.Count(new(Notice))
  75. return count
  76. }
  77. // Notices returns number of notices in given page.
  78. func Notices(page, pageSize int) ([]*Notice, error) {
  79. notices := make([]*Notice, 0, pageSize)
  80. return notices, x.Limit(pageSize, (page-1)*pageSize).Desc("id").Find(&notices)
  81. }
  82. // DeleteNotice deletes a system notice by given ID.
  83. func DeleteNotice(id int64) error {
  84. _, err := x.Id(id).Delete(new(Notice))
  85. return err
  86. }
  87. // DeleteNotices deletes all notices with ID from start to end (inclusive).
  88. func DeleteNotices(start, end int64) error {
  89. sess := x.Where("id >= ?", start)
  90. if end > 0 {
  91. sess.And("id <= ?", end)
  92. }
  93. _, err := sess.Delete(new(Notice))
  94. return err
  95. }
  96. // DeleteNoticesByIDs deletes notices by given IDs.
  97. func DeleteNoticesByIDs(ids []int64) error {
  98. if len(ids) == 0 {
  99. return nil
  100. }
  101. _, err := x.Where("id IN (" + strings.Join(base.Int64sToStrings(ids), ",") + ")").Delete(new(Notice))
  102. return err
  103. }