34 lines
992 B
Go
34 lines
992 B
Go
package handlers
|
|
|
|
import (
|
|
"authentication/helper"
|
|
"authentication/services"
|
|
"fmt"
|
|
"net/http"
|
|
"net/url"
|
|
)
|
|
|
|
func accessLog(w http.ResponseWriter, r *http.Request, user *string, actType int, fieldUpdated interface{}) {
|
|
email, err := helper.ExtractEmailFromToken(r.Header.Get(Authorization))
|
|
if err != nil {
|
|
helper.RespondWithError(w, http.StatusUnauthorized, UnauthorizedAccess)
|
|
return
|
|
}
|
|
userID, err := services.GetUserIDFromEmail(email)
|
|
if err != nil {
|
|
helper.LogError(err, ErrorExtractingMailFromToken)
|
|
helper.RespondWithError(w, http.StatusBadRequest, ErrorExtractingMailFromToken)
|
|
return
|
|
}
|
|
ipAddress := getIPAddress(r)
|
|
err = helper.LogEvent(userID, user, ipAddress, actType, fieldUpdated)
|
|
if err != nil {
|
|
errMsg, err := services.GetActivityMessages(actType)
|
|
if err == nil {
|
|
errMsg = "Perform Action"
|
|
}
|
|
http.Redirect(w, r, fmt.Sprintf(errorFormat, DashboardBaseURL, url.QueryEscape(fmt.Sprintf("Failed to %s", errMsg))), http.StatusSeeOther)
|
|
return
|
|
}
|
|
}
|