github.com/qichengzx/mattermost-server@v4.5.1-0.20180604164826-2c75247c97d0+incompatible/store/sqlstore/audit_store.go (about) 1 // Copyright (c) 2016-present Mattermost, Inc. All Rights Reserved. 2 // See License.txt for license information. 3 4 package sqlstore 5 6 import ( 7 "net/http" 8 9 "github.com/mattermost/mattermost-server/model" 10 "github.com/mattermost/mattermost-server/store" 11 ) 12 13 type SqlAuditStore struct { 14 SqlStore 15 } 16 17 func NewSqlAuditStore(sqlStore SqlStore) store.AuditStore { 18 s := &SqlAuditStore{sqlStore} 19 20 for _, db := range sqlStore.GetAllConns() { 21 table := db.AddTableWithName(model.Audit{}, "Audits").SetKeys(false, "Id") 22 table.ColMap("Id").SetMaxSize(26) 23 table.ColMap("UserId").SetMaxSize(26) 24 table.ColMap("Action").SetMaxSize(512) 25 table.ColMap("ExtraInfo").SetMaxSize(1024) 26 table.ColMap("IpAddress").SetMaxSize(64) 27 table.ColMap("SessionId").SetMaxSize(26) 28 } 29 30 return s 31 } 32 33 func (s SqlAuditStore) CreateIndexesIfNotExists() { 34 s.CreateIndexIfNotExists("idx_audits_user_id", "Audits", "UserId") 35 } 36 37 func (s SqlAuditStore) Save(audit *model.Audit) store.StoreChannel { 38 return store.Do(func(result *store.StoreResult) { 39 audit.Id = model.NewId() 40 audit.CreateAt = model.GetMillis() 41 42 if err := s.GetMaster().Insert(audit); err != nil { 43 result.Err = model.NewAppError("SqlAuditStore.Save", "store.sql_audit.save.saving.app_error", nil, "user_id="+audit.UserId+" action="+audit.Action, http.StatusInternalServerError) 44 } 45 }) 46 } 47 48 func (s SqlAuditStore) Get(user_id string, offset int, limit int) store.StoreChannel { 49 return store.Do(func(result *store.StoreResult) { 50 if limit > 1000 { 51 limit = 1000 52 result.Err = model.NewAppError("SqlAuditStore.Get", "store.sql_audit.get.limit.app_error", nil, "user_id="+user_id, http.StatusBadRequest) 53 return 54 } 55 56 query := "SELECT * FROM Audits" 57 58 if len(user_id) != 0 { 59 query += " WHERE UserId = :user_id" 60 } 61 62 query += " ORDER BY CreateAt DESC LIMIT :limit OFFSET :offset" 63 64 var audits model.Audits 65 if _, err := s.GetReplica().Select(&audits, query, map[string]interface{}{"user_id": user_id, "limit": limit, "offset": offset}); err != nil { 66 result.Err = model.NewAppError("SqlAuditStore.Get", "store.sql_audit.get.finding.app_error", nil, "user_id="+user_id, http.StatusInternalServerError) 67 } else { 68 result.Data = audits 69 } 70 }) 71 } 72 73 func (s SqlAuditStore) PermanentDeleteByUser(userId string) store.StoreChannel { 74 return store.Do(func(result *store.StoreResult) { 75 if _, err := s.GetMaster().Exec("DELETE FROM Audits WHERE UserId = :userId", 76 map[string]interface{}{"userId": userId}); err != nil { 77 result.Err = model.NewAppError("SqlAuditStore.Delete", "store.sql_audit.permanent_delete_by_user.app_error", nil, "user_id="+userId, http.StatusInternalServerError) 78 } 79 }) 80 } 81 82 func (s SqlAuditStore) PermanentDeleteBatch(endTime int64, limit int64) store.StoreChannel { 83 return store.Do(func(result *store.StoreResult) { 84 var query string 85 if s.DriverName() == "postgres" { 86 query = "DELETE from Audits WHERE Id = any (array (SELECT Id FROM Audits WHERE CreateAt < :EndTime LIMIT :Limit))" 87 } else { 88 query = "DELETE from Audits WHERE CreateAt < :EndTime LIMIT :Limit" 89 } 90 91 sqlResult, err := s.GetMaster().Exec(query, map[string]interface{}{"EndTime": endTime, "Limit": limit}) 92 if err != nil { 93 result.Err = model.NewAppError("SqlAuditStore.PermanentDeleteBatch", "store.sql_audit.permanent_delete_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError) 94 } else { 95 rowsAffected, err1 := sqlResult.RowsAffected() 96 if err1 != nil { 97 result.Err = model.NewAppError("SqlAuditStore.PermanentDeleteBatch", "store.sql_audit.permanent_delete_batch.app_error", nil, ""+err.Error(), http.StatusInternalServerError) 98 result.Data = int64(0) 99 } else { 100 result.Data = rowsAffected 101 } 102 } 103 }) 104 }