github.com/vnforks/kid/v5@v5.22.1-0.20200408055009-b89d99c65676/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/vnforks/kid/v5/model"
    11  	"github.com/vnforks/kid/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, *model.AppError) {
    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, *model.AppError) {
    44  	if role := s.rootStore.doStandardReadCache(s.rootStore.roleCache, name); role != nil {
    45  		return role.(*model.Role), nil
    46  	}
    47  
    48  	role, err := s.RoleStore.GetByName(name)
    49  	if err != nil {
    50  		return nil, err
    51  	}
    52  	s.rootStore.doStandardAddToCache(s.rootStore.roleCache, name, role)
    53  	return role, nil
    54  }
    55  
    56  func (s LocalCacheRoleStore) GetByNames(names []string) ([]*model.Role, *model.AppError) {
    57  	var foundRoles []*model.Role
    58  	var rolesToQuery []string
    59  
    60  	for _, roleName := range names {
    61  		if role := s.rootStore.doStandardReadCache(s.rootStore.roleCache, roleName); role != nil {
    62  			foundRoles = append(foundRoles, role.(*model.Role))
    63  		} else {
    64  			rolesToQuery = append(rolesToQuery, roleName)
    65  		}
    66  	}
    67  
    68  	roles, _ := s.RoleStore.GetByNames(rolesToQuery)
    69  
    70  	for _, role := range roles {
    71  		s.rootStore.doStandardAddToCache(s.rootStore.roleCache, role.Name, role)
    72  	}
    73  
    74  	return append(foundRoles, roles...), nil
    75  }
    76  
    77  func (s LocalCacheRoleStore) Delete(roleId string) (*model.Role, *model.AppError) {
    78  	role, err := s.RoleStore.Delete(roleId)
    79  
    80  	if err == nil {
    81  		s.rootStore.doInvalidateCacheCluster(s.rootStore.roleCache, role.Name)
    82  		defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache)
    83  	}
    84  	return role, err
    85  }
    86  
    87  func (s LocalCacheRoleStore) PermanentDeleteAll() *model.AppError {
    88  	defer s.rootStore.roleCache.Purge()
    89  	defer s.rootStore.doClearCacheCluster(s.rootStore.roleCache)
    90  	defer s.rootStore.doClearCacheCluster(s.rootStore.rolePermissionsCache)
    91  
    92  	return s.RoleStore.PermanentDeleteAll()
    93  }
    94  
    95  func (s LocalCacheRoleStore) ClassHigherScopedPermissions(roleNames []string) (map[string]*model.RolePermissions, *model.AppError) {
    96  	sort.Strings(roleNames)
    97  	cacheKey := strings.Join(roleNames, "/")
    98  	if rolePermissionsMap := s.rootStore.doStandardReadCache(s.rootStore.rolePermissionsCache, cacheKey); rolePermissionsMap != nil {
    99  		return rolePermissionsMap.(map[string]*model.RolePermissions), nil
   100  	}
   101  
   102  	rolePermissionsMap, err := s.RoleStore.ClassHigherScopedPermissions(roleNames)
   103  	if err != nil {
   104  		return nil, err
   105  	}
   106  
   107  	s.rootStore.doStandardAddToCache(s.rootStore.rolePermissionsCache, cacheKey, rolePermissionsMap)
   108  	return rolePermissionsMap, nil
   109  }