github.com/haalcala/mattermost-server-change-repo/v5@v5.33.2/store/localcachelayer/role_layer.go (about)

     1  // Copyright (c) 2015-present Mattermost, Inc. All Rights Reserved.
     2  // See LICENSE.txt for license information.
     3  
     4  package localcachelayer
     5  
     6  import (
     7  	"sort"
     8  	"strings"
     9  
    10  	"github.com/mattermost/mattermost-server/v5/model"
    11  	"github.com/mattermost/mattermost-server/v5/store"
    12  )
    13  
    14  type LocalCacheRoleStore struct {
    15  	store.RoleStore
    16  	rootStore *LocalCacheStore
    17  }
    18  
    19  func (s *LocalCacheRoleStore) handleClusterInvalidateRole(msg *model.ClusterMessage) {
    20  	if msg.Data == ClearCacheMessageData {
    21  		s.rootStore.roleCache.Purge()
    22  	} else {
    23  		s.rootStore.roleCache.Remove(msg.Data)
    24  	}
    25  }
    26  
    27  func (s *LocalCacheRoleStore) handleClusterInvalidateRolePermissions(msg *model.ClusterMessage) {
    28  	if msg.Data == ClearCacheMessageData {
    29  		s.rootStore.rolePermissionsCache.Purge()
    30  	} else {
    31  		s.rootStore.rolePermissionsCache.Remove(msg.Data)
    32  	}
    33  }
    34  
    35  func (s LocalCacheRoleStore) Save(role *model.Role) (*model.Role, error) {
    36  	if role.Name != "" {
    37  		defer s.rootStore.doInvalidateCacheCluster(s.rootStore.roleCache, role.Name)
    38  		defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache)
    39  	}
    40  	return s.RoleStore.Save(role)
    41  }
    42  
    43  func (s LocalCacheRoleStore) GetByName(name string) (*model.Role, error) {
    44  	var role *model.Role
    45  	if err := s.rootStore.doStandardReadCache(s.rootStore.roleCache, name, &role); err == nil {
    46  		return role, nil
    47  	}
    48  
    49  	role, err := s.RoleStore.GetByName(name)
    50  	if err != nil {
    51  		return nil, err
    52  	}
    53  	s.rootStore.doStandardAddToCache(s.rootStore.roleCache, name, role)
    54  	return role, nil
    55  }
    56  
    57  func (s LocalCacheRoleStore) GetByNames(names []string) ([]*model.Role, error) {
    58  	var foundRoles []*model.Role
    59  	var rolesToQuery []string
    60  
    61  	for _, roleName := range names {
    62  		var role *model.Role
    63  		if err := s.rootStore.doStandardReadCache(s.rootStore.roleCache, roleName, &role); err == nil {
    64  			foundRoles = append(foundRoles, role)
    65  		} else {
    66  			rolesToQuery = append(rolesToQuery, roleName)
    67  		}
    68  	}
    69  
    70  	roles, err := s.RoleStore.GetByNames(rolesToQuery)
    71  	if err != nil {
    72  		return nil, err
    73  	}
    74  
    75  	for _, role := range roles {
    76  		s.rootStore.doStandardAddToCache(s.rootStore.roleCache, role.Name, role)
    77  	}
    78  
    79  	return append(foundRoles, roles...), nil
    80  }
    81  
    82  func (s LocalCacheRoleStore) Delete(roleId string) (*model.Role, error) {
    83  	role, err := s.RoleStore.Delete(roleId)
    84  
    85  	if err == nil {
    86  		s.rootStore.doInvalidateCacheCluster(s.rootStore.roleCache, role.Name)
    87  		defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache)
    88  	}
    89  	return role, err
    90  }
    91  
    92  func (s LocalCacheRoleStore) PermanentDeleteAll() error {
    93  	defer s.rootStore.roleCache.Purge()
    94  	defer s.rootStore.doClearCacheCluster(s.rootStore.roleCache)
    95  	defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache)
    96  
    97  	return s.RoleStore.PermanentDeleteAll()
    98  }
    99  
   100  func (s LocalCacheRoleStore) ChannelHigherScopedPermissions(roleNames []string) (map[string]*model.RolePermissions, error) {
   101  	sort.Strings(roleNames)
   102  	cacheKey := strings.Join(roleNames, "/")
   103  	var rolePermissionsMap map[string]*model.RolePermissions
   104  	if err := s.rootStore.doStandardReadCache(s.rootStore.rolePermissionsCache, cacheKey, &rolePermissionsMap); err == nil {
   105  		return rolePermissionsMap, nil
   106  	}
   107  
   108  	rolePermissionsMap, err := s.RoleStore.ChannelHigherScopedPermissions(roleNames)
   109  	if err != nil {
   110  		return nil, err
   111  	}
   112  
   113  	s.rootStore.doStandardAddToCache(s.rootStore.rolePermissionsCache, cacheKey, rolePermissionsMap)
   114  	return rolePermissionsMap, nil
   115  }