fixed updating users on page roles and users

main
alex 2023-09-16 11:53:56 +02:00
parent 6493f0f689
commit e539552f7e
10 changed files with 165 additions and 44 deletions

BIN
main

Binary file not shown.

View File

@ -4,4 +4,10 @@ package structs
type AdminAreaRolesResponse struct { type AdminAreaRolesResponse struct {
Roles []Role Roles []Role
RolesPermissions []RolePermissions RolesPermissions []RolePermissions
Users []AdminAreaUserInfo
}
type AdminAreaUserInfo struct {
Id string
RoleId string
} }

View File

@ -63,7 +63,7 @@ type UserInfoResponse struct {
Avatar string Avatar string
Permissions []string Permissions []string
AvailableCategories []string AvailableCategories []string
Users []AllUsers Users []UserShortInfoResponse
TotalNotifications int TotalNotifications int
} }
@ -73,3 +73,10 @@ type UserProfileResponse struct {
Sessions []UserSessionSocket Sessions []UserSessionSocket
ApiKeys []UserApiKey ApiKeys []UserApiKey
} }
// swagger:model UserShortInfoResponse
type UserShortInfoResponse struct {
Id string
Avatar string
Username string
}

View File

@ -4,5 +4,12 @@ package structs
type UsersResponse struct { type UsersResponse struct {
RoleId string RoleId string
Users []AllUsers Users []AllUsers
Roles []Role Roles []UserRoleShortInfo
}
type UserRoleShortInfo struct {
Id string
Master bool
DisplayName string
SortingOrder int
} }

View File

@ -434,6 +434,11 @@
"description": "You can create a new api key in your user profile", "description": "You can create a new api key in your user profile",
"name": "X-Api-Key", "name": "X-Api-Key",
"in": "header" "in": "header"
},
{
"description": "Page number",
"name": "page",
"in": "query"
} }
], ],
"responses": { "responses": {
@ -639,7 +644,7 @@
"200": { "200": {
"description": "User info", "description": "User info",
"schema": { "schema": {
"$ref": "#/definitions/UserInfoResponse" "$ref": "#/definitions/UserShortInfoResponse"
} }
}, },
"401": { "401": {
@ -811,6 +816,24 @@
"items": { "items": {
"$ref": "#/definitions/RolePermissions" "$ref": "#/definitions/RolePermissions"
} }
},
"Users": {
"type": "array",
"items": {
"$ref": "#/definitions/AdminAreaUserInfo"
}
}
},
"x-go-package": "jannex/admin-dashboard-backend/modules/structs"
},
"AdminAreaUserInfo": {
"type": "object",
"properties": {
"Id": {
"type": "string"
},
"RoleId": {
"type": "string"
} }
}, },
"x-go-package": "jannex/admin-dashboard-backend/modules/structs" "x-go-package": "jannex/admin-dashboard-backend/modules/structs"
@ -1001,6 +1024,9 @@
"type": "string", "type": "string",
"x-go-name": "DisplayName" "x-go-name": "DisplayName"
}, },
"options": {
"x-go-name": "Options"
},
"parameterName": { "parameterName": {
"type": "string", "type": "string",
"x-go-name": "ParameterName" "x-go-name": "ParameterName"
@ -1201,6 +1227,10 @@
"items": { "items": {
"$ref": "#/definitions/Notification" "$ref": "#/definitions/Notification"
} }
},
"TotalPages": {
"type": "integer",
"format": "int64"
} }
}, },
"x-go-package": "jannex/admin-dashboard-backend/modules/structs" "x-go-package": "jannex/admin-dashboard-backend/modules/structs"
@ -1294,6 +1324,9 @@
"type": "boolean", "type": "boolean",
"x-go-name": "Global" "x-go-name": "Global"
}, },
"options": {
"x-go-name": "Options"
},
"parameterName": { "parameterName": {
"type": "string", "type": "string",
"x-go-name": "ParameterName" "x-go-name": "ParameterName"
@ -1338,7 +1371,7 @@
"UserInfoResponse": { "UserInfoResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
"AvailableCategoryGroups": { "AvailableCategories": {
"type": "array", "type": "array",
"items": { "items": {
"type": "string" "type": "string"
@ -1366,7 +1399,7 @@
"Users": { "Users": {
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/AllUsers" "$ref": "#/definitions/UserShortInfoResponse"
} }
} }
}, },
@ -1414,6 +1447,25 @@
}, },
"x-go-package": "jannex/admin-dashboard-backend/modules/structs" "x-go-package": "jannex/admin-dashboard-backend/modules/structs"
}, },
"UserRoleShortInfo": {
"type": "object",
"properties": {
"DisplayName": {
"type": "string"
},
"Id": {
"type": "string"
},
"Master": {
"type": "boolean"
},
"SortingOrder": {
"type": "integer",
"format": "int64"
}
},
"x-go-package": "jannex/admin-dashboard-backend/modules/structs"
},
"UserSessionSocket": { "UserSessionSocket": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -1438,6 +1490,21 @@
}, },
"x-go-package": "jannex/admin-dashboard-backend/modules/structs" "x-go-package": "jannex/admin-dashboard-backend/modules/structs"
}, },
"UserShortInfoResponse": {
"type": "object",
"properties": {
"Avatar": {
"type": "string"
},
"Id": {
"type": "string"
},
"Username": {
"type": "string"
}
},
"x-go-package": "jannex/admin-dashboard-backend/modules/structs"
},
"UsersResponse": { "UsersResponse": {
"type": "object", "type": "object",
"properties": { "properties": {
@ -1447,7 +1514,7 @@
"Roles": { "Roles": {
"type": "array", "type": "array",
"items": { "items": {
"$ref": "#/definitions/Role" "$ref": "#/definitions/UserRoleShortInfo"
} }
}, },
"Users": { "Users": {

View File

@ -1,6 +1,7 @@
package adminarea package adminarea
import ( import (
"jannex/admin-dashboard-backend/modules/database"
"jannex/admin-dashboard-backend/modules/structs" "jannex/admin-dashboard-backend/modules/structs"
"jannex/admin-dashboard-backend/modules/utils" "jannex/admin-dashboard-backend/modules/utils"
"jannex/admin-dashboard-backend/socketclients" "jannex/admin-dashboard-backend/socketclients"
@ -38,8 +39,13 @@ func GetRoles(c *fiber.Ctx) error {
return c.SendStatus(fiber.StatusUnauthorized) return c.SendStatus(fiber.StatusUnauthorized)
} }
var users []structs.AdminAreaUserInfo
database.DB.Model(&structs.User{}).Find(&users)
return c.JSON(structs.AdminAreaRolesResponse{ return c.JSON(structs.AdminAreaRolesResponse{
Roles: socketclients.GetAllRoles(), Roles: socketclients.GetAllRoles(),
RolesPermissions: socketclients.GetAdminAreaRolesPermissions(), RolesPermissions: socketclients.GetAdminAreaRolesPermissions(),
Users: users,
}) })
} }

View File

@ -25,7 +25,7 @@ func UserInfo(c *fiber.Ctx) error {
// '200': // '200':
// description: User info // description: User info
// schema: // schema:
// "$ref": "#/definitions/UserInfoResponse" // "$ref": "#/definitions/UserShortInfoResponse"
// '401': // '401':
// description: No permissions // description: No permissions
// '500': // '500':
@ -38,13 +38,17 @@ func UserInfo(c *fiber.Ctx) error {
categories := socketclients.GetAvailableCategories(userId) categories := socketclients.GetAvailableCategories(userId)
var users []structs.UserShortInfoResponse
database.DB.Model(&structs.User{}).Find(&users)
return c.JSON(structs.UserInfoResponse{ return c.JSON(structs.UserInfoResponse{
UserId: userId, UserId: userId,
Username: user.Username, Username: user.Username,
Avatar: user.Avatar, Avatar: user.Avatar,
Permissions: socketclients.GetPermissionsByRoleId(user.RoleId), Permissions: socketclients.GetPermissionsByRoleId(user.RoleId),
AvailableCategories: categories, AvailableCategories: categories,
Users: socketclients.GetAllUsers(), Users: users,
TotalNotifications: notification.GetTotalNotifications(userId), TotalNotifications: notification.GetTotalNotifications(userId),
}) })
} }

View File

@ -34,9 +34,13 @@ func GetUsers(c *fiber.Ctx) error {
database.DB.Select("role_id").First(&user, "id = ?", c.Locals("userId").(string)) database.DB.Select("role_id").First(&user, "id = ?", c.Locals("userId").(string))
var roles []structs.UserRoleShortInfo
database.DB.Model(&structs.Role{}).Select("id, master, display_name, sorting_order").Find(&roles)
return c.JSON(structs.UsersResponse{ return c.JSON(structs.UsersResponse{
RoleId: user.RoleId, RoleId: user.RoleId,
Users: socketclients.GetAllUsers(), Users: socketclients.GetAllUsers(),
Roles: socketclients.GetAllRoles(), Roles: roles,
}) })
} }

View File

@ -57,6 +57,12 @@ func BroadcastMessageToTopicExceptUserSessionId(topic string, ignoreUserSessionI
} }
} }
func BroadcastMessageToTopicsExceptUserSessionId(topics []string, ignoreUserSessionId string, sendSocketMessage structs.SendSocketMessage) {
for _, topic := range topics {
BroadcastMessageToTopicExceptUserSessionId(topic, ignoreUserSessionId, sendSocketMessage)
}
}
func hasClientSubscribedToTopic(topic string, clientTopic string) bool { func hasClientSubscribedToTopic(topic string, clientTopic string) bool {
return clientTopic == topic || strings.HasPrefix(clientTopic, topic) return clientTopic == topic || strings.HasPrefix(clientTopic, topic)
} }
@ -77,6 +83,12 @@ func BroadcastMessageToTopicExceptUserId(topic string, ignoreUserId string, send
} }
} }
func BroadcastMessageToTopicsExceptUserId(topics []string, ignoreUserId string, sendSocketMessage structs.SendSocketMessage) {
for _, topic := range topics {
BroadcastMessageToTopicExceptUserId(topic, ignoreUserId, sendSocketMessage)
}
}
func BroadcastMessageToUsersWithPermission(neededPermission string, sendSocketMessage structs.SendSocketMessage) { func BroadcastMessageToUsersWithPermission(neededPermission string, sendSocketMessage structs.SendSocketMessage) {
var rolePermissions []structs.RolePermission var rolePermissions []structs.RolePermission
@ -594,7 +606,8 @@ func AdminAreaUpdateRole(conn *websocket.Conn, body map[string]interface{}) {
}, },
}) })
BroadcastMessageToTopicExceptUserSessionId(utils.SubscribedTopicAdminAreaRoles, BroadcastMessageToTopicsExceptUserSessionId(
[]string{utils.SubscribedTopicAdminAreaRoles, utils.SubscribedTopicUsers},
sessionId, sessionId,
structs.SendSocketMessage{ structs.SendSocketMessage{
Cmd: utils.SentCmdAdminAreaRoleUpdated, Cmd: utils.SentCmdAdminAreaRoleUpdated,
@ -674,10 +687,12 @@ func AdminAreaMoveRoleToSortingOrder(conn *websocket.Conn, body map[string]inter
return return
} }
BroadcastMessageToTopic(utils.SubscribedTopicAdminAreaRoles, structs.SendSocketMessage{ BroadcastMessageToTopics(
Cmd: utils.SentCmdAdminAreaUpdateRoleSortingOrder, []string{utils.SubscribedTopicAdminAreaRoles, utils.SubscribedTopicUsers},
Body: body, structs.SendSocketMessage{
}) Cmd: utils.SentCmdAdminAreaUpdateRoleSortingOrder,
Body: body,
})
logger.AddSystemLog("User %s has changed the sorting order of role %s to %s", logger.AddSystemLog("User %s has changed the sorting order of role %s to %s",
conn.Locals("userId").(string), roleId, strconv.Itoa(newSortingOrder)) conn.Locals("userId").(string), roleId, strconv.Itoa(newSortingOrder))
@ -725,7 +740,8 @@ func UpdateUserRole(conn *websocket.Conn, userId string, roleId string) {
}, },
}) })
BroadcastMessageToTopicExceptUserId(utils.SubscribedTopicUsers, BroadcastMessageToTopicsExceptUserId(
[]string{utils.SubscribedTopicUsers, utils.SubscribedTopicAdminAreaRoles},
userId, structs.SendSocketMessage{ userId, structs.SendSocketMessage{
Cmd: utils.SentCmdAllUsersUserRoleUpdated, Cmd: utils.SentCmdAllUsersUserRoleUpdated,
Body: struct { Body: struct {
@ -858,22 +874,24 @@ func AllUsersCreateNewUser(conn *websocket.Conn, body map[string]interface{}) {
database.DB.Create(&newUser) database.DB.Create(&newUser)
BroadcastMessageToTopic(utils.SubscribedTopicUsers, structs.SendSocketMessage{ BroadcastMessageToTopics(
Cmd: utils.SentCmdAllUsersNewUserCreated, []string{utils.SubscribedTopicUsers, utils.SubscribedTopicAdminAreaRoles},
Body: struct { structs.SendSocketMessage{
Id string Cmd: utils.SentCmdAllUsersNewUserCreated,
Username string Body: struct {
RoleId string Id string
ConnectionStatus uint8 Username string
Deactivated bool RoleId string
}{ ConnectionStatus uint8
Id: newUser.Id, Deactivated bool
Username: username, }{
RoleId: roleId, Id: newUser.Id,
ConnectionStatus: utils.ConnectionStateOffline, Username: username,
Deactivated: false, RoleId: roleId,
}, ConnectionStatus: utils.ConnectionStateOffline,
}) Deactivated: false,
},
})
logger.AddSystemLog("User %s has created the user %s with the assigned role %s", logger.AddSystemLog("User %s has created the user %s with the assigned role %s",
conn.Locals("userId").(string), newUser.Id, roleId) conn.Locals("userId").(string), newUser.Id, roleId)
@ -903,16 +921,18 @@ func AllUsersDeleteUser(conn *websocket.Conn, userId string) {
CloseAndDeleteAllUserConnections(userId) CloseAndDeleteAllUserConnections(userId)
BroadcastMessageToTopic(utils.SubscribedTopicUsers, structs.SendSocketMessage{ BroadcastMessageToTopics(
Cmd: utils.SentCmdAllUsersUserDeleted, []string{utils.SubscribedTopicUsers, utils.SubscribedTopicAdminAreaRoles},
Body: struct { structs.SendSocketMessage{
UserId string Cmd: utils.SentCmdAllUsersUserDeleted,
ScannerId string Body: struct {
}{ UserId string
UserId: userId, ScannerId string
ScannerId: scannerInUsage.Id, }{
}, UserId: userId,
}) ScannerId: scannerInUsage.Id,
},
})
logger.AddSystemLog("User %s has deleted the user %s", logger.AddSystemLog("User %s has deleted the user %s",
conn.Locals("userId").(string), userId) conn.Locals("userId").(string), userId)

View File

@ -198,8 +198,8 @@ func RunHub() {
database.DB.Create(&role) database.DB.Create(&role)
socketclients.BroadcastMessageToTopic( socketclients.BroadcastMessageToTopics(
utils.SubscribedTopicAdminAreaRoles, []string{utils.SubscribedTopicAdminAreaRoles, utils.SubscribedTopicUsers},
structs.SendSocketMessage{ structs.SendSocketMessage{
Cmd: utils.SentCmdAdminAreaNewRoleCreated, Cmd: utils.SentCmdAdminAreaNewRoleCreated,
Body: role, Body: role,