Example #1
0
        protected override void ProcessSession(ScriptSession session)
        {
            if (State != RunspaceAvailability.None && State != session.State)
            {
                return;
            }

            if (SessionType != null && SessionType.Length > 0)
            {
                if (patterns == null)
                {
                    patterns = new List <WildcardPattern>(SessionType.Length);
                    foreach (var type in SessionType)
                    {
                        patterns.Add(WildcardUtils.GetWildcardPattern(type));
                    }
                }
                if (!patterns.Any(pattern => pattern.IsMatch(session.ApplianceType)))
                {
                    return;
                }
            }

            WriteObject(session);
        }
Example #2
0
        public static string ConvertToRegexPattern(MatchMode matchMode, string value)
        {
            switch (matchMode)
            {
            case MatchMode.None:
            case MatchMode.All:
                throw new InvalidOperationException();

            case MatchMode.Contains:
                return(Regex.Escape(value));

            case MatchMode.StartsWith:
                return("^" + Regex.Escape(value));

            case MatchMode.EndsWith:
                return(Regex.Escape(value) + "$");

            case MatchMode.Wildcard:
                return(WildcardUtils.ToRegexPattern(value));

            case MatchMode.Regex:
                return(value);

            default:
                throw new NotImplementedException();
            }
        }
 protected override void BeginProcessing()
 {
     base.BeginProcessing();
     if (Parameter != null)
     {
         paramPatterns = new Dictionary <string, WildcardPattern>();
         foreach (var key in Parameter.Keys)
         {
             var wildcardPattern = WildcardUtils.GetWildcardPattern(Parameter[key].ToString());
             paramPatterns.Add(key.ToString(), wildcardPattern);
         }
     }
 }
Example #4
0
        public static bool TryParse(XmlNode node, out AuthorizationEntry entry)
        {
            entry = new AuthorizationEntry();
            if (node?.Attributes == null)
            {
                return(false);
            }
            var accessPermissionStr = node.Attributes?["Permission"].Value;
            var accountTypeStr      = node?.Attributes["IdentityType"].Value;
            var identityStr         = node?.Attributes["Identity"].Value;

            AccessPermission accessPermission;

            if (!Enum.TryParse(accessPermissionStr, true, out accessPermission) ||
                accessPermission == AccessPermission.NotSet)
            {
                return(false);
            }

            AccountType accountType;

            if (!Enum.TryParse(accountTypeStr, true, out accountType) || accountType == AccountType.Unknown)
            {
                return(false);
            }

            AccountIdentity identity = null;

            try
            {
                identity = new AccountIdentity(identityStr, true);
            }
            catch
            {
                PowerShellLog.Error($"Invalid identity {identityStr} provided for service configuration.");
            }

            entry.AccessPermission = accessPermission;
            entry.IdentityType     = accountType;
            entry.Identity         = identity;
            entry.wildcardPattern  = WildcardUtils.GetWildcardPattern(identity.Name);
            return(true);
        }
Example #5
0
        private IEnumerable <Item> GetMatchingItemEx(string[] languages, int version, Item item)
        {
            // if language is forced get the item in proper language
            if (languages.Length > 0 || version != Version.Latest.Number)
            {
                var allVersions = item.Versions.GetVersions(languages.Length > 0);

                if (languages.Length > 0)
                {
                    foreach (var language in languages)
                    {
                        var pattern = WildcardUtils.GetWildcardPattern(language);
                        foreach (var matchingItem in allVersions.Where(
                                     curItem => (language == null || pattern.IsMatch(curItem.Language.Name)) &&
                                     (version == Int32.MaxValue ||
                                      (version == Version.Latest.Number && curItem.Versions.IsLatestVersion()) ||
                                      (version == curItem.Version.Number)
                                     )))
                        {
                            yield return(matchingItem);
                        }
                    }
                }
                else
                {
                    foreach (var matchingItem in allVersions.Where(
                                 curItem => version == Int32.MaxValue ||
                                 (version == Version.Latest.Number && curItem.Versions.IsLatestVersion()) ||
                                 version == curItem.Version.Number))
                    {
                        yield return(matchingItem);
                    }
                }
            }
            else
            {
                yield return(item);
            }
        }
Example #6
0
        private static IEnumerable <string> CompleteTypes(string completeToken, int position)
        {
            var results = new List <string>();

            completeToken = completeToken.Trim('[', ']');
            var             lastDotPosition = completeToken.LastIndexOf('.');
            var             hasdot          = lastDotPosition > -1;
            var             endsWithDot     = completeToken.Length == lastDotPosition - 1;
            WildcardPattern nameWildcard;
            WildcardPattern fullWildcard;

            if (hasdot)
            {
                var namespaceToken = completeToken.Substring(0, lastDotPosition);
                var nameToken      = completeToken.Substring(lastDotPosition + 1);
                nameWildcard   = WildcardUtils.GetWildcardPattern(endsWithDot ? "*" : $"{nameToken}*");
                namespaceToken = namespaceToken.Replace(".", "*.");
                fullWildcard   = WildcardUtils.GetWildcardPattern($"{namespaceToken}*");
            }
            else
            {
                nameWildcard = WildcardUtils.GetWildcardPattern($"{completeToken}*");
                fullWildcard = WildcardUtils.GetWildcardPattern($"{completeToken}.*");

                //autocomplete accelerators
                var accelerators = TypeAccelerators.AllAccelerators;
                results.AddRange(
                    accelerators.Keys
                    .Where(acc => (nameWildcard.IsMatch(acc)))
                    .Select(
                        type => $"Type|{type} (Accelerator -> {accelerators[type].FullName})|{position}|{type}"));
            }

            foreach (var assembly in System.AppDomain.CurrentDomain.GetAssemblies())
            {
                try
                {
                    if (hasdot)
                    {
                        results.AddRange(
                            assembly.GetExportedTypes()
                            .Where(type => nameWildcard.IsMatch(type.Name) &&
                                   fullWildcard.IsMatch(type.Namespace) &&
                                   !type.Name.Contains('`'))
                            .Select(type => $"Type|{type.Name} ({type.Namespace})|{position}|{type.FullName}"));
                    }
                    else
                    {
                        results.AddRange(
                            assembly.GetExportedTypes()
                            .Where(type => (nameWildcard.IsMatch(type.Name) ||
                                            fullWildcard.IsMatch(type.Namespace)) &&
                                   !type.Name.Contains('`'))
                            .Select(type => $"Type|{type.Name} ({type.Namespace})|{position}|{type.FullName}"));
                    }
                }
                catch //(Exception e)
                {
                    // PowerShellLog.Error("Error enumerating types", e);
                    // Ignoring intentionally...
                    // This just happens for some assembiles with no consequences to user experience
                }
            }
            results.Sort(StringComparer.OrdinalIgnoreCase);
            return(results.ToArray());
        }
 protected static IEnumerable <T> WildcardFilterMany <T>(string[] filters, IEnumerable <T> items,
                                                         Func <T, string> propertyName)
 {
     return(WildcardUtils.WildcardFilterMany(filters, items, propertyName));
 }