From 6ad4a886b38352bb7b0095141b99490052f425a0 Mon Sep 17 00:00:00 2001 From: DaanSelen Date: Wed, 11 Feb 2026 09:27:17 +0000 Subject: [PATCH] feat: add feature to delegate visibility per permissions (#4) Reviewed-on: https://darjeeling.systemec.nl/daanselen/raspscreen/pulls/4 --- .gitignore | 2 + src/draw.go | 4 ++ src/main.go | 75 ++++++++++++++++++++++++++-------- src/raspi.go | 14 ++++--- templates/raspis.yaml.template | 7 ++++ 5 files changed, 80 insertions(+), 22 deletions(-) diff --git a/.gitignore b/.gitignore index 7946587..74307cc 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ # https://github.com/github/gitignore/blob/main/community/Golang/Go.AllowList.gitignore # # Binaries for programs and plugins +* +!*.* *.exe *.exe~ *.dll diff --git a/src/draw.go b/src/draw.go index e4df58c..4b836d2 100644 --- a/src/draw.go +++ b/src/draw.go @@ -137,6 +137,10 @@ func drawTargetSection(raspiNames []string, raspiTarget *string, uploadBtn, relo var previousTarget string var verifyBtn *widget.Button + if len(raspiNames) == 0 { + raspiNames = append(raspiNames, "No available targets...") + } + // Left side for selection of target piSelection := widget.NewRadioGroup(raspiNames, func(selected string) { if selected == previousTarget { diff --git a/src/main.go b/src/main.go index 5b86989..5930973 100644 --- a/src/main.go +++ b/src/main.go @@ -2,6 +2,8 @@ package main import ( "log" + "os/user" + "slices" "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" @@ -46,12 +48,18 @@ func main() { w.Resize(windowSize) w.SetIcon(iconResource) + uCtx, err := user.Current() + if err != nil { + log.Fatal("Unable to determine user") + } + username := uCtx.Username + cfg, ok := readConfig() if !ok { dialog.ShowInformation("Failed to initialize", "Failed to read or unmarshal the raspis.yaml", w) } - raspiNames := getRaspiNames(cfg) + raspiNames := getRaspiNames(cfg, username) // Define variables and print them out for debug // Presentation = 1 @@ -60,6 +68,19 @@ func main() { var raspiTarget string = "" var localUploadPath string + // Predefine the containers in total + var modeBtnRow *fyne.Container + var fileSelectRow *fyne.Container + var tgrtSelectionRow *fyne.Container + var footerRow *fyne.Container + + // Predefine the buttons for future reference + var uploadBtn *widget.Button + var reloadBtn *widget.Button + + // Predefine some of the labels + var pathLabel *widget.Label + log.Println("Current mode:", targetMode) if raspiTarget == "" { log.Println("Current target: None") @@ -68,28 +89,24 @@ func main() { } // Call the draw functions -> ./src/draw.go - footerRow, uploadBtn, reloadBtn := drawFooter(app, &raspiTarget, &localUploadPath, &targetMode, cfg) - fileSelectRow, pathLabel := drawFileSelection(&localUploadPath, &targetMode, w) - modeBtnRow := drawModeRow(pathLabel, &localUploadPath, &targetMode) - selectionRow := drawTargetSection(raspiNames, &raspiTarget, uploadBtn, reloadBtn, cfg) + footerRow, uploadBtn, reloadBtn = drawFooter(app, &raspiTarget, &localUploadPath, &targetMode, cfg) + fileSelectRow, pathLabel = drawFileSelection(&localUploadPath, &targetMode, w) + modeBtnRow = drawModeRow(pathLabel, &localUploadPath, &targetMode) + tgrtSelectionRow = drawTargetSection(raspiNames, &raspiTarget, uploadBtn, reloadBtn, cfg) center := container.NewVBox( modeBtnRow, widget.NewLabel(""), fileSelectRow, drawSeparator(true, true), - selectionRow, - ) - - top := container.NewVBox( - widget.NewLabel(appName), - drawSeparator(false, false), + tgrtSelectionRow, ) content := container.NewBorder( - top, // top + nil, footerRow, // bottom - nil, nil, // left, right + nil, + nil, // left, right center, //center ) @@ -107,14 +124,40 @@ func refreshButtons(givenButtons ...*widget.Button) { } // The raspiList is defined in the other go file, must me maintained separately -func getRaspiNames(cfg RaspiConfig) []string { +func getRaspiNames(cfg RaspiConfig, username string) []string { raspiNames := make([]string, 0, len(cfg.Raspis)) + var superAdminPresent bool + if len(cfg.SuperAdmin) == 0 { + superAdminPresent = false + } else { + superAdminPresent = true + } + log.Printf("Adding super_admin to the authorized_users at runtime: %s", cfg.SuperAdmin) // Append the name of the pi to the list we need for the radio button // i is the index field correlated to the name as per defined in rpi-list.go - for _, n := range cfg.Raspis { - raspiNames = append(raspiNames, n.Name) // append only the first element + for _, indPi := range cfg.Raspis { + if superAdminPresent { + indPi.AuthorizedUsers = append(indPi.AuthorizedUsers, cfg.SuperAdmin) // Append the superadmin to the authorized users + } + + if allowedUser(indPi.AuthorizedUsers, username) { + raspiNames = append(raspiNames, indPi.Name) // append only the first element + } } return raspiNames } + +func allowedUser(authorized_users []string, username string) bool { + if len(authorized_users) == 0 { + return false + } + + if slices.Contains(authorized_users, username) { + log.Printf("Found a match for the user %s", username) + return true + } else { + return false + } +} diff --git a/src/raspi.go b/src/raspi.go index 42009e1..09e5780 100644 --- a/src/raspi.go +++ b/src/raspi.go @@ -8,15 +8,17 @@ import ( ) type RaspberryPi struct { - Name string `yaml:"name"` - Hostname string `yaml:"hostname"` - Port string `yaml:"port"` - Username string `yaml:"username"` - Password string `yaml:"password,omitempty"` // optional + Name string `yaml:"name"` + AuthorizedUsers []string `yaml:"authorized_users"` + Hostname string `yaml:"hostname"` + Port string `yaml:"port"` + Username string `yaml:"username"` + Password string `yaml:"password,omitempty"` // optional } type RaspiConfig struct { - Raspis []RaspberryPi `yaml:"raspis"` + SuperAdmin string `yaml:"super_admin"` + Raspis []RaspberryPi `yaml:"raspis"` } func readConfig() (RaspiConfig, bool) { diff --git a/templates/raspis.yaml.template b/templates/raspis.yaml.template index 5994443..08522a2 100644 --- a/templates/raspis.yaml.template +++ b/templates/raspis.yaml.template @@ -1,3 +1,4 @@ +super_admin: SYSTEMECCLOUD\systemecadmin raspis: - name: rpi1 ip: 192.168.1.10 @@ -5,11 +6,17 @@ raspis: user: pi password: mypassword1 - name: rpi2 + authorized_users: + - WORKGROUP\username ip: 192.168.1.11 port: 2222 user: pi password: mypassword2 - name: rpi3 + authorized_users: + - WORKGROUP\username + - AZUREAD\username + - DOMAIN\username ip: 192.168.1.12 port: 22 user: pi