mirror of
https://github.com/gravitl/netmaker.git
synced 2025-09-06 05:04:27 +08:00
add list roles to pro and ce (#3072)
This commit is contained in:
parent
936e1b4d45
commit
a39da31fa6
5 changed files with 48 additions and 24 deletions
|
@ -23,6 +23,8 @@ var (
|
|||
upgrader = websocket.Upgrader{}
|
||||
)
|
||||
|
||||
var ListRoles = listRoles
|
||||
|
||||
func userHandlers(r *mux.Router) {
|
||||
r.HandleFunc("/api/users/adm/hassuperadmin", hasSuperAdmin).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/users/adm/createsuperadmin", createSuperAdmin).Methods(http.MethodPost)
|
||||
|
@ -35,6 +37,7 @@ func userHandlers(r *mux.Router) {
|
|||
r.HandleFunc("/api/users/{username}", logic.SecurityCheck(false, logic.ContinueIfUserMatch(http.HandlerFunc(getUser)))).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/v1/users", logic.SecurityCheck(false, logic.ContinueIfUserMatch(http.HandlerFunc(getUserV1)))).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/users", logic.SecurityCheck(true, http.HandlerFunc(getUsers))).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/v1/users/roles", logic.SecurityCheck(true, http.HandlerFunc(ListRoles))).Methods(http.MethodGet)
|
||||
|
||||
}
|
||||
|
||||
|
@ -710,3 +713,24 @@ func socketHandler(w http.ResponseWriter, r *http.Request) {
|
|||
// Start handling the session
|
||||
go auth.SessionHandler(conn)
|
||||
}
|
||||
|
||||
// @Summary lists all user roles.
|
||||
// @Router /api/v1/user/roles [get]
|
||||
// @Tags Users
|
||||
// @Param role_id param string true "roleid required to get the role details"
|
||||
// @Success 200 {object} []models.UserRolePermissionTemplate
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
func listRoles(w http.ResponseWriter, r *http.Request) {
|
||||
var roles []models.UserRolePermissionTemplate
|
||||
var err error
|
||||
roles, err = logic.ListPlatformRoles()
|
||||
if err != nil {
|
||||
logic.ReturnErrorResponse(w, r, models.ErrorResponse{
|
||||
Code: http.StatusInternalServerError,
|
||||
Message: err.Error(),
|
||||
})
|
||||
return
|
||||
}
|
||||
|
||||
logic.ReturnSuccessResponseWithJson(w, r, roles, "successfully fetched user roles permission templates")
|
||||
}
|
||||
|
|
|
@ -66,6 +66,27 @@ func GetRole(roleID models.UserRoleID) (models.UserRolePermissionTemplate, error
|
|||
return ur, nil
|
||||
}
|
||||
|
||||
// ListPlatformRoles - lists user platform roles permission templates
|
||||
func ListPlatformRoles() ([]models.UserRolePermissionTemplate, error) {
|
||||
data, err := database.FetchRecords(database.USER_PERMISSIONS_TABLE_NAME)
|
||||
if err != nil && !database.IsEmptyRecord(err) {
|
||||
return []models.UserRolePermissionTemplate{}, err
|
||||
}
|
||||
userRoles := []models.UserRolePermissionTemplate{}
|
||||
for _, dataI := range data {
|
||||
userRole := models.UserRolePermissionTemplate{}
|
||||
err := json.Unmarshal([]byte(dataI), &userRole)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if userRole.NetworkID != "" {
|
||||
continue
|
||||
}
|
||||
userRoles = append(userRoles, userRole)
|
||||
}
|
||||
return userRoles, nil
|
||||
}
|
||||
|
||||
func userRolesInit() {
|
||||
d, _ := json.Marshal(SuperAdminPermissionTemplate)
|
||||
database.Insert(SuperAdminPermissionTemplate.ID.String(), string(d), database.USER_PERMISSIONS_TABLE_NAME)
|
||||
|
|
|
@ -30,7 +30,6 @@ func UserHandlers(r *mux.Router) {
|
|||
r.HandleFunc("/api/oauth/register/{regKey}", proAuth.RegisterHostSSO).Methods(http.MethodGet)
|
||||
|
||||
// User Role Handlers
|
||||
r.HandleFunc("/api/v1/users/roles", logic.SecurityCheck(true, http.HandlerFunc(listRoles))).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/v1/users/role", logic.SecurityCheck(true, http.HandlerFunc(getRole))).Methods(http.MethodGet)
|
||||
r.HandleFunc("/api/v1/users/role", logic.SecurityCheck(true, http.HandlerFunc(createRole))).Methods(http.MethodPost)
|
||||
r.HandleFunc("/api/v1/users/role", logic.SecurityCheck(true, http.HandlerFunc(updateRole))).Methods(http.MethodPut)
|
||||
|
@ -499,12 +498,12 @@ func deleteUserGroup(w http.ResponseWriter, r *http.Request) {
|
|||
// @Param role_id param string true "roleid required to get the role details"
|
||||
// @Success 200 {object} []models.UserRolePermissionTemplate
|
||||
// @Failure 500 {object} models.ErrorResponse
|
||||
func listRoles(w http.ResponseWriter, r *http.Request) {
|
||||
func ListRoles(w http.ResponseWriter, r *http.Request) {
|
||||
platform, _ := url.QueryUnescape(r.URL.Query().Get("platform"))
|
||||
var roles []models.UserRolePermissionTemplate
|
||||
var err error
|
||||
if platform == "true" {
|
||||
roles, err = proLogic.ListPlatformRoles()
|
||||
roles, err = logic.ListPlatformRoles()
|
||||
} else {
|
||||
roles, err = proLogic.ListNetworkRoles()
|
||||
}
|
||||
|
|
|
@ -34,6 +34,7 @@ func InitPro() {
|
|||
proControllers.FailOverHandlers,
|
||||
proControllers.InetHandlers,
|
||||
)
|
||||
controller.ListRoles = proControllers.ListRoles
|
||||
logic.EnterpriseCheckFuncs = append(logic.EnterpriseCheckFuncs, func() {
|
||||
// == License Handling ==
|
||||
enableLicenseHook := false
|
||||
|
|
|
@ -201,27 +201,6 @@ func ListNetworkRoles() ([]models.UserRolePermissionTemplate, error) {
|
|||
return userRoles, nil
|
||||
}
|
||||
|
||||
// ListPlatformRoles - lists user platform roles permission templates
|
||||
func ListPlatformRoles() ([]models.UserRolePermissionTemplate, error) {
|
||||
data, err := database.FetchRecords(database.USER_PERMISSIONS_TABLE_NAME)
|
||||
if err != nil && !database.IsEmptyRecord(err) {
|
||||
return []models.UserRolePermissionTemplate{}, err
|
||||
}
|
||||
userRoles := []models.UserRolePermissionTemplate{}
|
||||
for _, dataI := range data {
|
||||
userRole := models.UserRolePermissionTemplate{}
|
||||
err := json.Unmarshal([]byte(dataI), &userRole)
|
||||
if err != nil {
|
||||
continue
|
||||
}
|
||||
if userRole.NetworkID != "" {
|
||||
continue
|
||||
}
|
||||
userRoles = append(userRoles, userRole)
|
||||
}
|
||||
return userRoles, nil
|
||||
}
|
||||
|
||||
func ValidateCreateRoleReq(userRole *models.UserRolePermissionTemplate) error {
|
||||
// check if role exists with this id
|
||||
_, err := logic.GetRole(userRole.ID)
|
||||
|
|
Loading…
Add table
Reference in a new issue