github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/store/sqlstore/audit_store.go (about)

     1  // Copyright (c) 2015-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/vnforks/kid/v5/model"
    10  	"github.com/vnforks/kid/v5/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) *model.AppError {
    38  	audit.Id = model.NewId()
    39  	audit.CreateAt = model.GetMillis()
    40  
    41  	if err := s.GetMaster().Insert(audit); err != nil {
    42  		return model.NewAppError("SqlAuditStore.Save", "store.sql_audit.save.saving.app_error", nil, "user_id="+audit.UserId+" action="+audit.Action, http.StatusInternalServerError)
    43  	}
    44  	return nil
    45  }
    46  
    47  func (s SqlAuditStore) Get(user_id string, offset int, limit int) (model.Audits, *model.AppError) {
    48  	if limit > 1000 {
    49  		return nil, model.NewAppError("SqlAuditStore.Get", "store.sql_audit.get.limit.app_error", nil, "user_id="+user_id, http.StatusBadRequest)
    50  	}
    51  
    52  	query := "SELECT * FROM Audits"
    53  
    54  	if len(user_id) != 0 {
    55  		query += " WHERE UserId = :user_id"
    56  	}
    57  
    58  	query += " ORDER BY CreateAt DESC LIMIT :limit OFFSET :offset"
    59  
    60  	var audits model.Audits
    61  	if _, err := s.GetReplica().Select(&audits, query, map[string]interface{}{"user_id": user_id, "limit": limit, "offset": offset}); err != nil {
    62  		return nil, model.NewAppError("SqlAuditStore.Get", "store.sql_audit.get.finding.app_error", nil, "user_id="+user_id, http.StatusInternalServerError)
    63  	}
    64  	return audits, nil
    65  }
    66  
    67  func (s SqlAuditStore) PermanentDeleteByUser(userId string) *model.AppError {
    68  	if _, err := s.GetMaster().Exec("DELETE FROM Audits WHERE UserId = :userId",
    69  		map[string]interface{}{"userId": userId}); err != nil {
    70  		return model.NewAppError("SqlAuditStore.Delete", "store.sql_audit.permanent_delete_by_user.app_error", nil, "user_id="+userId, http.StatusInternalServerError)
    71  	}
    72  	return nil
    73  }