diff --git a/server/channels/app/plugin_hooks_test.go b/server/channels/app/plugin_hooks_test.go index b5911ecfee..b8880e8164 100644 --- a/server/channels/app/plugin_hooks_test.go +++ b/server/channels/app/plugin_hooks_test.go @@ -775,6 +775,55 @@ func TestUserHasLoggedIn(t *testing.T) { assert.Equal(t, user.FirstName, "plugin-callback-success", "Expected firstname overwrite, got default") } +func TestUserHasBeenDeactivated(t *testing.T) { + th := Setup(t) + defer th.TearDown() + + tearDown, _, _ := SetAppEnvironmentWithPlugins(t, + []string{ + ` + package main + + import ( + "github.com/mattermost/mattermost/server/public/plugin" + "github.com/mattermost/mattermost/server/public/model" + ) + + type MyPlugin struct { + plugin.MattermostPlugin + } + + func (p *MyPlugin) UserHasBeenDeactivated(c *plugin.Context, user *model.User) { + user.Nickname = "plugin-callback-success" + p.API.UpdateUser(user) + } + + func main() { + plugin.ClientMain(&MyPlugin{}) + } + `}, th.App, th.NewPluginAPI) + defer tearDown() + + user := &model.User{ + Email: "success+test@example.com", + Nickname: "testnickname", + Username: "testusername", + Password: "testpassword", + } + + _, err := th.App.CreateUser(th.Context, user) + require.Nil(t, err) + + _, err = th.App.UpdateActive(th.Context, user, false) + require.Nil(t, err) + + time.Sleep(1 * time.Second) + user, err = th.App.GetUser(user.Id) + + require.Nil(t, err) + require.Equal(t, "plugin-callback-success", user.Nickname) +} + func TestUserHasBeenCreated(t *testing.T) { th := Setup(t) defer th.TearDown() @@ -805,11 +854,10 @@ func TestUserHasBeenCreated(t *testing.T) { defer tearDown() user := &model.User{ - Email: model.NewId() + "success+test@example.com", - Nickname: "Darth Vader", - Username: "vader" + model.NewId(), - Password: "passwd1", - AuthService: "", + Email: "success+test@example.com", + Nickname: "testnickname", + Username: "testusername", + Password: "testpassword", } _, err := th.App.CreateUser(th.Context, user) require.Nil(t, err) @@ -991,11 +1039,10 @@ func TestActiveHooks(t *testing.T) { require.True(t, th.App.GetPluginsEnvironment().IsActive(pluginID)) user1 := &model.User{ - Email: model.NewId() + "success+test@example.com", - Nickname: "Darth Vader1", - Username: "vader" + model.NewId(), - Password: "passwd1", - AuthService: "", + Email: "success+test@example.com", + Nickname: "testnickname", + Username: "testusername", + Password: "testpassword", } _, appErr := th.App.CreateUser(th.Context, user1) require.Nil(t, appErr) @@ -1097,10 +1144,10 @@ func TestHookMetrics(t *testing.T) { require.True(t, th.App.GetPluginsEnvironment().IsActive(pluginID)) user1 := &model.User{ - Email: model.NewId() + "success+test@example.com", - Nickname: "Darth Vader1", - Username: "vader" + model.NewId(), - Password: "passwd1", + Email: "success+test@example.com", + Nickname: "testnickname", + Username: "testusername", + Password: "testpassword", AuthService: "", } _, appErr := th.App.CreateUser(th.Context, user1) diff --git a/server/channels/app/user.go b/server/channels/app/user.go index 9bee2682f5..a778e56196 100644 --- a/server/channels/app/user.go +++ b/server/channels/app/user.go @@ -1003,6 +1003,16 @@ func (a *App) UpdateActive(c request.CTX, user *model.User, active bool) (*model a.sendUpdatedUserEvent(*ruser) + if pluginsEnvironment := a.GetPluginsEnvironment(); pluginsEnvironment != nil && !active && user.DeleteAt != 0 { + a.Srv().Go(func() { + pluginContext := pluginContext(c) + pluginsEnvironment.RunMultiPluginHook(func(hooks plugin.Hooks) bool { + hooks.UserHasBeenDeactivated(pluginContext, user) + return true + }, plugin.UserHasBeenDeactivatedID) + }) + } + return ruser, nil } diff --git a/server/public/plugin/client_rpc_generated.go b/server/public/plugin/client_rpc_generated.go index d10a118b27..3e1e12a823 100644 --- a/server/public/plugin/client_rpc_generated.go +++ b/server/public/plugin/client_rpc_generated.go @@ -879,6 +879,40 @@ func (s *hooksRPCServer) NotificationWillBePushed(args *Z_NotificationWillBePush return nil } +func init() { + hookNameToId["UserHasBeenDeactivated"] = UserHasBeenDeactivatedID +} + +type Z_UserHasBeenDeactivatedArgs struct { + A *Context + B *model.User +} + +type Z_UserHasBeenDeactivatedReturns struct { +} + +func (g *hooksRPCClient) UserHasBeenDeactivated(c *Context, user *model.User) { + _args := &Z_UserHasBeenDeactivatedArgs{c, user} + _returns := &Z_UserHasBeenDeactivatedReturns{} + if g.implemented[UserHasBeenDeactivatedID] { + if err := g.client.Call("Plugin.UserHasBeenDeactivated", _args, _returns); err != nil { + g.log.Error("RPC call UserHasBeenDeactivated to plugin failed.", mlog.Err(err)) + } + } + +} + +func (s *hooksRPCServer) UserHasBeenDeactivated(args *Z_UserHasBeenDeactivatedArgs, returns *Z_UserHasBeenDeactivatedReturns) error { + if hook, ok := s.impl.(interface { + UserHasBeenDeactivated(c *Context, user *model.User) + }); ok { + hook.UserHasBeenDeactivated(args.A, args.B) + } else { + return encodableError(fmt.Errorf("Hook UserHasBeenDeactivated called but not implemented.")) + } + return nil +} + type Z_RegisterCommandArgs struct { A *model.Command } diff --git a/server/public/plugin/hooks.go b/server/public/plugin/hooks.go index e37ef5d95a..153ca4fae3 100644 --- a/server/public/plugin/hooks.go +++ b/server/public/plugin/hooks.go @@ -51,6 +51,7 @@ const ( deprecatedGetTopicMetadataByIdsID = 33 ConfigurationWillBeSavedID = 34 NotificationWillBePushedID = 35 + UserHasBeenDeactivatedID = 36 TotalHooksID = iota ) @@ -298,4 +299,9 @@ type Hooks interface { // // Minimum server version: 9.0 NotificationWillBePushed(pushNotification *model.PushNotification, userID string) (*model.PushNotification, string) + + // UserHasBeenDeactivated is invoked when a user is deactivated. + // + // Minimum server version: 9.1 + UserHasBeenDeactivated(c *Context, user *model.User) } diff --git a/server/public/plugin/hooks_timer_layer_generated.go b/server/public/plugin/hooks_timer_layer_generated.go index 957904fea2..fd561cc9df 100644 --- a/server/public/plugin/hooks_timer_layer_generated.go +++ b/server/public/plugin/hooks_timer_layer_generated.go @@ -225,3 +225,9 @@ func (hooks *hooksTimerLayer) NotificationWillBePushed(pushNotification *model.P hooks.recordTime(startTime, "NotificationWillBePushed", true) return _returnsA, _returnsB } + +func (hooks *hooksTimerLayer) UserHasBeenDeactivated(c *Context, user *model.User) { + startTime := timePkg.Now() + hooks.hooksImpl.UserHasBeenDeactivated(c, user) + hooks.recordTime(startTime, "UserHasBeenDeactivated", true) +} diff --git a/server/public/plugin/plugintest/hooks.go b/server/public/plugin/plugintest/hooks.go index 2319a6afcf..d4d9f37435 100644 --- a/server/public/plugin/plugintest/hooks.go +++ b/server/public/plugin/plugintest/hooks.go @@ -344,6 +344,11 @@ func (_m *Hooks) UserHasBeenCreated(c *plugin.Context, user *model.User) { _m.Called(c, user) } +// UserHasBeenDeactivated provides a mock function with given fields: c, user +func (_m *Hooks) UserHasBeenDeactivated(c *plugin.Context, user *model.User) { + _m.Called(c, user) +} + // UserHasJoinedChannel provides a mock function with given fields: c, channelMember, actor func (_m *Hooks) UserHasJoinedChannel(c *plugin.Context, channelMember *model.ChannelMember, actor *model.User) { _m.Called(c, channelMember, actor) diff --git a/server/public/plugin/product_hooks_generated.go b/server/public/plugin/product_hooks_generated.go index 5875214272..df2a40ba9c 100644 --- a/server/public/plugin/product_hooks_generated.go +++ b/server/public/plugin/product_hooks_generated.go @@ -122,6 +122,10 @@ type NotificationWillBePushedIFace interface { NotificationWillBePushed(pushNotification *model.PushNotification, userID string) (*model.PushNotification, string) } +type UserHasBeenDeactivatedIFace interface { + UserHasBeenDeactivated(c *Context, user *model.User) +} + type HooksAdapter struct { implemented map[int]struct{} productHooks any @@ -378,6 +382,15 @@ func NewAdapter(productHooks any) (*HooksAdapter, error) { return nil, errors.New("hook has NotificationWillBePushed method but does not implement plugin.NotificationWillBePushed interface") } + // Assessing the type of the productHooks if it individually implements UserHasBeenDeactivated interface. + tt = reflect.TypeOf((*UserHasBeenDeactivatedIFace)(nil)).Elem() + + if ft.Implements(tt) { + a.implemented[UserHasBeenDeactivatedID] = struct{}{} + } else if _, ok := ft.MethodByName("UserHasBeenDeactivated"); ok { + return nil, errors.New("hook has UserHasBeenDeactivated method but does not implement plugin.UserHasBeenDeactivated interface") + } + return a, nil } @@ -623,3 +636,12 @@ func (a *HooksAdapter) NotificationWillBePushed(pushNotification *model.PushNoti return a.productHooks.(NotificationWillBePushedIFace).NotificationWillBePushed(pushNotification, userID) } + +func (a *HooksAdapter) UserHasBeenDeactivated(c *Context, user *model.User) { + if _, ok := a.implemented[UserHasBeenDeactivatedID]; !ok { + panic("product hooks must implement UserHasBeenDeactivated") + } + + a.productHooks.(UserHasBeenDeactivatedIFace).UserHasBeenDeactivated(c, user) + +}