admin

package
v1.2.7 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Dec 25, 2022 License: BSD-3-Clause Imports: 17 Imported by: 0

Documentation

Index

Constants

This section is empty.

Variables

View Source
var AllModelsGet = func(c *kamux.Context) {
	model, ok := c.Params["model"]
	if !ok {
		c.Json(map[string]any{
			"error": "Error: No model given in params",
		})
		return
	}
	idString := "id"
	t, _ := orm.GetMemoryTable(model, orm.DefaultDB)
	if t.Pk != "" && t.Pk != "id" {
		idString = t.Pk
	}
	rows, err := orm.Table(model).OrderBy("-" + idString).Limit(PAGINATION_PER).Page(1).All()
	if err != nil {
		rows, err = orm.Table(model).All()
		if err != nil {

			if err.Error() != "no data found" {
				c.Status(http.StatusBadRequest).Json(map[string]any{
					"error": err.Error(),
				})
				return
			}
		}
	}
	dbCols := orm.GetAllColumnsTypes(model, orm.DefaultDB)
	if settings.Config.Db.Type != "" {
		c.Html("admin/admin_all_models.html", map[string]any{
			"dbType":         settings.Config.Db.Type,
			"model_name":     model,
			"rows":           rows,
			"columns":        t.ModelTypes,
			"dbcolumns":      dbCols,
			"pk":             t.Pk,
			"columnsOrdered": t.Columns,
		})
	} else {
		logger.Error("dbType not known, do you have .env", settings.Config.Db.Type, err)
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": "dbType not found",
		})
	}
}
View Source
var AllModelsSearch = func(c *kamux.Context) {
	model, ok := c.Params["model"]
	if !ok {
		c.Json(map[string]any{
			"error": "Error: No model given in params",
		})
		return
	}

	body := c.BodyJson()

	blder := orm.Table(model)
	if query, ok := body["query"]; ok {
		if v, ok := query.(string); ok {
			if v != "" {
				blder.Where(v)
			}
		}
	}

	oB := ""
	t, _ := orm.GetMemoryTable(model, orm.DefaultDB)
	if orderby, ok := body["orderby"]; ok {
		if v, ok := orderby.(string); ok && v != "" {
			oB = v
		} else {
			oB = "-" + t.Pk
		}
	} else {
		oB = "-" + t.Pk
	}
	blder.OrderBy(oB)
	if v, ok := body["page_num"]; ok && v != "" {
		if page, ok := v.(string); !ok {
			c.Status(http.StatusBadRequest).Json(map[string]any{
				"error": "expecting page_num to be a sring",
			})
			return
		} else {
			pagenum, err := strconv.Atoi(page)
			if err == nil {
				blder.Limit(PAGINATION_PER).Page(pagenum)
			} else {
				c.Status(http.StatusBadRequest).Json(map[string]any{
					"error": err.Error(),
				})
				return
			}
		}
	} else {
		blder.Limit(PAGINATION_PER).Page(1)
	}

	data, err := blder.All()
	if err != nil {
		c.Json(map[string]any{
			"error": err.Error(),
		})
		return
	}
	c.Json(map[string]any{
		"rows": data,
		"cols": t.Columns,
	})
}
View Source
var CreateModelView = func(c *kamux.Context) {
	parseErr := c.Request.ParseMultipartForm(int64(kamux.MultipartSize))
	if parseErr != nil {
		logger.Error("Parse error = ", parseErr)
	}
	data := c.Request.Form

	defer func() {
		err := c.Request.MultipartForm.RemoveAll()
		logger.CheckError(err)
	}()

	model := data["table"][0]

	fields := []string{}
	values := []any{}
	for key, val := range data {
		switch key {
		case "table":
			continue
		case "uuid":
			uuid, err := utils.GenerateUUID()
			logger.CheckError(err)
			fields = append(fields, key)
			values = append(values, uuid)
		case "password":
			hash, _ := hash.GenerateHash(val[0])
			fields = append(fields, key)
			values = append(values, hash)
		case "":
		default:
			if key != "" && val[0] != "" && val[0] != "null" {
				fields = append(fields, key)
				values = append(values, val[0])
			}
		}
	}

	_, err := orm.Table(model).Insert(
		strings.Join(fields, ","),
		values,
	)
	if logger.CheckError(err) {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": err.Error(),
		})
		return
	}

	c.Json(map[string]any{
		"success": "Done !",
	})
}
View Source
var DeleteRowPost = func(c *kamux.Context) {
	data := c.BodyJson()
	if data["mission"] == "delete_row" {
		if model, ok := data["model_name"]; ok {
			if mm, ok := model.(string); ok {
				idString := "id"
				t, _ := orm.GetMemoryTable(mm, orm.DefaultDB)
				if t.Pk != "" && t.Pk != "id" {
					idString = t.Pk
				}
				modelDB, err := orm.Table(mm).Where(idString+" = ?", data["id"]).One()
				if logger.CheckError(err) {
					logger.Info("data received DeleteRowPost:", data)
					c.Status(http.StatusBadRequest).Json(map[string]any{
						"error": err.Error(),
					})
					return
				}
				if val, ok := modelDB["image"]; ok {
					if vv, ok := val.(string); ok && vv != "" {
						_ = c.DeleteFile(vv)
					}
				}

				if idS, ok := data["id"].(string); ok {
					_, err = orm.Table(mm).Where(idString+" = ?", idS).Delete()

					if err != nil {
						c.Status(http.StatusBadRequest).Json(map[string]any{
							"error": err.Error(),
						})
					} else {
						c.Json(map[string]any{
							"success": "Done !",
							"id":      data["id"],
						})
						return
					}
				}
			} else {
				c.Status(http.StatusBadRequest).Json(map[string]any{
					"error": "expecting model_name to be string",
				})
				return
			}
		} else {
			c.Status(http.StatusBadRequest).Json(map[string]any{
				"error": "no model_name found in request body",
			})
			return
		}
	}
}
View Source
var DropTablePost = func(c *kamux.Context) {
	data := c.BodyJson()
	if table, ok := data["table"]; ok && table != "" {
		if t, ok := data["table"].(string); ok {
			_, err := orm.Table(t).Drop()
			if logger.CheckError(err) {
				c.Status(http.StatusBadRequest).Json(map[string]any{
					"error": err.Error(),
				})
				return
			}
		} else {
			c.Status(http.StatusBadRequest).Json(map[string]any{
				"error": "expecting 'table' to be string",
			})
		}
	} else {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": "missing 'table' in body request",
		})
	}
	c.Json(map[string]any{
		"success": fmt.Sprintf("table %s Deleted !", data["table"]),
	})
}
View Source
var ExportView = func(c *kamux.Context) {
	table, ok := c.Params["table"]
	if !ok {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": "no param table found",
		})
		return
	}
	data, err := orm.Table(table).All()
	logger.CheckError(err)

	data_bytes, err := json.Marshal(data)
	logger.CheckError(err)

	c.Download(data_bytes, table+".json")
}
View Source
var ImportView = func(c *kamux.Context) {

	table := c.Request.FormValue("table")
	if table == "" {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": "no table !",
		})
		return
	}

	_, dataBytes, err := c.UploadFile("thefile", "backup", "json")
	if logger.CheckError(err) {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": err.Error(),
		})
		return
	}

	modelsOld, _ := orm.Table(table).All()
	if len(modelsOld) > 0 {
		modelsOldBytes, err := json.Marshal(modelsOld)
		if !logger.CheckError(err) {
			_ = os.MkdirAll(settings.MEDIA_DIR+"/backup/", 0664)
			dst, err := os.Create(settings.MEDIA_DIR + "/backup/" + table + "-" + time.Now().Format("2006-01-02") + ".json")
			logger.CheckError(err)
			defer dst.Close()
			_, err = dst.Write(modelsOldBytes)
			logger.CheckError(err)
		}
	}

	list_map := []map[string]any{}
	json.Unmarshal(dataBytes, &list_map)

	cols := []string{}
	values := []any{}
	for _, m := range list_map {
		for k, v := range m {
			cols = append(cols, k)
			values = append(values, v)
		}
		_, _ = orm.Table(table).Insert(strings.Join(cols, ","), values)
		cols = cols[:0]
		values = values[:0]
	}

	c.Json(map[string]any{
		"success": fmt.Sprintf("Import Done , you can see uploaded backups at ./%s/backup folder", settings.MEDIA_DIR),
	})
}
View Source
var IndexView = func(c *kamux.Context) {
	allTables := orm.GetAllTables()
	c.Html("admin/admin_index.html", map[string]any{
		"tables": allTables,
	})
}
View Source
var LoginPOSTView = func(c *kamux.Context) {
	requestData := c.BodyJson()
	email := requestData["email"]
	passRequest := requestData["password"]

	data, err := orm.Table("users").Where("email = ?", email).One()
	if err != nil {
		c.Status(500).Json(map[string]any{
			"error": err.Error(),
		})
		return
	}
	if data["email"] == "" || data["email"] == nil {
		c.Status(http.StatusNotFound).Json(map[string]any{
			"error": "User doesn not Exist",
		})
		return
	}
	if data["is_admin"] == int64(0) || data["is_admin"] == 0 || data["is_admin"] == false {
		c.Status(http.StatusForbidden).Json(map[string]any{
			"error": "Not Allowed to access this page",
		})
		return
	}

	if passDB, ok := data["password"].(string); ok {
		if pp, ok := passRequest.(string); ok {
			match, err := hash.ComparePasswordToHash(pp, passDB)
			if !match || err != nil {
				c.Status(http.StatusForbidden).Json(map[string]any{
					"error": "Wrong Password",
				})
				return
			} else {
				if uuid, ok := data["uuid"].(string); ok {
					if kamux.SESSION_ENCRYPTION {
						uuid, err = encryptor.Encrypt(uuid)
						logger.CheckError(err)
					}
					c.SetCookie("session", uuid)
					c.Json(map[string]any{
						"success": "U Are Logged In",
					})
					return
				}
			}
		}
	}
}
View Source
var LoginView = func(c *kamux.Context) {
	c.Html("admin/admin_login.html", nil)
}
View Source
var LogoutView = func(c *kamux.Context) {
	c.DeleteCookie("session")
	c.Status(http.StatusTemporaryRedirect).Redirect("/")
}
View Source
var LogsGetView = func(c *kamux.Context) {
	c.Html("admin/logs.html", nil)
}
View Source
var LogsSSEView = func(c *kamux.Context) {
	lenStream := len(logger.StreamLogs)
	if lenStream > 0 {
		err := c.StreamResponse(logger.StreamLogs[lenStream-1])
		if err != nil {
			logger.Error(err)
		}
		if lenStream > 2 {
			err := c.StreamResponse(logger.StreamLogs[lenStream-2])
			if err != nil {
				logger.Error(err)
			}
		} else if lenStream > 50 {
			err := c.StreamResponse(logger.StreamLogs[lenStream-2])
			if err != nil {
				logger.Error(err)
			}
			logger.StreamLogs = []string{}
		}
	}
}
View Source
var ManifestView = func(c *kamux.Context) {
	if settings.Config.Embed.Static {
		f, err := kamux.Static.ReadFile(settings.STATIC_DIR + "/manifest.json")
		if err != nil {
			logger.Error("cannot embed manifest.json from static", err)
			return
		}
		c.ServeEmbededFile("application/json; charset=utf-8", f)
	} else {
		c.ServeFile("application/json; charset=utf-8", settings.STATIC_DIR+"/manifest.json")
	}
}
View Source
var OfflineView = func(c *kamux.Context) {
	c.Text("<h1>YOUR ARE OFFLINE, check connection</h1>")
}
View Source
var PAGINATION_PER = 10
View Source
var RobotsTxtView = func(c *kamux.Context) {
	c.ServeFile("text/plain; charset=utf-8", "./static/robots.txt")
}
View Source
var ServiceWorkerView = func(c *kamux.Context) {
	if settings.Config.Embed.Static {
		f, err := kamux.Static.ReadFile(settings.STATIC_DIR + "/sw.js")
		if err != nil {
			logger.Error("cannot embed sw.js from static", err)
			return
		}
		c.ServeEmbededFile("application/javascript; charset=utf-8", f)
	} else {
		c.ServeFile("application/javascript; charset=utf-8", settings.STATIC_DIR+"/sw.js")
	}
}
View Source
var SingleModelGet = func(c *kamux.Context) {
	model, ok := c.Params["model"]
	if !ok {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": "param model not defined",
		})
		return
	}
	id, ok := c.Params["id"]
	if !ok {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": "param id not defined",
		})
		return
	}
	idString := "id"
	t, _ := orm.GetMemoryTable(model, orm.DefaultDB)
	if t.Pk != "" && t.Pk != "id" {
		idString = t.Pk
	}

	modelRow, err := orm.Table(model).Where(idString+" = ?", id).One()
	if logger.CheckError(err) {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": err.Error(),
		})
		return
	}
	dbCols := orm.GetAllColumnsTypes(model, orm.DefaultDB)
	c.Html("admin/admin_single_model.html", map[string]any{
		"model":      modelRow,
		"model_name": model,
		"id":         id,
		"columns":    t.ModelTypes,
		"dbcolumns":  dbCols,
		"pk":         t.Pk,
	})
}
View Source
var UpdateRowPost = func(c *kamux.Context) {

	data, files := utils.ParseMultipartForm(c.Request)

	id := data["row_id"][0]

	idString := "id"
	t, _ := orm.GetMemoryTable(data["table"][0], orm.DefaultDB)
	if t.Pk != "" && t.Pk != "id" {
		idString = t.Pk
	}
	err := handleFilesUpload(files, data["table"][0], id, c, idString)
	if err != nil {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": err.Error(),
		})
		return
	}

	modelDB, err := orm.Table(data["table"][0]).Where(idString+" = ?", id).One()

	if err != nil {
		c.Status(http.StatusBadRequest).Json(map[string]any{
			"error": err.Error(),
		})
		return
	}

	ignored := []string{idString, "uuid", "file", "image", "photo", "img", "fichier", "row_id", "table"}
	toUpdate := map[string]any{}
	for key, val := range data {
		if !utils.SliceContains(ignored, key) {
			if modelDB[key] == val[0] {

				continue
			}
			toUpdate[key] = val[0]
		}
	}

	s := ""
	values := []any{}
	if len(toUpdate) > 0 {
		for col, v := range toUpdate {
			if s == "" {
				s += col + "= ?"
			} else {
				s += "," + col + "= ?"
			}
			values = append(values, v)
		}
	}
	if s != "" {
		_, err := orm.Table(data["table"][0]).Where(idString+" = ?", id).Set(s, values...)
		if err != nil {
			c.Status(http.StatusBadRequest).Json(map[string]any{
				"error": err.Error(),
			})
			return
		}
	}
	s = ""
	if len(files) > 0 {
		for f := range files {
			if s == "" {
				s += f
			} else {
				s += "," + f
			}
		}
	}
	if len(toUpdate) > 0 {
		for k := range toUpdate {
			if s == "" {
				s += k
			} else {
				s += "," + k
			}
		}
	}
	c.Json(map[string]string{
		"success": s + " updated successfully",
	})
}

Functions

func UrlPatterns

func UrlPatterns(r *kamux.Router)

Types

This section is empty.

Directories

Path Synopsis

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL