2022-02-03 10:32:03 +03:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
2022-02-04 11:51:48 +03:00
|
|
|
"encoding/json"
|
2022-05-02 21:05:43 +03:00
|
|
|
"fmt"
|
2022-02-03 10:32:03 +03:00
|
|
|
"net/http"
|
2022-05-19 13:32:04 +03:00
|
|
|
"strconv"
|
2022-02-03 10:32:03 +03:00
|
|
|
|
2022-06-27 17:09:06 +03:00
|
|
|
"github.com/usememos/memos/api"
|
|
|
|
"github.com/usememos/memos/common"
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
"github.com/labstack/echo/v4"
|
2022-02-06 11:19:20 +03:00
|
|
|
"golang.org/x/crypto/bcrypt"
|
2022-02-03 10:32:03 +03:00
|
|
|
)
|
|
|
|
|
|
|
|
func (s *Server) registerUserRoutes(g *echo.Group) {
|
2022-05-16 17:19:39 +03:00
|
|
|
g.POST("/user", func(c echo.Context) error {
|
|
|
|
userCreate := &api.UserCreate{}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(userCreate); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted post user request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(userCreate.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userCreate.PasswordHash = string(passwordHash)
|
|
|
|
user, err := s.Store.CreateUser(userCreate)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to create user").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
|
|
|
g.GET("/user", func(c echo.Context) error {
|
|
|
|
userList, err := s.Store.FindUserList(&api.UserFind{})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch user list").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-07-26 16:41:20 +03:00
|
|
|
for _, user := range userList {
|
|
|
|
// data desensitize
|
|
|
|
user.OpenID = ""
|
|
|
|
}
|
|
|
|
|
2022-05-16 17:19:39 +03:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(userList)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user list response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-07-09 03:31:07 +03:00
|
|
|
g.GET("/user/:id", func(c echo.Context) error {
|
2022-07-07 15:22:36 +03:00
|
|
|
id, err := strconv.Atoi(c.Param("id"))
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted user id").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
user, err := s.Store.FindUser(&api.UserFind{
|
|
|
|
ID: &id,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch user").SetInternal(err)
|
|
|
|
}
|
2022-07-08 18:38:24 +03:00
|
|
|
|
2022-07-09 16:01:09 +03:00
|
|
|
if user != nil {
|
|
|
|
// data desensitize
|
|
|
|
user.OpenID = ""
|
|
|
|
}
|
|
|
|
|
2022-07-07 15:22:36 +03:00
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-07-09 03:31:07 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
|
2022-07-07 15:22:36 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
|
|
|
|
2022-03-29 02:30:29 +03:00
|
|
|
// GET /api/user/me is used to check if the user is logged in.
|
2022-02-03 10:32:03 +03:00
|
|
|
g.GET("/user/me", func(c echo.Context) error {
|
2022-07-27 14:45:37 +03:00
|
|
|
userID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
2022-05-02 21:05:43 +03:00
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing auth session")
|
2022-02-04 13:54:24 +03:00
|
|
|
}
|
|
|
|
|
2022-02-03 10:32:03 +03:00
|
|
|
userFind := &api.UserFind{
|
2022-05-02 21:05:43 +03:00
|
|
|
ID: &userID,
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-05-16 02:37:23 +03:00
|
|
|
user, err := s.Store.FindUser(userFind)
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to fetch user").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
2022-02-04 12:06:04 +03:00
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
|
2022-02-05 06:43:25 +03:00
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-02-04 11:51:48 +03:00
|
|
|
return nil
|
|
|
|
})
|
2022-02-18 17:21:10 +03:00
|
|
|
|
2022-07-26 17:32:26 +03:00
|
|
|
g.PATCH("/user/:id", func(c echo.Context) error {
|
|
|
|
userID, err := strconv.Atoi(c.Param("id"))
|
2022-02-03 10:32:03 +03:00
|
|
|
if err != nil {
|
2022-07-26 17:32:26 +03:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|
2022-07-28 15:09:25 +03:00
|
|
|
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-05-19 13:32:04 +03:00
|
|
|
currentUser, err := s.Store.FindUser(&api.UserFind{
|
|
|
|
ID: ¤tUserID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
|
|
|
|
}
|
|
|
|
if currentUser == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Current session user not found with ID: %d", currentUserID)).SetInternal(err)
|
2022-07-26 17:32:26 +03:00
|
|
|
} else if currentUser.Role != api.Host && currentUserID != userID {
|
2022-05-19 13:32:04 +03:00
|
|
|
return echo.NewHTTPError(http.StatusForbidden, "Access forbidden for current session user").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
userPatch := &api.UserPatch{
|
|
|
|
ID: userID,
|
|
|
|
}
|
|
|
|
if err := json.NewDecoder(c.Request().Body).Decode(userPatch); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, "Malformatted patch user request").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if userPatch.Password != nil && *userPatch.Password != "" {
|
|
|
|
passwordHash, err := bcrypt.GenerateFromPassword([]byte(*userPatch.Password), bcrypt.DefaultCost)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to generate password hash").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
passwordHashStr := string(passwordHash)
|
|
|
|
userPatch.PasswordHash = &passwordHashStr
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:32:26 +03:00
|
|
|
if userPatch.ResetOpenID != nil && *userPatch.ResetOpenID {
|
|
|
|
openID := common.GenUUID()
|
|
|
|
userPatch.OpenID = &openID
|
|
|
|
}
|
|
|
|
|
2022-05-19 13:32:04 +03:00
|
|
|
user, err := s.Store.PatchUser(userPatch)
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to patch user").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Response().Header().Set(echo.HeaderContentType, echo.MIMEApplicationJSONCharsetUTF8)
|
|
|
|
if err := json.NewEncoder(c.Response().Writer).Encode(composeResponse(user)); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to encode user response").SetInternal(err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
})
|
2022-07-26 16:12:20 +03:00
|
|
|
|
2022-07-26 17:32:26 +03:00
|
|
|
g.DELETE("/user/:id", func(c echo.Context) error {
|
2022-07-28 15:09:25 +03:00
|
|
|
currentUserID, ok := c.Get(getUserIDContextKey()).(int)
|
|
|
|
if !ok {
|
|
|
|
return echo.NewHTTPError(http.StatusUnauthorized, "Missing user in session")
|
|
|
|
}
|
2022-07-26 16:12:20 +03:00
|
|
|
currentUser, err := s.Store.FindUser(&api.UserFind{
|
|
|
|
ID: ¤tUserID,
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to find user").SetInternal(err)
|
|
|
|
}
|
|
|
|
if currentUser == nil {
|
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("Current session user not found with ID: %d", currentUserID)).SetInternal(err)
|
|
|
|
} else if currentUser.Role != api.Host {
|
|
|
|
return echo.NewHTTPError(http.StatusForbidden, "Access forbidden for current session user").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
2022-07-26 17:32:26 +03:00
|
|
|
userID, err := strconv.Atoi(c.Param("id"))
|
2022-07-26 16:12:20 +03:00
|
|
|
if err != nil {
|
2022-07-26 17:32:26 +03:00
|
|
|
return echo.NewHTTPError(http.StatusBadRequest, fmt.Sprintf("ID is not a number: %s", c.Param("id"))).SetInternal(err)
|
2022-07-26 16:12:20 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
userDelete := &api.UserDelete{
|
|
|
|
ID: userID,
|
|
|
|
}
|
|
|
|
if err := s.Store.DeleteUser(userDelete); err != nil {
|
|
|
|
return echo.NewHTTPError(http.StatusInternalServerError, "Failed to delete user").SetInternal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return c.JSON(http.StatusOK, true)
|
|
|
|
})
|
2022-02-03 10:32:03 +03:00
|
|
|
}
|