needed save users that can view channel instead of checking
This commit is contained in:
@@ -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
|
||||
|
||||
Reference in New Issue
Block a user