Skip to content

Commit

Permalink
Resolved #101: Cache user & role permissions.
Browse files Browse the repository at this point in the history
  • Loading branch information
hikalkan committed Sep 4, 2015
1 parent 5939cd0 commit 81c4d90
Show file tree
Hide file tree
Showing 25 changed files with 561 additions and 314 deletions.
8 changes: 4 additions & 4 deletions src/Abp.Zero.EntityFramework/Abp.Zero.EntityFramework.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,12 @@
<DocumentationFile>bin\Release\Abp.Zero.EntityFramework.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Abp, Version=0.6.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.0.6.6.0\lib\net451\Abp.dll</HintPath>
<Reference Include="Abp, Version=0.6.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.0.6.9.0\lib\net451\Abp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Abp.EntityFramework, Version=0.6.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.EntityFramework.0.6.6.0\lib\net451\Abp.EntityFramework.dll</HintPath>
<Reference Include="Abp.EntityFramework, Version=0.6.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.EntityFramework.0.6.9.0\lib\net451\Abp.EntityFramework.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
Expand Down
4 changes: 2 additions & 2 deletions src/Abp.Zero.EntityFramework/packages.config
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Abp" version="0.6.6.0" targetFramework="net451" />
<package id="Abp.EntityFramework" version="0.6.6.0" targetFramework="net451" />
<package id="Abp" version="0.6.9.0" targetFramework="net451" />
<package id="Abp.EntityFramework" version="0.6.9.0" targetFramework="net451" />
<package id="Castle.Core" version="3.3.3" targetFramework="net451" />
<package id="Castle.LoggingFacility" version="3.3.0" targetFramework="net451" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net451" />
Expand Down
4 changes: 2 additions & 2 deletions src/Abp.Zero.Ldap/Abp.Zero.Ldap.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@
<WarningLevel>4</WarningLevel>
</PropertyGroup>
<ItemGroup>
<Reference Include="Abp, Version=0.6.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.0.6.6.0\lib\net451\Abp.dll</HintPath>
<Reference Include="Abp, Version=0.6.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.0.6.9.0\lib\net451\Abp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
Expand Down
2 changes: 1 addition & 1 deletion src/Abp.Zero.Ldap/packages.config
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Abp" version="0.6.6.0" targetFramework="net451" />
<package id="Abp" version="0.6.9.0" targetFramework="net451" />
<package id="Castle.Core" version="3.3.3" targetFramework="net451" />
<package id="Castle.LoggingFacility" version="3.3.0" targetFramework="net451" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net451" />
Expand Down
12 changes: 6 additions & 6 deletions src/Abp.Zero.NHibernate/Abp.Zero.NHibernate.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,16 +34,16 @@
<DocumentationFile>bin\Release\Abp.Zero.NHibernate.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Abp, Version=0.6.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.0.6.6.0\lib\net451\Abp.dll</HintPath>
<Reference Include="Abp, Version=0.6.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.0.6.9.0\lib\net451\Abp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Abp.FluentMigrator, Version=0.6.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.FluentMigrator.0.6.6.0\lib\net451\Abp.FluentMigrator.dll</HintPath>
<Reference Include="Abp.FluentMigrator, Version=0.6.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.FluentMigrator.0.6.9.0\lib\net451\Abp.FluentMigrator.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Abp.NHibernate, Version=0.6.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.NHibernate.0.6.6.0\lib\net451\Abp.NHibernate.dll</HintPath>
<Reference Include="Abp.NHibernate, Version=0.6.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.NHibernate.0.6.9.0\lib\net451\Abp.NHibernate.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
Expand Down
6 changes: 3 additions & 3 deletions src/Abp.Zero.NHibernate/packages.config
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
<?xml version="1.0" encoding="utf-8"?>
<packages>
<package id="Abp" version="0.6.6.0" targetFramework="net451" />
<package id="Abp.FluentMigrator" version="0.6.6.0" targetFramework="net451" />
<package id="Abp.NHibernate" version="0.6.6.0" targetFramework="net451" />
<package id="Abp" version="0.6.9.0" targetFramework="net451" />
<package id="Abp.FluentMigrator" version="0.6.9.0" targetFramework="net451" />
<package id="Abp.NHibernate" version="0.6.9.0" targetFramework="net451" />
<package id="Castle.Core" version="3.3.3" targetFramework="net451" />
<package id="Castle.LoggingFacility" version="3.3.0" targetFramework="net451" />
<package id="Castle.Windsor" version="3.3.0" targetFramework="net451" />
Expand Down
7 changes: 5 additions & 2 deletions src/Abp.Zero/Abp.Zero.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@
<DocumentationFile>bin\Release\Abp.Zero.XML</DocumentationFile>
</PropertyGroup>
<ItemGroup>
<Reference Include="Abp, Version=0.6.6.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.0.6.6.0\lib\net451\Abp.dll</HintPath>
<Reference Include="Abp, Version=0.6.9.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Abp.0.6.9.0\lib\net451\Abp.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Castle.Core, Version=3.3.0.0, Culture=neutral, PublicKeyToken=407dd0808d44fbdc, processorArchitecture=MSIL">
Expand Down Expand Up @@ -91,6 +91,8 @@
<Compile Include="Authorization\Roles\IRolePermissionStore.cs" />
<Compile Include="Authorization\Users\AbpLoginResultType.cs" />
<Compile Include="Authorization\Users\AbpUserManagerExtensions.cs" />
<Compile Include="Authorization\Roles\RolePermissionCacheItem.cs" />
<Compile Include="Authorization\Users\UserPermissionCacheItem.cs" />
<Compile Include="Authorization\Users\UserPermissionSetting.cs" />
<Compile Include="Configuration\Setting.cs" />
<Compile Include="Configuration\SettingExtensions.cs" />
Expand All @@ -101,6 +103,7 @@
<Compile Include="MultiTenancy\IMayHaveTenant.cs" />
<Compile Include="MultiTenancy\IMustHaveTenant.cs" />
<Compile Include="Authorization\Roles\AbpRoleManager.cs" />
<Compile Include="Runtime\Caching\AbpZeroCacheProviderExtensions.cs" />
<Compile Include="Runtime\Session\AbpSession.cs" />
<Compile Include="Configuration\SettingStore.cs" />
<Compile Include="Properties\AssemblyInfo.cs" />
Expand Down
54 changes: 42 additions & 12 deletions src/Abp.Zero/Authorization/Roles/AbpRoleManager.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using Abp.Authorization.Users;
using Abp.Dependency;
using Abp.Domain.Uow;
using Abp.IdentityFramework;
using Abp.Localization;
using Abp.MultiTenancy;
using Abp.Runtime.Caching;
using Abp.Runtime.Session;
using Abp.Zero;
using Abp.Zero.Configuration;
Expand All @@ -26,7 +27,7 @@ public abstract class AbpRoleManager<TTenant, TRole, TUser> : RoleManager<TRole,
public ILocalizationManager LocalizationManager { get; set; }

public IAbpSession AbpSession { get; set; }

public IRoleManagementConfig RoleManagementConfig { get; private set; }

private IRolePermissionStore<TTenant, TRole, TUser> RolePermissionStore
Expand All @@ -45,22 +46,25 @@ private IRolePermissionStore<TTenant, TRole, TUser> RolePermissionStore
protected AbpRoleStore<TTenant, TRole, TUser> AbpStore { get; private set; }

private readonly IPermissionManager _permissionManager;
private readonly IUnitOfWorkManager _unitOfWorkManager;
private readonly ITypedCache<int, RolePermissionCacheItem> _rolePermissionCache;

/// <summary>
/// Constructor.
/// </summary>
protected AbpRoleManager(
AbpRoleStore<TTenant, TRole, TUser> store,
IPermissionManager permissionManager,
AbpRoleStore<TTenant, TRole, TUser> store,
IPermissionManager permissionManager,
IRoleManagementConfig roleManagementConfig,
IUnitOfWorkManager unitOfWorkManager)
ICacheManager cacheManager)
: base(store)
{
RoleManagementConfig = roleManagementConfig;
_permissionManager = permissionManager;
_unitOfWorkManager = unitOfWorkManager;
RoleManagementConfig = roleManagementConfig;

_rolePermissionCache = cacheManager.GetRolePermissionCache();

AbpStore = store;

AbpSession = NullAbpSession.Instance;
LocalizationManager = NullLocalizationManager.Instance;
}
Expand Down Expand Up @@ -106,9 +110,13 @@ public Task<bool> HasPermissionAsync(TRole role, Permission permission)
/// <returns>True, if the role has the permission</returns>
public async Task<bool> HasPermissionAsync(int roleId, Permission permission)
{
//Get cached role permissions
var cacheItem = await GetRolePermissionCacheItemAsync(roleId);

//Check the permission
return permission.IsGrantedByDefault
? !(await RolePermissionStore.HasPermissionAsync(roleId, new PermissionGrantInfo(permission.Name, false)))
: (await RolePermissionStore.HasPermissionAsync(roleId, new PermissionGrantInfo(permission.Name, true)));
? !(cacheItem.ProhibitedPermissions.Contains(permission.Name))
: (cacheItem.GrantedPermissions.Contains(permission.Name));
}

/// <summary>
Expand Down Expand Up @@ -267,7 +275,7 @@ public override async Task<IdentityResult> CreateAsync(TRole role)
{
role.TenantId = AbpSession.TenantId.Value;
}

return await base.CreateAsync(role);
}

Expand Down Expand Up @@ -337,7 +345,7 @@ public async Task GrantAllPermissionsAsync(TRole role)
var permissions = _permissionManager.GetAllPermissions(role.GetMultiTenancySide());
await SetGrantedPermissionsAsync(role, permissions);
}

public virtual async Task<IdentityResult> CreateStaticRoles(int tenantId)
{
var staticRoleDefinitions = RoleManagementConfig.StaticRoles.Where(sr => sr.Side == MultiTenancySides.Tenant);
Expand Down Expand Up @@ -384,6 +392,28 @@ private Task<TRole> FindByDisplayNameAsync(string displayName)
return AbpStore.FindByDisplayNameAsync(displayName);
}

private async Task<RolePermissionCacheItem> GetRolePermissionCacheItemAsync(int roleId)
{
return await _rolePermissionCache.GetAsync(roleId, async () =>
{
var newCacheItem = new RolePermissionCacheItem(roleId);
foreach (var permissionInfo in await RolePermissionStore.GetPermissionsAsync(roleId))
{
if (permissionInfo.IsGranted)
{
newCacheItem.GrantedPermissions.Add(permissionInfo.Name);
}
else
{
newCacheItem.ProhibitedPermissions.Add(permissionInfo.Name);
}
}
return newCacheItem;
});
}

private string L(string name)
{
return LocalizationManager.GetString(AbpZeroConsts.LocalizationSourceName, name);
Expand Down
Loading

0 comments on commit 81c4d90

Please sign in to comment.