fix(server): reduce perms to collab when creating/deleting repos

Ayman Bagabas created

Change summary

server/cmd/collab.go | 4 ++--
server/cmd/create.go | 5 ++++-
server/cmd/delete.go | 2 +-
server/cmd/import.go | 2 +-
server/cmd/list.go   | 9 ++++++++-
5 files changed, 16 insertions(+), 6 deletions(-)

Detailed changes

server/cmd/collab.go 🔗

@@ -25,7 +25,7 @@ func collabAddCommand() *cobra.Command {
 		Use:               "add REPOSITORY USERNAME",
 		Short:             "Add a collaborator to a repo",
 		Args:              cobra.ExactArgs(2),
-		PersistentPreRunE: checkIfAdmin,
+		PersistentPreRunE: checkIfCollab,
 		RunE: func(cmd *cobra.Command, args []string) error {
 			cfg, _ := fromContext(cmd)
 			repo := args[0]
@@ -43,7 +43,7 @@ func collabRemoveCommand() *cobra.Command {
 		Use:               "remove REPOSITORY USERNAME",
 		Args:              cobra.ExactArgs(2),
 		Short:             "Remove a collaborator from a repo",
-		PersistentPreRunE: checkIfAdmin,
+		PersistentPreRunE: checkIfCollab,
 		RunE: func(cmd *cobra.Command, args []string) error {
 			cfg, _ := fromContext(cmd)
 			repo := args[0]

server/cmd/create.go 🔗

@@ -10,12 +10,13 @@ func createCommand() *cobra.Command {
 	var private bool
 	var description string
 	var projectName string
+	var hidden bool
 
 	cmd := &cobra.Command{
 		Use:               "create REPOSITORY",
 		Short:             "Create a new repository",
 		Args:              cobra.ExactArgs(1),
-		PersistentPreRunE: checkIfAdmin,
+		PersistentPreRunE: checkIfCollab,
 		RunE: func(cmd *cobra.Command, args []string) error {
 			cfg, _ := fromContext(cmd)
 			name := args[0]
@@ -23,6 +24,7 @@ func createCommand() *cobra.Command {
 				Private:     private,
 				Description: description,
 				ProjectName: projectName,
+				Hidden:      hidden,
 			}); err != nil {
 				return err
 			}
@@ -33,6 +35,7 @@ func createCommand() *cobra.Command {
 	cmd.Flags().BoolVarP(&private, "private", "p", false, "make the repository private")
 	cmd.Flags().StringVarP(&description, "description", "d", "", "set the repository description")
 	cmd.Flags().StringVarP(&projectName, "name", "n", "", "set the project name")
+	cmd.Flags().BoolVarP(&hidden, "hidden", "H", false, "hide the repository from the list")
 
 	return cmd
 }

server/cmd/delete.go 🔗

@@ -8,7 +8,7 @@ func deleteCommand() *cobra.Command {
 		Aliases:           []string{"del", "remove", "rm"},
 		Short:             "Delete a repository",
 		Args:              cobra.ExactArgs(1),
-		PersistentPreRunE: checkIfAdmin,
+		PersistentPreRunE: checkIfCollab,
 		RunE: func(cmd *cobra.Command, args []string) error {
 			cfg, _ := fromContext(cmd)
 			name := args[0]

server/cmd/import.go 🔗

@@ -16,7 +16,7 @@ func importCommand() *cobra.Command {
 		Use:               "import REPOSITORY REMOTE",
 		Short:             "Import a new repository from remote",
 		Args:              cobra.ExactArgs(2),
-		PersistentPreRunE: checkIfAdmin,
+		PersistentPreRunE: checkIfCollab,
 		RunE: func(cmd *cobra.Command, args []string) error {
 			cfg, _ := fromContext(cmd)
 			name := args[0]

server/cmd/list.go 🔗

@@ -7,6 +7,8 @@ import (
 
 // listCommand returns a command that list file or directory at path.
 func listCommand() *cobra.Command {
+	var all bool
+
 	listCmd := &cobra.Command{
 		Use:     "list",
 		Aliases: []string{"ls"},
@@ -20,11 +22,16 @@ func listCommand() *cobra.Command {
 			}
 			for _, r := range repos {
 				if cfg.Backend.AccessLevelByPublicKey(r.Name(), s.PublicKey()) >= backend.ReadOnlyAccess {
-					cmd.Println(r.Name())
+					if !r.IsHidden() || all {
+						cmd.Println(r.Name())
+					}
 				}
 			}
 			return nil
 		},
 	}
+
+	listCmd.Flags().BoolVarP(&all, "all", "a", false, "List all repositories")
+
 	return listCmd
 }