From 62ce493474fc1893123f3be7924d60c5002d470b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Halil=20=C4=B0brahim=20Kalkan?= Date: Mon, 12 Feb 2018 11:49:56 +0300 Subject: [PATCH] Worked on user permission value provider. --- .../Abp/Permissions/PermissionDefinition.cs | 5 ++ .../Permissions/PermissionValueProvider.cs | 10 +++- .../Session/RolePermissionValueProvider.cs | 47 ----------------- .../Session/UserPermissionValueProvider.cs | 51 ++++++++++++++++--- .../Abp/Settings/ISettingValueProvider.cs | 2 + 5 files changed, 60 insertions(+), 55 deletions(-) delete mode 100644 src/Volo.Abp.Session/Volo/Abp/Session/RolePermissionValueProvider.cs diff --git a/src/Volo.Abp.Permissions/Volo/Abp/Permissions/PermissionDefinition.cs b/src/Volo.Abp.Permissions/Volo/Abp/Permissions/PermissionDefinition.cs index 807269f72e..8c37cf16ec 100644 --- a/src/Volo.Abp.Permissions/Volo/Abp/Permissions/PermissionDefinition.cs +++ b/src/Volo.Abp.Permissions/Volo/Abp/Permissions/PermissionDefinition.cs @@ -27,5 +27,10 @@ Parent = this }; } + + public override string ToString() + { + return $"[Permission {Name}]"; + } } } \ No newline at end of file diff --git a/src/Volo.Abp.Permissions/Volo/Abp/Permissions/PermissionValueProvider.cs b/src/Volo.Abp.Permissions/Volo/Abp/Permissions/PermissionValueProvider.cs index 559e000655..a3db1b69f6 100644 --- a/src/Volo.Abp.Permissions/Volo/Abp/Permissions/PermissionValueProvider.cs +++ b/src/Volo.Abp.Permissions/Volo/Abp/Permissions/PermissionValueProvider.cs @@ -1,4 +1,7 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; +using Microsoft.Extensions.Logging.Abstractions; using Volo.Abp.DependencyInjection; namespace Volo.Abp.Permissions @@ -7,8 +10,13 @@ namespace Volo.Abp.Permissions { public abstract string Name { get; } + public ILoggerFactory LoggerFactory { get; set; } + protected IPermissionStore PermissionStore { get; } + protected ILogger Logger => _lazyLogger.Value; + private Lazy _lazyLogger => new Lazy(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true); + protected PermissionValueProvider(IPermissionStore permissionStore) { PermissionStore = permissionStore; diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/RolePermissionValueProvider.cs b/src/Volo.Abp.Session/Volo/Abp/Session/RolePermissionValueProvider.cs deleted file mode 100644 index ca3a8fd70a..0000000000 --- a/src/Volo.Abp.Session/Volo/Abp/Session/RolePermissionValueProvider.cs +++ /dev/null @@ -1,47 +0,0 @@ -using System.Threading.Tasks; -using Volo.Abp.Permissions; - -namespace Volo.Abp.Session -{ - //TODO: This should be located under Identity, or we should create a Volo.Abp.Roles package (with role store implementation)...! - - //public class RolePermissionValueProvider : PermissionValueProvider - //{ - // public const string ProviderName = "Role"; - - // public override string Name => ProviderName; - - // protected ICurrentUser CurrentUser { get; } - - // public RolePermissionValueProvider(IPermissionStore permissionStore, ICurrentUser currentUser) - // : base(permissionStore) - // { - // CurrentUser = currentUser; - // } - - // public override async Task IsGrantedAsync(PermissionDefinition permission, string providerKey) - // { - // if (providerKey == null) - // { - // if (CurrentUser.Id == null) - // { - // return null; - // } - - // providerKey = CurrentUser.Id.ToString(); - // } - - // return await PermissionStore.IsGrantedAsync(permission.Name, Name, providerKey); - // } - - // public override Task SetAsync(PermissionDefinition permission, bool isGranted, string providerKey) - // { - // return PermissionStore.SetAsync(permission.Name, isGranted, Name, providerKey); - // } - - // public override Task ClearAsync(PermissionDefinition permission, string providerKey) - // { - // return PermissionStore.DeleteAsync(permission.Name, Name, providerKey); - // } - //} -} diff --git a/src/Volo.Abp.Session/Volo/Abp/Session/UserPermissionValueProvider.cs b/src/Volo.Abp.Session/Volo/Abp/Session/UserPermissionValueProvider.cs index 03740781a7..7a89c988c0 100644 --- a/src/Volo.Abp.Session/Volo/Abp/Session/UserPermissionValueProvider.cs +++ b/src/Volo.Abp.Session/Volo/Abp/Session/UserPermissionValueProvider.cs @@ -1,4 +1,6 @@ -using System.Threading.Tasks; +using System; +using System.Threading.Tasks; +using Microsoft.Extensions.Logging; using Volo.Abp.Permissions; namespace Volo.Abp.Session @@ -19,26 +21,61 @@ namespace Volo.Abp.Session public override async Task IsGrantedAsync(PermissionDefinition permission, string providerName, string providerKey) { - if (providerKey == null) + var userId = ParseOrGetCurrentUser(providerName, providerKey); + if (userId == null) { - if (CurrentUser.Id == null) + return null; + } + + return await PermissionStore.IsGrantedAsync(permission.Name, Name, userId.ToString()); + } + + protected virtual Guid? ParseOrGetCurrentUser(string providerName, string providerKey) + { + if (providerName == null) + { + return CurrentUser.Id; + } + + if (providerName == Name) + { + if (providerKey == null) { - return null; + return CurrentUser.Id; } - providerKey = CurrentUser.Id.ToString(); + if (!Guid.TryParse(providerKey, out var result)) + { + throw new AbpException("UserId should be a Guid!"); + } + + return result; } - return await PermissionStore.IsGrantedAsync(permission.Name, Name, providerKey); + return null; } public override Task SetAsync(PermissionDefinition permission, bool isGranted, string providerKey) { - return PermissionStore.SetAsync(permission.Name, isGranted, Name, providerKey); + var userId = ParseOrGetCurrentUser(Name, providerKey); + if (userId == null) + { + Logger.LogWarning($"Could not set the permission '{permission}' because the user id is not available!"); + return Task.CompletedTask; + } + + return PermissionStore.SetAsync(permission.Name, isGranted, Name, userId.ToString()); } public override Task ClearAsync(PermissionDefinition permission, string providerKey) { + var userId = ParseOrGetCurrentUser(Name, providerKey); + if (userId == null) + { + Logger.LogWarning($"Could not clear the permission '{permission}' because the user id is not available!"); + return Task.CompletedTask; + } + return PermissionStore.DeleteAsync(permission.Name, Name, providerKey); } } diff --git a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs index 830546b23d..3063a7cb15 100644 --- a/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs +++ b/src/Volo.Abp.Settings/Volo/Abp/Settings/ISettingValueProvider.cs @@ -7,6 +7,8 @@ namespace Volo.Abp.Settings { string Name { get; } + //TODO: There is a bug here, because we are checking the same providerKey in all providers in a fallback system! + Task GetOrNullAsync([NotNull] SettingDefinition setting, [CanBeNull] string providerKey); Task SetAsync([NotNull] SettingDefinition setting, [NotNull] string value, [CanBeNull] string providerKey);