Skip to content
This repository has been archived by the owner on Mar 1, 2023. It is now read-only.

Commit

Permalink
Fix/extend address assignment. (#2)
Browse files Browse the repository at this point in the history
* 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 <[email protected]>
  • Loading branch information
3f8wohoo and fionera authored Feb 4, 2020
1 parent 97074d6 commit 42d9596
Showing 1 changed file with 59 additions and 5 deletions.
64 changes: 59 additions & 5 deletions cmd/assign/address/address.go
Original file line number Diff line number Diff line change
@@ -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: "<TEAMDRIVE> <ADDRESS>",
UsageText: "<TEAMDRIVE-NAME> <ADDRESS> <TYPE> <ROLE>",
}
}

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
Expand All @@ -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
Expand Down

0 comments on commit 42d9596

Please sign in to comment.