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
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user