예제 #1
0
        private async Task <RolePermissionCacheItem> GetRolePermissionCacheItemAsync(string roleId, string tenantId)
        {
            var cacheKey = roleId + "@" + (!string.IsNullOrEmpty(tenantId) ? tenantId : "default");

            return(await cacheManager.GetRolePermissionCache().GetCancelableAsync(cacheKey, async(cancellationToken) =>
            {
                var cacheItem = new RolePermissionCacheItem(roleId);
                var normalizedRoleId = KeyNormalizer.NormalizeName(roleId);
                var staticRole = options.Role.StaticRoles.FirstOrDefault(c =>
                                                                         string.Equals(c.Id, normalizedRoleId, StringComparison.OrdinalIgnoreCase));

                if (staticRole != null)
                {
                    foreach (var permission in permissionStore.GetAll())
                    {
                        if (staticRole.IsGrantedByDefault(permission))
                        {
                            cacheItem.GrantedPermissions.Add(permission.Name);
                        }
                    }
                }
                else
                {
                    var role = await Store.FindByIdAsync(normalizedRoleId, cancellationToken);

                    if (role == null)
                    {
                        throw new StructureException("There is no role with given id: " + normalizedRoleId);
                    }
                }

                if (Store is IRolePermissionStore <TRolePermission> rolePermissionStore)
                {
                    foreach (var rolePermission in await rolePermissionStore.GetPermissionsAsync(normalizedRoleId, cancellationToken))
                    {
                        if (rolePermission.IsGranted)
                        {
                            cacheItem.GrantedPermissions.AddIfNotContains(rolePermission.PermissionName);
                        }
                        else
                        {
                            cacheItem.GrantedPermissions.Remove(rolePermission.PermissionName);
                        }
                    }
                }

                return cacheItem;
            }));
        }
예제 #2
0
 /// <summary>
 /// Gets a normalized representation of the specified <paramref name="key"/>.
 /// </summary>
 /// <param name="key">The value to normalize.</param>
 /// <returns>A normalized representation of the specified <paramref name="key"/>.</returns>
 public virtual string NormalizeKey(string key)
 {
     return((KeyNormalizer == null) ? key : KeyNormalizer.NormalizeName(key));
 }