Add some group plugin APIs (#12180)
* Add group store GetByUser * Add group store GetByName * Add group plugin APIs * Minor naming fixes * Add minimum version comments
Этот коммит содержится в:
коммит произвёл
GitHub
родитель
fbf4d6d139
Коммит
9307f75fe9
@@ -123,6 +123,18 @@ func (s *SqlGroupStore) Get(groupId string) (*model.Group, *model.AppError) {
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (s *SqlGroupStore) GetByName(name string) (*model.Group, *model.AppError) {
|
||||
var group *model.Group
|
||||
if err := s.GetReplica().SelectOne(&group, "SELECT * from UserGroups WHERE Name = :Name", map[string]interface{}{"Name": name}); err != nil {
|
||||
if err == sql.ErrNoRows {
|
||||
return nil, model.NewAppError("SqlGroupStore.GroupGetByName", "store.sql_group.no_rows", nil, err.Error(), http.StatusNotFound)
|
||||
}
|
||||
return nil, model.NewAppError("SqlGroupStore.GroupGetByName", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return group, nil
|
||||
}
|
||||
|
||||
func (s *SqlGroupStore) GetByIDs(groupIDs []string) ([]*model.Group, *model.AppError) {
|
||||
var groups []*model.Group
|
||||
query := s.getQueryBuilder().Select("*").From("UserGroups").Where(sq.Eq{"Id": groupIDs})
|
||||
@@ -158,6 +170,26 @@ func (s *SqlGroupStore) GetAllBySource(groupSource model.GroupSource) ([]*model.
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func (s *SqlGroupStore) GetByUser(userId string) ([]*model.Group, *model.AppError) {
|
||||
var groups []*model.Group
|
||||
|
||||
query := `
|
||||
SELECT
|
||||
UserGroups.*
|
||||
FROM
|
||||
GroupMembers
|
||||
JOIN UserGroups ON UserGroups.Id = GroupMembers.GroupId
|
||||
WHERE
|
||||
GroupMembers.DeleteAt = 0
|
||||
AND UserId = :UserId`
|
||||
|
||||
if _, err := s.GetReplica().Select(&groups, query, map[string]interface{}{"UserId": userId}); err != nil {
|
||||
return nil, model.NewAppError("SqlGroupStore.GetByUser", "store.select_error", nil, err.Error(), http.StatusInternalServerError)
|
||||
}
|
||||
|
||||
return groups, nil
|
||||
}
|
||||
|
||||
func (s *SqlGroupStore) Update(group *model.Group) (*model.Group, *model.AppError) {
|
||||
var retrievedGroup *model.Group
|
||||
if err := s.GetMaster().SelectOne(&retrievedGroup, "SELECT * FROM UserGroups WHERE Id = :Id", map[string]interface{}{"Id": group.Id}); err != nil {
|
||||
|
||||
@@ -569,9 +569,11 @@ type UserTermsOfServiceStore interface {
|
||||
type GroupStore interface {
|
||||
Create(group *model.Group) (*model.Group, *model.AppError)
|
||||
Get(groupID string) (*model.Group, *model.AppError)
|
||||
GetByName(name string) (*model.Group, *model.AppError)
|
||||
GetByIDs(groupIDs []string) ([]*model.Group, *model.AppError)
|
||||
GetByRemoteID(remoteID string, groupSource model.GroupSource) (*model.Group, *model.AppError)
|
||||
GetAllBySource(groupSource model.GroupSource) ([]*model.Group, *model.AppError)
|
||||
GetByUser(userId string) ([]*model.Group, *model.AppError)
|
||||
Update(group *model.Group) (*model.Group, *model.AppError)
|
||||
Delete(groupID string) (*model.Group, *model.AppError)
|
||||
|
||||
|
||||
@@ -11,6 +11,7 @@ import (
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/mattermost/mattermost-server/model"
|
||||
@@ -20,9 +21,11 @@ import (
|
||||
func TestGroupStore(t *testing.T, ss store.Store) {
|
||||
t.Run("Create", func(t *testing.T) { testGroupStoreCreate(t, ss) })
|
||||
t.Run("Get", func(t *testing.T) { testGroupStoreGet(t, ss) })
|
||||
t.Run("GetByName", func(t *testing.T) { testGroupStoreGetByName(t, ss) })
|
||||
t.Run("GetByIDs", func(t *testing.T) { testGroupStoreGetByIDs(t, ss) })
|
||||
t.Run("GetByRemoteID", func(t *testing.T) { testGroupStoreGetByRemoteID(t, ss) })
|
||||
t.Run("GetAllBySource", func(t *testing.T) { testGroupStoreGetAllByType(t, ss) })
|
||||
t.Run("GetByUser", func(t *testing.T) { testGroupStoreGetByUser(t, ss) })
|
||||
t.Run("Update", func(t *testing.T) { testGroupStoreUpdate(t, ss) })
|
||||
t.Run("Delete", func(t *testing.T) { testGroupStoreDelete(t, ss) })
|
||||
|
||||
@@ -181,6 +184,37 @@ func testGroupStoreGet(t *testing.T, ss store.Store) {
|
||||
require.Equal(t, err.Id, "store.sql_group.no_rows")
|
||||
}
|
||||
|
||||
func testGroupStoreGetByName(t *testing.T, ss store.Store) {
|
||||
// Create a group
|
||||
g1 := &model.Group{
|
||||
Name: model.NewId(),
|
||||
DisplayName: model.NewId(),
|
||||
Description: model.NewId(),
|
||||
Source: model.GroupSourceLdap,
|
||||
RemoteId: model.NewId(),
|
||||
}
|
||||
d1, err := ss.Group().Create(g1)
|
||||
require.Nil(t, err)
|
||||
require.Len(t, d1.Id, 26)
|
||||
|
||||
// Get the group
|
||||
d2, err := ss.Group().GetByName(d1.Name)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, d1.Id, d2.Id)
|
||||
require.Equal(t, d1.Name, d2.Name)
|
||||
require.Equal(t, d1.DisplayName, d2.DisplayName)
|
||||
require.Equal(t, d1.Description, d2.Description)
|
||||
require.Equal(t, d1.RemoteId, d2.RemoteId)
|
||||
require.Equal(t, d1.CreateAt, d2.CreateAt)
|
||||
require.Equal(t, d1.UpdateAt, d2.UpdateAt)
|
||||
require.Equal(t, d1.DeleteAt, d2.DeleteAt)
|
||||
|
||||
// Get an invalid group
|
||||
_, err = ss.Group().GetByName(model.NewId())
|
||||
require.NotNil(t, err)
|
||||
require.Equal(t, err.Id, "store.sql_group.no_rows")
|
||||
}
|
||||
|
||||
func testGroupStoreGetByIDs(t *testing.T, ss store.Store) {
|
||||
var group1 *model.Group
|
||||
var group2 *model.Group
|
||||
@@ -280,6 +314,76 @@ func testGroupStoreGetAllByType(t *testing.T, ss store.Store) {
|
||||
}
|
||||
}
|
||||
|
||||
func testGroupStoreGetByUser(t *testing.T, ss store.Store) {
|
||||
// Save a group
|
||||
g1 := &model.Group{
|
||||
Name: model.NewId(),
|
||||
DisplayName: model.NewId(),
|
||||
Description: model.NewId(),
|
||||
Source: model.GroupSourceLdap,
|
||||
RemoteId: model.NewId(),
|
||||
}
|
||||
g1, err := ss.Group().Create(g1)
|
||||
require.Nil(t, err)
|
||||
|
||||
g2 := &model.Group{
|
||||
Name: model.NewId(),
|
||||
DisplayName: model.NewId(),
|
||||
Description: model.NewId(),
|
||||
Source: model.GroupSourceLdap,
|
||||
RemoteId: model.NewId(),
|
||||
}
|
||||
g2, err = ss.Group().Create(g2)
|
||||
require.Nil(t, err)
|
||||
|
||||
u1 := &model.User{
|
||||
Email: MakeEmail(),
|
||||
Username: model.NewId(),
|
||||
}
|
||||
u1, err = ss.User().Save(u1)
|
||||
require.Nil(t, err)
|
||||
|
||||
_, err = ss.Group().UpsertMember(g1.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
_, err = ss.Group().UpsertMember(g2.Id, u1.Id)
|
||||
require.Nil(t, err)
|
||||
|
||||
u2 := &model.User{
|
||||
Email: MakeEmail(),
|
||||
Username: model.NewId(),
|
||||
}
|
||||
u2, err = ss.User().Save(u2)
|
||||
require.Nil(t, err)
|
||||
|
||||
_, err = ss.Group().UpsertMember(g2.Id, u2.Id)
|
||||
require.Nil(t, err)
|
||||
|
||||
groups, err := ss.Group().GetByUser(u1.Id)
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 2, len(groups))
|
||||
found1 := false
|
||||
found2 := false
|
||||
for _, g := range groups {
|
||||
if g.Id == g1.Id {
|
||||
found1 = true
|
||||
}
|
||||
if g.Id == g2.Id {
|
||||
found2 = true
|
||||
}
|
||||
}
|
||||
assert.True(t, found1)
|
||||
assert.True(t, found2)
|
||||
|
||||
groups, err = ss.Group().GetByUser(u2.Id)
|
||||
require.Nil(t, err)
|
||||
require.Equal(t, 1, len(groups))
|
||||
assert.Equal(t, g2.Id, groups[0].Id)
|
||||
|
||||
groups, err = ss.Group().GetByUser(model.NewId())
|
||||
require.Nil(t, err)
|
||||
assert.Equal(t, 0, len(groups))
|
||||
}
|
||||
|
||||
func testGroupStoreUpdate(t *testing.T, ss store.Store) {
|
||||
// Save a new group
|
||||
g1 := &model.Group{
|
||||
|
||||
@@ -406,6 +406,31 @@ func (_m *GroupStore) GetByIDs(groupIDs []string) ([]*model.Group, *model.AppErr
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetByName provides a mock function with given fields: name
|
||||
func (_m *GroupStore) GetByName(name string) (*model.Group, *model.AppError) {
|
||||
ret := _m.Called(name)
|
||||
|
||||
var r0 *model.Group
|
||||
if rf, ok := ret.Get(0).(func(string) *model.Group); ok {
|
||||
r0 = rf(name)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).(*model.Group)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 *model.AppError
|
||||
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
|
||||
r1 = rf(name)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*model.AppError)
|
||||
}
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetByRemoteID provides a mock function with given fields: remoteID, groupSource
|
||||
func (_m *GroupStore) GetByRemoteID(remoteID string, groupSource model.GroupSource) (*model.Group, *model.AppError) {
|
||||
ret := _m.Called(remoteID, groupSource)
|
||||
@@ -431,6 +456,31 @@ func (_m *GroupStore) GetByRemoteID(remoteID string, groupSource model.GroupSour
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetByUser provides a mock function with given fields: userId
|
||||
func (_m *GroupStore) GetByUser(userId string) ([]*model.Group, *model.AppError) {
|
||||
ret := _m.Called(userId)
|
||||
|
||||
var r0 []*model.Group
|
||||
if rf, ok := ret.Get(0).(func(string) []*model.Group); ok {
|
||||
r0 = rf(userId)
|
||||
} else {
|
||||
if ret.Get(0) != nil {
|
||||
r0 = ret.Get(0).([]*model.Group)
|
||||
}
|
||||
}
|
||||
|
||||
var r1 *model.AppError
|
||||
if rf, ok := ret.Get(1).(func(string) *model.AppError); ok {
|
||||
r1 = rf(userId)
|
||||
} else {
|
||||
if ret.Get(1) != nil {
|
||||
r1 = ret.Get(1).(*model.AppError)
|
||||
}
|
||||
}
|
||||
|
||||
return r0, r1
|
||||
}
|
||||
|
||||
// GetGroupSyncable provides a mock function with given fields: groupID, syncableID, syncableType
|
||||
func (_m *GroupStore) GetGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) (*model.GroupSyncable, *model.AppError) {
|
||||
ret := _m.Called(groupID, syncableID, syncableType)
|
||||
|
||||
@@ -2776,6 +2776,23 @@ func (s *TimerLayerGroupStore) GetByIDs(groupIDs []string) ([]*model.Group, *mod
|
||||
return resultVar0, resultVar1
|
||||
}
|
||||
|
||||
func (s *TimerLayerGroupStore) GetByName(name string) (*model.Group, *model.AppError) {
|
||||
start := timemodule.Now()
|
||||
|
||||
resultVar0, resultVar1 := s.GroupStore.GetByName(name)
|
||||
|
||||
t := timemodule.Now()
|
||||
elapsed := t.Sub(start)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if resultVar1 == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("GroupStore.GetByName", success, float64(elapsed))
|
||||
}
|
||||
return resultVar0, resultVar1
|
||||
}
|
||||
|
||||
func (s *TimerLayerGroupStore) GetByRemoteID(remoteID string, groupSource model.GroupSource) (*model.Group, *model.AppError) {
|
||||
start := timemodule.Now()
|
||||
|
||||
@@ -2792,6 +2809,23 @@ func (s *TimerLayerGroupStore) GetByRemoteID(remoteID string, groupSource model.
|
||||
return resultVar0, resultVar1
|
||||
}
|
||||
|
||||
func (s *TimerLayerGroupStore) GetByUser(userId string) ([]*model.Group, *model.AppError) {
|
||||
start := timemodule.Now()
|
||||
|
||||
resultVar0, resultVar1 := s.GroupStore.GetByUser(userId)
|
||||
|
||||
t := timemodule.Now()
|
||||
elapsed := t.Sub(start)
|
||||
if s.Root.Metrics != nil {
|
||||
success := "false"
|
||||
if resultVar1 == nil {
|
||||
success = "true"
|
||||
}
|
||||
s.Root.Metrics.ObserveStoreMethodDuration("GroupStore.GetByUser", success, float64(elapsed))
|
||||
}
|
||||
return resultVar0, resultVar1
|
||||
}
|
||||
|
||||
func (s *TimerLayerGroupStore) GetGroupSyncable(groupID string, syncableID string, syncableType model.GroupSyncableType) (*model.GroupSyncable, *model.AppError) {
|
||||
start := timemodule.Now()
|
||||
|
||||
|
||||
Ссылка в новой задаче
Block a user