291 lines
7.4 KiB
Go
291 lines
7.4 KiB
Go
package users
|
|
|
|
import (
|
|
"system-trace/core/database"
|
|
"system-trace/core/parsers"
|
|
"system-trace/core/types"
|
|
|
|
"github.com/gofiber/fiber/v2"
|
|
)
|
|
|
|
// MARK: GetUsers godoc
|
|
// @Summary Get users (with pagination)
|
|
// @Description Returns array of users and count
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param count query int true "Count of rows" minimum(10) maximum(100)
|
|
// @Param offset query int true "Rows to skip" minumum(0)
|
|
// @Success 200 {object} types.JSONPagination
|
|
// @Router /users [get]
|
|
func GetUsersHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
s, err := parsers.GetSearch(c)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
p, err := parsers.GetPagination(c)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
ob, err := parsers.GetOrderBy(c)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
users, cursor, err := database.FindUsers(s, p, ob)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(types.JSONPagination{
|
|
Data: users,
|
|
Cursor: cursor,
|
|
})
|
|
}
|
|
|
|
// MARK: GetUserByID godoc
|
|
// @Summary Get user by ID
|
|
// @Description Returns user instance
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param id path int true "User ID" minimum(1)
|
|
// @Success 200 {object} database.User
|
|
// @Router /users/:id [get]
|
|
func GetUserByIDHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
id, err := c.ParamsInt("id")
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
user, err := database.FindUserByID(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(user)
|
|
}
|
|
|
|
// MARK: CreateUser godoc
|
|
// @Summary Create user
|
|
// @Description Create user with specified data
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param request body database.User true "Request body"
|
|
// @Success 200 {object} types.NewCredentials
|
|
// @Router /users [post]
|
|
func CreateUserHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
u := new(database.User)
|
|
if err := c.BodyParser(u); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
pass, err := resetPassword(u)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := database.InsertUser(u); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(types.NewCredentials{
|
|
Email: u.Email,
|
|
Password: pass,
|
|
})
|
|
}
|
|
|
|
// MARK: UpdateUser godoc
|
|
// @Summary Update user
|
|
// @Description Update user with specified data
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param request body database.User true "Request body"
|
|
// @Success 200
|
|
// @Router /users [patch]
|
|
func UpdateUserHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
u := new(database.User)
|
|
if err := c.BodyParser(u); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := database.UpdateUser(u, []string{"email", "real_name", "group_id"}); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
// MARK: DeleteUser godoc
|
|
// @Summary Delete user
|
|
// @Description Delete user by ID
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param id path int true "User ID" minimum(1)
|
|
// @Success 200
|
|
// @Router /users/:id [delete]
|
|
func DeleteUserHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
id, err := c.ParamsInt("id")
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err = database.DeleteUser(id); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
// MARK: DeleteUsers godoc
|
|
// @Summary Delete many users
|
|
// @Description Delete users by ID
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param request body types.ManyIDs true "Request body"
|
|
// @Success 200
|
|
// @Router /users [delete]
|
|
func DeleteUsersHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
arr := new(types.ManyIDs)
|
|
if err := c.BodyParser(arr); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := database.DeleteManyUsers(arr.Array); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
// MARK: BlockUsers godoc
|
|
// @Summary Block many users
|
|
// @Description Block users by ID
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param request body types.ManyIDs true "Request body"
|
|
// @Success 200
|
|
// @Router /users/block [patch]
|
|
func BlockUsersHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
arr, err := parsers.GetManyIDs(c)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := database.BlockManyUsers(arr.Array); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
// MARK: UnblockUsers godoc
|
|
// @Summary Unblock many users
|
|
// @Description Unblock users by ID
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param request body types.ManyIDs true "Request body"
|
|
// @Success 200
|
|
// @Router /users/unblock [patch]
|
|
func UnblockUsersHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
arr, err := parsers.GetManyIDs(c)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := database.UnblockManyUsers(arr.Array); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.SendStatus(fiber.StatusOK)
|
|
}
|
|
|
|
// MARK: ResetUserPassword godoc
|
|
// @Summary Reset user password
|
|
// @Description Reset user password by user ID
|
|
// @Tags users
|
|
// @Produce json
|
|
// @Param id path int true "User ID" minimum(1)
|
|
// @Success 200 {object} types.NewCredentials
|
|
// @Router /users/password/:id [patch]
|
|
func ResetUserPasswordHandler(c *fiber.Ctx) error {
|
|
// TODO permission validate
|
|
id, err := c.ParamsInt("id")
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
user, err := database.FindUserByID(id)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
pass, err := resetPassword(user)
|
|
if err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
if err := database.UpdateUser(user, []string{"password_hash", "password_length", "is_required_to_set_password"}); err != nil {
|
|
return c.Status(fiber.StatusBadRequest).JSON(types.JSONError{
|
|
Error: err.Error(),
|
|
})
|
|
}
|
|
|
|
return c.Status(fiber.StatusOK).JSON(types.NewCredentials{
|
|
Email: user.Email,
|
|
Password: pass,
|
|
})
|
|
}
|