change client to user
This commit is contained in:
@@ -22,23 +22,23 @@ func isMethodAllowed(response *http.ResponseWriter, request *http.Request) bool
|
||||
return true
|
||||
}
|
||||
|
||||
func getClient(ctx context.Context, token string) (*Client, error) {
|
||||
clientId, err := TokenValidateGetId(token)
|
||||
func getUser(ctx context.Context, token string) (*User, error) {
|
||||
userId, err := TokenValidateGetId(token)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
client, err := CacheGetClientById(clientId)
|
||||
user, err := CacheGetUserById(userId)
|
||||
if err != nil {
|
||||
client = &Client{Id: clientId}
|
||||
err = DbSetClientById(ctx, client)
|
||||
user = &User{Id: userId}
|
||||
err = DbSetUserById(ctx, user)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
CacheSaveClient(client)
|
||||
CacheSaveUser(user)
|
||||
}
|
||||
|
||||
return client, nil
|
||||
return user, nil
|
||||
}
|
||||
|
||||
func getGroup(ctx context.Context, groupId uint32) (*Group, error) {
|
||||
@@ -54,15 +54,15 @@ func getGroup(ctx context.Context, groupId uint32) (*Group, error) {
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func isOwner(client *Client, group *Group) bool {
|
||||
if group.OwnerId == client.Id {
|
||||
func isOwner(user *User, group *Group) bool {
|
||||
if group.OwnerId == user.Id {
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func getIfOwnerClientAndGroup(ctx context.Context, response *http.ResponseWriter, request *http.Request) (*Client, *Group, error) {
|
||||
client, err := getClient(ctx, request.FormValue("token"))
|
||||
func getIfOwnerUserAndGroup(ctx context.Context, response *http.ResponseWriter, request *http.Request) (*User, *Group, error) {
|
||||
user, err := getUser(ctx, request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(*response, "invalid token", http.StatusUnauthorized)
|
||||
return nil, nil, err
|
||||
@@ -80,14 +80,14 @@ func getIfOwnerClientAndGroup(ctx context.Context, response *http.ResponseWriter
|
||||
return nil, nil, err
|
||||
}
|
||||
|
||||
if !isOwner(client, group) {
|
||||
if !isOwner(user, group) {
|
||||
http.Error(*response, "no such group", http.StatusUnauthorized)
|
||||
return nil, nil, err
|
||||
}
|
||||
return client, group, nil
|
||||
return user, group, nil
|
||||
}
|
||||
|
||||
func HttpHandleNewClient(response http.ResponseWriter, request *http.Request) {
|
||||
func HttpHandleNewUser(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
@@ -116,7 +116,7 @@ func HttpHandleNewClient(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
newClient := &Client{
|
||||
newUser := &User{
|
||||
Name: username,
|
||||
PasswordHash: hashedPassword,
|
||||
Color: color,
|
||||
@@ -125,14 +125,13 @@ func HttpHandleNewClient(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
ctx := request.Context()
|
||||
|
||||
err = DbSaveClientWithoutGroups(ctx, newClient)
|
||||
err = DbSaveUserWithoutGroups(ctx, newUser)
|
||||
if err != nil {
|
||||
http.Error(response, "name taken", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusCreated)
|
||||
response.Write([]byte("created"))
|
||||
}
|
||||
|
||||
func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
@@ -154,30 +153,30 @@ func HttpHandleNewToken(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
var (
|
||||
client *Client
|
||||
err error
|
||||
ctx = request.Context()
|
||||
user *User
|
||||
err error
|
||||
ctx = request.Context()
|
||||
)
|
||||
|
||||
client, err = CacheGetClientByName(username)
|
||||
user, err = CacheGetUserByName(username)
|
||||
if err != nil {
|
||||
client = &Client{Name: username}
|
||||
user = &User{Name: username}
|
||||
|
||||
err := DbSetClientByName(ctx, client)
|
||||
err := DbSetUserByName(ctx, user)
|
||||
if err != nil {
|
||||
http.Error(response, "bad login1", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
CacheSaveClient(client)
|
||||
CacheSaveUser(user)
|
||||
}
|
||||
|
||||
err = bcrypt.CompareHashAndPassword([]byte(client.PasswordHash), []byte(password))
|
||||
err = bcrypt.CompareHashAndPassword([]byte(user.PasswordHash), []byte(password))
|
||||
if err != nil {
|
||||
http.Error(response, "bad login2", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
token, err := TokenCreate(client.Id)
|
||||
token, err := TokenCreate(user.Id)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
@@ -194,7 +193,7 @@ func HttpHandeGroupCreate(response http.ResponseWriter, request *http.Request) {
|
||||
|
||||
ctx := request.Context()
|
||||
|
||||
client, err := getClient(ctx, request.FormValue("token"))
|
||||
user, err := getUser(ctx, request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
@@ -211,18 +210,18 @@ func HttpHandeGroupCreate(response http.ResponseWriter, request *http.Request) {
|
||||
group := Group{
|
||||
Name: name,
|
||||
CreatedAt: time.Now(),
|
||||
OwnerId: client.Id,
|
||||
CreatorId: client.Id,
|
||||
OwnerId: user.Id,
|
||||
CreatorId: user.Id,
|
||||
Color: color,
|
||||
Clients: map[uint32]struct{}{client.Id: {}},
|
||||
Users: map[uint32]struct{}{user.Id: {}},
|
||||
}
|
||||
|
||||
enableClientColors := request.FormValue("enableClientColors")
|
||||
if enableClientColors == "1" {
|
||||
group.EnableClientColors = true
|
||||
enableUserColors := request.FormValue("enableUserColors")
|
||||
if enableUserColors == "1" {
|
||||
group.EnableUserColors = true
|
||||
}
|
||||
|
||||
err = DbSaveGroupWithoutClients(ctx, &group)
|
||||
err = DbSaveGroupWithoutUsers(ctx, &group)
|
||||
if err != nil {
|
||||
http.Error(response, err.Error(), http.StatusInternalServerError)
|
||||
return
|
||||
@@ -237,7 +236,7 @@ func HttpHandleGroupRemove(response http.ResponseWriter, request *http.Request)
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
_, group, err := getIfOwnerClientAndGroup(ctx, &response, request)
|
||||
_, group, err := getIfOwnerUserAndGroup(ctx, &response, request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -251,35 +250,35 @@ func HttpHandleGroupRemove(response http.ResponseWriter, request *http.Request)
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Request) {
|
||||
func HttpHandleGroupAddUser(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
|
||||
_, group, err := getIfOwnerClientAndGroup(ctx, &response, request)
|
||||
_, group, err := getIfOwnerUserAndGroup(ctx, &response, request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
clientsString := request.FormValue("clients")
|
||||
var remainingUsersCount = int(MaxClientsInGroup) - len(group.Clients)
|
||||
usersString := request.FormValue("users")
|
||||
var remainingUsersCount = int(MaxUsersInGroup) - len(group.Users)
|
||||
if remainingUsersCount < 1 {
|
||||
http.Error(response, "max users", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
clientsStringSlice := strings.SplitN(clientsString, ",", remainingUsersCount+1)
|
||||
if len(clientsStringSlice) == 0 {
|
||||
usersStringSlice := strings.SplitN(usersString, ",", remainingUsersCount+1)
|
||||
if len(usersStringSlice) == 0 {
|
||||
http.Error(response, "no users to add", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
var ids [MaxClientsInGroup]uint32
|
||||
var ids [MaxUsersInGroup]uint32
|
||||
var idx uint32 = 0
|
||||
for _, s := range clientsStringSlice {
|
||||
if idx >= MaxClientsInGroup {
|
||||
for _, s := range usersStringSlice {
|
||||
if idx >= MaxUsersInGroup {
|
||||
break
|
||||
}
|
||||
id, err := ConvertStringUint32(strings.TrimSpace(s))
|
||||
@@ -294,39 +293,39 @@ func HttpHandleGroupAddClient(response http.ResponseWriter, request *http.Reques
|
||||
return
|
||||
}
|
||||
|
||||
err = DbAddClientsToGroup(ctx, group.Id, &ids)
|
||||
err = DbAddUsersToGroup(ctx, group.Id, &ids)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
for i := uint32(0); i < idx; i++ {
|
||||
group.Clients[ids[i]] = struct{}{}
|
||||
group.Users[ids[i]] = struct{}{}
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
}
|
||||
|
||||
func HttpHandleGroupRemoveClient(response http.ResponseWriter, request *http.Request) {
|
||||
func HttpHandleGroupRemoveUser(response http.ResponseWriter, request *http.Request) {
|
||||
if !isMethodAllowed(&response, request) {
|
||||
return
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
|
||||
_, group, err := getIfOwnerClientAndGroup(ctx, &response, request)
|
||||
_, group, err := getIfOwnerUserAndGroup(ctx, &response, request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
clientsString := request.FormValue("clients")
|
||||
usersString := request.FormValue("users")
|
||||
|
||||
clientsStringSlice := strings.SplitN(clientsString, ",", int(MaxClientsInGroup)+1)
|
||||
usersStringSlice := strings.SplitN(usersString, ",", int(MaxUsersInGroup)+1)
|
||||
|
||||
var ids [MaxClientsInGroup]uint32
|
||||
var ids [MaxUsersInGroup]uint32
|
||||
var idx uint32 = 0
|
||||
for _, s := range clientsStringSlice {
|
||||
if idx >= MaxClientsInGroup {
|
||||
for _, s := range usersStringSlice {
|
||||
if idx >= MaxUsersInGroup {
|
||||
break
|
||||
}
|
||||
id, err := ConvertStringUint32(strings.TrimSpace(s))
|
||||
@@ -341,14 +340,14 @@ func HttpHandleGroupRemoveClient(response http.ResponseWriter, request *http.Req
|
||||
return
|
||||
}
|
||||
|
||||
count, err := DbRemoveClientsFromGroup(ctx, group.Id, &ids)
|
||||
count, err := DbRemoveUsersFromGroup(ctx, group.Id, &ids)
|
||||
if err != nil {
|
||||
http.Error(response, "internal server error", http.StatusInternalServerError)
|
||||
return
|
||||
}
|
||||
|
||||
for i := uint32(0); i < idx; i++ {
|
||||
delete(group.Clients, ids[i])
|
||||
delete(group.Users, ids[i])
|
||||
}
|
||||
|
||||
response.WriteHeader(http.StatusAccepted)
|
||||
@@ -361,7 +360,7 @@ func HttpHandleGroupChangeColor(response http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
_, group, err := getIfOwnerClientAndGroup(ctx, &response, request)
|
||||
_, group, err := getIfOwnerUserAndGroup(ctx, &response, request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
@@ -389,28 +388,28 @@ func HttpHandleGroupChangeOwner(response http.ResponseWriter, request *http.Requ
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
client, group, err := getIfOwnerClientAndGroup(ctx, &response, request)
|
||||
user, group, err := getIfOwnerUserAndGroup(ctx, &response, request)
|
||||
if err != nil {
|
||||
return
|
||||
}
|
||||
|
||||
newOwnerName := request.FormValue("newOwner")
|
||||
|
||||
newOwner, err := CacheGetClientByName(newOwnerName)
|
||||
newOwner, err := CacheGetUserByName(newOwnerName)
|
||||
if err != nil {
|
||||
newOwner = &Client{Name: newOwnerName}
|
||||
err = DbSetClientByName(ctx, newOwner)
|
||||
newOwner = &User{Name: newOwnerName}
|
||||
err = DbSetUserByName(ctx, newOwner)
|
||||
if err != nil {
|
||||
http.Error(response, "client not in group", http.StatusBadRequest)
|
||||
http.Error(response, "user not in group", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
CacheSaveClient(client)
|
||||
CacheSaveUser(user)
|
||||
}
|
||||
|
||||
_, ok := group.Clients[newOwner.Id]
|
||||
_, ok := group.Users[newOwner.Id]
|
||||
if !ok {
|
||||
http.Error(response, "client not in group", http.StatusBadRequest)
|
||||
http.Error(response, "user not in group", http.StatusBadRequest)
|
||||
return
|
||||
}
|
||||
|
||||
@@ -430,7 +429,7 @@ func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
client, err := getClient(ctx, request.FormValue("token"))
|
||||
user, err := getUser(ctx, request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
@@ -453,7 +452,7 @@ func HttpHandleNewMessage(response http.ResponseWriter, request *http.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
err = WsSendToGroup(ctx, targetId, client.Id, content)
|
||||
err = WsSendToGroup(ctx, targetId, user.Id, content)
|
||||
if err != nil {
|
||||
http.Error(response, err.Error(), http.StatusBadRequest)
|
||||
return
|
||||
@@ -468,28 +467,28 @@ func HttpHandleGroupsGetWithoutMembers(response http.ResponseWriter, request *ht
|
||||
|
||||
ctx := request.Context()
|
||||
|
||||
client, err := getClient(ctx, request.FormValue("token"))
|
||||
user, err := getUser(ctx, request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
}
|
||||
|
||||
groups := make([]GroupNoMembers, 0, len(client.Groups))
|
||||
groups := make([]GroupNoMembers, 0, len(user.Groups))
|
||||
|
||||
for groupId := range client.Groups {
|
||||
for groupId := range user.Groups {
|
||||
group, err := getGroup(ctx, groupId)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
|
||||
groups = append(groups, GroupNoMembers{
|
||||
Id: groupId,
|
||||
Name: group.Name,
|
||||
CreatedAt: group.CreatedAt,
|
||||
CreatorId: group.CreatorId,
|
||||
OwnerId: group.OwnerId,
|
||||
Color: group.Color,
|
||||
EnableClientsColors: group.EnableClientColors,
|
||||
Id: groupId,
|
||||
Name: group.Name,
|
||||
CreatedAt: group.CreatedAt,
|
||||
CreatorId: group.CreatorId,
|
||||
OwnerId: group.OwnerId,
|
||||
Color: group.Color,
|
||||
EnableUsersColors: group.EnableUserColors,
|
||||
})
|
||||
}
|
||||
|
||||
@@ -509,7 +508,7 @@ func HttpHandleGroupMembersGet(response http.ResponseWriter, request *http.Reque
|
||||
}
|
||||
|
||||
ctx := request.Context()
|
||||
client, err := getClient(ctx, request.FormValue("token"))
|
||||
user, err := getUser(ctx, request.FormValue("token"))
|
||||
if err != nil {
|
||||
http.Error(response, "invalid token", http.StatusUnauthorized)
|
||||
return
|
||||
@@ -522,7 +521,7 @@ func HttpHandleGroupMembersGet(response http.ResponseWriter, request *http.Reque
|
||||
return
|
||||
}
|
||||
|
||||
_, ok := client.Groups[groupId]
|
||||
_, ok := user.Groups[groupId]
|
||||
if !ok {
|
||||
http.Error(response, "no such group", http.StatusUnauthorized)
|
||||
return
|
||||
@@ -534,7 +533,7 @@ func HttpHandleGroupMembersGet(response http.ResponseWriter, request *http.Reque
|
||||
return
|
||||
}
|
||||
|
||||
groupMembers := slices.Collect(maps.Keys(group.Clients))
|
||||
groupMembers := slices.Collect(maps.Keys(group.Users))
|
||||
|
||||
json, err := json2.Marshal(groupMembers)
|
||||
if err != nil {
|
||||
|
||||
Reference in New Issue
Block a user