Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add status change confirmation #73

Merged
merged 29 commits into from
May 4, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
6c76941
Add status change confirmation
larkox Apr 3, 2020
3089056
Improve error handling and messages
larkox Apr 6, 2020
6e12f49
Improve detection of meetings by checking the start time
larkox Apr 7, 2020
4fbe800
Add event information to status change notification
larkox Apr 8, 2020
585eb4b
Merge branch 'master' into GH60StatusChangeConfirmation
levb Apr 8, 2020
18d40cc
Change start and end times types to pointers and be more clear with h…
larkox Apr 13, 2020
d151b09
Merge branch 'GH60StatusChangeConfirmation' of /~https://github.com/lar…
larkox Apr 13, 2020
a3ae95c
Merge branch 'master' into GH60StatusChangeConfirmation
levb Apr 13, 2020
4ba8776
Merge branch 'master' into GH60StatusChangeConfirmation
mickmister Apr 14, 2020
b2f4f5c
Use GetCalendarView to determine user's current status
mickmister Apr 15, 2020
189be08
Merge branch 'master' into GH60StatusChangeConfirmation
mickmister Apr 16, 2020
3b40dc1
Test status sync job
mickmister Apr 17, 2020
f92f794
treat bot as admin
mickmister Apr 21, 2020
e91f44b
Merge branch 'master' into GH60StatusChangeConfirmation
larkox Apr 22, 2020
0607f52
Use pretty statuses on slack action response
larkox Apr 22, 2020
a9c482b
Take in mind cancelled events for notifications and status change
larkox Apr 22, 2020
5528545
Merge branch 'master' into GH60StatusChangeConfirmation
larkox Apr 22, 2020
18d48bd
Use GetSchedule for status and GetCalendarView for reminders
mickmister Apr 23, 2020
dfbd7e0
Revert "Use GetSchedule for status and GetCalendarView for reminders"
larkox Apr 23, 2020
d67c0e3
Merge branch 'master' into GH60StatusChangeConfirmation
larkox Apr 23, 2020
7ead072
Fix panic due to error handling and change check to include events am…
larkox Apr 23, 2020
cd3cc6d
Add GetReminder setting and fix issue where a user that decided not t…
larkox Apr 23, 2020
fd5c45d
short circuit when no users want status, same with reminders. fix tests
mickmister Apr 24, 2020
78d13e9
Improve error testing
mickmister Apr 24, 2020
e268fcf
Name changes refactoring, and changes on how upcoming events and cale…
larkox Apr 24, 2020
fd149c7
Fix test
larkox Apr 24, 2020
5a698c8
Remove unneeded botUser references
larkox Apr 24, 2020
6db9aee
Merge branch 'master' into GH60StatusChangeConfirmation
mickmister Apr 29, 2020
7b2a6fe
Handle mistaken string when the availability handles an ongoing event…
larkox Apr 30, 2020
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions server/command/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
package command

func (c *Command) availability(parameters ...string) (string, error) {
authorized, err := c.MSCalendar.IsAuthorizedAdmin(c.Args.UserId)
if err != nil {
return "", err
}

if !authorized {
return "Not authorized", nil
}

switch {
case len(parameters) == 0:
resString, err := c.MSCalendar.Sync(c.Args.UserId)
Expand Down
1 change: 0 additions & 1 deletion server/command/connect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func TestConnect(t *testing.T) {

conf := &config.Config{
PluginURL: "http://localhost",
BotUserID: "bot_user_id",
}

mscal := mock_mscalendar.NewMockMSCalendar(ctrl)
Expand Down
1 change: 0 additions & 1 deletion server/command/disconnect_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ func TestDisconnect(t *testing.T) {

conf := &config.Config{
PluginURL: "http://localhost",
BotUserID: "bot_user_id",
}

mscal := mock_mscalendar.NewMockMSCalendar(ctrl)
Expand Down
1 change: 0 additions & 1 deletion server/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,6 @@ type StoredConfig struct {
type Config struct {
StoredConfig

BotUserID string
BuildDate string
BuildHash string
BuildHashShort string
Expand Down
3 changes: 0 additions & 3 deletions server/jobs/job_manager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
"github.com/stretchr/testify/require"

"github.com/mattermost/mattermost-plugin-api/cluster"
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/jobs/mock_cluster"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/mock_plugin_api"
Expand All @@ -29,9 +28,7 @@ func newTestEnv(ctrl *gomock.Controller) mscalendar.Env {
mockPluginAPI := mock_plugin_api.NewMockPluginAPI(ctrl)

logger := &bot.NilLogger{}
conf := &config.Config{BotUserID: "bot_mm_id"}
return mscalendar.Env{
Config: conf,
Dependencies: &mscalendar.Dependencies{
Store: s,
Logger: logger,
Expand Down
10 changes: 1 addition & 9 deletions server/mscalendar/availability.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (
"github.com/mattermost/mattermost-plugin-mscalendar/server/store"
"github.com/mattermost/mattermost-plugin-mscalendar/server/utils"
"github.com/mattermost/mattermost-server/v5/model"
"github.com/pkg/errors"
)

const (
Expand All @@ -39,14 +38,7 @@ func (m *mscalendar) Sync(mattermostUserID string) (string, error) {
}

func (m *mscalendar) SyncAll() (string, error) {
isAdmin, err := m.IsAuthorizedAdmin(m.actingUser.MattermostUserID)
if err != nil {
return "", err
}
if !isAdmin {
return "", errors.Errorf("Non-admin user attempting SyncAll %s", m.actingUser.MattermostUserID)
}
err = m.Filter(withSuperuserClient)
err := m.Filter(withSuperuserClient)
if err != nil {
return "", err
}
Expand Down
14 changes: 6 additions & 8 deletions server/mscalendar/availability_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,16 +312,14 @@ func makeStatusSyncTestEnv(ctrl *gomock.Controller) (Env, remote.Client) {
mockPluginAPI := mock_plugin_api.NewMockPluginAPI(ctrl)
logger := mock_bot.NewMockLogger(ctrl)

conf := &config.Config{BotUserID: "bot_mm_id"}
env := Env{
Config: conf,
Config: &config.Config{},
Dependencies: &Dependencies{
Store: s,
Logger: logger,
Poster: poster,
Remote: mockRemote,
PluginAPI: mockPluginAPI,
IsAuthorizedAdmin: func(mattermostUserID string) (bool, error) { return true, nil },
Store: s,
Logger: logger,
Poster: poster,
Remote: mockRemote,
PluginAPI: mockPluginAPI,
},
}

Expand Down
8 changes: 0 additions & 8 deletions server/mscalendar/daily_summary.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,14 +102,6 @@ func (m *mscalendar) SetDailySummaryEnabled(user *User, enable bool) (*store.Dai
}

func (m *mscalendar) ProcessAllDailySummary(now time.Time) error {
isAdmin, err := m.IsAuthorizedAdmin(m.actingUser.MattermostUserID)
if err != nil {
return err
}
if !isAdmin {
return errors.Errorf("Non-admin user attempting ProcessAllDailySummary %s", m.actingUser.MattermostUserID)
}

dsumIndex, err := m.Store.LoadDailySummaryIndex()
if err != nil {
return err
Expand Down
32 changes: 5 additions & 27 deletions server/mscalendar/daily_summary_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ import (
"time"

"github.com/golang/mock/gomock"
"github.com/mattermost/mattermost-plugin-mscalendar/server/config"
"github.com/mattermost/mattermost-plugin-mscalendar/server/mscalendar/mock_plugin_api"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote"
"github.com/mattermost/mattermost-plugin-mscalendar/server/remote/mock_remote"
Expand All @@ -23,24 +22,6 @@ func TestProcessAllDailySummary(t *testing.T) {
err string
runAssertions func(deps *Dependencies, client remote.Client)
}{
{
name: "Error fetching user admin",
err: "admin store error",
runAssertions: func(deps *Dependencies, client remote.Client) {
deps.IsAuthorizedAdmin = func(string) (bool, error) {
return false, errors.New("admin store error")
}
},
},
{
name: "User is not admin",
err: "Non-admin user attempting ProcessAllDailySummary bot_mm_id",
runAssertions: func(deps *Dependencies, client remote.Client) {
deps.IsAuthorizedAdmin = func(string) (bool, error) {
return false, nil
}
},
},
{
name: "Error fetching index",
err: "index store error",
Expand Down Expand Up @@ -159,16 +140,13 @@ Wednesday February 12
mockPluginAPI := mock_plugin_api.NewMockPluginAPI(ctrl)

logger := mock_bot.NewMockLogger(ctrl)
conf := &config.Config{BotUserID: "bot_mm_id"}
env := Env{
Config: conf,
Dependencies: &Dependencies{
Store: s,
Logger: logger,
Poster: poster,
Remote: mockRemote,
PluginAPI: mockPluginAPI,
IsAuthorizedAdmin: func(mattermostUserID string) (bool, error) { return true, nil },
Store: s,
Logger: logger,
Poster: poster,
Remote: mockRemote,
PluginAPI: mockPluginAPI,
},
}

Expand Down
11 changes: 1 addition & 10 deletions server/mscalendar/filters.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,6 @@

package mscalendar

import "github.com/mattermost/mattermost-plugin-mscalendar/server/remote"

type filterf func(*mscalendar) error

func (m *mscalendar) Filter(filters ...filterf) error {
Expand Down Expand Up @@ -44,14 +42,7 @@ func withClient(m *mscalendar) error {
return nil
}

var client remote.Client
var err error
if m.actingUser.MattermostUserID == m.Config.BotUserID {
client, err = m.MakeSuperuserClient()

} else {
client, err = m.MakeClient()
}
client, err := m.MakeClient()

if err != nil {
return err
Expand Down
16 changes: 6 additions & 10 deletions server/mscalendar/mscalendar.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,13 +25,12 @@ type MSCalendar interface {

// Dependencies contains all API dependencies
type Dependencies struct {
Logger bot.Logger
PluginAPI PluginAPI
Poster bot.Poster
Remote remote.Remote
Store store.Store
SettingsPanel settingspanel.Panel
IsAuthorizedAdmin func(string) (bool, error)
Logger bot.Logger
PluginAPI PluginAPI
Poster bot.Poster
Remote remote.Remote
Store store.Store
SettingsPanel settingspanel.Panel
}

type PluginAPI interface {
Expand All @@ -56,9 +55,6 @@ type mscalendar struct {
}

func New(env Env, actingMattermostUserID string) MSCalendar {
if actingMattermostUserID == "" {
actingMattermostUserID = env.Config.BotUserID
}
return &mscalendar{
Env: env,
actingUser: NewUser(actingMattermostUserID),
Expand Down
2 changes: 1 addition & 1 deletion server/mscalendar/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ func TestProcessNotification(t *testing.T) {
mockPluginAPI := mock_plugin_api.NewMockPluginAPI(ctrl)
mockClient := mock_remote.NewMockClient(ctrl)

conf := &config.Config{BotUserID: "bot_mm_id", PluginVersion: "x.x.x"}
conf := &config.Config{PluginVersion: "x.x.x"}
env := Env{
Config: conf,
Dependencies: &Dependencies{
Expand Down
12 changes: 5 additions & 7 deletions server/mscalendar/oauth2_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -363,18 +363,16 @@ func newOAuth2TestApp(ctrl *gomock.Controller) (oauth2connect.App, Env) {
OAuth2ClientSecret: "fakeclientsecret",
},
PluginURL: "http://localhost",
BotUserID: "bot-user-id",
}

env := Env{
Config: conf,
Dependencies: &Dependencies{
Store: mock_store.NewMockStore(ctrl),
Logger: &bot.NilLogger{},
Poster: mock_bot.NewMockPoster(ctrl),
Remote: remote.Makers[msgraph.Kind](conf, &bot.NilLogger{}),
PluginAPI: mock_plugin_api.NewMockPluginAPI(ctrl),
IsAuthorizedAdmin: func(mattermostUserID string) (bool, error) { return false, nil },
Store: mock_store.NewMockStore(ctrl),
Logger: &bot.NilLogger{},
Poster: mock_bot.NewMockPoster(ctrl),
Remote: remote.Makers[msgraph.Kind](conf, &bot.NilLogger{}),
PluginAPI: mock_plugin_api.NewMockPluginAPI(ctrl),
},
}

Expand Down
14 changes: 13 additions & 1 deletion server/mscalendar/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ package mscalendar

import (
"fmt"
"strings"

"github.com/mattermost/mattermost-server/v5/model"
"github.com/pkg/errors"
Expand Down Expand Up @@ -168,7 +169,18 @@ func (m *mscalendar) GetRemoteUser(mattermostUserID string) (*remote.User, error
}

func (m *mscalendar) IsAuthorizedAdmin(mattermostUserID string) (bool, error) {
return m.Dependencies.IsAuthorizedAdmin(mattermostUserID)
for _, userID := range strings.Split(m.AdminUserIDs, ",") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a pretty unexpected change, especially for the scope of the PR. Is there value lost by removing the injected dependency here? What packages had access to this functionality that no longer have access?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This was implemented on plugin.go, and passed to MSCalendar plugin, and only used from there. Therefore, implementing it here made more sense, and simplified the dependencies.

if userID == mattermostUserID {
return true, nil
}
}

ok, err := m.PluginAPI.IsSysAdmin(mattermostUserID)
if err != nil {
return false, err
}

return ok, nil
}

func (m *mscalendar) GetUserSettings(user *User) (*store.Settings, error) {
Expand Down
17 changes: 0 additions & 17 deletions server/plugin/plugin.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,11 +127,9 @@ func (p *Plugin) OnConfigurationChange() (err error) {
e.Dependencies.Remote = remote.Makers[msgraph.Kind](e.Config, e.Logger)

e.bot = e.bot.WithConfig(stored.BotConfig)
e.Config.BotUserID = e.bot.MattermostUserID()
e.Dependencies.Logger = e.bot
e.Dependencies.Poster = e.bot
e.Dependencies.Store = store.NewPluginStore(p.API, e.bot)
e.Dependencies.IsAuthorizedAdmin = p.IsAuthorizedAdmin
e.Dependencies.SettingsPanel = mscalendar.NewSettingsPanel(
e.bot,
e.Dependencies.Store,
Expand Down Expand Up @@ -216,21 +214,6 @@ func (p *Plugin) ServeHTTP(pc *plugin.Context, w http.ResponseWriter, req *http.
env.httpHandler.ServeHTTP(w, req)
}

func (p *Plugin) IsAuthorizedAdmin(mattermostUserID string) (bool, error) {
env := p.getEnv()
if mattermostUserID == env.BotUserID {
return true, nil
}

for _, userID := range strings.Split(env.Config.AdminUserIDs, ",") {
if userID == mattermostUserID {
return true, nil
}
}

return env.PluginAPI.IsSysAdmin(mattermostUserID)
}

func (p *Plugin) getEnv() Env {
p.envLock.RLock()
defer p.envLock.RUnlock()
Expand Down