audited

package module
v0.0.0-...-b52c9c2 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 28, 2017 License: MIT Imports: 3 Imported by: 22

README

Audited

Audited is used to record the last User who created and/or updated your GORM model. It does so using a CreatedBy and UpdatedBy field. While Audited can be used alone (with GORM), it integrates nicely with QOR to log and display this extra information.

GoDoc

Register GORM Callbacks

Audited utilizes GORM callbacks to log data, so you will need to register callbacks first:

import (
  "github.com/jinzhu/gorm"
  "github.com/qor/audited"
)

db, err := gorm.Open("sqlite3", "demo_db")
audited.RegisterCallbacks(db)
Making a Model Auditable

Embed audited.AuditedModel into your model as an anonymous field to make the model auditable:

type Product struct {
  gorm.Model
  Name string
  audited.AuditedModel
}
Usage
import "github.com/qor/audited"
import "github.com/jinzhu/gorm"

func main() {
  var db, err = gorm.Open("sqlite3", "demo_db")
  var currentUser = User{ID: 100}
  var product Product

  // Create will set product's `CreatedBy`, `UpdatedBy` to `currentUser`'s primary key if `audited:current_user` is a valid model
  db.Set("audited:current_user", currentUser).Create(&product)
  // product.CreatedBy => 100
  // product.UpdatedBy => 100

  // If it is not a valid model, then will set `CreatedBy`, `UpdatedBy` to the passed value
  db.Set("audited:current_user", "admin").Create(&product)
  // product.CreatedBy => "admin"
  // product.UpdatedBy => "admin"

  // When updating a record, it will update the `UpdatedBy` to `audited:current_user`'s value
  db.Set("audited:current_user", "dev").Model(&product).Update("Code", "L1212")
  // product.UpdatedBy => "dev"
}

QOR Demo: http://demo.getqor.com/admin

License

Released under the MIT License.

Documentation

Overview

Package audited is used to log last UpdatedBy and CreatedBy for your models

Github: http://github.com/qor/audited

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RegisterCallbacks

func RegisterCallbacks(db *gorm.DB)

RegisterCallbacks register callback into GORM DB

Types

type AuditedModel

type AuditedModel struct {
	CreatedBy string
	UpdatedBy string
}

AuditedModel make Model Auditable, embed `audited.AuditedModel` into your model as anonymous field to make the model auditable

type User struct {
  gorm.Model
  audited.AuditedModel
}

func (AuditedModel) GetCreatedBy

func (model AuditedModel) GetCreatedBy() string

GetCreatedBy get created by

func (AuditedModel) GetUpdatedBy

func (model AuditedModel) GetUpdatedBy() string

GetUpdatedBy get updated by

func (*AuditedModel) SetCreatedBy

func (model *AuditedModel) SetCreatedBy(createdBy interface{})

SetCreatedBy set created by

func (*AuditedModel) SetUpdatedBy

func (model *AuditedModel) SetUpdatedBy(updatedBy interface{})

SetUpdatedBy set updated by

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL