Add UserHasBeenDeactivated plugin hook (#20894)

Co-authored-by: Ben Schumacher <ben.schumacher@mattermost.com>
This commit is contained in:
Matthew Dorner 2023-08-30 01:55:20 -05:00 committed by GitHub
parent 8c8b8b7e79
commit 5de1e306de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
7 changed files with 144 additions and 14 deletions

View File

@ -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)

View File

@ -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
}

View File

@ -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
}

View File

@ -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)
}

View File

@ -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)
}

View File

@ -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)

View File

@ -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)
}