mirror of
https://github.com/netbirdio/netbird.git
synced 2025-06-20 17:58:02 +02:00
Support user role update (#478)
This commit is contained in:
parent
68ff97ba84
commit
af69a48745
@ -47,16 +47,16 @@ components:
|
|||||||
UserRequest:
|
UserRequest:
|
||||||
type: object
|
type: object
|
||||||
properties:
|
properties:
|
||||||
|
role:
|
||||||
|
description: User's NetBird account role
|
||||||
|
type: string
|
||||||
auto_groups:
|
auto_groups:
|
||||||
description: Groups to auto-assign to peers registered by this user
|
description: Groups to auto-assign to peers registered by this user
|
||||||
type: array
|
type: array
|
||||||
items:
|
items:
|
||||||
type: string
|
type: string
|
||||||
required:
|
required:
|
||||||
- name
|
- role
|
||||||
- type
|
|
||||||
- expires_in
|
|
||||||
- revoked
|
|
||||||
- auto_groups
|
- auto_groups
|
||||||
PeerMinimum:
|
PeerMinimum:
|
||||||
type: object
|
type: object
|
||||||
|
@ -376,6 +376,9 @@ type User struct {
|
|||||||
type UserRequest struct {
|
type UserRequest struct {
|
||||||
// Groups to auto-assign to peers registered by this user
|
// Groups to auto-assign to peers registered by this user
|
||||||
AutoGroups []string `json:"auto_groups"`
|
AutoGroups []string `json:"auto_groups"`
|
||||||
|
|
||||||
|
// User's NetBird account role
|
||||||
|
Role string `json:"role"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// PostApiGroupsJSONBody defines parameters for PostApiGroups.
|
// PostApiGroupsJSONBody defines parameters for PostApiGroups.
|
||||||
|
@ -45,7 +45,7 @@ func (h *UserHandler) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
vars := mux.Vars(r)
|
vars := mux.Vars(r)
|
||||||
userID := vars["id"]
|
userID := vars["id"]
|
||||||
if len(userID) == 0 {
|
if len(userID) == 0 {
|
||||||
http.Error(w, "invalid key Id", http.StatusBadRequest)
|
http.Error(w, "invalid user ID", http.StatusBadRequest)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -56,8 +56,15 @@ func (h *UserHandler) UpdateUser(w http.ResponseWriter, r *http.Request) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
userRole := server.StrRoleToUserRole(req.Role)
|
||||||
|
if userRole == server.UserRoleUnknown {
|
||||||
|
http.Error(w, "invalid user role", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
newUser, err := h.accountManager.SaveUser(account.Id, &server.User{
|
newUser, err := h.accountManager.SaveUser(account.Id, &server.User{
|
||||||
Id: userID,
|
Id: userID,
|
||||||
|
Role: userRole,
|
||||||
AutoGroups: req.AutoGroups,
|
AutoGroups: req.AutoGroups,
|
||||||
})
|
})
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -11,10 +11,23 @@ import (
|
|||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
UserRoleAdmin UserRole = "admin"
|
UserRoleAdmin UserRole = "admin"
|
||||||
UserRoleUser UserRole = "user"
|
UserRoleUser UserRole = "user"
|
||||||
|
UserRoleUnknown UserRole = "unknown"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// StrRoleToUserRole returns UserRole for a given strRole or UserRoleUnknown if the specified role is unknown
|
||||||
|
func StrRoleToUserRole(strRole string) UserRole {
|
||||||
|
switch strings.ToLower(strRole) {
|
||||||
|
case "admin":
|
||||||
|
return UserRoleAdmin
|
||||||
|
case "user":
|
||||||
|
return UserRoleUser
|
||||||
|
default:
|
||||||
|
return UserRoleUnknown
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// UserRole is the role of the User
|
// UserRole is the role of the User
|
||||||
type UserRole string
|
type UserRole string
|
||||||
|
|
||||||
@ -116,6 +129,7 @@ func (am *DefaultAccountManager) SaveUser(accountID string, update *User) (*User
|
|||||||
// only auto groups, revoked status, and name can be updated for now
|
// only auto groups, revoked status, and name can be updated for now
|
||||||
newUser := oldUser.Copy()
|
newUser := oldUser.Copy()
|
||||||
newUser.AutoGroups = update.AutoGroups
|
newUser.AutoGroups = update.AutoGroups
|
||||||
|
newUser.Role = update.Role
|
||||||
|
|
||||||
account.Users[newUser.Id] = newUser
|
account.Users[newUser.Id] = newUser
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user