/Raven.Studio/Models/ApiKeysSectionModel.cs

https://github.com/jalchr/ravendb · C# · 122 lines · 102 code · 20 blank · 0 comment · 4 complexity · d14992df7871d52b038e87330cc07441 MD5 · raw file

  1. using System;
  2. using System.Collections.Generic;
  3. using System.Collections.ObjectModel;
  4. using System.Linq;
  5. using System.Threading.Tasks;
  6. using System.Windows.Input;
  7. using Microsoft.Expression.Interactivity.Core;
  8. using Raven.Abstractions.Data;
  9. using Raven.Abstractions.Util;
  10. using Raven.Client.Changes;
  11. using Raven.Studio.Behaviors;
  12. using Raven.Studio.Infrastructure;
  13. namespace Raven.Studio.Models
  14. {
  15. public class ApiKeysSectionModel : SettingsSectionModel, IAutoCompleteSuggestionProvider
  16. {
  17. public ApiKeysSectionModel()
  18. {
  19. SectionName = "Api Keys";
  20. ApplicationModel.Current.Server.Value.RawUrl = null;
  21. OriginalApiKeys = new ObservableCollection<ApiKeyDefinition>();
  22. ApiKeys = new ObservableCollection<ApiKeyDefinition>();
  23. var session = ApplicationModel.Current.Server.Value.DocumentStore.OpenAsyncSession();
  24. session.Advanced.LoadStartingWithAsync<ApiKeyDefinition>("Raven/ApiKeys/", pageSize:256).ContinueOnSuccessInTheUIThread(
  25. apiKeys =>
  26. {
  27. OriginalApiKeys = new ObservableCollection<ApiKeyDefinition>(apiKeys);
  28. ApiKeys = new ObservableCollection<ApiKeyDefinition>(apiKeys);
  29. OnPropertyChanged(() => ApiKeys);
  30. });
  31. }
  32. public ObservableCollection<ApiKeyDefinition> ApiKeys { get; set; }
  33. public ObservableCollection<ApiKeyDefinition> OriginalApiKeys { get; set; }
  34. public string SearchApiKeys { get; set; }
  35. public ApiKeyDefinition SelectedApiKey { get; set; }
  36. public ICommand AddApiKeyCommand
  37. {
  38. get { return new ActionCommand(() => ApiKeys.Add(new ApiKeyDefinition())); }
  39. }
  40. public ICommand DeleteApiKey
  41. {
  42. get { return new ActionCommand(DeleteApi); }
  43. }
  44. public ICommand GenerateSecretCommand {get{return new ActionCommand(GenerateSecret);}}
  45. public ICommand AddDatabaseAccess
  46. {
  47. get
  48. {
  49. return new ActionCommand(() =>
  50. {
  51. SelectedApiKey.Databases.Add(new DatabaseAccess());
  52. Update();
  53. });
  54. }
  55. }
  56. public ICommand DeleteDatabaseAccess { get { return new ActionCommand(DeleteDatabaseAccessCommand); } }
  57. public ICommand Search { get { return new ActionCommand(SearchApiKeysCommand); } }
  58. private void SearchApiKeysCommand()
  59. {
  60. var session = ApplicationModel.Current.Server.Value.DocumentStore.OpenAsyncSession();
  61. session.Advanced.LoadStartingWithAsync<ApiKeyDefinition>("Raven/ApiKeys/" + SearchApiKeys).ContinueOnSuccessInTheUIThread(
  62. apiKeys =>
  63. {
  64. OriginalApiKeys = new ObservableCollection<ApiKeyDefinition>(apiKeys);
  65. ApiKeys = new ObservableCollection<ApiKeyDefinition>(apiKeys);
  66. OnPropertyChanged(() => ApiKeys);
  67. });
  68. }
  69. private void DeleteDatabaseAccessCommand(object parameter)
  70. {
  71. var access = parameter as DatabaseAccess;
  72. if (access == null)
  73. return;
  74. SelectedApiKey.Databases.Remove(access);
  75. Update();
  76. }
  77. private void DeleteApi(object parameter)
  78. {
  79. var key = parameter as ApiKeyDefinition;
  80. ApiKeys.Remove(key ?? SelectedApiKey);
  81. Update();
  82. }
  83. private void GenerateSecret(object parameter)
  84. {
  85. var key = parameter as ApiKeyDefinition;
  86. if(key == null)
  87. return;
  88. key.Secret = Base62Util.ToBase62(Guid.NewGuid()).Replace("-", "");
  89. }
  90. public Task<IList<object>> ProvideSuggestions(string enteredText)
  91. {
  92. var list = ApplicationModel.Current.Server.Value.Databases.Cast<object>().ToList();
  93. list.Add("*");
  94. return TaskEx.FromResult<IList<object>>(list);
  95. }
  96. public void Update()
  97. {
  98. ApiKeys = new ObservableCollection<ApiKeyDefinition>(ApiKeys);
  99. OnPropertyChanged(() => ApiKeys);
  100. }
  101. }
  102. }