add channel: remove create set name logic
This commit is contained in:
@@ -25,7 +25,7 @@ func haveHubUserPermission(u *types.HubUser, h *types.Hub, needed types.Permissi
|
||||
if role == nil {
|
||||
continue
|
||||
}
|
||||
if !u.Roles.Has(role.Id) {
|
||||
if !u.Roles.ContainsRoleId(role.Id) {
|
||||
continue
|
||||
}
|
||||
if (needed&role.Permissions) == needed && role.Id > id {
|
||||
@@ -44,7 +44,7 @@ func hubUserHighestRoleId(u *types.HubUser, h *types.Hub) (id uint8) {
|
||||
if role == nil {
|
||||
continue
|
||||
}
|
||||
if !u.Roles.Has(role.Id) {
|
||||
if !u.Roles.ContainsRoleId(role.Id) {
|
||||
continue
|
||||
}
|
||||
if role.Id > id {
|
||||
@@ -54,6 +54,34 @@ func hubUserHighestRoleId(u *types.HubUser, h *types.Hub) (id uint8) {
|
||||
return id
|
||||
}
|
||||
|
||||
func updateUserChannelCache(u *types.HubUser, c *types.HubChannel) {
|
||||
c.Mu.Lock()
|
||||
defer c.Mu.Unlock()
|
||||
if u.Roles.DoesIntersect(c.RolesCanView) {
|
||||
c.UsersCachedPermissions[u.OriginalId] |= types.CachedUserCanView
|
||||
} else {
|
||||
c.UsersCachedPermissions[u.OriginalId] &^= types.CachedUserCanView
|
||||
}
|
||||
if u.Roles.DoesIntersect(c.RolesCanReadHistory) {
|
||||
c.UsersCachedPermissions[u.OriginalId] |= types.CachedUserCanReadHistory
|
||||
} else {
|
||||
c.UsersCachedPermissions[u.OriginalId] &^= types.CachedUserCanReadHistory
|
||||
}
|
||||
if u.Roles.DoesIntersect(c.RolesCanMessage) {
|
||||
c.UsersCachedPermissions[u.OriginalId] |= types.CachedUserCanMessage
|
||||
} else {
|
||||
c.UsersCachedPermissions[u.OriginalId] &^= types.CachedUserCanMessage
|
||||
}
|
||||
}
|
||||
|
||||
func updateChannelCache(c *types.HubChannel, h *types.Hub) {
|
||||
h.Mu.RLock()
|
||||
defer h.Mu.RUnlock()
|
||||
for _, u := range h.Users {
|
||||
updateUserChannelCache(u, c)
|
||||
}
|
||||
}
|
||||
|
||||
func haveHubUserCachedPermissions(needed types.CachedUserPermissions, user *types.HubUser, channel *types.HubChannel) bool {
|
||||
channel.Mu.RLock()
|
||||
checkAgainst, ok := channel.UsersCachedPermissions[user.OriginalId]
|
||||
@@ -117,7 +145,6 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
channel := types.NewHubChannel()
|
||||
channel.Name = "main channel"
|
||||
channel.Position = uint8(0)
|
||||
channel.Id = uuid.New()
|
||||
channel.Description = "The fist channel!"
|
||||
channel.CreatedAt = hub.CreatedAt
|
||||
@@ -128,7 +155,7 @@ func HandleHubCreate(response http.ResponseWriter, request *http.Request) {
|
||||
channel.RolesCanReadHistory.Add(rootRole.Id)
|
||||
channel.RolesCanReadHistory.Add(memberRole.Id)
|
||||
channel.UsersCachedPermissions[creator.OriginalId] = types.CachedUserPermissionsAll
|
||||
hub.Channels[channel.Id] = channel
|
||||
hub.Channels[0] = channel
|
||||
|
||||
cache.SaveHub(hub)
|
||||
}
|
||||
@@ -534,7 +561,7 @@ func HandleHubRemoveRole(response http.ResponseWriter, request *http.Request) {
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandlePermissionSetRoleName(response http.ResponseWriter, request *http.Request) {
|
||||
func HandleRoleSetName(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRoleName)
|
||||
if !ok {
|
||||
return
|
||||
@@ -565,7 +592,7 @@ func HandlePermissionSetRoleName(response http.ResponseWriter, request *http.Req
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandlePermissionSetRoleColor(response http.ResponseWriter, request *http.Request) {
|
||||
func HandleRoleSetColor(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRoleColor)
|
||||
if !ok {
|
||||
return
|
||||
@@ -596,7 +623,7 @@ func HandlePermissionSetRoleColor(response http.ResponseWriter, request *http.Re
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandlePermissionSetRolePermissions(response http.ResponseWriter, request *http.Request) {
|
||||
func HandleRoleSetPermissions(response http.ResponseWriter, request *http.Request) {
|
||||
requestor, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionSetRolePermissions)
|
||||
if !ok {
|
||||
return
|
||||
@@ -642,7 +669,7 @@ func HandlePermissionSetRolePermissions(response http.ResponseWriter, request *h
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandlePermissionSelfRoleRemove(response http.ResponseWriter, request *http.Request) {
|
||||
func HandleRoleSelfRemove(response http.ResponseWriter, request *http.Request) {
|
||||
requestor, _, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionUserSelfRoleRemove)
|
||||
if !ok {
|
||||
return
|
||||
@@ -650,3 +677,86 @@ func HandlePermissionSelfRoleRemove(response http.ResponseWriter, request *http.
|
||||
requestor.Roles.Remove(usedRoleId)
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleChannelCreate(response http.ResponseWriter, request *http.Request) {
|
||||
requestor, hub, _, usedRoleId, ok := hubPermissionContext(response, request, normal, types.PermissionCreateChannel)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
name := request.FormValue("name")
|
||||
if name == "" { // TODO think about some restriction to not be too big
|
||||
http.Error(response, "name empty", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
newHubChannel := types.NewHubChannel()
|
||||
newHubChannel.Name = name
|
||||
newHubChannel.Id = uuid.New()
|
||||
newHubChannel.CreatedAt = time.Now()
|
||||
|
||||
newHubChannel.RolesCanView.Add(usedRoleId)
|
||||
newHubChannel.RolesCanMessage.Add(usedRoleId)
|
||||
newHubChannel.RolesCanReadHistory.Add(usedRoleId)
|
||||
|
||||
if requestor.Roles.ContainsRoleId(types.HubBoundRolesMax) {
|
||||
newHubChannel.RolesCanView.Add(types.HubBoundRolesMax)
|
||||
newHubChannel.RolesCanMessage.Add(types.HubBoundRolesMax)
|
||||
newHubChannel.RolesCanReadHistory.Add(types.HubBoundRolesMax)
|
||||
}
|
||||
hub.Mu.Lock()
|
||||
for i, channel := range hub.Channels {
|
||||
if channel != nil {
|
||||
continue
|
||||
}
|
||||
hub.Channels[i] = newHubChannel
|
||||
break
|
||||
}
|
||||
hub.Mu.Unlock()
|
||||
|
||||
updateChannelCache(newHubChannel, hub)
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HandleChannelRemove(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionRemoveChannel)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
channelId, err := convertions.StringToUint8(request.FormValue("id"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid channel id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
hub.Mu.Lock()
|
||||
hub.Channels[channelId] = nil
|
||||
hub.Mu.Unlock()
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
func HandleChannelSetName(response http.ResponseWriter, request *http.Request) {
|
||||
_, hub, _, _, ok := hubPermissionContext(response, request, normal, types.PermissionSetChannelName)
|
||||
if !ok {
|
||||
return
|
||||
}
|
||||
newName := request.FormValue("newname")
|
||||
if newName == "" {
|
||||
http.Error(response, "newname empty", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
channelId, err := convertions.StringToUint8(request.FormValue("id"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid channel id", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
hub.Mu.Lock()
|
||||
channel := hub.Channels[channelId]
|
||||
if channel == nil {
|
||||
http.Error(response, "no such channel", http.StatusNotFound)
|
||||
return
|
||||
}
|
||||
channel.Name = newName
|
||||
hub.Mu.Unlock()
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user