mirror of
https://codeberg.org/shroff/phylum.git
synced 2026-01-04 02:31:14 -06:00
[server][cmd] improve chperm
This commit is contained in:
@@ -2,53 +2,33 @@ package fs
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"os"
|
||||
|
||||
"github.com/google/uuid"
|
||||
"github.com/shroff/phylum/server/internal/core"
|
||||
"github.com/sirupsen/logrus"
|
||||
"github.com/spf13/cobra"
|
||||
)
|
||||
|
||||
func setupChpermCommand() *cobra.Command {
|
||||
cmd := cobra.Command{
|
||||
Use: "chperm user <path | uuid> [ none | read | write | share ]",
|
||||
Short: "Change Resource Permissions",
|
||||
Use: "chperm <path | uuid> <user> [ none | read | write | share ]",
|
||||
Short: "Change Permissions",
|
||||
Args: cobra.ExactArgs(3),
|
||||
PreRun: func(cmd *cobra.Command, args []string) {
|
||||
openFileSystemFromFlags(cmd)
|
||||
},
|
||||
Run: func(cmd *cobra.Command, args []string) {
|
||||
var user string
|
||||
if _, err := core.Default.UserByEmail(context.Background(), args[0]); err != nil {
|
||||
logrus.Fatal(err)
|
||||
pathOrUuid := args[0]
|
||||
r, err := resourceByPathOrUuid(pathOrUuid)
|
||||
if err != nil {
|
||||
fmt.Println("cannot update permissions for '" + pathOrUuid + "': " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
var r core.Resource
|
||||
path := args[1]
|
||||
if path[0] != '/' {
|
||||
var id uuid.UUID
|
||||
var err error
|
||||
if id, err = uuid.Parse(path); err != nil {
|
||||
logrus.Fatal(err)
|
||||
}
|
||||
|
||||
if r, err = fs.ResourceByID(id); err != nil {
|
||||
if errors.Is(err, core.ErrResourceNotFound) {
|
||||
logrus.Fatal("Resource not found: " + path)
|
||||
} else {
|
||||
logrus.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
} else {
|
||||
var err error
|
||||
if r, err = fs.ResourceByPath(path); err != nil {
|
||||
if errors.Is(err, core.ErrResourceNotFound) {
|
||||
logrus.Fatal("Resource not found: " + path)
|
||||
} else {
|
||||
logrus.Fatal(err.Error())
|
||||
}
|
||||
}
|
||||
user := args[1]
|
||||
if _, err := core.Default.UserByEmail(context.Background(), user); err != nil {
|
||||
fmt.Println("cannot update permissions for user '" + user + "': " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
permission := core.PermissionNone
|
||||
@@ -61,11 +41,13 @@ func setupChpermCommand() *cobra.Command {
|
||||
case "share":
|
||||
permission = core.PermissionReadWriteShare
|
||||
default:
|
||||
logrus.Fatal("Unrecognized premission: " + args[2])
|
||||
fmt.Println("cannot update permissions for '" + pathOrUuid + "': unrecognized permission: " + args[2])
|
||||
os.Exit(1)
|
||||
}
|
||||
|
||||
if err := fs.UpdatePermissions(r, user, permission); err != nil {
|
||||
logrus.Fatal(err)
|
||||
fmt.Println("cannot update permissions for '" + pathOrUuid + "': " + err.Error())
|
||||
os.Exit(1)
|
||||
}
|
||||
},
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user