func SaveOrUpdateMessage(entity models.MessageStruct, enc Encoder, db gorp.SqlExecutor, user sessionauth.User) (int, string) { u := UserById(user.UniqueId().(int), db) if user.IsAuthenticated() && u.UserRole == models.ROLE_ADMIN { entity.Last_modified_by = user.UniqueId().(int) if entity.Id < 1 { err := db.Insert(&entity) if err != nil { checkErr(err, "insert failed") return http.StatusBadRequest, "" } } else { obj, _ := db.Get(models.MessageStruct{}, entity.Id) if obj == nil { // Invalid id, or does not exist return http.StatusBadRequest, "" } _, err := db.Update(&entity) if err != nil { checkErr(err, "update failed") return http.StatusBadRequest, "" } } return http.StatusOK, Must(enc.EncodeOne(entity)) } return http.StatusUnauthorized, "" }
func IsAuthenticated(enc Encoder, user sessionauth.User, db gorp.SqlExecutor) (int, string) { u := UserById(user.UniqueId().(int), db) if user.IsAuthenticated() && u.UserRole == models.ROLE_ADMIN { return http.StatusOK, "" } return http.StatusUnauthorized, Must(enc.EncodeOne(user)) }
func GetAuthenticatedUser(enc Encoder, user sessionauth.User, db gorp.SqlExecutor) (int, string) { if user.IsAuthenticated() { authUser := UserById(user.UniqueId().(int), db) return http.StatusOK, Must(enc.EncodeOne(authUser)) } return http.StatusUnauthorized, Must(enc.EncodeOne(user)) }
func GetMessagesStats(enc Encoder, db gorp.SqlExecutor, user sessionauth.User) (int, string) { u := UserById(user.UniqueId().(int), db) if user.IsAuthenticated() && u.UserRole == models.ROLE_ADMIN { var messages []models.Messages _, err := db.Select(&messages, SQL_MESSAGES_ALL) if err != nil { checkErr(err, "select failed") return http.StatusBadRequest, "" } return http.StatusOK, Must(enc.Encode(messagesToIfaceM(messages)...)) } return http.StatusUnauthorized, "" }
func GetUserById(enc Encoder, db gorp.SqlExecutor, user sessionauth.User, parms martini.Params) (int, string) { u := UserById(user.UniqueId().(int), db) if user.IsAuthenticated() && u.UserRole == models.ROLE_ADMIN { id, err := strconv.Atoi(parms["id"]) if err != nil { return http.StatusNotFound, "" } entity := UserById(id, db) return http.StatusOK, Must(enc.EncodeOne(entity)) } return http.StatusUnauthorized, "" }
func GetWarningDetail(enc Encoder, db gorp.SqlExecutor, user sessionauth.User, parms martini.Params) (int, string) { u := UserById(user.UniqueId().(int), db) if user.IsAuthenticated() && u.UserRole == models.ROLE_ADMIN { id, err := strconv.Atoi(parms["id"]) obj, _ := db.Get(models.Warning{}, id) if err != nil || obj == nil { checkErr(err, "GET WARNING DETAIL FAILED") // Invalid id, or does not exist return http.StatusNotFound, "" } entity := obj.(*models.Warning) return http.StatusOK, Must(enc.EncodeOne(entity)) } return http.StatusUnauthorized, "" }
// count all warnings registered func WarnaCounter(enc Encoder, db gorp.SqlExecutor, user sessionauth.User) (int, string) { counts := models.CountWarning{} u := UserById(user.UniqueId().(int), db) if user.IsAuthenticated() && u.UserRole == models.ROLE_ADMIN { err := db.SelectOne(&counts, SQL_WARN_COUNT) checkErr(err, "COUNT SENT WARNINGS ERROR") if err == nil { return http.StatusOK, Must(enc.EncodeOne(counts)) } else { return http.StatusBadRequest, "" } } return http.StatusUnauthorized, "" }
func DoLogout(enc Encoder, session sessions.Session, user sessionauth.User, db gorp.SqlExecutor) (int, string) { status := &models.DefaultStruct{ Id: http.StatusOK, Name: messages.GetLocaleMessage("en", "MSG_LOGIN_REQUIRED"), Lang_key: "en", } if user.IsAuthenticated() { sessionauth.Logout(session, user) session.Clear() status.Name = messages.GetLocaleMessage("en", "MSG_SUCCESSFUL_LOGOUT") } updateUser := UserById(user.UniqueId().(int), db) updateUser.Authenticated = false db.Update(updateUser) return http.StatusOK, Must(enc.EncodeOne(status)) }
func ListWarnings(entity models.Warn, enc Encoder, user sessionauth.User, db gorp.SqlExecutor) (int, string) { u := UserById(user.UniqueId().(int), db) if user.IsAuthenticated() && u.UserRole == models.ROLE_ADMIN { sql := "SELECT w.id, msg.name AS message, ct.name AS contact_type, w.contact, w.sent, w.created_date FROM warnings AS w " sql += "INNER JOIN messages AS msg ON (msg.id = w.id_message) " sql += "INNER JOIN contact_types AS ct ON (ct.id = w.id_contact_type) " sql += "ORDER BY w.created_date DESC " var warns []models.Warn _, err := db.Select(&warns, sql) checkErr(err, "SELECT ALL WARNINGS ERROR") if err != nil { return http.StatusBadRequest, "" } return http.StatusOK, Must(enc.Encode(warnsToIface(warns)...)) } return http.StatusUnauthorized, "" }