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 {
Roles []Role
RolesPermissions []RolePermissions
Users []AdminAreaUserInfo
}
type AdminAreaUserInfo struct {
Id string
RoleId string
}

View File

@ -63,7 +63,7 @@ type UserInfoResponse struct {
Avatar string
Permissions []string
AvailableCategories []string
Users []AllUsers
Users []UserShortInfoResponse
TotalNotifications int
}
@ -73,3 +73,10 @@ type UserProfileResponse struct {
Sessions []UserSessionSocket
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 {
RoleId string
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",
"name": "X-Api-Key",
"in": "header"
},
{
"description": "Page number",
"name": "page",
"in": "query"
}
],
"responses": {
@ -639,7 +644,7 @@
"200": {
"description": "User info",
"schema": {
"$ref": "#/definitions/UserInfoResponse"
"$ref": "#/definitions/UserShortInfoResponse"
}
},
"401": {
@ -811,6 +816,24 @@
"items": {
"$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"
@ -1001,6 +1024,9 @@
"type": "string",
"x-go-name": "DisplayName"
},
"options": {
"x-go-name": "Options"
},
"parameterName": {
"type": "string",
"x-go-name": "ParameterName"
@ -1201,6 +1227,10 @@
"items": {
"$ref": "#/definitions/Notification"
}
},
"TotalPages": {
"type": "integer",
"format": "int64"
}
},
"x-go-package": "jannex/admin-dashboard-backend/modules/structs"
@ -1294,6 +1324,9 @@
"type": "boolean",
"x-go-name": "Global"
},
"options": {
"x-go-name": "Options"
},
"parameterName": {
"type": "string",
"x-go-name": "ParameterName"
@ -1338,7 +1371,7 @@
"UserInfoResponse": {
"type": "object",
"properties": {
"AvailableCategoryGroups": {
"AvailableCategories": {
"type": "array",
"items": {
"type": "string"
@ -1366,7 +1399,7 @@
"Users": {
"type": "array",
"items": {
"$ref": "#/definitions/AllUsers"
"$ref": "#/definitions/UserShortInfoResponse"
}
}
},
@ -1414,6 +1447,25 @@
},
"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": {
"type": "object",
"properties": {
@ -1438,6 +1490,21 @@
},
"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": {
"type": "object",
"properties": {
@ -1447,7 +1514,7 @@
"Roles": {
"type": "array",
"items": {
"$ref": "#/definitions/Role"
"$ref": "#/definitions/UserRoleShortInfo"
}
},
"Users": {

View File

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

View File

@ -25,7 +25,7 @@ func UserInfo(c *fiber.Ctx) error {
// '200':
// description: User info
// schema:
// "$ref": "#/definitions/UserInfoResponse"
// "$ref": "#/definitions/UserShortInfoResponse"
// '401':
// description: No permissions
// '500':
@ -38,13 +38,17 @@ func UserInfo(c *fiber.Ctx) error {
categories := socketclients.GetAvailableCategories(userId)
var users []structs.UserShortInfoResponse
database.DB.Model(&structs.User{}).Find(&users)
return c.JSON(structs.UserInfoResponse{
UserId: userId,
Username: user.Username,
Avatar: user.Avatar,
Permissions: socketclients.GetPermissionsByRoleId(user.RoleId),
AvailableCategories: categories,
Users: socketclients.GetAllUsers(),
Users: users,
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))
var roles []structs.UserRoleShortInfo
database.DB.Model(&structs.Role{}).Select("id, master, display_name, sorting_order").Find(&roles)
return c.JSON(structs.UsersResponse{
RoleId: user.RoleId,
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 {
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) {
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,
structs.SendSocketMessage{
Cmd: utils.SentCmdAdminAreaRoleUpdated,
@ -674,10 +687,12 @@ func AdminAreaMoveRoleToSortingOrder(conn *websocket.Conn, body map[string]inter
return
}
BroadcastMessageToTopic(utils.SubscribedTopicAdminAreaRoles, structs.SendSocketMessage{
Cmd: utils.SentCmdAdminAreaUpdateRoleSortingOrder,
Body: body,
})
BroadcastMessageToTopics(
[]string{utils.SubscribedTopicAdminAreaRoles, utils.SubscribedTopicUsers},
structs.SendSocketMessage{
Cmd: utils.SentCmdAdminAreaUpdateRoleSortingOrder,
Body: body,
})
logger.AddSystemLog("User %s has changed the sorting order of role %s to %s",
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{
Cmd: utils.SentCmdAllUsersUserRoleUpdated,
Body: struct {
@ -858,22 +874,24 @@ func AllUsersCreateNewUser(conn *websocket.Conn, body map[string]interface{}) {
database.DB.Create(&newUser)
BroadcastMessageToTopic(utils.SubscribedTopicUsers, structs.SendSocketMessage{
Cmd: utils.SentCmdAllUsersNewUserCreated,
Body: struct {
Id string
Username string
RoleId string
ConnectionStatus uint8
Deactivated bool
}{
Id: newUser.Id,
Username: username,
RoleId: roleId,
ConnectionStatus: utils.ConnectionStateOffline,
Deactivated: false,
},
})
BroadcastMessageToTopics(
[]string{utils.SubscribedTopicUsers, utils.SubscribedTopicAdminAreaRoles},
structs.SendSocketMessage{
Cmd: utils.SentCmdAllUsersNewUserCreated,
Body: struct {
Id string
Username string
RoleId string
ConnectionStatus uint8
Deactivated bool
}{
Id: newUser.Id,
Username: username,
RoleId: roleId,
ConnectionStatus: utils.ConnectionStateOffline,
Deactivated: false,
},
})
logger.AddSystemLog("User %s has created the user %s with the assigned role %s",
conn.Locals("userId").(string), newUser.Id, roleId)
@ -903,16 +921,18 @@ func AllUsersDeleteUser(conn *websocket.Conn, userId string) {
CloseAndDeleteAllUserConnections(userId)
BroadcastMessageToTopic(utils.SubscribedTopicUsers, structs.SendSocketMessage{
Cmd: utils.SentCmdAllUsersUserDeleted,
Body: struct {
UserId string
ScannerId string
}{
UserId: userId,
ScannerId: scannerInUsage.Id,
},
})
BroadcastMessageToTopics(
[]string{utils.SubscribedTopicUsers, utils.SubscribedTopicAdminAreaRoles},
structs.SendSocketMessage{
Cmd: utils.SentCmdAllUsersUserDeleted,
Body: struct {
UserId string
ScannerId string
}{
UserId: userId,
ScannerId: scannerInUsage.Id,
},
})
logger.AddSystemLog("User %s has deleted the user %s",
conn.Locals("userId").(string), userId)

View File

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