158 lines
4.1 KiB
Go
158 lines
4.1 KiB
Go
package httpRequest
|
|
|
|
import (
|
|
"net/http"
|
|
"time"
|
|
|
|
"go-socket/packages/cache"
|
|
"go-socket/packages/types"
|
|
|
|
"github.com/google/uuid"
|
|
)
|
|
|
|
func haveHubUserPermissionsOnChannel(permissions types.CachedUserPermissions, user *types.HubUser, channel *types.HubChannel) bool {
|
|
channel.Mu.RLock()
|
|
checkAgainst, ok := channel.UsersCachedPermissions[user.OriginalId]
|
|
channel.Mu.RUnlock()
|
|
if !ok || (permissions&checkAgainst) != permissions {
|
|
return false
|
|
}
|
|
return true
|
|
}
|
|
|
|
func addHubUserToPermissionCache(hub *types.Hub, user *types.HubUser) {
|
|
user.Mu.RLock()
|
|
roles := user.Roles
|
|
userId := user.OriginalId
|
|
user.Mu.RUnlock()
|
|
|
|
for _, group := range hub.Groups {
|
|
if group == nil {
|
|
continue
|
|
}
|
|
if !roles.HasSameId(group.RolesCanView) {
|
|
continue
|
|
}
|
|
group.UsersCachedPermissions[userId] = types.CachedUserCanView
|
|
|
|
for _, channel := range group.Channels {
|
|
var perms types.CachedUserPermissions
|
|
|
|
if roles.HasSameId(channel.RolesCanView) {
|
|
perms |= types.CachedUserCanView
|
|
}
|
|
if roles.HasSameId(channel.RolesCanReadHistory) {
|
|
perms |= types.CachedUserCanReadHistory
|
|
}
|
|
if roles.HasSameId(channel.RolesCanMessage) {
|
|
perms |= types.CachedUserCanMessage
|
|
}
|
|
channel.Mu.Lock()
|
|
channel.UsersCachedPermissions[userId] = perms
|
|
channel.Mu.Unlock()
|
|
}
|
|
}
|
|
}
|
|
|
|
func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
|
if !validCheckWithResponseOnFail(&response, request, normal) {
|
|
return
|
|
}
|
|
|
|
ctx := request.Context()
|
|
user, err := getUserByToken(ctx, request.Header.Get("token"))
|
|
if err != nil {
|
|
http.Error(response, "invalid token", http.StatusUnauthorized)
|
|
return
|
|
}
|
|
|
|
hubName := request.FormValue("hubname")
|
|
if hubName == "" {
|
|
http.Error(response, "hub name is required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
hub := types.NewHub()
|
|
hub.Name = hubName
|
|
hub.Color = types.Rgba{}.GetRandom()
|
|
hub.Id = uuid.New()
|
|
hub.Creator = user.Id
|
|
hub.CreatedAt = time.Now()
|
|
|
|
creator := types.NewHubUser()
|
|
creator.OriginalId = user.Id
|
|
creator.CreatedAt = hub.CreatedAt
|
|
hub.Users[creator.OriginalId] = creator
|
|
|
|
rootRole := &types.HubRole{
|
|
Id: uint8(0),
|
|
Permissions: types.PermissionAll(),
|
|
Name: "root",
|
|
Color: types.Rgba{}.GetRandom(),
|
|
CreatedAt: hub.CreatedAt,
|
|
}
|
|
hub.Roles[rootRole.Id] = rootRole
|
|
creator.Roles.Add(rootRole.Id)
|
|
|
|
memberRole := &types.HubRole{
|
|
Id: types.HubBoundRolesMax,
|
|
Name: "member",
|
|
Color: types.Rgba{}.GetRandom(),
|
|
CreatedAt: hub.CreatedAt,
|
|
}
|
|
hub.JoinRole = memberRole
|
|
hub.Roles[memberRole.Id] = memberRole
|
|
creator.Roles.Add(memberRole.Id)
|
|
|
|
rootGroup := types.NewHubGroup()
|
|
rootGroup.Name = "root"
|
|
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()
|
|
channel.Name = "main channel"
|
|
channel.Position = uint8(0)
|
|
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)
|
|
channel.UsersCachedPermissions[creator.OriginalId] = types.CachedUserPermissionsAll
|
|
rootGroup.Channels[channel.Id] = channel
|
|
|
|
cache.SaveHub(hub)
|
|
}
|
|
|
|
func HandleHubJoin(response http.ResponseWriter, request *http.Request) {
|
|
if !validCheckWithResponseOnFail(&response, request, normal) {
|
|
return
|
|
}
|
|
ctx := request.Context()
|
|
user, err := getUserByToken(ctx, request.Header.Get("token"))
|
|
if err != nil {
|
|
http.Error(response, "invalid token", http.StatusBadRequest)
|
|
return
|
|
}
|
|
hub, err := getHubByIdStr(ctx, request.Header.Get("hubid"))
|
|
if err != nil {
|
|
http.Error(response, "invalid hubid", http.StatusBadRequest)
|
|
return
|
|
}
|
|
|
|
hubUser := types.NewHubUser()
|
|
hubUser.OriginalId = user.Id
|
|
hubUser.Roles.Add(hub.JoinRole.Id)
|
|
hubUser.CreatedAt = time.Now()
|
|
hub.Users[hubUser.OriginalId] = hubUser
|
|
addHubUserToPermissionCache(hub, hubUser)
|
|
}
|