From 42d9596287d7ec6a442981e5ee0c20d9c3aa8535 Mon Sep 17 00:00:00 2001 From: 3f8wohoo <34687245+3f8wohoo@users.noreply.github.com> Date: Tue, 4 Feb 2020 09:20:22 +0100 Subject: [PATCH] Fix/extend address assignment. (#2) * Fix/extend shared drive address assignment. * Declare address and role types as constants. * Declare the supportedroles and types globally and make the ifs a little bit easier Co-authored-by: Fionera --- cmd/assign/address/address.go | 64 ++++++++++++++++++++++++++++++++--- 1 file changed, 59 insertions(+), 5 deletions(-) diff --git a/cmd/assign/address/address.go b/cmd/assign/address/address.go index 8cf2c1d..e8d8bb7 100644 --- a/cmd/assign/address/address.go +++ b/cmd/assign/address/address.go @@ -1,29 +1,83 @@ package address import ( + "strings" + "github.com/sirupsen/logrus" "github.com/urfave/cli" "github.com/fionera/TeamDriveManager/api" "github.com/fionera/TeamDriveManager/api/admin" "github.com/fionera/TeamDriveManager/api/drive" - "github.com/fionera/TeamDriveManager/api/iam" . "github.com/fionera/TeamDriveManager/config" ) +var ( + supportedTypes = []string{ + "user", + "group", + } + + supportedRoles = []string{ + "organizer", + "fileOrganizer", + "writer", + "commenter", + "reader", + } +) + func NewAssignAddressCmd() cli.Command { return cli.Command{ Name: "address", Usage: "Assign an address to a specified teamdrive", Action: CmdAssignAddress, Flags: []cli.Flag{}, - UsageText: "
", + UsageText: "
", } } +func contains(s []string, e string) bool { + for _, a := range s { + if a == e { + return true + } + } + return false +} + func CmdAssignAddress(c *cli.Context) { + teamDriveName := c.Args().Get(0) + address := c.Args().Get(1) + addressType := c.Args().Get(2) + role := c.Args().Get(3) + + if teamDriveName == "" { + logrus.Error("Please supply a teamdrive name") + return + } + + if address == "" { + logrus.Error("Please supply an address") + return + } + + if addressType == "" || !contains(supportedTypes, addressType) { + logrus.Errorf("Unsupported or empty address type (allowed: %s)", strings.Join(supportedTypes, ", ")) + return + } + + if role == "" { + logrus.Info("No role supplied. Setting 'reader' permission...") + role = "reader" + } + + if !contains(supportedRoles, role) { + logrus.Error("Unsupported role: '"+role+"' (allowed: %s)", strings.Join(supportedRoles, ", ")) + return + } - client, err := api.CreateClient(App.AppConfig.ServiceAccountFile, App.AppConfig.Impersonate, []string{iam.CloudPlatformScope, admin.AdminDirectoryGroupScope}) + client, err := api.CreateClient(App.AppConfig.ServiceAccountFile, App.AppConfig.Impersonate, []string{drive.DriveScope, admin.AdminDirectoryGroupScope}) if err != nil { logrus.Error(err) return @@ -42,8 +96,8 @@ func CmdAssignAddress(c *cli.Context) { } for _, teamDrive := range teamDrives { - if teamDrive.Name == c.Args().Get(0) { - _, err := driveApi.CreatePermission(teamDrive.Id, "organizer", c.Args().Get(1), "user") + if teamDrive.Name == teamDriveName { + _, err := driveApi.CreatePermission(teamDrive.Id, role, address, addressType) if err != nil { logrus.Error(err) return