initial import

This commit is contained in:
ycc
2021-10-29 23:37:32 +02:00
parent 99865b0d6c
commit 5e9ad05a9c
29 changed files with 2737 additions and 0 deletions

128
controllers/login.go Normal file
View File

@ -0,0 +1,128 @@
package controllers
import (
"encoding/json"
"fmt"
"net/http"
"os"
"sqldb-ws/models"
"forge.redroom.link/yves/sqldb"
beego "github.com/beego/beego/v2/server/web"
"github.com/lib/pq"
"github.com/matthewhartstonge/argon2"
"github.com/rs/zerolog/log"
)
// Operations about login
type LoginController struct {
beego.Controller
}
type Credential struct {
Login string `json:"login,omitempty"`
Password string `json:"password,omitempty"`
}
// @Title AddUser
// @Description Add user
// @Param username query string true "The username for register format"
// @Param password query string true "The password for register"
// @Success 200
// @Failure 403 user already exist
// @router /adduser [post]
func (l *LoginController) AddUser() {
argon := argon2.DefaultConfig()
username := l.GetString("username")
pass := l.GetString("password")
hash, err := argon.HashEncoded([]byte(pass))
if err != nil {
log.Error().Msg(err.Error())
}
record := make(sqldb.AssRow)
record["login"] = username
record["password"] = string(hash)
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
existing, err := db.QueryAssociativeArray("SELECT * FROM dbuser WHERE login =" + pq.QuoteLiteral(username) + ";")
if err != nil {
log.Error().Msg(err.Error())
}
if existing != nil {
l.Ctx.Output.SetStatus(403)
} else {
_, err := db.Table("dbuser").Insert(record)
if err != nil {
log.Error().Msg(err.Error())
}
}
db.Close()
}
// @Title Login
// @Description User login
// @Param body body Credential true "Credentials"
// @Success 200 {string} success !
// @Failure 403 user does not exist
// @Failure 402 user already connected
// @router /login [post]
func (l *LoginController) Login() {
var creds Credential
json.Unmarshal(l.Ctx.Input.RequestBody, &creds)
if l.GetSession("user_id") != creds.Login {
if creds.Login == "" || creds.Password == "" {
l.Ctx.Output.SetStatus(403)
}
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
user, err := db.Table("dbuser").GetAssociativeArray([]string{"password"}, "login="+sqldb.Quote(creds.Login), []string{}, "")
pass := user[0].GetString("password")
if err != nil {
log.Error().Msg(err.Error())
}
ok, err := argon2.VerifyEncoded([]byte(creds.Password), []byte(pass))
if err != nil {
log.Error().Msg(err.Error())
}
matches := "no 🔒"
if ok {
matches = "yes 🔓"
username := l.GetString("username")
l.SetSession("user_id", username)
models.GetLogin(username)
l.Ctx.Output.SetStatus(http.StatusOK)
l.Data["json"] = map[string]string{"login": "ok"}
}
fmt.Printf("Password Matches: %s\n", matches)
//security.Test()
} else {
l.Ctx.Output.SetStatus(403)
l.Data["json"] = map[string]string{"login": "fail"}
}
l.ServeJSON()
}
// @Title Logout
// @Description Logs user
// @Success 200
// @Failure 403 user not exist
// @router /logout [post]
func (l *LoginController) Logout() {
user := l.GetSession("user_id")
if user != nil {
l.DelSession("user_id")
}
}

52
controllers/schema.go Normal file
View File

@ -0,0 +1,52 @@
package controllers
import (
"os"
"forge.redroom.link/yves/sqldb"
beego "github.com/beego/beego/v2/server/web"
"github.com/rs/zerolog/log"
)
// Operations about schema
type SchemaController struct {
beego.Controller
}
// @Title GetTable
// @Description get list table
// @Success 200 {string} success !
// @Failure 403 no table
// @router / [get]
func (s *SchemaController) GetTable() {
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
data, err := db.ListTables()
if err != nil {
log.Error().Msg(err.Error())
s.Data["json"] = map[string]string{"error": err.Error()}
}
s.Data["json"] = data
s.ServeJSON()
db.Close()
}
// @Title GetSchema
// @Description get table schema
// @Param table path string true "Name of the table"
// @Success 200 success !
// @Failure 403 no table
// @router /:table [get]
func (s *SchemaController) GetSchema() {
table := s.GetString(":table")
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
data, err := db.Table(table).GetSchema()
if err != nil {
log.Error().Msg(err.Error())
s.Data["json"] = map[string]string{"error": err.Error()}
}
s.Data["json"] = data
s.ServeJSON()
db.Close()
}

214
controllers/table.go Normal file
View File

@ -0,0 +1,214 @@
package controllers
import (
"encoding/json"
"fmt"
"net/http"
"os"
"sqldb-ws/security"
"strings"
"forge.redroom.link/yves/sqldb"
beego "github.com/beego/beego/v2/server/web"
"github.com/rs/zerolog/log"
)
// Operations about table
type TableController struct {
beego.Controller
}
// @Title Post
// @Description post data in table
// @Param table path string true "Name of the table"
// @Param data body json true "body for data content (Json format)"
// @Success 200 {string} success
// @Failure 403 post issue
// @router /:table [post]
func (t *TableController) Post() {
// var FilterUserPost = func(ctx *context.Context) {
// if strings.HasPrefix(ctx, "/") {
// return
// }
// _, ok := ctx.Input.Session("user_id").(int)
// if !ok {
// ctx.Redirect(302, "/l")
// }
table := t.GetString(":table")
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
defer db.Close()
var data sqldb.AssRow
json.Unmarshal(t.Ctx.Input.RequestBody, &data)
println(fmt.Sprintf("%v", data))
uid, err := db.Table(table).UpdateOrInsert(data)
if err != nil {
log.Error().Msg(err.Error())
}
t.Data["json"] = map[string]int{"uid": uid}
t.Ctx.Output.SetStatus(http.StatusOK)
t.ServeJSON()
}
// web.InsertFilter("/*", web.BeforeRouter, FilterUserPost)
// }
// @Title Delete
// @Description delete the data in table
// @Param table path string true "Name of the table"
// @Param body body true "body for data content (Json format)"
// @Success 200 {string} delete success!
// @Failure 403 delete issue
// @router /:table [delete]
func (t *TableController) Delete() {
table := t.GetString(":table")
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
var data sqldb.AssRow
json.Unmarshal(t.Ctx.Input.RequestBody, &data)
println(fmt.Sprintf("%v", data))
db.Table(table).Delete(data)
t.Data["json"] = "delete success!"
t.Ctx.Output.SetStatus(http.StatusOK)
t.ServeJSON()
db.Close()
}
// @Title GetAllTable
// @Description get all Datas
// @Param table path string true "Name of the table"
// @Success 200 {string} success !
// @Failure 403 no table
// @router /:table [get]
func (t *TableController) GetAllTable() {
table := t.GetString(":table")
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
columns := []string{"*"}
restriction := ""
sortkeys := []string{}
dir := ""
data, err := db.Table(table).GetAssociativeArray(columns, restriction, sortkeys, dir)
if err != nil {
log.Error().Msg(err.Error())
t.Data["json"] = map[string]string{"error": err.Error()}
} else {
t.Data["json"] = data
}
t.ServeJSON()
db.Close()
}
// @Title GetAllTableColumn
// @Description get all Datas
// @Param table path string true "Name of the table"
// @Param columns path string true "Name of the columns (separate with a comma)"
// @Success 200 {string} success !
// @Failure 403 no table
// @router /:table/:columns [get]
func (t *TableController) GetAllTableColumn() {
table := t.GetString(":table")
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
columns := strings.Split(t.GetString(":columns"), ",")
restriction := ""
sortkeys := []string{}
dir := ""
data, err := db.Table(table).GetAssociativeArray(columns, restriction, sortkeys, dir)
if err != nil {
log.Error().Msg(err.Error())
t.Data["json"] = map[string]string{"error": err.Error()}
} else {
t.Data["json"] = data
}
t.ServeJSON()
db.Close()
}
// @Title GetAllTableColumnRestriction
// @Description get all Datas
// @Param table path string true "Name of the table"
// @Param columns path string true "Name of the columns (separate with a comma)"
// @Param restriction path string true "SQL restriction"
// @Success 200 {string} success !
// @Failure 403 no table
// @router /:table/:columns/:restriction [get]
func (t *TableController) GetAllTableColumnRestriction() {
table := t.GetString(":table")
columns := fmt.Sprintf("%v", strings.Split(t.GetString(":columns"), ","))
cols := strings.Split(t.GetString(":columns"), ",")
restriction := t.GetString(":restriction")
sortkeys := []string{}
dir := ""
dbuser_id := fmt.Sprintf("%v", 1)
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
security.CheckSelect(dbuser_id, &table, &columns, &restriction)
data, err := db.Table(table).GetAssociativeArray(cols, restriction, sortkeys, dir)
if err != nil {
log.Error().Msg(err.Error())
}
data2 := fmt.Sprintf("%v", data)
fmt.Println(data2)
t.Data["json"] = data
t.ServeJSON()
db.Close()
}
// @Title GetAllTableColumnRestrictionSortkeys
// @Description get all Datas
// @Param table path string true "Name of the table"
// @Param columns path string true "Name of the columns (separate with a comma)"
// @Param restriction path string true "SQL restriction"
// @param sortkeys path string true "column name"
// @Success 200 {string} success !
// @Failure 403 no table
// @router /:table/:columns/:restriction/:sortkeys [get]
func (t *TableController) GetAllTableColumnRestrictionSortkeys() {
table := t.GetString(":table")
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
columns := strings.Split(t.GetString(":columns"), ",")
restriction := t.GetString(":restriction")
sortkeys := strings.Split(t.GetString(":sortkeys"), ",")
dir := ""
data, err := db.Table(table).GetAssociativeArray(columns, restriction, sortkeys, dir)
if err != nil {
log.Error().Msg(err.Error())
}
t.Data["json"] = data
t.ServeJSON()
db.Close()
}
// @Title GetAllTableColumnRestrictionSortkeysDir
// @Description get all Datas
// @Param table path string true "Name of the table"
// @Param columns path string true "Name of the columns (separate with a comma)"
// @Param restriction path string true "SQL restriction"
// @param sortkeys path string true "column name"
// @param dir path string true "asc or desc"
// @Success 200 {string} success !
// @Failure 403 no table
// @router /:table/:columns/:restriction/:sortkeys/:dir [get]
func (t *TableController) GetAllTableColumnRestrictionSortkeysDir() {
table := t.GetString(":table")
db := sqldb.Open(os.Getenv("driverdb"), os.Getenv("paramsdb"))
columns := strings.Split(t.GetString(":columns"), ",")
restriction := t.GetString(":restriction")
sortkeys := strings.Split(t.GetString(":sortkeys"), ",")
dir := t.GetString(":dir")
data, err := db.Table(table).GetAssociativeArray(columns, restriction, sortkeys, dir)
if err != nil {
log.Error().Msg(err.Error())
}
t.Data["json"] = data
t.ServeJSON()
db.Close()
}