Worked on user permission value provider.

pull/208/head
Halil İbrahim Kalkan 7 years ago
parent ed28669f6d
commit 62ce493474

@ -27,5 +27,10 @@
Parent = this
};
}
public override string ToString()
{
return $"[Permission {Name}]";
}
}
}

@ -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<ILogger> _lazyLogger => new Lazy<ILogger>(() => LoggerFactory?.CreateLogger(GetType().FullName) ?? NullLogger.Instance, true);
protected PermissionValueProvider(IPermissionStore permissionStore)
{
PermissionStore = permissionStore;

@ -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<bool?> 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);
// }
//}
}

@ -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<bool?> 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);
}
}

@ -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<string> GetOrNullAsync([NotNull] SettingDefinition setting, [CanBeNull] string providerKey);
Task SetAsync([NotNull] SettingDefinition setting, [NotNull] string value, [CanBeNull] string providerKey);

Loading…
Cancel
Save