2018-02-06 15:34:08 +00:00
|
|
|
// Copyright (c) 2017-present Mattermost, Inc. All Rights Reserved.
|
|
|
|
|
// See License.txt for license information.
|
|
|
|
|
|
|
|
|
|
package store
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
|
|
|
|
|
"github.com/mattermost/mattermost-server/model"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func (s *LocalCacheSupplier) handleClusterInvalidateRole(msg *model.ClusterMessage) {
|
|
|
|
|
if msg.Data == CLEAR_CACHE_MESSAGE_DATA {
|
|
|
|
|
s.roleCache.Purge()
|
|
|
|
|
} else {
|
|
|
|
|
s.roleCache.Remove(msg.Data)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
func (s *LocalCacheSupplier) RoleSave(ctx context.Context, role *model.Role, hints ...LayeredStoreHint) (*model.Role, *model.AppError) {
|
2019-01-10 15:17:31 -05:00
|
|
|
if len(role.Name) != 0 {
|
2018-11-22 13:37:56 -05:00
|
|
|
defer s.doInvalidateCacheCluster(s.roleCache, role.Name)
|
2018-02-06 15:34:08 +00:00
|
|
|
}
|
2018-11-22 13:37:56 -05:00
|
|
|
return s.Next().RoleSave(ctx, role, hints...)
|
2018-02-06 15:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
func (s *LocalCacheSupplier) RoleGet(ctx context.Context, roleId string, hints ...LayeredStoreHint) (*model.Role, *model.AppError) {
|
2018-02-07 18:15:07 +00:00
|
|
|
// 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 looked up by ID.
|
2018-02-06 15:34:08 +00:00
|
|
|
return s.Next().RoleGet(ctx, roleId, hints...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
func (s *LocalCacheSupplier) RoleGetAll(ctx context.Context, hints ...LayeredStoreHint) ([]*model.Role, *model.AppError) {
|
2019-03-07 16:07:09 +01:00
|
|
|
// 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...)
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
func (s *LocalCacheSupplier) RoleGetByName(ctx context.Context, name string, hints ...LayeredStoreHint) (*model.Role, *model.AppError) {
|
2018-02-06 15:34:08 +00:00
|
|
|
if result := s.doStandardReadCache(ctx, s.roleCache, name, hints...); result != nil {
|
2019-05-17 17:03:05 +02:00
|
|
|
return result.Data.(*model.Role), nil
|
2018-02-06 15:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
role, err := s.Next().RoleGetByName(ctx, name, hints...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
|
|
|
|
}
|
2018-02-06 15:34:08 +00:00
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
result := NewSupplierResult()
|
|
|
|
|
result.Data = role
|
2018-02-06 15:34:08 +00:00
|
|
|
s.doStandardAddToCache(ctx, s.roleCache, name, result, hints...)
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
return role, nil
|
2018-02-06 15:34:08 +00:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
func (s *LocalCacheSupplier) RoleGetByNames(ctx context.Context, roleNames []string, hints ...LayeredStoreHint) ([]*model.Role, *model.AppError) {
|
2018-02-06 15:34:08 +00:00
|
|
|
var foundRoles []*model.Role
|
|
|
|
|
var rolesToQuery []string
|
|
|
|
|
|
|
|
|
|
for _, roleName := range roleNames {
|
|
|
|
|
if result := s.doStandardReadCache(ctx, s.roleCache, roleName, hints...); result != nil {
|
|
|
|
|
foundRoles = append(foundRoles, result.Data.(*model.Role))
|
|
|
|
|
} else {
|
|
|
|
|
rolesToQuery = append(rolesToQuery, roleName)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
rolesFound, err := s.Next().RoleGetByNames(ctx, rolesToQuery, hints...)
|
2018-02-06 15:34:08 +00:00
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
for _, role := range rolesFound {
|
|
|
|
|
res := NewSupplierResult()
|
|
|
|
|
res.Data = role
|
|
|
|
|
s.doStandardAddToCache(ctx, s.roleCache, role.Name, res, hints...)
|
2018-02-06 15:34:08 +00:00
|
|
|
}
|
2019-05-17 17:03:05 +02:00
|
|
|
foundRoles = append(foundRoles, rolesFound...)
|
2018-02-06 15:34:08 +00:00
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
return foundRoles, err
|
2018-02-06 15:34:08 +00:00
|
|
|
}
|
2018-04-18 10:18:07 +01:00
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
func (s *LocalCacheSupplier) RoleDelete(ctx context.Context, roleId string, hints ...LayeredStoreHint) (*model.Role, *model.AppError) {
|
|
|
|
|
role, err := s.Next().RoleDelete(ctx, roleId, hints...)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return nil, err
|
2018-04-20 19:49:13 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
s.doInvalidateCacheCluster(s.roleCache, role.Name)
|
|
|
|
|
|
|
|
|
|
return role, nil
|
2018-04-20 19:49:13 +01:00
|
|
|
}
|
|
|
|
|
|
2019-05-17 17:03:05 +02:00
|
|
|
func (s *LocalCacheSupplier) RolePermanentDeleteAll(ctx context.Context, hints ...LayeredStoreHint) *model.AppError {
|
2018-11-22 13:37:56 -05:00
|
|
|
defer s.roleCache.Purge()
|
|
|
|
|
defer s.doClearCacheCluster(s.roleCache)
|
2018-04-18 10:18:07 +01:00
|
|
|
|
2018-11-22 13:37:56 -05:00
|
|
|
return s.Next().RolePermanentDeleteAll(ctx, hints...)
|
2018-04-18 10:18:07 +01:00
|
|
|
}
|