Remove IsGranted from PermissionGrant

pull/208/head
Halil İbrahim Kalkan 8 years ago
parent 62ce493474
commit b42ca414d3

@ -9,8 +9,6 @@ namespace Volo.Abp.Permissions
[NotNull] [NotNull]
public virtual string Name { get; protected set; } public virtual string Name { get; protected set; }
public virtual bool IsGranted { get; internal set; }
[CanBeNull] [CanBeNull]
public virtual string ProviderName { get; protected set; } public virtual string ProviderName { get; protected set; }
@ -25,7 +23,6 @@ namespace Volo.Abp.Permissions
public PermissionGrant( public PermissionGrant(
Guid id, Guid id,
[NotNull] string name, [NotNull] string name,
bool isGranted,
[CanBeNull] string providerName = null, [CanBeNull] string providerName = null,
[CanBeNull] string providerKey = null) [CanBeNull] string providerKey = null)
{ {
@ -33,7 +30,6 @@ namespace Volo.Abp.Permissions
Id = id; Id = id;
Name = name; Name = name;
IsGranted = isGranted;
ProviderName = providerName; ProviderName = providerName;
ProviderKey = providerKey; ProviderKey = providerKey;
} }

@ -14,34 +14,31 @@ namespace Volo.Abp.Permissions
_permissionGrantRepository = permissionGrantRepository; _permissionGrantRepository = permissionGrantRepository;
} }
public async Task<bool?> IsGrantedAsync(string name, string providerName, string providerKey) public async Task<bool> IsGrantedAsync(string name, string providerName, string providerKey)
{ {
var permissionGrant = await _permissionGrantRepository.FindAsync(name, providerName, providerKey); return await _permissionGrantRepository.FindAsync(name, providerName, providerKey) != null;
return permissionGrant?.IsGranted;
} }
public async Task SetAsync(string name, bool isGranted, string providerName, string providerKey) public async Task AddAsync(string name, string providerName, string providerKey)
{ {
var permissionGrant = await _permissionGrantRepository.FindAsync(name, providerName, providerKey); var permissionGrant = await _permissionGrantRepository.FindAsync(name, providerName, providerKey);
if (permissionGrant == null) if (permissionGrant != null)
{
permissionGrant = new PermissionGrant(GuidGenerator.Create(), name, isGranted, providerName, providerKey);
await _permissionGrantRepository.InsertAsync(permissionGrant);
}
else
{ {
permissionGrant.IsGranted = isGranted; return;
await _permissionGrantRepository.UpdateAsync(permissionGrant);
} }
await _permissionGrantRepository.InsertAsync(
new PermissionGrant(GuidGenerator.Create(), name, providerName, providerKey)
);
} }
public async Task<List<PermissionGrantInfo>> GetListAsync(string providerName, string providerKey) public async Task<List<string>> GetAllGrantedAsync(string providerName, string providerKey)
{ {
var permissionGrants = await _permissionGrantRepository.GetListAsync(providerName, providerKey); var permissionGrants = await _permissionGrantRepository.GetListAsync(providerName, providerKey);
return permissionGrants.Select(s => new PermissionGrantInfo(s.Name, s.IsGranted)).ToList(); return permissionGrants.Select(s => s.Name).ToList();
} }
public async Task DeleteAsync(string name, string providerName, string providerKey) public async Task RemoveAsync(string name, string providerName, string providerKey)
{ {
var permissionGrant = await _permissionGrantRepository.FindAsync(name, providerName, providerKey); var permissionGrant = await _permissionGrantRepository.FindAsync(name, providerName, providerKey);
if (permissionGrant != null) if (permissionGrant != null)

@ -22,9 +22,8 @@ namespace Volo.Abp.Permissions.EntityFrameworkCore
b.ToTable(tablePrefix + "Permissions", schema); b.ToTable(tablePrefix + "Permissions", schema);
b.Property(x => x.Name).HasMaxLength(PermissionGrantConsts.MaxNameLength).IsRequired(); b.Property(x => x.Name).HasMaxLength(PermissionGrantConsts.MaxNameLength).IsRequired();
b.Property(x => x.IsGranted).IsRequired().HasDefaultValue(true); b.Property(x => x.ProviderName).HasMaxLength(PermissionGrantConsts.MaxProviderNameLength).IsRequired();
b.Property(x => x.ProviderName).HasMaxLength(PermissionGrantConsts.MaxProviderNameLength); b.Property(x => x.ProviderKey).HasMaxLength(PermissionGrantConsts.MaxProviderKeyLength).IsRequired();
b.Property(x => x.ProviderKey).HasMaxLength(PermissionGrantConsts.MaxProviderKeyLength);
b.HasIndex(x => new {x.Name, x.ProviderName, x.ProviderKey}); b.HasIndex(x => new {x.Name, x.ProviderName, x.ProviderKey});
}); });

@ -16,6 +16,6 @@ namespace Volo.Abp.Permissions
Task<List<PermissionGrantInfo>> GetAllAsync([NotNull] string providerName, [CanBeNull] string providerKey, bool fallback = true); Task<List<PermissionGrantInfo>> GetAllAsync([NotNull] string providerName, [CanBeNull] string providerKey, bool fallback = true);
Task SetAsync([NotNull] string name, bool? isGranted, [NotNull] string providerName, [CanBeNull] string providerKey, bool forceToSet = false); Task SetAsync([NotNull] string name, bool isGranted, [NotNull] string providerName, [CanBeNull] string providerKey, bool forceToSet = false);
} }
} }

@ -6,12 +6,12 @@ namespace Volo.Abp.Permissions
{ {
public interface IPermissionStore public interface IPermissionStore
{ {
Task<bool?> IsGrantedAsync([NotNull] string name, [CanBeNull] string providerName, [CanBeNull] string providerKey); Task<bool> IsGrantedAsync([NotNull] string name, [CanBeNull] string providerName, [CanBeNull] string providerKey);
Task SetAsync([NotNull] string name, bool isGranted, [CanBeNull] string providerName, [CanBeNull] string providerKey); Task<List<string>> GetAllGrantedAsync([CanBeNull] string providerName, [CanBeNull] string providerKey);
Task<List<PermissionGrantInfo>> GetListAsync([CanBeNull] string providerName, [CanBeNull] string providerKey); Task AddAsync([NotNull] string name, [CanBeNull] string providerName, [CanBeNull] string providerKey);
Task DeleteAsync([NotNull] string name, [CanBeNull]string providerName, [CanBeNull]string providerKey); Task RemoveAsync([NotNull] string name, [CanBeNull]string providerName, [CanBeNull]string providerKey);
} }
} }

@ -15,23 +15,23 @@ namespace Volo.Abp.Permissions
Logger = NullLogger<NullPermissionStore>.Instance; Logger = NullLogger<NullPermissionStore>.Instance;
} }
public Task<bool?> IsGrantedAsync(string name, string providerName, string providerKey) public Task<bool> IsGrantedAsync(string name, string providerName, string providerKey)
{ {
return Task.FromResult((bool?)null); return Task.FromResult(false);
} }
public Task SetAsync(string name, bool isGranted, string providerName, string providerKey) public Task AddAsync(string name, string providerName, string providerKey)
{ {
Logger.LogWarning($"Setting the grant value for {name} is not possible because current permission store is {nameof(NullPermissionStore)}"); Logger.LogWarning($"Setting the grant value for {name} is not possible because current permission store is {nameof(NullPermissionStore)}");
return Task.CompletedTask; return Task.CompletedTask;
} }
public Task<List<PermissionGrantInfo>> GetListAsync(string providerName, string providerKey) public Task<List<string>> GetAllGrantedAsync(string providerName, string providerKey)
{ {
return Task.FromResult(new List<PermissionGrantInfo>()); return Task.FromResult(new List<string>());
} }
public Task DeleteAsync(string name, string providerName, string providerKey) public Task RemoveAsync(string name, string providerName, string providerKey)
{ {
return Task.CompletedTask; return Task.CompletedTask;
} }

@ -103,7 +103,7 @@ namespace Volo.Abp.Permissions
return await GetAllFromProvidersAsync(providerList, providerKey); return await GetAllFromProvidersAsync(providerList, providerKey);
} }
public virtual async Task SetAsync(string name, bool? isGranted, string providerName, string providerKey, bool forceToSet = false) public virtual async Task SetAsync(string name, bool isGranted, string providerName, string providerKey, bool forceToSet = false)
{ {
Check.NotNull(name, nameof(name)); Check.NotNull(name, nameof(name));
Check.NotNull(providerName, nameof(providerName)); Check.NotNull(providerName, nameof(providerName));
@ -126,7 +126,7 @@ namespace Volo.Abp.Permissions
var fallbackValue = await IsGrantedInternalAsync(name, providers[1].Name, providerKey); var fallbackValue = await IsGrantedInternalAsync(name, providers[1].Name, providerKey);
if (fallbackValue == isGranted) if (fallbackValue == isGranted)
{ {
isGranted = null; return;
} }
} }
@ -134,19 +134,9 @@ namespace Volo.Abp.Permissions
.TakeWhile(p => p.Name == providerName) .TakeWhile(p => p.Name == providerName)
.ToList(); //Getting list for case of there are more than one provider with same name .ToList(); //Getting list for case of there are more than one provider with same name
if (isGranted == null) foreach (var provider in providers)
{
foreach (var provider in providers)
{
await provider.ClearAsync(permission, providerKey);
}
}
else
{ {
foreach (var provider in providers) await provider.SetAsync(permission, isGranted, providerKey);
{
await provider.SetAsync(permission, isGranted.Value, providerKey);
}
} }
} }

@ -28,12 +28,12 @@ namespace Volo.Abp.Permissions
return permissionManager.GetAllAsync(UserPermissionValueProvider.ProviderName, null, fallback); return permissionManager.GetAllAsync(UserPermissionValueProvider.ProviderName, null, fallback);
} }
public static Task SetForUserAsync(this IPermissionManager permissionManager, Guid userId, [NotNull] string name, bool? isGranted, bool forceToSet = false) public static Task SetForUserAsync(this IPermissionManager permissionManager, Guid userId, [NotNull] string name, bool isGranted, bool forceToSet = false)
{ {
return permissionManager.SetAsync(name, isGranted, UserPermissionValueProvider.ProviderName, userId.ToString(), forceToSet); return permissionManager.SetAsync(name, isGranted, UserPermissionValueProvider.ProviderName, userId.ToString(), forceToSet);
} }
public static Task SetForCurrentUserAsync(this IPermissionManager permissionManager, [NotNull] string name, bool? isGranted, bool forceToSet = false) public static Task SetForCurrentUserAsync(this IPermissionManager permissionManager, [NotNull] string name, bool isGranted, bool forceToSet = false)
{ {
return permissionManager.SetAsync(name, isGranted, UserPermissionValueProvider.ProviderName, null, forceToSet); return permissionManager.SetAsync(name, isGranted, UserPermissionValueProvider.ProviderName, null, forceToSet);
} }

@ -64,7 +64,16 @@ namespace Volo.Abp.Session
return Task.CompletedTask; return Task.CompletedTask;
} }
return PermissionStore.SetAsync(permission.Name, isGranted, Name, userId.ToString()); //TODO: Seperate SetAsync to AddGrant / RemoveGrant
if (isGranted)
{
return PermissionStore.AddAsync(permission.Name, Name, userId.ToString());
}
else
{
return PermissionStore.RemoveAsync(permission.Name, Name, userId.ToString());
}
} }
public override Task ClearAsync(PermissionDefinition permission, string providerKey) public override Task ClearAsync(PermissionDefinition permission, string providerKey)
@ -76,7 +85,7 @@ namespace Volo.Abp.Session
return Task.CompletedTask; return Task.CompletedTask;
} }
return PermissionStore.DeleteAsync(permission.Name, Name, providerKey); return PermissionStore.RemoveAsync(permission.Name, Name, providerKey);
} }
} }
} }

@ -25,7 +25,6 @@ namespace Volo.Abp.Permissions
new PermissionGrant( new PermissionGrant(
_guidGenerator.Create(), _guidGenerator.Create(),
"MyPermission1", "MyPermission1",
true,
UserPermissionValueProvider.ProviderName, UserPermissionValueProvider.ProviderName,
User1Id.ToString() User1Id.ToString()
) )

Loading…
Cancel
Save