This commit is contained in:
@@ -75,6 +75,7 @@ func DeleteUser(c *gin.Context) {
|
||||
|
||||
func AddUser(c *gin.Context) {
|
||||
var input AddUserInput
|
||||
var RequestingUserId int
|
||||
|
||||
if err := c.ShouldBindJSON(&input); err != nil {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
|
||||
@@ -91,6 +92,13 @@ func AddUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
if val, ok := c.Get("user-id"); !ok {
|
||||
c.JSON(http.StatusBadRequest, gin.H{"error": "error determining user"})
|
||||
return
|
||||
} else {
|
||||
RequestingUserId = val.(int)
|
||||
}
|
||||
|
||||
u := models.User{}
|
||||
u.UserName = input.UserName
|
||||
u.Password = input.Password
|
||||
@@ -155,6 +163,13 @@ func AddUser(c *gin.Context) {
|
||||
return
|
||||
}
|
||||
|
||||
// Create audit record
|
||||
a := models.Audit{
|
||||
UserId: RequestingUserId,
|
||||
EventText: fmt.Sprintf("Created User Id %d", u.UserId),
|
||||
}
|
||||
a.AuditAdd()
|
||||
|
||||
c.JSON(http.StatusOK, gin.H{"message": "user registration success", "data": u})
|
||||
}
|
||||
|
||||
|
@@ -1,3 +1,39 @@
|
||||
package models
|
||||
|
||||
import (
|
||||
"log"
|
||||
"time"
|
||||
)
|
||||
|
||||
// Define audit functions
|
||||
type Audit struct {
|
||||
AuditId int `db:"AuditId" json:"auditId"`
|
||||
UserId int `db:"UserId" json:"userId"`
|
||||
SecretId int `db:"SecretId" json:"secretId"`
|
||||
EventText string `db:"EventText" json:"eventText"`
|
||||
Timestamp time.Time `db:"Timestamp" json:"Timestamp"`
|
||||
}
|
||||
|
||||
// AuditAdd adds a new audit record to the database
|
||||
func (a *Audit) AuditAdd() (*Audit, error) {
|
||||
var err error
|
||||
|
||||
// Populate timestamp field if not already set
|
||||
if a.Timestamp.IsZero() {
|
||||
a.Timestamp = time.Now()
|
||||
}
|
||||
|
||||
result, err := db.NamedExec(("INSERT INTO audit (UserId, SecretId, EventText, Timestamp) VALUES (:UserId, :SecretId, :EventText, :Timestamp);"), a)
|
||||
|
||||
if err != nil {
|
||||
log.Printf("AuditAdd error executing sql record : '%s'\n", err)
|
||||
return &Audit{}, err
|
||||
} else {
|
||||
affected, _ := result.RowsAffected()
|
||||
id, _ := result.LastInsertId()
|
||||
a.AuditId = int(id)
|
||||
log.Printf("AuditAdd insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
||||
}
|
||||
|
||||
return a, nil
|
||||
}
|
||||
|
@@ -20,16 +20,6 @@ const (
|
||||
sqlFile = "smt.db"
|
||||
)
|
||||
|
||||
/*
|
||||
const createRoles string = `
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
RoleId INTEGER PRIMARY KEY ASC,
|
||||
RoleName VARCHAR,
|
||||
ReadOnly BOOLEAN
|
||||
);
|
||||
`
|
||||
*/
|
||||
|
||||
const createUsers string = `
|
||||
CREATE TABLE IF NOT EXISTS users (
|
||||
UserId INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
@@ -90,10 +80,11 @@ const createSchema string = `
|
||||
|
||||
const createAudit string = `
|
||||
CREATE TABLE IF NOT EXISTS audit (
|
||||
EventId INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
UserName VARCHAR,
|
||||
AuditId INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||
UserId INTEGER DEFAULT 0,
|
||||
SecretId INTEGER DEFAULT 0,
|
||||
EventText VARCHAR,
|
||||
EventTime INTEGER
|
||||
Timestamp datetime
|
||||
);
|
||||
`
|
||||
|
||||
|
@@ -62,6 +62,7 @@ func (u *User) SaveUser() (*User, error) {
|
||||
} else {
|
||||
affected, _ := result.RowsAffected()
|
||||
id, _ := result.LastInsertId()
|
||||
u.UserId = int(id)
|
||||
log.Printf("SaveUser insert returned result id '%d' affecting %d row(s).\n", id, affected)
|
||||
}
|
||||
} else {
|
||||
|
Reference in New Issue
Block a user