add bit mask for bounded roles resolving

This commit is contained in:
2026-04-25 20:42:37 +02:00
parent df1e969d49
commit f22bb43346
2 changed files with 108 additions and 133 deletions
+33 -101
View File
@@ -4,32 +4,11 @@ import (
"net/http" "net/http"
"time" "time"
"go-socket/packages/Enums/permission"
"go-socket/packages/cache"
"go-socket/packages/postgresql"
"go-socket/packages/types" "go-socket/packages/types"
"github.com/google/uuid" "github.com/google/uuid"
) )
func hasHubUserGlobalPermission(user *types.HubUser, hub *types.Hub, perm permission.Global) bool {
for _, roleId := range user.GlobalRoles {
if role, ok := hub.GlobalRoles[roleId]; ok && role.HasPermission(perm) {
return true
}
}
return false
}
func hasHubUserChannelGroupPermission(user *types.HubUser, hub *types.Hub, perm permission.ChannelGroup) bool {
for _, roleId := range user.ChannelGroupRoles {
if role, ok := hub.ChannelGroupRoles[roleId]; ok && role.HasPermission(perm) {
return true
}
}
return false
}
func HandleHubCreate(response http.ResponseWriter, request *http.Request) { func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
if !validCheckWithResponseOnFail(&response, request, normal) { if !validCheckWithResponseOnFail(&response, request, normal) {
return return
@@ -48,96 +27,49 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
return return
} }
hub := types.CreateHub() hub := types.NewHub()
hub.Name = hubName hub.Name = hubName
hub.Creator = user.Id
hub.Color = types.Rgba{}.GetRandom() hub.Color = types.Rgba{}.GetRandom()
hub.Id = uuid.New()
hub.Creator = user.Id
hub.CreatedAt = time.Now() hub.CreatedAt = time.Now()
rootGrp := &types.HubChannelGroup{ creator := types.NewHubUser()
Id: uuid.New(), creator.Name = user.Name
Name: "root", creator.OriginalId = user.Id
Color: types.Rgba{6, 2, 1, 255}, creator.CreatedAt = hub.CreatedAt
Position: uint8(0),
}
hub.ChannelGroups[rootGrp.Id] = rootGrp
rootRole := &types.HubRole{ rootRole := &types.HubRole{
Id: 0, Id: uint8(0),
Permissions: types.PermissionAll(),
Name: "root", Name: "root",
Color: types.Rgba{255, 0, 0, 255}, Color: types.Rgba{}.GetRandom(),
Permissions: ^permission.Global(0),
}
hub.GlobalRoles[rootRole.Id] = rootRole
rootUser := &types.HubUser{
Id: user.Id,
Username: user.Name,
GlobalRoles: []uint8{rootRole.Id},
CreatedAt: hub.CreatedAt, CreatedAt: hub.CreatedAt,
} }
hub.Users[rootUser.Id] = rootUser hub.Roles[rootRole.Id] = rootRole
creator.Roles.Add(rootRole.Id)
err = postgresql.HubSave(ctx, hub) memberRole := &types.HubRole{
if err != nil { Id: uint8(255),
http.Error(response, "internal server error", http.StatusInternalServerError) Name: "member",
return Color: types.Rgba{}.GetRandom(),
} CreatedAt: hub.CreatedAt,
if err = postgresql.HubGlobalRoleSave(ctx, hub.Id, rootRole); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
if err = postgresql.HubChannelGroupSave(ctx, hub.Id, rootGrp); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
if err = postgresql.HubUserAdd(ctx, hub.Id, rootUser); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
if err = postgresql.HubUserGlobalRoleAdd(ctx, hub.Id, user.Id, rootRole.Id); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
cache.SaveHub(hub)
response.WriteHeader(http.StatusCreated)
response.Write([]byte(hub.Id.String()))
}
func HandleHubDelete(response http.ResponseWriter, request *http.Request) {
if !validCheckWithResponseOnFail(&response, request, normal) {
return
}
ctx := request.Context()
_, hubUser, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request.Header.Get("token"), request.Header.Get("hubid"))
if err != nil {
return
}
if !hasHubUserGlobalPermission(hubUser, hub, permission.GlobalRemoveHub) {
http.Error(response, "forbidden", http.StatusForbidden)
return
}
if err := postgresql.HubDelete(ctx, hub); err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
cache.DeleteHub(hub)
response.WriteHeader(http.StatusOK)
}
func HandleHubGet(response http.ResponseWriter, request *http.Request) {
if !validCheckWithResponseOnFail(&response, request, normal) {
return
}
ctx := request.Context()
_, _, hub, err := getHubUserIfValidWithResponseOnFail(ctx, response, request.Header.Get("token"), request.Header.Get("hubid"))
if err != nil {
return
} }
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
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!"
hub.Channels[channel.Id] = channel
} }
+75 -32
View File
@@ -2,6 +2,7 @@ package types
import ( import (
"crypto/sha256" "crypto/sha256"
"math"
"math/rand/v2" "math/rand/v2"
"sync" "sync"
"time" "time"
@@ -146,6 +147,8 @@ const (
PermissionChangeRoleName PermissionChangeRoleName
PermissionChangeRoleColor PermissionChangeRoleColor
PermissionChangeRoleGlobals PermissionChangeRoleGlobals
PermissionNoSelfRoleRemove
PermissionOnlySelfRoleRemove PermissionOnlySelfRoleRemove
// Channel group permissions // Channel group permissions
@@ -165,26 +168,57 @@ const (
PermissionSetChannelPermittedReadHistoryRoles PermissionSetChannelPermittedReadHistoryRoles
) )
func (p *Permissions) Grant(permissions Permissions) {
*p |= permissions
}
func (p *Permissions) GrantMultiple(perms []Permissions) {
for _, perm := range perms {
*p |= perm
}
}
func (p *Permissions) Revoke(permissions Permissions) {
*p &^= permissions
}
func (p *Permissions) Has(permissions Permissions) bool {
return *p&permissions != 0
}
func PermissionAll() Permissions {
return math.MaxUint32
}
type BoundRoles uint64
func (b *BoundRoles) Add(id uint8) {
*b |= 1 << id
}
func (b *BoundRoles) Remove(id uint8) {
*b &^= 1 << id
}
func (b *BoundRoles) Has(id uint8) bool {
return *b&(1<<id) != 0
}
type Hub struct { type Hub struct {
Mu sync.RWMutex `json:"-"` Mu sync.RWMutex `json:"-"`
Roles []*HubRole `json:"-"` CreatedAt time.Time `json:"createdAt"`
Users []*HubUser `json:"-"` Roles [256]*HubRole `json:"-"`
Groups []*HubGroup `json:"-"` Users [256]*HubUser `json:"-"`
Channels []*HubChannel `json:"-"` Groups [256]*HubGroup `json:"-"`
Name string `json:"name"` Channels map[uuid.UUID]*HubChannel `json:"-"`
IconUrl string `json:"iconUrl"` Name string `json:"name"`
BgUrl string `json:"bgUrl"` IconUrl string `json:"iconUrl"`
Id uuid.UUID `json:"id"` BgUrl string `json:"backgroundUrl"`
Color Rgba `json:"color"` JoinRole *HubRole `json:"joinRole"`
UserColorAllowed bool `json:"userColorAllowed"` Id uuid.UUID `json:"id"`
Creator uuid.UUID `json:"creator"`
Color Rgba `json:"color"`
UserColorAllowed bool `json:"userColorAllowed"`
} }
func NewHub() *Hub { func NewHub() *Hub {
return &Hub{ return &Hub{
Roles: make([]*HubRole, 0, 255), Channels: make(map[uuid.UUID]*HubChannel),
Users: make([]*HubUser, 0, 255),
Groups: make([]*HubGroup, 0, 255),
Channels: make([]*HubChannel, 0, 255),
} }
} }
@@ -208,36 +242,45 @@ func (h *HubRole) HasPermission(r Permissions) bool {
} }
type HubGroup struct { type HubGroup struct {
Name string `json:"name"` Name string `json:"name"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
Color Rgba `json:"color"` Color Rgba `json:"color"`
Id uint8 `json:"id"` // Id 0 for unused RolesCanView BoundRoles `json:"rolesCanView"`
Id uint8 `json:"id"` // Id 0 for unused
}
func NewHubGroup() *HubGroup {
return &HubGroup{}
} }
type HubUser struct { type HubUser struct {
Mu sync.RWMutex `json:"mu"` Mu sync.RWMutex `json:"mu"`
Roles []*HubRole `json:"roles"` CreatedAt time.Time `json:"createdAt"`
Roles BoundRoles `json:"-"`
Name string `json:"name"` Name string `json:"name"`
OriginalId uuid.UUID `json:"originalId"` OriginalId uuid.UUID `json:"originalId"`
IsMuted bool `json:"isMuted"` IsMuted bool `json:"isMuted"`
} }
func NewHubUser() *HubUser { func NewHubUser() *HubUser {
return &HubUser{ return &HubUser{}
Roles: make([]*HubRole, 0, config.MaxUserHubRoles),
}
} }
type HubChannel struct { type HubChannel struct {
Mu sync.RWMutex `json:"-"` Mu sync.RWMutex `json:"-"`
MessagesBuff []*Message `json:"-"` MessagesBuff []*Message `json:"-"`
Name string `json:"name"` Name string `json:"name"`
Description string `json:"description"` Description string `json:"description"`
IconUrl string `json:"iconUrl"` IconUrl string `json:"iconUrl"`
CreatedAt time.Time `json:"createdAt"` CreatedAt time.Time `json:"createdAt"`
NextBuffIdx uint32 `json:"-"` RolesCanView BoundRoles `json:"rolesCanView"`
Id uint8 `json:"id"` RolesCanMessage BoundRoles `json:"rolesCanMessage"`
HaveOverflowed bool `json:"-"` RolesCanReadHistory BoundRoles `json:"rolesCanReadHistory"`
NextBuffIdx uint32 `json:"-"`
Id uuid.UUID `json:"id"`
ParentId uint8 `json:"parentId"`
Position uint8 `json:"position"`
HaveOverflowed bool `json:"-"`
} }
func NewHubChannel() *HubChannel { func NewHubChannel() *HubChannel {