Added func for delete user
parent
ae65dcfc98
commit
e0b0c7edb6
|
@ -2,6 +2,8 @@ package user
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"math/big"
|
"math/big"
|
||||||
"regexp"
|
"regexp"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -9,6 +11,7 @@ import (
|
||||||
|
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/config"
|
"git.umbach.dev/app-idea/rest-api/modules/config"
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/database"
|
"git.umbach.dev/app-idea/rest-api/modules/database"
|
||||||
|
"git.umbach.dev/app-idea/rest-api/modules/rabbitmq"
|
||||||
"git.umbach.dev/app-idea/rest-api/modules/structs"
|
"git.umbach.dev/app-idea/rest-api/modules/structs"
|
||||||
"github.com/gofiber/fiber/v2"
|
"github.com/gofiber/fiber/v2"
|
||||||
log "github.com/sirupsen/logrus"
|
log "github.com/sirupsen/logrus"
|
||||||
|
@ -133,40 +136,146 @@ func getUserIdBySessionId(sessionId string) (string, error) {
|
||||||
return session.UserId, nil
|
return session.UserId, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func GetUserById(c *fiber.Ctx) error {
|
||||||
|
// swagger:operation GET /users User user
|
||||||
|
// ---
|
||||||
|
// summary: Informations about the user by id (except password)
|
||||||
|
// parameters:
|
||||||
|
// - name: v
|
||||||
|
// in: query
|
||||||
|
// description: Example -> { "v"; ["name", "state", "language_id"] }
|
||||||
|
// type: string
|
||||||
|
// required: true
|
||||||
|
// responses:
|
||||||
|
// '200':
|
||||||
|
// description: User informations
|
||||||
|
// '400':
|
||||||
|
// description: Values wrong format
|
||||||
|
|
||||||
|
return userInfos(c, c.Params("id"))
|
||||||
|
}
|
||||||
|
|
||||||
func GetUser(c *fiber.Ctx) error {
|
func GetUser(c *fiber.Ctx) error {
|
||||||
log.Infoln("body", c.Body())
|
// swagger:operation GET /user User user
|
||||||
|
// ---
|
||||||
|
// summary: Informations about the user (except password)
|
||||||
|
// parameters:
|
||||||
|
// - name: v
|
||||||
|
// in: query
|
||||||
|
// description: Example -> { "v"; ["name", "state", "language_id"] }
|
||||||
|
// type: string
|
||||||
|
// required: true
|
||||||
|
// responses:
|
||||||
|
// '200':
|
||||||
|
// description: User informations
|
||||||
|
// '400':
|
||||||
|
// description: Values wrong format
|
||||||
|
|
||||||
|
return userInfos(c, "")
|
||||||
|
}
|
||||||
|
|
||||||
|
func userInfos(c *fiber.Ctx, userId string) error {
|
||||||
type Input struct {
|
type Input struct {
|
||||||
Val []string `json:"val"`
|
Values []string `json:"v"`
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infoln("arr", string(c.Body()))
|
|
||||||
|
|
||||||
var input Input
|
var input Input
|
||||||
|
|
||||||
if err := c.BodyParser(&input); err != nil {
|
if err := c.BodyParser(&input); err != nil {
|
||||||
log.Infoln("bad", input)
|
|
||||||
return c.SendStatus(fiber.StatusBadRequest)
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
}
|
}
|
||||||
|
|
||||||
log.Infoln("test", strings.Join(input.Val, ","))
|
value := strings.Join(input.Values, ",")
|
||||||
|
|
||||||
log.Println("input", input)
|
if strings.Contains(value, "password") {
|
||||||
|
return c.SendStatus(fiber.StatusForbidden)
|
||||||
|
}
|
||||||
|
|
||||||
db := database.DB
|
db := database.DB
|
||||||
userId, err := getUserIdBySessionId(c.Cookies("session_id"))
|
|
||||||
|
if userId == "" {
|
||||||
|
var err error
|
||||||
|
|
||||||
|
userId, err = getUserIdBySessionId(c.Cookies("session_id"))
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
user := map[string]interface{}{}
|
||||||
|
|
||||||
|
res := db.Model(&structs.User{}).Select(value).First(&user, "id = ?", userId)
|
||||||
|
|
||||||
|
if res.Error != nil {
|
||||||
|
return c.SendStatus(fiber.StatusBadRequest)
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.JSON(&user)
|
||||||
|
}
|
||||||
|
|
||||||
|
func getUserBySessionId(sessionId string) (structs.User, error) {
|
||||||
|
db := database.DB
|
||||||
|
user := structs.User{}
|
||||||
|
userId, err := getUserIdBySessionId(sessionId)
|
||||||
|
|
||||||
|
if err != nil {
|
||||||
|
return user, err
|
||||||
|
}
|
||||||
|
|
||||||
|
err = db.Where("id = ?", userId).First(&user).Error
|
||||||
|
|
||||||
|
if errors.Is(err, gorm.ErrRecordNotFound) {
|
||||||
|
return user, err
|
||||||
|
}
|
||||||
|
|
||||||
|
log.Infoln("getUserBySessionId", user)
|
||||||
|
|
||||||
|
return user, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func deleteUser(c *fiber.Ctx) error {
|
||||||
|
userAction := structs.UserAction{Id: c.Params("actionId")}
|
||||||
|
|
||||||
|
db := database.DB
|
||||||
|
|
||||||
|
db.First(&userAction)
|
||||||
|
|
||||||
|
db.Delete(&structs.User{Id: userAction.UserId})
|
||||||
|
db.Where("user_id = ?", userAction.UserId).Delete(&structs.Session{})
|
||||||
|
db.Delete(&userAction)
|
||||||
|
|
||||||
|
return c.SendStatus(fiber.StatusOK)
|
||||||
|
}
|
||||||
|
|
||||||
|
func DeleteUser(c *fiber.Ctx) error {
|
||||||
|
user, err := getUserBySessionId(c.Cookies("session_id"))
|
||||||
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return c.SendStatus(fiber.StatusInternalServerError)
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
}
|
}
|
||||||
|
|
||||||
user := structs.User{}
|
userActivationId, err := createUserAction(user.Id)
|
||||||
|
|
||||||
db.Table("users").Select(strings.Join(input.Val, ",")).First(&user, "id = ?", userId)
|
if err != nil {
|
||||||
|
return c.SendStatus(fiber.StatusInternalServerError)
|
||||||
|
}
|
||||||
|
|
||||||
return c.JSON(&user)
|
user.State = 2
|
||||||
|
|
||||||
|
db := database.DB
|
||||||
|
|
||||||
|
db.Save(&user)
|
||||||
|
|
||||||
|
rabbitmq.PublishMail(user.Email, 1, user.LanguageId, json.RawMessage(`{"name": "`+user.Name+`",
|
||||||
|
|
||||||
|
"email": "`+user.Email+`",
|
||||||
|
"url": "http://localhost:3000/api/v1/user/action/1/`+userActivationId+`"}`))
|
||||||
|
|
||||||
|
return c.SendStatus(fiber.StatusOK)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
func GetUsers(c *fiber.Ctx) error {
|
func GetUsers(c *fiber.Ctx) error {
|
||||||
|
|
||||||
list := []string{}
|
list := []string{}
|
||||||
|
@ -174,17 +283,18 @@ func GetUsers(c *fiber.Ctx) error {
|
||||||
var (
|
var (
|
||||||
name string
|
name string
|
||||||
)*/
|
)*/
|
||||||
/*
|
/*
|
||||||
rows, err := db.Query("SELECT username FROM users;")
|
rows, err := db.Query("SELECT username FROM users;")
|
||||||
fmt.Println("err", err)
|
fmt.Println("err", err)
|
||||||
defer rows.Close()
|
defer rows.Close()
|
||||||
fmt.Println("reading data:")
|
fmt.Println("reading data:")
|
||||||
for rows.Next() {
|
for rows.Next() {
|
||||||
err := rows.Scan(&name)
|
err := rows.Scan(&name)
|
||||||
fmt.Printf("Data row = (%s, %s)\n", name, err)
|
fmt.Printf("Data row = (%s, %s)\n", name, err)
|
||||||
list = append(list, name)
|
list = append(list, name)
|
||||||
}
|
}
|
||||||
err = rows.Err()*/
|
err = rows.Err()*/
|
||||||
|
/*
|
||||||
return c.JSON(list)
|
return c.JSON(list)
|
||||||
}
|
}
|
||||||
|
*/
|
||||||
|
|
Loading…
Reference in New Issue