public ApiKeyViewModel(CredentialViewModel cred)
        {
            if (cred == null)
            {
                throw new ArgumentNullException(nameof(cred));
            }

            if (cred.Scopes == null)
            {
                throw new ArgumentNullException(nameof(cred.Scopes));
            }

            // Currently ApiKeys.cshtml has single Owner per ApiKey restriction.
            var owner = cred
                        .Scopes
                        .Select(s => s.Owner)
                        .Distinct()
                        .SingleOrDefault();

            var scopes = cred
                         .Scopes
                         .Select(s => s.AllowedAction)
                         .Distinct()
                         .OrderBy(s => s)
                         .ToList();

            var subjects = cred
                           .Scopes
                           .Select(s => s.Subject)
                           .Distinct()
                           .OrderBy(s => s)
                           .ToList();

            var globPattern = subjects
                              .FirstOrDefault(s => s != null && s.Contains("*"));

            var packages = subjects
                           .Except(new[] { globPattern })
                           .ToList();

            Key               = cred.Key;
            Type              = cred.Type;
            Value             = cred.Value;
            Description       = cred.Description;
            Expires           = cred.Expires?.ToString("O");
            HasExpired        = cred.HasExpired;
            IsNonScopedApiKey = cred.IsNonScopedApiKey;
            RevocationSource  = cred.RevocationSource;
            Owner             = owner;
            Scopes            = scopes;
            Packages          = packages;
            GlobPattern       = globPattern;
        }
        private void SendApiKeyChangeNotice(User user, CredentialViewModel changedCredentialViewModel, string bodyTemplate, string subjectTemplate)
        {
            string body = String.Format(
                CultureInfo.CurrentCulture,
                bodyTemplate,
                changedCredentialViewModel.Description);

            string subject = String.Format(
                CultureInfo.CurrentCulture,
                subjectTemplate,
                Config.GalleryOwner.DisplayName,
                Strings.CredentialType_ApiKey);

            SendSupportMessage(user, body, subject);
        }
        private void SendCredentialChangeNotice(User user, CredentialViewModel changedCredentialViewModel, string bodyTemplate, string subjectTemplate)
        {
            // What kind of credential is this?
            string name = changedCredentialViewModel.AuthUI == null ? changedCredentialViewModel.TypeCaption : changedCredentialViewModel.AuthUI.AccountNoun;

            string body = String.Format(
                CultureInfo.CurrentCulture,
                bodyTemplate,
                name);

            string subject = String.Format(
                CultureInfo.CurrentCulture,
                subjectTemplate,
                Config.GalleryOwner.DisplayName,
                name);

            SendSupportMessage(user, body, subject);
        }
 public void SendCredentialAddedNotice(User user, CredentialViewModel addedCredentialViewModel)
 {
     if (CredentialTypes.IsApiKey(addedCredentialViewModel.Type))
     {
         SendApiKeyChangeNotice(
             user,
             addedCredentialViewModel,
             Strings.Emails_ApiKeyAdded_Body,
             Strings.Emails_CredentialAdded_Subject);
     }
     else
     {
         SendCredentialChangeNotice(
             user,
             addedCredentialViewModel,
             Strings.Emails_CredentialAdded_Body,
             Strings.Emails_CredentialAdded_Subject);
     }
 }
 public Task SendCredentialAddedNoticeAsync(User user, CredentialViewModel addedCredentialViewModel)
 {
     if (CredentialTypes.IsApiKey(addedCredentialViewModel.Type))
     {
         return(SendApiKeyChangeNoticeAsync(
                    user,
                    addedCredentialViewModel,
                    Strings.Emails_ApiKeyAdded_Body,
                    Strings.Emails_CredentialAdded_Subject));
     }
     else
     {
         return(SendCredentialChangeNoticeAsync(
                    user,
                    addedCredentialViewModel,
                    Strings.Emails_CredentialAdded_Body,
                    Strings.Emails_CredentialAdded_Subject));
     }
 }
        public ApiKeyViewModel(CredentialViewModel cred)
        {
            if (cred == null)
            {
                throw new ArgumentNullException(nameof(cred));
            }

            var scopes = cred
                         .Scopes
                         .Select(s => s.AllowedAction)
                         .Distinct()
                         .OrderBy(s => s)
                         .ToList();

            var subjects = cred
                           .Scopes
                           .Select(s => s.Subject)
                           .Distinct()
                           .OrderBy(s => s)
                           .ToList();

            var globPattern = subjects
                              .FirstOrDefault(s => s != null && s.Contains("*"));

            var packages = subjects
                           .Except(new[] { globPattern })
                           .ToList();

            Key                 = cred.Key;
            Type                = cred.Type;
            Value               = cred.Value;
            Description         = cred.Description;
            Expires             = cred.Expires?.ToString("O");
            HasExpired          = cred.HasExpired;
            IsNonScopedV1ApiKey = cred.IsNonScopedV1ApiKey;
            Scopes              = scopes;
            Packages            = packages;
            GlobPattern         = globPattern;
        }