коммит произвёл
Christopher Speller
родитель
08e54ed824
Коммит
d23ca07133
22
app/login.go
22
app/login.go
@@ -11,6 +11,7 @@ import (
|
||||
|
||||
"github.com/avct/uasurfer"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
"github.com/mattermost/mattermost-server/store"
|
||||
)
|
||||
|
||||
@@ -65,6 +66,27 @@ func (a *App) AuthenticateUserForLogin(id, loginId, password, mfaToken string, l
|
||||
return nil, err
|
||||
}
|
||||
|
||||
if a.PluginsReady() {
|
||||
var rejectionReason string
|
||||
pluginContext := &plugin.Context{}
|
||||
a.Plugins.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
|
||||
rejectionReason = hooks.UserWillLogIn(pluginContext, user)
|
||||
return rejectionReason == ""
|
||||
}, plugin.UserWillLogInId)
|
||||
|
||||
if rejectionReason != "" {
|
||||
return nil, model.NewAppError("AuthenticateUserForLogin", "Login rejected by plugin: "+rejectionReason, nil, "", http.StatusBadRequest)
|
||||
}
|
||||
|
||||
a.Go(func() {
|
||||
pluginContext := &plugin.Context{}
|
||||
a.Plugins.RunMultiPluginHook(func(hooks plugin.Hooks) bool {
|
||||
hooks.UserHasLoggedIn(pluginContext, user)
|
||||
return true
|
||||
}, plugin.UserHasLoggedInId)
|
||||
})
|
||||
}
|
||||
|
||||
return user, nil
|
||||
}
|
||||
|
||||
|
||||
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/mattermost/mattermost-server/plugin/plugintest/mock"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
"time"
|
||||
)
|
||||
|
||||
func compileGo(t *testing.T, sourceCode, outputPath string) {
|
||||
@@ -371,3 +372,144 @@ func TestHookFileWillBeUploaded(t *testing.T) {
|
||||
io.Copy(&resultBuf, fileReader)
|
||||
assert.Equal(t, "changedtext", resultBuf.String())
|
||||
}
|
||||
|
||||
func TestUserWillLogIn_Blocked(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
err := th.App.UpdatePassword(th.BasicUser, "hunter2")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Error updating user password: %s", err)
|
||||
}
|
||||
|
||||
SetAppEnvironmentWithPlugins(t,
|
||||
[]string{
|
||||
`
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) UserWillLogIn(c *plugin.Context, user *model.User) string {
|
||||
return "Blocked By Plugin"
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
`}, th.App, th.App.NewPluginAPI)
|
||||
|
||||
user, err := th.App.AuthenticateUserForLogin("", th.BasicUser.Email, "hunter2", "", false)
|
||||
|
||||
if user != nil {
|
||||
t.Errorf("Expected nil, got %+v", user)
|
||||
}
|
||||
|
||||
if err == nil {
|
||||
t.Errorf("Expected err, got nil")
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserWillLogInIn_Passed(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
err := th.App.UpdatePassword(th.BasicUser, "hunter2")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Error updating user password: %s", err)
|
||||
}
|
||||
|
||||
SetAppEnvironmentWithPlugins(t,
|
||||
[]string{
|
||||
`
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) UserWillLogIn(c *plugin.Context, user *model.User) string {
|
||||
return ""
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
`}, th.App, th.App.NewPluginAPI)
|
||||
|
||||
user, err := th.App.AuthenticateUserForLogin("", th.BasicUser.Email, "hunter2", "", false)
|
||||
|
||||
if user == nil {
|
||||
t.Errorf("Expected user object, got nil")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil, got %s", err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestUserHasLoggedIn(t *testing.T) {
|
||||
th := Setup().InitBasic()
|
||||
defer th.TearDown()
|
||||
|
||||
err := th.App.UpdatePassword(th.BasicUser, "hunter2")
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Error updating user password: %s", err)
|
||||
}
|
||||
|
||||
SetAppEnvironmentWithPlugins(t,
|
||||
[]string{
|
||||
`
|
||||
package main
|
||||
|
||||
import (
|
||||
"github.com/mattermost/mattermost-server/plugin"
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
)
|
||||
|
||||
type MyPlugin struct {
|
||||
plugin.MattermostPlugin
|
||||
}
|
||||
|
||||
func (p *MyPlugin) UserHasLoggedIn(c *plugin.Context, user *model.User) {
|
||||
user.FirstName = "plugin-callback-success"
|
||||
p.API.UpdateUser(user)
|
||||
}
|
||||
|
||||
func main() {
|
||||
plugin.ClientMain(&MyPlugin{})
|
||||
}
|
||||
`}, th.App, th.App.NewPluginAPI)
|
||||
|
||||
user, err := th.App.AuthenticateUserForLogin("", th.BasicUser.Email, "hunter2", "", false)
|
||||
|
||||
if user == nil {
|
||||
t.Errorf("Expected user object, got nil")
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
t.Errorf("Expected nil, got %s", err)
|
||||
}
|
||||
|
||||
time.Sleep(2 * time.Second)
|
||||
|
||||
user, err = th.App.GetUser(th.BasicUser.Id)
|
||||
|
||||
if user.FirstName != "plugin-callback-success" {
|
||||
t.Errorf("Expected firstname overwrite, got default")
|
||||
}
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user