diff --git a/src/Greenshot.Addons/Core/ICoreConfiguration.cs b/src/Greenshot.Addons/Core/ICoreConfiguration.cs index 126383072..ec6f3bc06 100644 --- a/src/Greenshot.Addons/Core/ICoreConfiguration.cs +++ b/src/Greenshot.Addons/Core/ICoreConfiguration.cs @@ -358,5 +358,8 @@ namespace Greenshot.Addons.Core [IniPropertyBehavior(Read = false, Write = false)] [DefaultValue(false)] bool IsPortable { get; set; } + + [Description("The permissions for Greenshot functionality")] + ISet Permissions { get; set; } } } \ No newline at end of file diff --git a/src/Greenshot.Addons/IGreenshotLanguage.cs b/src/Greenshot.Addons/IGreenshotLanguage.cs index 0b25c70c7..f8c73ed69 100644 --- a/src/Greenshot.Addons/IGreenshotLanguage.cs +++ b/src/Greenshot.Addons/IGreenshotLanguage.cs @@ -179,5 +179,8 @@ namespace Greenshot.Addons string LatestVersion { get; } [DefaultValue("Current version")] string CurrentVersion { get; } + + [DefaultValue("I know what I am doing! (expert mode)")] + string Expert { get; } } } \ No newline at end of file diff --git a/src/Greenshot/Components/AuthenticationProvider.cs b/src/Greenshot/Components/AuthenticationProvider.cs new file mode 100644 index 000000000..b10cc145d --- /dev/null +++ b/src/Greenshot/Components/AuthenticationProvider.cs @@ -0,0 +1,97 @@ +#region Greenshot GNU General License + +// Greenshot - a free and open source screenshot tool +// Copyright (C) 2007-2018 Thomas Braun, Jens Klingen, Robin Krom +// +// For more information see: http://getgreenshot.org/ +// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General License as published by +// the Free Software Foundation, either version 1 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General License for more details. +// +// You should have received a copy of the GNU General License +// along with this program. If not, see . + +#endregion + +using System; +using System.Collections.Generic; +using System.ComponentModel.Composition; +using System.Linq; +using Caliburn.Micro; +using Dapplo.CaliburnMicro.Security; +using Greenshot.Addons.Core; + +namespace Greenshot.Components +{ + /// + /// This exports a IAuthenticationProvider which manages the rights in the configuration + /// This is used to show or hide elements in the UI depending on the available rights + /// + [Export(typeof(IAuthenticationProvider))] + public class AuthenticationProvider : PropertyChangedBase, IAuthenticationProvider + { + [Import] + private ICoreConfiguration CoreConfiguration { get; set; } + + public bool HasPermissions(IEnumerable neededPermissions, PermissionOperations permissionOperation = PermissionOperations.Or) + { + // Argument check + if (neededPermissions == null) + { + throw new ArgumentNullException(nameof(neededPermissions)); + } + + if (CoreConfiguration.Permissions== null || CoreConfiguration.Permissions.Count == 0) + { + return false; + } + + // Create a clean list of permissions needed + var permissionsToCompare = neededPermissions.Where(s => !string.IsNullOrWhiteSpace(s)).Select(permission => permission.Trim().ToLowerInvariant()).ToList(); + + if (permissionOperation == PermissionOperations.Or) + { + return permissionsToCompare.Any(permission => CoreConfiguration.Permissions.Contains(permission)); + } + return permissionsToCompare.All(permission => CoreConfiguration.Permissions.Contains(permission)); + } + + /// + /// Add a permission and inform via INotifyPropertyChanged events of changes + /// + /// string with the permission + public void AddPermission(string permission) + { + if (string.IsNullOrWhiteSpace(permission)) + { + throw new ArgumentNullException(nameof(permission)); + } + var newPermission = permission.Trim().ToLowerInvariant(); + CoreConfiguration.Permissions.Add(newPermission); + NotifyOfPropertyChange(nameof(HasPermissions)); + } + + /// + /// Remove a permission and inform via INotifyPropertyChanged events of changes + /// + /// string with the permission + public void RemovePermission(string permission) + { + if (string.IsNullOrWhiteSpace(permission)) + { + throw new ArgumentNullException(nameof(permission)); + } + var removingPermission = permission.Trim().ToLowerInvariant(); + CoreConfiguration.Permissions.Remove(removingPermission); + NotifyOfPropertyChange(nameof(HasPermissions)); + } + } +} diff --git a/src/Greenshot/Greenshot.csproj b/src/Greenshot/Greenshot.csproj index d3253f7ee..5ce8ab2bd 100644 --- a/src/Greenshot/Greenshot.csproj +++ b/src/Greenshot/Greenshot.csproj @@ -198,6 +198,7 @@ + @@ -265,6 +266,7 @@ BugReportForm.cs + @@ -492,6 +494,10 @@ MSBuild:Compile Designer + + MSBuild:Compile + Designer + MSBuild:Compile Designer diff --git a/src/Greenshot/Ui/Configuration/ViewModels/ConfigViewModel.cs b/src/Greenshot/Ui/Configuration/ViewModels/ConfigViewModel.cs index fa2bea0f3..5dcc1ed1a 100644 --- a/src/Greenshot/Ui/Configuration/ViewModels/ConfigViewModel.cs +++ b/src/Greenshot/Ui/Configuration/ViewModels/ConfigViewModel.cs @@ -94,13 +94,11 @@ namespace Greenshot.Ui.Configuration.ViewModels { // Prepare disposables _disposables?.Dispose(); - _disposables = new CompositeDisposable(); - // automatically update the DisplayName - var greenshotLanguageBinding = GreenshotLanguage.CreateDisplayNameBinding(this, nameof(IGreenshotLanguage.SettingsTitle)); - - // Make sure the greenshotLanguageBinding is disposed when this is no longer active - _disposables.Add(greenshotLanguageBinding); + _disposables = new CompositeDisposable + { + GreenshotLanguage.CreateDisplayNameBinding(this, nameof(IGreenshotLanguage.SettingsTitle)) + }; base.OnActivate(); } diff --git a/src/Greenshot/Ui/Configuration/ViewModels/DestinationPickerConfigViewModel.cs b/src/Greenshot/Ui/Configuration/ViewModels/DestinationPickerConfigViewModel.cs index f825b3d40..01f5bbf85 100644 --- a/src/Greenshot/Ui/Configuration/ViewModels/DestinationPickerConfigViewModel.cs +++ b/src/Greenshot/Ui/Configuration/ViewModels/DestinationPickerConfigViewModel.cs @@ -27,14 +27,11 @@ using System.Collections.ObjectModel; using System.ComponentModel.Composition; using System.Linq; using System.Reactive.Disposables; -using System.Windows; using Dapplo.CaliburnMicro.Configuration; using Dapplo.CaliburnMicro.Extensions; -using GongSolutions.Wpf.DragDrop; using Greenshot.Addons; using Greenshot.Addons.Addons; using Greenshot.Addons.Core; -using Greenshot.Addons.Extensions; using Greenshot.Configuration; namespace Greenshot.Ui.Configuration.ViewModels @@ -63,7 +60,6 @@ namespace Greenshot.Ui.Configuration.ViewModels { // Prepare disposables _disposables?.Dispose(); - _disposables = new CompositeDisposable(); // Place this under the Ui parent ParentId = nameof(ConfigIds.Destinations); @@ -72,10 +68,10 @@ namespace Greenshot.Ui.Configuration.ViewModels config.Register(CoreConfiguration); // automatically update the DisplayName - var greenshotLanguageBinding = GreenshotLanguage.CreateDisplayNameBinding(this, nameof(IGreenshotLanguage.SettingsDestinationPicker)); - - // Make sure the greenshotLanguageBinding is disposed when this is no longer active - _disposables.Add(greenshotLanguageBinding); + _disposables = new CompositeDisposable + { + GreenshotLanguage.CreateDisplayNameBinding(this, nameof(IGreenshotLanguage.SettingsDestinationPicker)) + }; UsedDestinations.Clear(); if (CoreConfiguration.PickerDestinations.Any()) diff --git a/src/Greenshot/Ui/Configuration/ViewModels/GeneralConfigViewModel.cs b/src/Greenshot/Ui/Configuration/ViewModels/GeneralConfigViewModel.cs index 1fc7b8152..a27e9e230 100644 --- a/src/Greenshot/Ui/Configuration/ViewModels/GeneralConfigViewModel.cs +++ b/src/Greenshot/Ui/Configuration/ViewModels/GeneralConfigViewModel.cs @@ -25,9 +25,10 @@ using System.ComponentModel.Composition; using System.Reactive.Disposables; using Dapplo.CaliburnMicro.Configuration; using Dapplo.CaliburnMicro.Extensions; +using Dapplo.CaliburnMicro.Security; using Greenshot.Addons; using Greenshot.Addons.Core; -using Greenshot.Configuration; +using Greenshot.Components; namespace Greenshot.Ui.Configuration.ViewModels { @@ -42,12 +43,12 @@ namespace Greenshot.Ui.Configuration.ViewModels [Import] public ICoreConfiguration CoreConfiguration { get; set; } - [Import] - public IConfigTranslations ConfigTranslations { get; set; } - [Import] public IGreenshotLanguage GreenshotLanguage { get; set; } + [Import(typeof(IAuthenticationProvider))] + public AuthenticationProvider AuthenticationProvider { get; set; } + public override void Initialize(IConfig config) { // Prepare disposables @@ -70,5 +71,30 @@ namespace Greenshot.Ui.Configuration.ViewModels _disposables.Dispose(); base.OnDeactivate(close); } + + /// + /// Change the expert mode + /// + public bool Expert + { + get => AuthenticationProvider?.HasPermissions(new[] { "Expert" }) == true; + set + { + if (AuthenticationProvider == null) + { + return; + } + if (value) + { + AuthenticationProvider.AddPermission("Expert"); + } + else + { + AuthenticationProvider.RemovePermission("Expert"); + } + NotifyOfPropertyChange(); + } + + } } } diff --git a/src/Greenshot/Ui/Configuration/ViewModels/NetworkConfigViewModel.cs b/src/Greenshot/Ui/Configuration/ViewModels/NetworkConfigViewModel.cs index ca19f72f6..cfa38ee6e 100644 --- a/src/Greenshot/Ui/Configuration/ViewModels/NetworkConfigViewModel.cs +++ b/src/Greenshot/Ui/Configuration/ViewModels/NetworkConfigViewModel.cs @@ -23,15 +23,17 @@ using System.ComponentModel.Composition; using System.Reactive.Disposables; +using System.Windows; using Dapplo.CaliburnMicro.Configuration; using Dapplo.CaliburnMicro.Extensions; +using Dapplo.CaliburnMicro.Security; using Greenshot.Addons; using Greenshot.Addons.Core; namespace Greenshot.Ui.Configuration.ViewModels { [Export(typeof(IConfigScreen))] - public sealed class NetworkConfigViewModel : SimpleConfigScreen + public sealed class NetworkConfigViewModel : AuthenticatedConfigNode { /// /// Here all disposables are registered, so we can clean the up @@ -52,6 +54,8 @@ namespace Greenshot.Ui.Configuration.ViewModels // Make sure Commit/Rollback is called on the IUiConfiguration config.Register(NetworkConfiguration); + this.VisibleOnPermissions("Expert"); + // automatically update the DisplayName _disposables = new CompositeDisposable { diff --git a/src/Greenshot/Ui/Configuration/ViewModels/OutputConfigViewModel.cs b/src/Greenshot/Ui/Configuration/ViewModels/OutputConfigViewModel.cs index 4308a54ad..92bc6aaa6 100644 --- a/src/Greenshot/Ui/Configuration/ViewModels/OutputConfigViewModel.cs +++ b/src/Greenshot/Ui/Configuration/ViewModels/OutputConfigViewModel.cs @@ -28,7 +28,6 @@ using Dapplo.CaliburnMicro.Extensions; using Greenshot.Addons; using Greenshot.Addons.Core; using Greenshot.Addons.ViewModels; -using Greenshot.Configuration; namespace Greenshot.Ui.Configuration.ViewModels { @@ -43,9 +42,6 @@ namespace Greenshot.Ui.Configuration.ViewModels [Import] public ICoreConfiguration CoreConfiguration { get; set; } - [Import] - public IConfigTranslations ConfigTranslations { get; set; } - [Import] public IGreenshotLanguage GreenshotLanguage { get; set; } diff --git a/src/Greenshot/Ui/Configuration/ViewModels/PrintConfigViewModel.cs b/src/Greenshot/Ui/Configuration/ViewModels/PrintConfigViewModel.cs new file mode 100644 index 000000000..a43cb96de --- /dev/null +++ b/src/Greenshot/Ui/Configuration/ViewModels/PrintConfigViewModel.cs @@ -0,0 +1,70 @@ +#region Greenshot GNU General Public License + +// Greenshot - a free and open source screenshot tool +// Copyright (C) 2007-2018 Thomas Braun, Jens Klingen, Robin Krom +// +// For more information see: http://getgreenshot.org/ +// The Greenshot project is hosted on GitHub https://github.com/greenshot/greenshot +// +// This program is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by +// the Free Software Foundation, either version 1 of the License, or +// (at your option) any later version. +// +// This program is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License +// along with this program. If not, see . + +#endregion + +using System.ComponentModel.Composition; +using System.Reactive.Disposables; +using Dapplo.CaliburnMicro.Configuration; +using Dapplo.CaliburnMicro.Extensions; +using Greenshot.Addons; +using Greenshot.Addons.Core; + +namespace Greenshot.Ui.Configuration.ViewModels +{ + [Export(typeof(IConfigScreen))] + public sealed class PrintConfigViewModel : SimpleConfigScreen + { + /// + /// Here all disposables are registered, so we can clean the up + /// + private CompositeDisposable _disposables; + + [Import] + public ICoreConfiguration CoreConfiguration { get; set; } + + [Import] + public IGreenshotLanguage GreenshotLanguage { get; set; } + + public override void Initialize(IConfig config) + { + // Prepare disposables + _disposables?.Dispose(); + + // Make sure Commit/Rollback is called on the IUiConfiguration + config.Register(CoreConfiguration); + + // automatically update the DisplayName + _disposables = new CompositeDisposable + { + GreenshotLanguage.CreateDisplayNameBinding(this, nameof(IGreenshotLanguage.SettingsPrinter)) + }; + + base.Initialize(config); + } + + protected override void OnDeactivate(bool close) + { + _disposables.Dispose(); + base.OnDeactivate(close); + } + } +} diff --git a/src/Greenshot/Ui/Configuration/Views/GeneralConfigView.xaml b/src/Greenshot/Ui/Configuration/Views/GeneralConfigView.xaml index 6f1470e04..821364f7e 100644 --- a/src/Greenshot/Ui/Configuration/Views/GeneralConfigView.xaml +++ b/src/Greenshot/Ui/Configuration/Views/GeneralConfigView.xaml @@ -8,7 +8,8 @@ d:DataContext="{d:DesignInstance viewModels:GeneralConfigViewModel,IsDesignTimeCreatable=False}" > - + +