2016-10-17 14:07:17 +02:00
|
|
|
package host
|
|
|
|
|
|
|
|
import (
|
|
|
|
"net/http"
|
2016-10-17 19:21:17 +02:00
|
|
|
"strconv"
|
2016-10-17 14:07:17 +02:00
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
"goji.io/pat"
|
2016-10-17 14:07:17 +02:00
|
|
|
"golang.org/x/net/context"
|
|
|
|
|
|
|
|
libapi "dev.sum7.eu/sum7/warehost/lib/api"
|
2016-10-18 19:28:57 +02:00
|
|
|
system "dev.sum7.eu/sum7/warehost/system"
|
2016-10-17 14:07:17 +02:00
|
|
|
)
|
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
func getDatabase(ctx context.Context, w http.ResponseWriter) (database Database, returnerr *libapi.ErrorResult) {
|
2016-10-18 19:28:57 +02:00
|
|
|
login := ctx.Value("login").(*system.Login)
|
2016-10-17 19:21:17 +02:00
|
|
|
profil := ctx.Value("profil").(*Profil)
|
|
|
|
id, err := strconv.ParseInt(pat.Param(ctx, "databaseid"), 10, 64)
|
|
|
|
if err != nil {
|
|
|
|
returnerr = &libapi.ErrorResult{
|
|
|
|
Message: "Internal Request Error",
|
|
|
|
}
|
|
|
|
w.WriteHeader(http.StatusBadRequest)
|
|
|
|
return
|
|
|
|
}
|
2016-10-18 19:28:57 +02:00
|
|
|
|
|
|
|
if login.Superadmin {
|
|
|
|
dbconnection.Where("id = ?", id).Find(&database)
|
|
|
|
} else {
|
|
|
|
dbconnection.Where(map[string]int64{"id": id, "profil": profil.ID}).Find(&database)
|
|
|
|
}
|
|
|
|
|
2016-10-17 19:21:17 +02:00
|
|
|
if database.ID <= 0 {
|
|
|
|
returnerr = &libapi.ErrorResult{Fields: []string{"database"}, Message: "not found"}
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2016-10-17 14:07:17 +02:00
|
|
|
func databaseList(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
2016-10-18 19:28:57 +02:00
|
|
|
login := ctx.Value("login").(*system.Login)
|
2016-10-17 14:07:17 +02:00
|
|
|
profil := ctx.Value("profil").(*Profil)
|
|
|
|
returndata = false
|
|
|
|
logger := log.GetLog(r, "databaselist")
|
|
|
|
var database []*Database
|
2016-10-18 19:28:57 +02:00
|
|
|
if login.Superadmin && r.URL.Query().Get("filter") == "all" {
|
2016-10-18 20:26:17 +02:00
|
|
|
dbconnection.Preload("Profil").Preload("Profil.Login").Find(&database)
|
2016-10-18 19:28:57 +02:00
|
|
|
} else {
|
2016-10-18 20:26:17 +02:00
|
|
|
dbconnection.Where("profil = ?", profil.ID).Preload("Profil").Find(&database)
|
2016-10-18 19:28:57 +02:00
|
|
|
}
|
2016-10-17 14:07:17 +02:00
|
|
|
logger.Info("done")
|
|
|
|
returndata = database
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func databaseAdd(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
|
|
profil := ctx.Value("profil").(*Profil)
|
|
|
|
returndata = false
|
|
|
|
logger := log.GetLog(r, "databaseadd")
|
|
|
|
|
|
|
|
var databaseRequest Database
|
|
|
|
returnerr = libapi.JSONDecoder(r.Body, &databaseRequest, w, logger)
|
|
|
|
if returnerr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
database := &Database{
|
|
|
|
ProfilID: profil.ID,
|
|
|
|
Password: databaseRequest.Password,
|
|
|
|
Comment: databaseRequest.Comment,
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dbconnection.Create(database).Error; err != nil {
|
|
|
|
logger.Error("database: during create host database: ", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
2016-10-17 19:21:17 +02:00
|
|
|
|
|
|
|
func databaseEdit(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
|
|
returndata = false
|
|
|
|
logger := log.GetLog(r, "databaseedit")
|
|
|
|
|
|
|
|
database, returnerr := getDatabase(ctx, w)
|
|
|
|
if returnerr != nil {
|
|
|
|
logger.Info("not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
var databaseRequest Database
|
|
|
|
returnerr = libapi.JSONDecoder(r.Body, &databaseRequest, w, logger)
|
|
|
|
if returnerr != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
database.Password = databaseRequest.Password
|
|
|
|
database.Comment = databaseRequest.Comment
|
|
|
|
|
|
|
|
if err := dbconnection.Save(database).Error; err != nil {
|
|
|
|
logger.Error("database: during modify host database: ", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
func databaseDelete(ctx context.Context, w http.ResponseWriter, r *http.Request) (returndata interface{}, returnerr *libapi.ErrorResult) {
|
|
|
|
returndata = false
|
|
|
|
logger := log.GetLog(r, "databasedelete")
|
|
|
|
|
|
|
|
databaseRequest, returnerr := getDatabase(ctx, w)
|
|
|
|
if returnerr != nil {
|
|
|
|
logger.Info("not found")
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err := dbconnection.Unscoped().Delete(databaseRequest).Error; err != nil {
|
|
|
|
logger.Error("database: during create host database: ", err)
|
|
|
|
w.WriteHeader(http.StatusInternalServerError)
|
|
|
|
returnerr = &libapi.ErrorResult{Message: "Internal Database Error with Database"}
|
|
|
|
return
|
|
|
|
}
|
|
|
|
returndata = true
|
|
|
|
logger.Info("done")
|
|
|
|
return
|
|
|
|
}
|