add bit mask for bounded roles resolving
This commit is contained in:
+33
-101
@@ -4,32 +4,11 @@ import (
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"go-socket/packages/Enums/permission"
|
||||
"go-socket/packages/cache"
|
||||
"go-socket/packages/postgresql"
|
||||
"go-socket/packages/types"
|
||||
|
||||
"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) {
|
||||
if !validCheckWithResponseOnFail(&response, request, normal) {
|
||||
return
|
||||
@@ -48,96 +27,49 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
hub := types.CreateHub()
|
||||
hub := types.NewHub()
|
||||
hub.Name = hubName
|
||||
hub.Creator = user.Id
|
||||
hub.Color = types.Rgba{}.GetRandom()
|
||||
hub.Id = uuid.New()
|
||||
hub.Creator = user.Id
|
||||
hub.CreatedAt = time.Now()
|
||||
|
||||
rootGrp := &types.HubChannelGroup{
|
||||
Id: uuid.New(),
|
||||
Name: "root",
|
||||
Color: types.Rgba{6, 2, 1, 255},
|
||||
Position: uint8(0),
|
||||
}
|
||||
hub.ChannelGroups[rootGrp.Id] = rootGrp
|
||||
creator := types.NewHubUser()
|
||||
creator.Name = user.Name
|
||||
creator.OriginalId = user.Id
|
||||
creator.CreatedAt = hub.CreatedAt
|
||||
|
||||
rootRole := &types.HubRole{
|
||||
Id: 0,
|
||||
Id: uint8(0),
|
||||
Permissions: types.PermissionAll(),
|
||||
Name: "root",
|
||||
Color: types.Rgba{255, 0, 0, 255},
|
||||
Permissions: ^permission.Global(0),
|
||||
}
|
||||
hub.GlobalRoles[rootRole.Id] = rootRole
|
||||
|
||||
rootUser := &types.HubUser{
|
||||
Id: user.Id,
|
||||
Username: user.Name,
|
||||
GlobalRoles: []uint8{rootRole.Id},
|
||||
Color: types.Rgba{}.GetRandom(),
|
||||
CreatedAt: hub.CreatedAt,
|
||||
}
|
||||
hub.Users[rootUser.Id] = rootUser
|
||||
hub.Roles[rootRole.Id] = rootRole
|
||||
creator.Roles.Add(rootRole.Id)
|
||||
|
||||
err = postgresql.HubSave(ctx, hub)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
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
|
||||
memberRole := &types.HubRole{
|
||||
Id: uint8(255),
|
||||
Name: "member",
|
||||
Color: types.Rgba{}.GetRandom(),
|
||||
CreatedAt: hub.CreatedAt,
|
||||
}
|
||||
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
@@ -2,6 +2,7 @@ package types
|
||||
|
||||
import (
|
||||
"crypto/sha256"
|
||||
"math"
|
||||
"math/rand/v2"
|
||||
"sync"
|
||||
"time"
|
||||
@@ -146,6 +147,8 @@ const (
|
||||
PermissionChangeRoleName
|
||||
PermissionChangeRoleColor
|
||||
PermissionChangeRoleGlobals
|
||||
|
||||
PermissionNoSelfRoleRemove
|
||||
PermissionOnlySelfRoleRemove
|
||||
|
||||
// Channel group permissions
|
||||
@@ -165,26 +168,57 @@ const (
|
||||
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 {
|
||||
Mu sync.RWMutex `json:"-"`
|
||||
Roles []*HubRole `json:"-"`
|
||||
Users []*HubUser `json:"-"`
|
||||
Groups []*HubGroup `json:"-"`
|
||||
Channels []*HubChannel `json:"-"`
|
||||
Name string `json:"name"`
|
||||
IconUrl string `json:"iconUrl"`
|
||||
BgUrl string `json:"bgUrl"`
|
||||
Id uuid.UUID `json:"id"`
|
||||
Color Rgba `json:"color"`
|
||||
UserColorAllowed bool `json:"userColorAllowed"`
|
||||
Mu sync.RWMutex `json:"-"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Roles [256]*HubRole `json:"-"`
|
||||
Users [256]*HubUser `json:"-"`
|
||||
Groups [256]*HubGroup `json:"-"`
|
||||
Channels map[uuid.UUID]*HubChannel `json:"-"`
|
||||
Name string `json:"name"`
|
||||
IconUrl string `json:"iconUrl"`
|
||||
BgUrl string `json:"backgroundUrl"`
|
||||
JoinRole *HubRole `json:"joinRole"`
|
||||
Id uuid.UUID `json:"id"`
|
||||
Creator uuid.UUID `json:"creator"`
|
||||
Color Rgba `json:"color"`
|
||||
UserColorAllowed bool `json:"userColorAllowed"`
|
||||
}
|
||||
|
||||
func NewHub() *Hub {
|
||||
return &Hub{
|
||||
Roles: make([]*HubRole, 0, 255),
|
||||
Users: make([]*HubUser, 0, 255),
|
||||
Groups: make([]*HubGroup, 0, 255),
|
||||
Channels: make([]*HubChannel, 0, 255),
|
||||
Channels: make(map[uuid.UUID]*HubChannel),
|
||||
}
|
||||
}
|
||||
|
||||
@@ -208,36 +242,45 @@ func (h *HubRole) HasPermission(r Permissions) bool {
|
||||
}
|
||||
|
||||
type HubGroup struct {
|
||||
Name string `json:"name"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Color Rgba `json:"color"`
|
||||
Id uint8 `json:"id"` // Id 0 for unused
|
||||
Name string `json:"name"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Color Rgba `json:"color"`
|
||||
RolesCanView BoundRoles `json:"rolesCanView"`
|
||||
Id uint8 `json:"id"` // Id 0 for unused
|
||||
}
|
||||
|
||||
func NewHubGroup() *HubGroup {
|
||||
return &HubGroup{}
|
||||
}
|
||||
|
||||
type HubUser struct {
|
||||
Mu sync.RWMutex `json:"mu"`
|
||||
Roles []*HubRole `json:"roles"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
Roles BoundRoles `json:"-"`
|
||||
Name string `json:"name"`
|
||||
OriginalId uuid.UUID `json:"originalId"`
|
||||
IsMuted bool `json:"isMuted"`
|
||||
}
|
||||
|
||||
func NewHubUser() *HubUser {
|
||||
return &HubUser{
|
||||
Roles: make([]*HubRole, 0, config.MaxUserHubRoles),
|
||||
}
|
||||
return &HubUser{}
|
||||
}
|
||||
|
||||
type HubChannel struct {
|
||||
Mu sync.RWMutex `json:"-"`
|
||||
MessagesBuff []*Message `json:"-"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
IconUrl string `json:"iconUrl"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
NextBuffIdx uint32 `json:"-"`
|
||||
Id uint8 `json:"id"`
|
||||
HaveOverflowed bool `json:"-"`
|
||||
Mu sync.RWMutex `json:"-"`
|
||||
MessagesBuff []*Message `json:"-"`
|
||||
Name string `json:"name"`
|
||||
Description string `json:"description"`
|
||||
IconUrl string `json:"iconUrl"`
|
||||
CreatedAt time.Time `json:"createdAt"`
|
||||
RolesCanView BoundRoles `json:"rolesCanView"`
|
||||
RolesCanMessage BoundRoles `json:"rolesCanMessage"`
|
||||
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 {
|
||||
|
||||
Reference in New Issue
Block a user