Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/qor/audited
Audited is used to log last UpdatedBy and CreatedBy for your models
https://github.com/qor/audited
Last synced: 5 days ago
JSON representation
Audited is used to log last UpdatedBy and CreatedBy for your models
- Host: GitHub
- URL: https://github.com/qor/audited
- Owner: qor
- License: mit
- Created: 2016-01-11T05:42:27.000Z (almost 9 years ago)
- Default Branch: master
- Last Pushed: 2019-04-23T09:05:15.000Z (over 5 years ago)
- Last Synced: 2024-06-18T18:34:12.341Z (5 months ago)
- Language: Go
- Homepage:
- Size: 7.81 KB
- Stars: 23
- Watchers: 15
- Forks: 10
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE.txt
- Audit: audited.go
Awesome Lists containing this project
README
# Audited
Audited is used to record the last User who created and/or updated your [GORM](https://github.com/jinzhu/gorm) model. It does so using a `CreatedBy` and `UpdatedBy` field. While Audited can be used alone (with [GORM](https://github.com/jinzhu/gorm)), it integrates nicely with [QOR](https://github.com/qor/qor) to log and display this extra information.
[![GoDoc](https://godoc.org/github.com/qor/audited?status.svg)](https://godoc.org/github.com/qor/audited)
### Register GORM Callbacks
Audited utilizes [GORM](https://github.com/jinzhu/gorm) callbacks to log data, so you will need to register callbacks first:
```go
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:
```go
type Product struct {
gorm.Model
Name string
audited.AuditedModel
}
```### Usage
```go
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](http://demo.getqor.com/admin)
## License
Released under the [MIT License](http://opensource.org/licenses/MIT).