Implemented logging of user actions

This commit is contained in:
kolaente 2017-12-05 14:46:54 +01:00
parent d13f543c5e
commit 48b309cd51
Signed by: konrad
GPG Key ID: F40E70337AB24C9B
9 changed files with 94 additions and 0 deletions

View File

@ -2,6 +2,8 @@ package models
import (
"fmt"
"github.com/dgrijalva/jwt-go"
"github.com/labstack/echo"
"golang.org/x/crypto/bcrypt"
)
@ -22,6 +24,15 @@ type User struct {
Updated int64 `xorm:"updated"`
}
// UserLog logs user actions
type UserLog struct {
ID int64 `xorm:"int(11) autoincr not null unique pk"`
UserID int64 `xorm:"int(11)"`
Log string `xorm:"varchar(250)"`
ItemID int64 `xorm:"int(11)"`
Time int64 `xorm:"created"`
}
// TableName returns the table name for users
func (User) TableName() string {
return "users"
@ -102,3 +113,38 @@ func CheckUserCredentials(u *UserLogin) (User, error) {
return user, nil
}
// GetCurrentUser returns the current user based on its jwt token
func GetCurrentUser(c echo.Context) (user User, err error) {
jwtinf := c.Get("user").(*jwt.Token)
claims := jwtinf.Claims.(jwt.MapClaims)
userID, ok := claims["id"].(float64)
if !ok {
return user, fmt.Errorf("Error getting UserID")
}
user = User{
ID: int64(userID),
Name: claims["name"].(string),
Email: claims["email"].(string),
Username: claims["username"].(string),
}
return
}
// LogAction logs a user action
func logAction(action string, user User, itemID int64) (err error) {
_, err = x.Insert(UserLog{Log: action, UserID: user.ID, ItemID: itemID})
return
}
// LogAction logs a user action
func LogAction(action string, itemID int64, c echo.Context) (err error) {
// Get the user options
user, err := GetCurrentUser(c)
if err != nil {
return err
}
return logAction(action, user, itemID)
}

View File

@ -37,5 +37,11 @@ func AuthorDelete(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete author"})
}
// Log the action
err = models.LogAction("Deleted an author", authorID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -59,5 +59,11 @@ func AuthorAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}
// Log the action
err = models.LogAction("Added or updated an author", newAuthor.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
}
return c.JSON(http.StatusOK, newAuthor)
}

View File

@ -37,5 +37,11 @@ func BookDelete(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete book"})
}
// Log the action
err = models.LogAction("Deleted a book", bookID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -59,5 +59,11 @@ func BookAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}
// Log the action
err = models.LogAction("Added or updated a book", newBook.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
}
return c.JSON(http.StatusOK, newBook)
}

View File

@ -54,5 +54,11 @@ func ItemAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}
// Log the action
err = models.LogAction("Added or updated an item", newItem.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
}
return c.JSON(http.StatusOK, newItem)
}

View File

@ -37,5 +37,11 @@ func ItemDelete(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete item"})
}
// Log the action
err = models.LogAction("Deleted an item", itemID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}

View File

@ -54,5 +54,11 @@ func PublisherAddOrUpdate(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Error"})
}
// Log the action
err = models.LogAction("Added or updated a publisher",newPublisher.ID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
}
return c.JSON(http.StatusOK, newPublisher)
}

View File

@ -37,5 +37,11 @@ func PublisherDelete(c echo.Context) error {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not delete publisher"})
}
// Log the action
err = models.LogAction("Deleted a publisher", publisherID, c)
if err != nil {
return c.JSON(http.StatusInternalServerError, models.Message{"Could not log"})
}
return c.JSON(http.StatusOK, models.Message{"success"})
}