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

Can implement dynamic data filtering? #5650

Closed
hexianggui opened this issue Sep 30, 2020 · 1 comment
Closed

Can implement dynamic data filtering? #5650

hexianggui opened this issue Sep 30, 2020 · 1 comment

Comments

@hexianggui
Copy link

I've defined a filter,
The first user to use the filter works, but subsequent users cannot implement dynamic data filtering.
It looks like the expression is fixed。
Is there any way to refresh an expression?

protected bool IsDataPermissionFilterEnabled => DataFilter?.IsEnabled<IDataPermission>() ?? false;

 protected override Expression<Func<TEntity, bool>> CreateFilterExpression<TEntity>()
  {
            var expression = base.CreateFilterExpression<TEntity>();

            if (typeof(IDataPermission).IsAssignableFrom(typeof(TEntity)))
            {
                var userName = CurrentUser.UserName;
                Expression<Func<TEntity, bool>> dataFilterExp = e => false;
                if (userName != null)
                {
                    var filters = new List<string>();

                    var userFilter = $"\"Target\":\"{CurrentUser.UserName}\",\"TargetType\":\"U\"";
                    filters.Add(userFilter);
                    for (int i = 0; i < CurrentUser.Roles.Length; i++)
                    {
                        filters.Add($"\"Target\":\"{CurrentUser.Roles[i]}\",\"TargetType\":\"R\"");
                    }
                    dataFilterExp =
                       e => !IsDataPermissionFilterEnabled;
                    filters.ForEach(filter =>
                    {
                        Expression<Func<TEntity, bool>> dataExp =
                        e => EF.Property<string>(e, "DataPermissions").IndexOf(filter) > -1;
                        dataFilterExp = PredicateBuilder.Or(dataFilterExp, dataExp);
                    });
                }
                expression = expression == null
                    ? dataFilterExp
                    : CombineExpressions(expression, dataFilterExp);
            }
            return expression;
 }
@maliming
Copy link
Member

This problem has nothing to do with ABP but ef core global filter https://docs.microsoft.com/en-us/ef/core/querying/filters

I think some logic is needed inside the expression, you can continue to debug it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants