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