Split Emojis and Webhooks permissions (#10239)

* Split Emojis and Webhooks permissions

* Fixing some tests

* Fixing more tests

* Fix more tests

* Fixed review comments

* Fixing review comments
Этот коммит содержится в:
Jesús Espino
2019-03-07 16:07:09 +01:00
коммит произвёл GitHub
родитель a4e3dfaebc
Коммит 84afd47021
29 изменённых файлов: 653 добавлений и 167 удалений

Просмотреть файл

@@ -284,6 +284,12 @@ func (s *LayeredRoleStore) Get(roleId string) StoreChannel {
})
}
func (s *LayeredRoleStore) GetAll() StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.RoleGetAll(s.TmpContext)
})
}
func (s *LayeredRoleStore) GetByName(name string) StoreChannel {
return s.RunQuery(func(supplier LayeredStoreSupplier) *LayeredStoreSupplierResult {
return supplier.RoleGetByName(s.TmpContext, name)

Просмотреть файл

@@ -34,6 +34,7 @@ type LayeredStoreSupplier interface {
// Roles
RoleSave(ctx context.Context, role *model.Role, hints ...LayeredStoreHint) *LayeredStoreSupplierResult
RoleGet(ctx context.Context, roleId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult
RoleGetAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult
RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult
RoleGetByNames(ctx context.Context, names []string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult
RoleDelete(ctx context.Context, roldId string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult

Просмотреть файл

@@ -30,6 +30,12 @@ func (s *LocalCacheSupplier) RoleGet(ctx context.Context, roleId string, hints .
return s.Next().RoleGet(ctx, roleId, hints...)
}
func (s *LocalCacheSupplier) RoleGetAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
// Roles are cached by name, as that is most commonly how they are looked up.
// This means that no caching is supported on roles being listed.
return s.Next().RoleGetAll(ctx, hints...)
}
func (s *LocalCacheSupplier) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
if result := s.doStandardReadCache(ctx, s.roleCache, name, hints...); result != nil {
return result

Просмотреть файл

@@ -29,6 +29,12 @@ func (s *RedisSupplier) RoleGet(ctx context.Context, roleId string, hints ...Lay
return s.Next().RoleGet(ctx, roleId, hints...)
}
func (s *RedisSupplier) RoleGetAll(ctx context.Context, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
// Roles are cached by name, as that is most commonly how they are looked up.
// This means that no caching is supported on roles being listed.
return s.Next().RoleGetAll(ctx, hints...)
}
func (s *RedisSupplier) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) *LayeredStoreSupplierResult {
key := buildRedisKeyForRoleName(name)

Просмотреть файл

@@ -160,6 +160,28 @@ func (s *SqlSupplier) RoleGet(ctx context.Context, roleId string, hints ...store
return result
}
func (s *SqlSupplier) RoleGetAll(ctx context.Context, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
result := store.NewSupplierResult()
var dbRoles []Role
if _, err := s.GetReplica().Select(&dbRoles, "SELECT * from Roles", map[string]interface{}{}); err != nil {
if err == sql.ErrNoRows {
result.Err = model.NewAppError("SqlRoleStore.GetAll", "store.sql_role.get_all.app_error", nil, err.Error(), http.StatusNotFound)
} else {
result.Err = model.NewAppError("SqlRoleStore.GetAll", "store.sql_role.get_all.app_error", nil, err.Error(), http.StatusInternalServerError)
}
}
var roles []*model.Role
for _, dbRole := range dbRoles {
roles = append(roles, dbRole.ToModel())
}
result.Data = roles
return result
}
func (s *SqlSupplier) RoleGetByName(ctx context.Context, name string, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
result := store.NewSupplierResult()

Просмотреть файл

@@ -523,6 +523,7 @@ type PluginStore interface {
type RoleStore interface {
Save(role *model.Role) StoreChannel
Get(roleId string) StoreChannel
GetAll() StoreChannel
GetByName(name string) StoreChannel
GetByNames(names []string) StoreChannel
Delete(roldId string) StoreChannel

Просмотреть файл

@@ -968,6 +968,29 @@ func (_m *LayeredStoreDatabaseLayer) RoleGet(ctx context.Context, roleId string,
return r0
}
// RoleGetAll provides a mock function with given fields: ctx, hints
func (_m *LayeredStoreDatabaseLayer) RoleGetAll(ctx context.Context, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
_va := make([]interface{}, len(hints))
for _i := range hints {
_va[_i] = hints[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
var r0 *store.LayeredStoreSupplierResult
if rf, ok := ret.Get(0).(func(context.Context, ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult); ok {
r0 = rf(ctx, hints...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*store.LayeredStoreSupplierResult)
}
}
return r0
}
// RoleGetByName provides a mock function with given fields: ctx, name, hints
func (_m *LayeredStoreDatabaseLayer) RoleGetByName(ctx context.Context, name string, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
_va := make([]interface{}, len(hints))

Просмотреть файл

@@ -628,6 +628,29 @@ func (_m *LayeredStoreSupplier) RoleGet(ctx context.Context, roleId string, hint
return r0
}
// RoleGetAll provides a mock function with given fields: ctx, hints
func (_m *LayeredStoreSupplier) RoleGetAll(ctx context.Context, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
_va := make([]interface{}, len(hints))
for _i := range hints {
_va[_i] = hints[_i]
}
var _ca []interface{}
_ca = append(_ca, ctx)
_ca = append(_ca, _va...)
ret := _m.Called(_ca...)
var r0 *store.LayeredStoreSupplierResult
if rf, ok := ret.Get(0).(func(context.Context, ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult); ok {
r0 = rf(ctx, hints...)
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(*store.LayeredStoreSupplierResult)
}
}
return r0
}
// RoleGetByName provides a mock function with given fields: ctx, name, hints
func (_m *LayeredStoreSupplier) RoleGetByName(ctx context.Context, name string, hints ...store.LayeredStoreHint) *store.LayeredStoreSupplierResult {
_va := make([]interface{}, len(hints))

Просмотреть файл

@@ -45,6 +45,22 @@ func (_m *RoleStore) Get(roleId string) store.StoreChannel {
return r0
}
// GetAll provides a mock function with given fields:
func (_m *RoleStore) GetAll() store.StoreChannel {
ret := _m.Called()
var r0 store.StoreChannel
if rf, ok := ret.Get(0).(func() store.StoreChannel); ok {
r0 = rf()
} else {
if ret.Get(0) != nil {
r0 = ret.Get(0).(store.StoreChannel)
}
}
return r0
}
// GetByName provides a mock function with given fields: name
func (_m *RoleStore) GetByName(name string) store.StoreChannel {
ret := _m.Called(name)

Просмотреть файл

@@ -7,6 +7,7 @@ import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/mattermost/mattermost-server/model"
"github.com/mattermost/mattermost-server/store"
@@ -15,6 +16,7 @@ import (
func TestRoleStore(t *testing.T, ss store.Store) {
t.Run("Save", func(t *testing.T) { testRoleStoreSave(t, ss) })
t.Run("Get", func(t *testing.T) { testRoleStoreGet(t, ss) })
t.Run("GetAll", func(t *testing.T) { testRoleStoreGetAll(t, ss) })
t.Run("GetByName", func(t *testing.T) { testRoleStoreGetByName(t, ss) })
t.Run("GetNames", func(t *testing.T) { testRoleStoreGetByNames(t, ss) })
t.Run("Delete", func(t *testing.T) { testRoleStoreDelete(t, ss) })
@@ -96,6 +98,47 @@ func testRoleStoreSave(t *testing.T, ss store.Store) {
assert.NotNil(t, res4.Err)
}
func testRoleStoreGetAll(t *testing.T, ss store.Store) {
prev := <-ss.Role().GetAll()
require.Nil(t, prev.Err)
prevCount := len(prev.Data.([]*model.Role))
// Save a role to test with.
r1 := &model.Role{
Name: model.NewId(),
DisplayName: model.NewId(),
Description: model.NewId(),
Permissions: []string{
"invite_user",
"create_public_channel",
"add_user_to_team",
},
SchemeManaged: false,
}
res1 := <-ss.Role().Save(r1)
require.Nil(t, res1.Err)
r2 := &model.Role{
Name: model.NewId(),
DisplayName: model.NewId(),
Description: model.NewId(),
Permissions: []string{
"invite_user",
"create_public_channel",
"add_user_to_team",
},
SchemeManaged: false,
}
res2 := <-ss.Role().Save(r2)
require.Nil(t, res2.Err)
res3 := <-ss.Role().GetAll()
require.Nil(t, res3.Err)
data := res3.Data.([]*model.Role)
assert.Len(t, data, prevCount+2)
}
func testRoleStoreGet(t *testing.T, ss store.Store) {
// Save a role to test with.
r1 := &model.Role{