diff --git a/models/user.go b/models/user.go index 5eead47..15ecbc1 100644 --- a/models/user.go +++ b/models/user.go @@ -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) +} diff --git a/routes/api/v1/author_delete.go b/routes/api/v1/author_delete.go index 57175ca..5049902 100644 --- a/routes/api/v1/author_delete.go +++ b/routes/api/v1/author_delete.go @@ -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"}) } diff --git a/routes/api/v1/authors_add_update.go b/routes/api/v1/authors_add_update.go index 6c162d8..fae51ff 100644 --- a/routes/api/v1/authors_add_update.go +++ b/routes/api/v1/authors_add_update.go @@ -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) } diff --git a/routes/api/v1/book_delete.go b/routes/api/v1/book_delete.go index 7fa1c09..19882e0 100644 --- a/routes/api/v1/book_delete.go +++ b/routes/api/v1/book_delete.go @@ -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"}) } diff --git a/routes/api/v1/books_add_update.go b/routes/api/v1/books_add_update.go index 6bb5ded..e84fcd9 100644 --- a/routes/api/v1/books_add_update.go +++ b/routes/api/v1/books_add_update.go @@ -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) } diff --git a/routes/api/v1/items_add_update.go b/routes/api/v1/items_add_update.go index 1b8062e..7b9bd0c 100644 --- a/routes/api/v1/items_add_update.go +++ b/routes/api/v1/items_add_update.go @@ -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) } diff --git a/routes/api/v1/items_delete.go b/routes/api/v1/items_delete.go index ac63a8b..51751c3 100644 --- a/routes/api/v1/items_delete.go +++ b/routes/api/v1/items_delete.go @@ -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"}) } diff --git a/routes/api/v1/publishers_add_update.go b/routes/api/v1/publishers_add_update.go index 7d74876..406ea04 100644 --- a/routes/api/v1/publishers_add_update.go +++ b/routes/api/v1/publishers_add_update.go @@ -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) } diff --git a/routes/api/v1/publishers_delete.go b/routes/api/v1/publishers_delete.go index 15b3306..0251fc8 100644 --- a/routes/api/v1/publishers_delete.go +++ b/routes/api/v1/publishers_delete.go @@ -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"}) }