/
idm_acls.go
132 lines (105 loc) · 3.75 KB
/
idm_acls.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
package cmd
import (
"fmt"
"log"
"os"
"time"
"github.com/manifoldco/promptui"
"github.com/olekukonko/tablewriter"
"github.com/spf13/cobra"
"github.com/pydio/cells-sdk-go/v5/client/acl_service"
"github.com/pydio/cells-sdk-go/v5/models"
"github.com/pydio/cells-client/v4/rest"
)
var (
listAclsByNodeIds []string
listAclsDeleteResult bool
)
var listAcls = &cobra.Command{
Use: "list-acls",
Short: "List acls by node UUID",
Long: `
DESCRIPTION
List ACLs attached to one or more given nodes, and optionally delete them afterward.
Can be handy for debugging purposes.
EXAMPLE
# Given this listing in "test" workspace:
$` + os.Args[0] + ` ls -d test
Found 4 nodes at test:
+--------+--------------------------------------+-----------+--------+-------------+
| TYPE | UUID | NAME | SIZE | MODIFIED |
+--------+--------------------------------------+-----------+--------+-------------+
| Folder | 8ec79c1e-2464-40d0-a762-c36d8a9e5886 | . | 2.6 MB | 2 years ago |
| File | 1c989848-5eff-49cf-8727-4db754e02c25 | buro4.jpg | 541 kB | 2 years ago |
| File | d796d7c5-dce9-4994-bca3-3cf03c27c39d | büro1.jpg | 1.0 MB | 2 years ago |
| File | 15f09f59-9171-4e25-809a-488e475dafa4 | büro2.jpg | 996 kB | 2 years ago |
+--------+--------------------------------------+-----------+--------+-------------+
# List ACLs for file "buro4.txt":
` + os.Args[0] + ` idm list-acls --uuid 1c989848-5eff-49cf-8727-4db754e02c25
# List ACLs for multiple files:
` + os.Args[0] + ` idm list-acls -n 1c989848-5eff-49cf-8727-4db754e02c25 -n d796d7c5-dce9-4994-bca3-3cf03c27c39d
# Delete all ACLs on a given node
` + os.Args[0] + ` idm list-acls -n 1c989848-5eff-49cf-8727-4db754e02c25 --delete
`,
Run: func(cmd *cobra.Command, args []string) {
apiClient, err := rest.GetApiClient()
if err != nil {
log.Fatal(err)
}
ctx := cmd.Context()
if len(listAclsByNodeIds) == 0 {
log.Fatal("Cannot list ACLS. Please precise *at least* one node UUID")
}
params := &acl_service.SearchAclsParams{
Body: &models.RestSearchACLRequest{
Queries: []*models.IdmACLSingleQuery{{
NodeIDs: listAclsByNodeIds,
}},
},
Context: ctx,
}
result, err := apiClient.ACLService.SearchAcls(params)
if err != nil {
fmt.Printf("could not list acls: %s\n", err.Error())
log.Fatal(err)
}
if len(result.Payload.ACLs) > 0 {
fmt.Printf("Found %d ACLs:\n", len(result.Payload.ACLs))
for _, u := range result.Payload.ACLs {
fmt.Println(" - " + u.NodeID + " | " + u.RoleID + " | " + u.Action.Name + " | " + u.WorkspaceID)
}
table := tablewriter.NewWriter(os.Stdout)
table.SetHeader([]string{"UUID", "Role ID", "Action Name", "WS ID"})
table.SetAlignment(tablewriter.ALIGN_LEFT)
table.SetAutoWrapText(false)
for _, u := range result.Payload.ACLs {
table.Append([]string{u.NodeID, u.RoleID, u.Action.Name, u.WorkspaceID})
}
table.Render()
}
if listAclsDeleteResult {
pr := promptui.Prompt{Label: "Do you want to delete all these ACLs ?", IsConfirm: true}
if _, e := pr.Run(); e != nil {
fmt.Println("Aborting operation...")
return
}
for _, u := range result.Payload.ACLs {
_, er := apiClient.ACLService.DeleteACL(&acl_service.DeleteACLParams{
Body: u,
Context: ctx,
})
if er != nil {
log.Fatal("Could not delete ACL", u.ID, ":", er.Error())
} else {
fmt.Println(" - Removed ACL " + u.ID)
<-time.After(100 * time.Millisecond)
}
}
}
},
}
func init() {
listAcls.Flags().StringSliceVarP(&listAclsByNodeIds, "uuid", "n", []string{}, "Search by node UUID, can be used multiple times")
listAcls.Flags().BoolVarP(&listAclsDeleteResult, "delete", "", false, "Delete all found ACLs")
idmCmd.AddCommand(listAcls)
}