浏览代码

feat(go): add filters for users and groups;

Vishal Dalwadi 4 月之前
父节点
当前提交
be43d69f63
共有 2 个文件被更改,包括 63 次插入30 次删除
  1. 32 30
      models/settings.go
  2. 31 0
      pro/auth/sync.go

+ 32 - 30
models/settings.go

@@ -9,34 +9,36 @@ const (
 )
 
 type ServerSettings struct {
-	NetclientAutoUpdate        bool   `json:"netclientautoupdate"`
-	Verbosity                  int32  `json:"verbosity"`
-	AuthProvider               string `json:"authprovider"`
-	OIDCIssuer                 string `json:"oidcissuer"`
-	ClientID                   string `json:"client_id"`
-	ClientSecret               string `json:"client_secret"`
-	GoogleAdminEmail           string `json:"google_admin_email"`
-	GoogleSACredsJson          string `json:"google_sa_creds_json"`
-	AzureTenant                string `json:"azure_tenant"`
-	Telemetry                  string `json:"telemetry"`
-	BasicAuth                  bool   `json:"basic_auth"`
-	JwtValidityDuration        int    `json:"jwt_validity_duration"`
-	RacAutoDisable             bool   `json:"rac_auto_disable"`
-	RacRestrictToSingleNetwork bool   `json:"rac_restrict_to_single_network"`
-	EndpointDetection          bool   `json:"endpoint_detection"`
-	AllowedEmailDomains        string `json:"allowed_email_domains"`
-	EmailSenderAddr            string `json:"email_sender_addr"`
-	EmailSenderUser            string `json:"email_sender_user"`
-	EmailSenderPassword        string `json:"email_sender_password"`
-	SmtpHost                   string `json:"smtp_host"`
-	SmtpPort                   int    `json:"smtp_port"`
-	MetricInterval             string `json:"metric_interval"`
-	MetricsPort                int    `json:"metrics_port"`
-	ManageDNS                  bool   `json:"manage_dns"`
-	DefaultDomain              string `json:"default_domain"`
-	Stun                       bool   `json:"stun"`
-	StunServers                string `json:"stun_servers"`
-	Theme                      Theme  `json:"theme"`
-	TextSize                   string `json:"text_size"`
-	ReducedMotion              bool   `json:"reduced_motion"`
+	NetclientAutoUpdate        bool     `json:"netclientautoupdate"`
+	Verbosity                  int32    `json:"verbosity"`
+	AuthProvider               string   `json:"authprovider"`
+	OIDCIssuer                 string   `json:"oidcissuer"`
+	ClientID                   string   `json:"client_id"`
+	ClientSecret               string   `json:"client_secret"`
+	GoogleAdminEmail           string   `json:"google_admin_email"`
+	GoogleSACredsJson          string   `json:"google_sa_creds_json"`
+	AzureTenant                string   `json:"azure_tenant"`
+	UserFilters                []string `json:"user_filters"`
+	GroupFilters               []string `json:"group_filters"`
+	Telemetry                  string   `json:"telemetry"`
+	BasicAuth                  bool     `json:"basic_auth"`
+	JwtValidityDuration        int      `json:"jwt_validity_duration"`
+	RacAutoDisable             bool     `json:"rac_auto_disable"`
+	RacRestrictToSingleNetwork bool     `json:"rac_restrict_to_single_network"`
+	EndpointDetection          bool     `json:"endpoint_detection"`
+	AllowedEmailDomains        string   `json:"allowed_email_domains"`
+	EmailSenderAddr            string   `json:"email_sender_addr"`
+	EmailSenderUser            string   `json:"email_sender_user"`
+	EmailSenderPassword        string   `json:"email_sender_password"`
+	SmtpHost                   string   `json:"smtp_host"`
+	SmtpPort                   int      `json:"smtp_port"`
+	MetricInterval             string   `json:"metric_interval"`
+	MetricsPort                int      `json:"metrics_port"`
+	ManageDNS                  bool     `json:"manage_dns"`
+	DefaultDomain              string   `json:"default_domain"`
+	Stun                       bool     `json:"stun"`
+	StunServers                string   `json:"stun_servers"`
+	Theme                      Theme    `json:"theme"`
+	TextSize                   string   `json:"text_size"`
+	ReducedMotion              bool     `json:"reduced_motion"`
 }

+ 31 - 0
pro/auth/sync.go

@@ -11,6 +11,7 @@ import (
 	proLogic "github.com/gravitl/netmaker/pro/logic"
 	"github.com/gravitl/netmaker/servercfg"
 	"os"
+	"strings"
 	"time"
 )
 
@@ -76,7 +77,22 @@ func SyncUsers(idpClient idp.Client) error {
 		}
 	}
 
+	filters := logic.GetServerSettings().UserFilters
+
 	for _, user := range idpUsers {
+		var found bool
+		for _, filter := range filters {
+			if strings.HasPrefix(user.Username, filter) {
+				found = true
+				break
+			}
+		}
+
+		// if there are filters but none of them match, then skip this user.
+		if !found {
+			continue
+		}
+
 		dbUser, ok := dbUsersMap[user.ID]
 		if !ok {
 			// create the user only if it doesn't exist.
@@ -150,7 +166,22 @@ func SyncGroups(idpClient idp.Client) error {
 
 	modifiedUsers := make(map[string]struct{})
 
+	filters := logic.GetServerSettings().GroupFilters
+
 	for _, group := range idpGroups {
+		var found bool
+		for _, filter := range filters {
+			if strings.HasPrefix(group.Name, filter) {
+				found = true
+				break
+			}
+		}
+
+		// if there are filters but none of them match, then skip this group.
+		if len(filters) > 0 && !found {
+			continue
+		}
+
 		if _, ok := dbGroupsMap[group.ID]; !ok {
 			// create the group only if it doesn't exist.
 			err := proLogic.CreateUserGroup(models.UserGroup{