Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(Hangfire): add a new field that name is enableTenant to AbpHangfireAuthorizationFilter #10275

Merged
merged 3 commits into from
Oct 24, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/en/Background-Jobs-Hangfire.md
Original file line number Diff line number Diff line change
Expand Up @@ -95,12 +95,21 @@ app.UseHangfireDashboard("/hangfire", new DashboardOptions
});
```

* `AbpHangfireAuthorizationFilter` is an implementation of an authorization filter.

#### AbpHangfireAuthorizationFilter

`AbpHangfireAuthorizationFilter` class has the following fields:

* **`enableTenant` (`bool`, default: `false`):** Enables/disables accessing the Hangfire dashboard on tenant users.
* **`requiredPermissionName` (`string`, default: `null`):** Hangfire dashboard is accessible only if the current user has the specified permission. In this case, if we specify a permission name, we don't need to set `enableTenant` `true` because the permission system already does it.

If you want to require an additional permission, you can pass it into the constructor as below:

```csharp
app.UseHangfireDashboard("/hangfire", new DashboardOptions
{
AsyncAuthorization = new[] { new AbpHangfireAuthorizationFilter("MyHangFireDashboardPermissionName") }
AsyncAuthorization = new[] { new AbpHangfireAuthorizationFilter(requiredPermissionName: "MyHangFireDashboardPermissionName") }
});
```

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,18 @@ namespace Volo.Abp.Hangfire
{
public class AbpHangfireAuthorizationFilter : IDashboardAsyncAuthorizationFilter
{
private readonly bool _enableTenant;
private readonly string _requiredPermissionName;

public AbpHangfireAuthorizationFilter(string requiredPermissionName = null)
public AbpHangfireAuthorizationFilter(bool enableTenant = false, string requiredPermissionName = null)
{
_enableTenant = requiredPermissionName.IsNullOrWhiteSpace() ? enableTenant : true;
_requiredPermissionName = requiredPermissionName;
}

public async Task<bool> AuthorizeAsync(DashboardContext context)
{
if (!IsLoggedIn(context))
if (!IsLoggedIn(context, _enableTenant))
{
return false;
}
Expand All @@ -31,9 +33,15 @@ public async Task<bool> AuthorizeAsync(DashboardContext context)
return await IsPermissionGrantedAsync(context, _requiredPermissionName);
}

private static bool IsLoggedIn(DashboardContext context)
private static bool IsLoggedIn(DashboardContext context, bool enableTenant)
{
var currentUser = context.GetHttpContext().RequestServices.GetRequiredService<ICurrentUser>();

if (!enableTenant)
{
return currentUser.IsAuthenticated && !currentUser.TenantId.HasValue;
}

return currentUser.IsAuthenticated;
}

Expand Down