ad group logic except somes

This commit is contained in:
GitProtogen
2026-03-23 14:24:53 +01:00
parent 9693e9ee88
commit c679b18f39
6 changed files with 191 additions and 13 deletions
+106 -1
View File
@@ -1,6 +1,7 @@
package main
import (
"encoding/binary"
"fmt"
"net/http"
"strconv"
@@ -71,7 +72,7 @@ func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
err = DbSaveClientWithoutGroups(ctx, newClient)
if err != nil {
http.Error(response, "name taken", http.StatusInternalServerError)
http.Error(response, "name taken", http.StatusUnauthorized)
return
}
}
@@ -97,6 +98,7 @@ func HttpHandleLogin(response http.ResponseWriter, request *http.Request) {
err := DbSetClientByName(ctx, &client)
if err != nil {
http.Error(response, "bad login", http.StatusUnauthorized)
return
}
@@ -119,4 +121,107 @@ func HttpHandleGroupCreate(response http.ResponseWriter, request *http.Request)
if !isMethodAllowed(&response, request) {
return
}
token := request.FormValue("token")
clientId, err := TokenValidateGetId(token)
if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized)
return
}
name := request.FormValue("name")
if name == "" {
name = "Best group ever"
}
colorString := request.FormValue("color")
color, err := parseRgb(colorString)
if err != nil {
var ok bool
color, ok = Colors[colorString]
if !ok {
color = Colors["default"]
}
}
ctx := request.Context()
client := Client{Id: clientId}
cacheClient, err := CacheGetClientById(clientId)
if err == nil {
client = *cacheClient
} else {
err = DbSetClientById(ctx, &client)
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
}
group := Group{
Name: name,
CreatedAt: time.Now(),
OwnerId: clientId,
CreatorId: clientId,
Color: color,
}
enableClientColors := request.FormValue("enableClientColors")
if enableClientColors == "1" {
group.EnableClientColors = true
}
err = DbSaveGroupWithoutClients(ctx, &group)
if err != nil {
http.Error(response, "internal server error", http.StatusInternalServerError)
return
}
groupIdBytes := make([]byte, 4)
binary.BigEndian.PutUint32(groupIdBytes, group.Id)
response.Write(groupIdBytes)
}
func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) {
if !isMethodAllowed(&response, request) {
return
}
token := request.FormValue("token")
clientId, err := TokenValidateGetId(token)
if err != nil {
http.Error(response, "invalid token", http.StatusUnauthorized)
return
}
affectedGroupId, err := ConvertStringUint32(request.FormValue("groupid"))
if err != nil {
http.Error(response, "no such group", http.StatusUnauthorized)
return
}
ctx := request.Context()
var group Group
groupPtr, err := CacheGetGroup(affectedGroupId)
if err == nil {
group = *groupPtr
} else {
err = DbSetGroupById(ctx, &group)
if err != nil {
http.Error(response, "no such group", http.StatusUnauthorized)
return
}
}
if group.OwnerId != clientId {
http.Error(response, "no such group", http.StatusUnauthorized)
return
}
usersToAddString := request.FormValue("users")
usersToAdd := strings.SplitN(usersToAddString, ",", int(MaxGroupsForClient))
if len(usersToAdd) == 0 {
http.Error(response, "no users to add", http.StatusBadRequest)
}
}