|
@@ -19,6 +19,8 @@ import (
|
|
var (
|
|
var (
|
|
globalNetworksAdminGroupID = models.UserGroupID(fmt.Sprintf("global-%s-grp", models.NetworkAdmin))
|
|
globalNetworksAdminGroupID = models.UserGroupID(fmt.Sprintf("global-%s-grp", models.NetworkAdmin))
|
|
globalNetworksUserGroupID = models.UserGroupID(fmt.Sprintf("global-%s-grp", models.NetworkUser))
|
|
globalNetworksUserGroupID = models.UserGroupID(fmt.Sprintf("global-%s-grp", models.NetworkUser))
|
|
|
|
+ globalNetworksAdminRoleID = models.UserRoleID(fmt.Sprintf("global-%s", models.NetworkAdmin))
|
|
|
|
+ globalNetworksUserRoleID = models.UserRoleID(fmt.Sprintf("global-%s", models.NetworkUser))
|
|
)
|
|
)
|
|
|
|
|
|
var ServiceUserPermissionTemplate = models.UserRolePermissionTemplate{
|
|
var ServiceUserPermissionTemplate = models.UserRolePermissionTemplate{
|
|
@@ -35,7 +37,7 @@ var PlatformUserUserPermissionTemplate = models.UserRolePermissionTemplate{
|
|
}
|
|
}
|
|
|
|
|
|
var NetworkAdminAllPermissionTemplate = models.UserRolePermissionTemplate{
|
|
var NetworkAdminAllPermissionTemplate = models.UserRolePermissionTemplate{
|
|
- ID: models.UserRoleID(fmt.Sprintf("global-%s", models.NetworkAdmin)),
|
|
|
|
|
|
+ ID: globalNetworksAdminRoleID,
|
|
Name: "Network Admins",
|
|
Name: "Network Admins",
|
|
MetaData: "can manage configuration of all networks",
|
|
MetaData: "can manage configuration of all networks",
|
|
Default: true,
|
|
Default: true,
|
|
@@ -44,7 +46,7 @@ var NetworkAdminAllPermissionTemplate = models.UserRolePermissionTemplate{
|
|
}
|
|
}
|
|
|
|
|
|
var NetworkUserAllPermissionTemplate = models.UserRolePermissionTemplate{
|
|
var NetworkUserAllPermissionTemplate = models.UserRolePermissionTemplate{
|
|
- ID: models.UserRoleID(fmt.Sprintf("global-%s", models.NetworkUser)),
|
|
|
|
|
|
+ ID: globalNetworksUserRoleID,
|
|
Name: "Network Users",
|
|
Name: "Network Users",
|
|
MetaData: "Can connect to nodes in your networks via Netmaker Desktop App.",
|
|
MetaData: "Can connect to nodes in your networks via Netmaker Desktop App.",
|
|
Default: true,
|
|
Default: true,
|
|
@@ -124,7 +126,7 @@ func UserGroupsInit() {
|
|
MetaData: "can manage configuration of all networks",
|
|
MetaData: "can manage configuration of all networks",
|
|
NetworkRoles: map[models.NetworkID]map[models.UserRoleID]struct{}{
|
|
NetworkRoles: map[models.NetworkID]map[models.UserRoleID]struct{}{
|
|
models.AllNetworks: {
|
|
models.AllNetworks: {
|
|
- models.UserRoleID(fmt.Sprintf("global-%s", models.NetworkAdmin)): {},
|
|
|
|
|
|
+ globalNetworksAdminRoleID: {},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
}
|
|
}
|
|
@@ -134,7 +136,7 @@ func UserGroupsInit() {
|
|
Default: true,
|
|
Default: true,
|
|
NetworkRoles: map[models.NetworkID]map[models.UserRoleID]struct{}{
|
|
NetworkRoles: map[models.NetworkID]map[models.UserRoleID]struct{}{
|
|
models.AllNetworks: {
|
|
models.AllNetworks: {
|
|
- models.UserRoleID(fmt.Sprintf("global-%s", models.NetworkUser)): {},
|
|
|
|
|
|
+ globalNetworksUserRoleID: {},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
MetaData: "Provides read-only dashboard access to platform users and allows connection to network nodes via the Netmaker Desktop App.",
|
|
MetaData: "Provides read-only dashboard access to platform users and allows connection to network nodes via the Netmaker Desktop App.",
|
|
@@ -150,7 +152,7 @@ func CreateDefaultNetworkRolesAndGroups(netID models.NetworkID) {
|
|
return
|
|
return
|
|
}
|
|
}
|
|
var NetworkAdminPermissionTemplate = models.UserRolePermissionTemplate{
|
|
var NetworkAdminPermissionTemplate = models.UserRolePermissionTemplate{
|
|
- ID: models.UserRoleID(fmt.Sprintf("%s-%s", netID, models.NetworkAdmin)),
|
|
|
|
|
|
+ ID: GetDefaultNetworkAdminRoleID(netID),
|
|
Name: fmt.Sprintf("%s Admin", netID),
|
|
Name: fmt.Sprintf("%s Admin", netID),
|
|
MetaData: fmt.Sprintf("can manage your network `%s` configuration.", netID),
|
|
MetaData: fmt.Sprintf("can manage your network `%s` configuration.", netID),
|
|
Default: true,
|
|
Default: true,
|
|
@@ -160,7 +162,7 @@ func CreateDefaultNetworkRolesAndGroups(netID models.NetworkID) {
|
|
}
|
|
}
|
|
|
|
|
|
var NetworkUserPermissionTemplate = models.UserRolePermissionTemplate{
|
|
var NetworkUserPermissionTemplate = models.UserRolePermissionTemplate{
|
|
- ID: models.UserRoleID(fmt.Sprintf("%s-%s", netID, models.NetworkUser)),
|
|
|
|
|
|
+ ID: GetDefaultNetworkUserRoleID(netID),
|
|
Name: fmt.Sprintf("%s User", netID),
|
|
Name: fmt.Sprintf("%s User", netID),
|
|
MetaData: fmt.Sprintf("Can connect to nodes in your network `%s` via Netmaker Desktop App.", netID),
|
|
MetaData: fmt.Sprintf("Can connect to nodes in your network `%s` via Netmaker Desktop App.", netID),
|
|
Default: true,
|
|
Default: true,
|
|
@@ -227,7 +229,7 @@ func CreateDefaultNetworkRolesAndGroups(netID models.NetworkID) {
|
|
Default: true,
|
|
Default: true,
|
|
NetworkRoles: map[models.NetworkID]map[models.UserRoleID]struct{}{
|
|
NetworkRoles: map[models.NetworkID]map[models.UserRoleID]struct{}{
|
|
netID: {
|
|
netID: {
|
|
- models.UserRoleID(fmt.Sprintf("%s-%s", netID, models.NetworkAdmin)): {},
|
|
|
|
|
|
+ GetDefaultNetworkAdminRoleID(netID): {},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
MetaData: fmt.Sprintf("can manage your network `%s` configuration including adding and removing devices.", netID),
|
|
MetaData: fmt.Sprintf("can manage your network `%s` configuration including adding and removing devices.", netID),
|
|
@@ -238,7 +240,7 @@ func CreateDefaultNetworkRolesAndGroups(netID models.NetworkID) {
|
|
Default: true,
|
|
Default: true,
|
|
NetworkRoles: map[models.NetworkID]map[models.UserRoleID]struct{}{
|
|
NetworkRoles: map[models.NetworkID]map[models.UserRoleID]struct{}{
|
|
netID: {
|
|
netID: {
|
|
- models.UserRoleID(fmt.Sprintf("%s-%s", netID, models.NetworkUser)): {},
|
|
|
|
|
|
+ GetDefaultNetworkUserRoleID(netID): {},
|
|
},
|
|
},
|
|
},
|
|
},
|
|
MetaData: fmt.Sprintf("Can connect to nodes in your network `%s` via Netmaker Desktop App. Platform users will have read-only access to the the dashboard.", netID),
|
|
MetaData: fmt.Sprintf("Can connect to nodes in your network `%s` via Netmaker Desktop App. Platform users will have read-only access to the the dashboard.", netID),
|
|
@@ -403,14 +405,32 @@ func ValidateUpdateRoleReq(userRole *models.UserRolePermissionTemplate) error {
|
|
|
|
|
|
// CreateRole - inserts new role into DB
|
|
// CreateRole - inserts new role into DB
|
|
func CreateRole(r models.UserRolePermissionTemplate) error {
|
|
func CreateRole(r models.UserRolePermissionTemplate) error {
|
|
- // check if role already exists
|
|
|
|
- if r.ID.String() == "" {
|
|
|
|
- return errors.New("role id cannot be empty")
|
|
|
|
|
|
+ // default roles are currently created directly in the db.
|
|
|
|
+ // this check is only to prevent future errors.
|
|
|
|
+ if r.Default && r.ID == "" {
|
|
|
|
+ return errors.New("role id cannot be empty for default role")
|
|
}
|
|
}
|
|
- _, err := database.FetchRecord(database.USER_PERMISSIONS_TABLE_NAME, r.ID.String())
|
|
|
|
- if err == nil {
|
|
|
|
- return errors.New("role already exists")
|
|
|
|
|
|
+
|
|
|
|
+ if !r.Default {
|
|
|
|
+ r.ID = models.UserRoleID(uuid.NewString())
|
|
}
|
|
}
|
|
|
|
+
|
|
|
|
+ // check if the role already exists
|
|
|
|
+ if r.Name == "" {
|
|
|
|
+ return errors.New("role name cannot be empty")
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ roles, err := ListNetworkRoles()
|
|
|
|
+ if err != nil {
|
|
|
|
+ return err
|
|
|
|
+ }
|
|
|
|
+
|
|
|
|
+ for _, role := range roles {
|
|
|
|
+ if role.Name == r.Name {
|
|
|
|
+ return errors.New("role already exists")
|
|
|
|
+ }
|
|
|
|
+ }
|
|
|
|
+
|
|
d, err := json.Marshal(r)
|
|
d, err := json.Marshal(r)
|
|
if err != nil {
|
|
if err != nil {
|
|
return err
|
|
return err
|
|
@@ -586,6 +606,14 @@ func GetDefaultNetworkUserGroupID(networkID models.NetworkID) models.UserGroupID
|
|
return models.UserGroupID(fmt.Sprintf("%s-%s-grp", networkID, models.NetworkUser))
|
|
return models.UserGroupID(fmt.Sprintf("%s-%s-grp", networkID, models.NetworkUser))
|
|
}
|
|
}
|
|
|
|
|
|
|
|
+func GetDefaultNetworkAdminRoleID(networkID models.NetworkID) models.UserRoleID {
|
|
|
|
+ return models.UserRoleID(fmt.Sprintf("%s-%s", networkID, models.NetworkAdmin))
|
|
|
|
+}
|
|
|
|
+
|
|
|
|
+func GetDefaultNetworkUserRoleID(networkID models.NetworkID) models.UserRoleID {
|
|
|
|
+ return models.UserRoleID(fmt.Sprintf("%s-%s", networkID, models.NetworkUser))
|
|
|
|
+}
|
|
|
|
+
|
|
// ListUserGroups - lists user groups
|
|
// ListUserGroups - lists user groups
|
|
func ListUserGroups() ([]models.UserGroup, error) {
|
|
func ListUserGroups() ([]models.UserGroup, error) {
|
|
data, err := database.FetchRecords(database.USER_GROUPS_TABLE_NAME)
|
|
data, err := database.FetchRecords(database.USER_GROUPS_TABLE_NAME)
|