github.com/mattermosttest/mattermost-server/v5@v5.0.0-20200917143240-9dfa12e121f9/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 == CLEAR_CACHE_MESSAGE_DATA {
    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 == CLEAR_CACHE_MESSAGE_DATA {
    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 len(role.Name) != 0 {
    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, _ := s.RoleStore.GetByNames(rolesToQuery)
    71  
    72  	for _, role := range roles {
    73  		s.rootStore.doStandardAddToCache(s.rootStore.roleCache, role.Name, role)
    74  	}
    75  
    76  	return append(foundRoles, roles...), nil
    77  }
    78  
    79  func (s LocalCacheRoleStore) Delete(roleId string) (*model.Role, error) {
    80  	role, err := s.RoleStore.Delete(roleId)
    81  
    82  	if err == nil {
    83  		s.rootStore.doInvalidateCacheCluster(s.rootStore.roleCache, role.Name)
    84  		defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache)
    85  	}
    86  	return role, err
    87  }
    88  
    89  func (s LocalCacheRoleStore) PermanentDeleteAll() error {
    90  	defer s.rootStore.roleCache.Purge()
    91  	defer s.rootStore.doClearCacheCluster(s.rootStore.roleCache)
    92  	defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache)
    93  
    94  	return s.RoleStore.PermanentDeleteAll()
    95  }
    96  
    97  func (s LocalCacheRoleStore) ChannelHigherScopedPermissions(roleNames []string) (map[string]*model.RolePermissions, error) {
    98  	sort.Strings(roleNames)
    99  	cacheKey := strings.Join(roleNames, "/")
   100  	var rolePermissionsMap map[string]*model.RolePermissions
   101  	if err := s.rootStore.doStandardReadCache(s.rootStore.rolePermissionsCache, cacheKey, &rolePermissionsMap); err == nil {
   102  		return rolePermissionsMap, nil
   103  	}
   104  
   105  	rolePermissionsMap, err := s.RoleStore.ChannelHigherScopedPermissions(roleNames)
   106  	if err != nil {
   107  		return nil, err
   108  	}
   109  
   110  	s.rootStore.doStandardAddToCache(s.rootStore.rolePermissionsCache, cacheKey, rolePermissionsMap)
   111  	return rolePermissionsMap, nil
   112  }