needed save users that can view channel instead of checking
This commit is contained in:
@@ -45,7 +45,7 @@ func HandleAttachmentFileUpload(response http.ResponseWriter, request *http.Requ
|
||||
key := minio.GetKey(minio.GetKeyOptions{
|
||||
ConnectionId: conn.Id,
|
||||
MimeType: contentType,
|
||||
UploadType: minio.File,
|
||||
UploadType: minio.ConnectionFile,
|
||||
})
|
||||
|
||||
if err = minio.Upload(ctx, key, file, header.Size, contentType, map[string]string{
|
||||
|
||||
@@ -3,6 +3,7 @@ package httpRequest
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"log/slog"
|
||||
"net/http"
|
||||
|
||||
"go-socket/packages/convertions"
|
||||
@@ -57,13 +58,11 @@ func getHubByIdStr(ctx context.Context, hubId string) (*types.Hub, error) {
|
||||
}
|
||||
|
||||
hub, ok := cache.GetHubById(hubUuid)
|
||||
|
||||
if !ok {
|
||||
hub = types.CreateHub()
|
||||
hub.Id = hubUuid
|
||||
if err := postgresql.GetWholeHub(ctx, hub); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New("hub not found")
|
||||
}
|
||||
|
||||
return hub, nil
|
||||
}
|
||||
|
||||
@@ -91,3 +90,39 @@ func getHubUserIfValidWithResponseOnFail(ctx context.Context, response http.Resp
|
||||
|
||||
return user, hubUser, hub, nil
|
||||
}
|
||||
|
||||
func getHubChannelIfValidWithResponseOnFail(ctx context.Context, response http.ResponseWriter, hub *types.Hub, hubUser *types.HubUser, channelId string) (
|
||||
*types.HubChannel, error) {
|
||||
channelUuid, err := convertions.StringToUuid(channelId)
|
||||
if err != nil {
|
||||
http.Error(response, "invalid channelid", http.StatusBadRequest)
|
||||
return nil, errors.New("invalid channelid")
|
||||
}
|
||||
channel, ok := hub.Channels[channelUuid]
|
||||
if !ok {
|
||||
http.Error(response, "invalid channelid", http.StatusUnauthorized)
|
||||
return nil, errors.New("invalid channelid")
|
||||
}
|
||||
|
||||
group := hub.Groups[channel.ParentId]
|
||||
if group == nil {
|
||||
slog.Warn("hub channel has no parent group", "Hub", hub.Id, "Channel", channel.Id)
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return nil, errors.New("internal server error")
|
||||
}
|
||||
|
||||
if !group.RolesCanView.HasSameId(hubUser.Roles) {
|
||||
http.Error(response, "invalid channelid", http.StatusUnauthorized)
|
||||
return nil, errors.New("invalid channelid")
|
||||
}
|
||||
|
||||
if !channel.RolesCanView.HasSameId(hubUser.Roles) {
|
||||
http.Error(response, "invalid channelid", http.StatusUnauthorized)
|
||||
return nil, errors.New("invalid channelid")
|
||||
}
|
||||
|
||||
return channel, nil
|
||||
}
|
||||
|
||||
// TODO cache on roles or channels needed for quick lookup
|
||||
func getHubChannelReadHistorayAndViewChannel(hub *types.Hub, channel *types.HubChannel) []*types.HubUser
|
||||
|
||||
@@ -4,11 +4,14 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go-socket/packages/convertions"
|
||||
"go-socket/packages/types"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
func canHubUserMessage(channel)
|
||||
|
||||
func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
@@ -21,7 +24,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hubName := request.Header.Get("hubname")
|
||||
hubName := request.FormValue("hubname")
|
||||
if hubName == "" {
|
||||
http.Error(response, "hub name is required", http.StatusBadRequest)
|
||||
return
|
||||
@@ -35,9 +38,9 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
hub.CreatedAt = time.Now()
|
||||
|
||||
creator := types.NewHubUser()
|
||||
creator.Name = user.Name
|
||||
creator.OriginalId = user.Id
|
||||
creator.CreatedAt = hub.CreatedAt
|
||||
hub.Users[creator.OriginalId] = creator
|
||||
|
||||
rootRole := &types.HubRole{
|
||||
Id: uint8(0),
|
||||
@@ -50,7 +53,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
creator.Roles.Add(rootRole.Id)
|
||||
|
||||
memberRole := &types.HubRole{
|
||||
Id: uint8(255),
|
||||
Id: types.BoundRolesMax,
|
||||
Name: "member",
|
||||
Color: types.Rgba{}.GetRandom(),
|
||||
CreatedAt: hub.CreatedAt,
|
||||
@@ -63,6 +66,8 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
rootGroup.Id = uint8(1)
|
||||
rootGroup.Color = types.Rgba{}.GetRandom()
|
||||
rootGroup.CreatedAt = hub.CreatedAt
|
||||
rootGroup.RolesCanView.Add(rootRole.Id)
|
||||
rootGroup.RolesCanView.Add(memberRole.Id)
|
||||
hub.Groups[rootGroup.Id] = rootGroup
|
||||
|
||||
channel := types.NewHubChannel()
|
||||
@@ -71,5 +76,49 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
channel.Id = uuid.New()
|
||||
channel.ParentId = rootGroup.Id
|
||||
channel.Description = "The fist channel!"
|
||||
channel.CreatedAt = hub.CreatedAt
|
||||
channel.RolesCanMessage.Add(rootGroup.Id)
|
||||
channel.RolesCanMessage.Add(memberRole.Id)
|
||||
channel.RolesCanView.Add(rootGroup.Id)
|
||||
channel.RolesCanView.Add(memberRole.Id)
|
||||
channel.RolesCanReadHistory.Add(rootGroup.Id)
|
||||
channel.RolesCanReadHistory.Add(memberRole.Id)
|
||||
hub.Channels[channel.Id] = channel
|
||||
}
|
||||
|
||||
func HandleChannelSendMessage(response http.ResponseWriter, request *http.Request) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
}
|
||||
ctx := request.Context()
|
||||
user, hubUser, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request.Header.Get("token"), request.FormValue("hubid"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
channelId, err := convertions.StringToUuid(request.FormValue("channelid"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid channelid", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
msgContent := request.FormValue("msgContent")
|
||||
attachedFile := request.FormValue("attachedFile")
|
||||
|
||||
if msgContent == "" && attachedFile == "" {
|
||||
http.Error(response, "empty msgContent", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
// TODO add check in future
|
||||
// if attachedFile != "" && !strings.HasPrefix(attachedFile, conn.Id.String()+"/") {
|
||||
// http.Error(response, "invalid attachedFile", http.StatusBadRequest)
|
||||
// return
|
||||
// }
|
||||
|
||||
channel, err := getHubChannelIfValidWithResponseOnFail(ctx, response, hub, hubUser, request.FormValue("hubid"))
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user