Skip to content

Commit

Permalink
Disconnect users on instance uninstall (#41)
Browse files Browse the repository at this point in the history
  • Loading branch information
levb committed Aug 10, 2020
1 parent 1b847d5 commit b13e5b0
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 18 deletions.
13 changes: 13 additions & 0 deletions server/instances.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,19 @@ func (p *Plugin) UninstallInstance(instanceID types.ID, instanceType InstanceTyp
if instanceType != instance.Common().Type {
return errors.Errorf("%s did not match instance %s type %s", instanceType, instanceID, instance.Common().Type)
}

p.userStore.MapUsers(func(user *User) error {
if !user.ConnectedInstances.Contains(instance.GetID()) {
return nil
}

_, err = p.disconnectUser(instance, user)
if err != nil {
p.infof("UninstallInstance: failed to disconnect user: %v", err)
}
return nil
})

instances.Delete(instanceID)
updated = instances
return p.instanceStore.DeleteInstance(instanceID)
Expand Down
48 changes: 30 additions & 18 deletions server/kv.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"crypto/rsa"
"encoding/json"
"fmt"
"regexp"
"strings"

"github.com/mattermost/mattermost-plugin-jira/server/utils/kvstore"
"github.com/mattermost/mattermost-plugin-jira/server/utils/types"
Expand Down Expand Up @@ -63,6 +63,7 @@ type UserStore interface {
LoadMattermostUserId(instanceID types.ID, jiraUsername string) (types.ID, error)
DeleteConnection(instanceID, mattermostUserID types.ID) error
CountUsers() (int, error)
MapUsers(func(user *User) error) error
}

type OTSStore interface {
Expand Down Expand Up @@ -258,8 +259,6 @@ func (store store) LoadUser(mattermostUserID types.ID) (*User, error) {
return user, nil
}

var reHexKeyFormat = regexp.MustCompile("^[[:xdigit:]]{32}$")

func (store store) CountUsers() (int, error) {
count := 0
for i := 0; ; i++ {
Expand All @@ -269,34 +268,47 @@ func (store store) CountUsers() (int, error) {
}

for _, key := range keys {
// User records are not currently prefixed. Consider any 32-hex key.
if !reHexKeyFormat.MatchString(key) {
continue
if strings.HasPrefix(key, prefixUser) {
count++
}
}

if len(keys) < listPerPage {
break
}
}
return count, nil
}

func (store store) MapUsers(f func(user *User) error) error {
for i := 0; ; i++ {
keys, appErr := store.plugin.API.KVList(i, listPerPage)
if appErr != nil {
return appErr
}

var data []byte
data, appErr = store.plugin.API.KVGet(key)
if appErr != nil {
return 0, appErr
for _, key := range keys {
if !strings.HasPrefix(key, prefixUser) {
continue
}
v := map[string]interface{}{}
err := json.Unmarshal(data, &v)

user := NewUser("")
err := store.get(key, user)
if err != nil {
// Skip non-JSON values.
continue
return errors.WithMessage(err, fmt.Sprintf("failed to load Jira user for key:%s", key))
}

// A valid user record?
if v["Settings"] != nil && (v["accountId"] != nil || v["name"] != nil && v["key"] != nil) {
count++
err = f(user)
if err != nil {
return err
}
}

if len(keys) < listPerPage {
break
}
}
return count, nil
return nil
}

func (store store) EnsureAuthTokenEncryptSecret() (secret []byte, returnErr error) {
Expand Down
3 changes: 3 additions & 0 deletions server/kv_mock_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ func (store mockUserStore) DeleteConnection(instanceID, mattermostUserID types.I
func (store mockUserStore) CountUsers() (int, error) {
return 0, nil
}
func (store mockUserStore) MapUsers(func(*User) error) error {
return nil
}

type mockInstanceStore struct{}

Expand Down
4 changes: 4 additions & 0 deletions server/user.go
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,10 @@ func (p *Plugin) disconnectUser(instance Instance, user *User) (*Connection, err
return nil, err
}

if user.DefaultInstanceID == instance.GetID() {
user.DefaultInstanceID = ""
}

user.ConnectedInstances.Delete(instance.GetID())

err = p.userStore.DeleteConnection(instance.GetID(), user.MattermostUserID)
Expand Down

0 comments on commit b13e5b0

Please sign in to comment.