Base solution for your next web application
Ends in:
01 DAYS
01 HRS
01 MIN
01 SEC
Open Closed

Using SetTenantId() & SaveChangesAsync() #2861


User avatar
0
bilalhaidar created

Hello, I am going through the framework code and notice many places you use a combination of setting a Tenant Id and SaveChanges.

Shall I use this combination whenever I am saving records that belong to a specific Tenant?

Also, I know that SaveChanges() is usually used to get the Id of the newly added record, in the case of setting a Tenant, should I always follow it by Savechanges?

Finally, if an Entity has a TenantId, do I need to feed in the value or the framework would set this property to the current TenantID?

Thanks


16 Answer(s)
  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    In most cases you don't need to do it because ABP handles setting tenantId. If you want to operate on another tenant than current tenant, you need to swithc to target tenant's context by setting tenantId.

  • User Avatar
    0
    bilalhaidar created

    If I have some objects that don't have a TenantId as I need them in all Tenants. When retrieving them, shall I set the SetTenant(null)? Or irrespective of Tenant I will get all the data?

    One more thing, is there a documentation on how Abp applies data filters, so that I fully understand how things work behind the scenes when it comes to Tenant Filter, etc.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    SetTenantId(null) will make a connection to host db, so you will get all data right. We only have this document <a class="postlink" href="https://aspnetboilerplate.com/Pages/Documents/Data-Filters">https://aspnetboilerplate.com/Pages/Doc ... ta-Filters</a>.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    And if I'm using same DB for all tenants and host and entities without tenant property, still I need to set tenant?

  • User Avatar
    0
    bilalhaidar created

    Well, There is something wrong going on here.

    I have Host-Level entities that is data shared across all tenants.

    The following is defined for permissions on one of those entities:

    [AbpAuthorize(SmsPermissions.Pages_HOSTREFDATA_ENTITY, SmsPermissions.Pages_REFDATA_GLOBAL, RequireAllPermissions = false)]
    

    The Host Admin is given Pages_HOSTREFDATA_ENTITY (This permission defined on Host level). The Tenant Admin is given Pages_REFDATA_GLOBAL (This permission is defined on Tenant level).

    Now, when I log in with Tenant Admin and using SetTenantId(null), I get an exception saying at least one of the above 2 permissions are required.

    How come?

  • User Avatar
    0
    bilalhaidar created

    Any idea please?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    Can you share your full code for this ? Especially the section wrapped in SetTenantId(null) and the method contains SetTenantId(null).

    There might be a bug in ABP, we will check it according to your response.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Hello, I will email you the code of that is causing the exception. Check your inbox please.

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Thanks,

    I have got the email. We will test it probably today.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Hello Ismail, Were you able to check this issue?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    I couldn't figure it out from the code block you sent. Can you share full source code with me ? I will try to take a look at it as soon as possible.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Sure. I am sharing the source code.

    The code that is not working is commented out, just commented in and comment out the other code to be able to see what's going on.

    Regards Bilal

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi,

    We have replied your question via email. In current version ABP does not allow checking permissions for current user in a switched tenant/host context. You need to change your design according to that.

  • User Avatar
    0
    bilalhaidar created

    Hi Ismail

    Thano you for your reply.

    I'm not getting what you mean when you say cannot check permissions in a switched host and tenant context. An example pls ?

    Thanks

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi Bilal,

    In current design of ABP, when you enter an app service code using, let's say: UserId=2, TenantId=1 and then when you switch to host context using SetTenantId(null).

    In below using statement, if you try to check permisison for current user, ABP tries to find user with TenantId=null and UserId=2 and since this user does not exists, permission check fails.

    using(CurrentUnitOfWork.SetTenantId(null)){
    ...check permisison here...
    }
    

    I will redirect this question to Halil and I think he will put a better explanation here.

    Thanks.

  • User Avatar
    0
    bilalhaidar created

    Thank you.

    But I see a lot of use for the SetTenantId(Null) in the Framework code, how come it works? Aren't they checking permissions too?

    But how would I solve such an issue? What's the recommendation?

    I am getting data from host and tenant, at the same time, I have permissions so that not everyone can access the data.

    Thank you Bilal