-
Notifications
You must be signed in to change notification settings - Fork 173
/
actions.go
100 lines (87 loc) · 2.76 KB
/
actions.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
package grpc
import (
"context"
"fmt"
"time"
"google.golang.org/protobuf/types/known/anypb"
"github.com/pydio/cells/v4/common"
"github.com/pydio/cells/v4/common/client/grpc"
"github.com/pydio/cells/v4/common/forms"
"github.com/pydio/cells/v4/common/proto/idm"
"github.com/pydio/cells/v4/common/proto/jobs"
"github.com/pydio/cells/v4/common/proto/service"
"github.com/pydio/cells/v4/scheduler/actions"
)
var (
DeleteUsersActionName = "actions.idm.users.delete"
)
type DeleteUsersAction struct {
common.RuntimeHolder
task *jobs.Task
params map[string]string
}
func (a *DeleteUsersAction) GetDescription(lang ...string) actions.ActionDescription {
return actions.ActionDescription{
ID: DeleteUsersActionName,
Label: "Remove Users",
Icon: "account-off",
Description: "Batch-delete users and groups, based on login or group path. Use one of the parameters.",
SummaryTemplate: "",
Category: actions.ActionCategoryIDM,
HasForm: true,
}
}
func (a *DeleteUsersAction) GetParametersForm() *forms.Form {
return &forms.Form{Groups: []*forms.Group{{Fields: []forms.Field{
&forms.FormField{
Name: "login",
Type: forms.ParamString,
Label: "Login",
Description: "Specific user login",
},
&forms.FormField{
Name: "groupPath",
Type: forms.ParamString,
Label: "groupPath",
Description: "Path to group (all users and groups will be deleted)",
},
}}}}
}
func (a *DeleteUsersAction) GetName() string {
return DeleteUsersActionName
}
// SetTask implements TaskUpdaterDelegateAction as the target
// service will update the task status on its side.
func (a *DeleteUsersAction) SetTask(task *jobs.Task) {
a.task = task
}
func (a *DeleteUsersAction) ProvidesProgress() bool {
return true
}
func (a *DeleteUsersAction) Init(job *jobs.Job, action *jobs.Action) error {
a.params = action.Parameters
return nil
}
func (a *DeleteUsersAction) Run(ctx context.Context, channels *actions.RunnableChannels, input *jobs.ActionMessage) (*jobs.ActionMessage, error) {
singleQ := &idm.UserSingleQuery{}
if login, ok := a.params["login"]; ok {
singleQ.Login = login
} else if gPath, ok := a.params["groupPath"]; ok {
singleQ.GroupPath = gPath
singleQ.Recursive = true
} else {
e := fmt.Errorf("params must provide either login or groupPath")
return input.WithError(e), e
}
q, _ := anypb.New(singleQ)
uCl := idm.NewUserServiceClient(grpc.GetClientConnFromCtx(ctx, common.ServiceUser, grpc.WithCallTimeout(30*time.Minute)))
_, e := uCl.DeleteUser(ctx, &idm.DeleteUserRequest{Query: &service.Query{SubQueries: []*anypb.Any{q}}})
if e != nil {
input = input.WithError(e)
} else {
input.AppendOutput(&jobs.ActionOutput{
Success: true,
})
}
return input, nil
}