Base solution for your next web application
Open Closed

Access Tenant Data (cross) from Host #559


User avatar
0
cashonledger created

Hi Halil Ibrahim,

in our project, where we are using the tenant/host distinction, we also have a use case of a call-center where the agents are expected to have a global access to all tenants' tickets in the system (imagine it as a kind of 'superview‘ if you like). No change for tenants, they will be able to create & edit their own data, namely tickets.

I'm not sure if this question was already covered in another topic or on GitHub (eventually issue #530 which seems to have similarity?) and wanted to ask for your advice on how to achieve this.

Generally I think that this feature would be very helpful in your Product (out-of-the-box).

Thank you very much in advance, Best regards, Tuna


4 Answer(s)
  • User Avatar
    0
    hikalkan created
    Support Team

    Hi Tuna,

    ABP automatically filters entities if Entity class implements IMustHaveTenant or IMayHaveTenant. If your Ticket entity is like that, a tenant can only see it's own Tickets. I think this is your case and what you want.

    And you need to get All Tickets of all tenants in some case. Actually, you want to disable this auto filtering. This is possible: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Data-Filters#DocDisableFilters">http://www.aspnetboilerplate.com/Pages/ ... bleFilters</a> Just use DisableFilter method. Filter name is: AbpDataFilter.MustHaveTenant or AbpDataFilter.MayHaveTenant based on your case. This is example for User entity: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Zero/User-Management#multi-tenancy">http://www.aspnetboilerplate.com/Pages/ ... ti-tenancy</a>

  • User Avatar
    0
    cashonledger created

    Thanks! Disabling the filter seems to work to some extent. But I still have trouble with objects that contain foreign keys. For example:

    This is the Request object:

    public class Request : FullAuditedEntity, IMustHaveTenant
    {
        ...
    
        [ForeignKey("RequestorId")]
        public virtual User Requestor { get; set; }
        public virtual long? RequestorId { get; set; }
    
        ...
    }
    

    This is the GetRequests service method:

    public ListResultOutput<RequestListDto> GetRequests(GetRequestsInput input)
    {
        using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MustHaveTenant))
        {
            var requests = _requestRepository
                .GetAll()
                .Include(p => p.Requestor)
                .WhereIf(
                    !input.Filter.IsNullOrEmpty(),
                    p =>
                        p.Requestor.UserName.Contains(input.Filter) ||
                        p.Requestor.Name.Contains(input.Filter) ||
                        p.Requestor.Surname.Contains(input.Filter) ||
                        p.Requestor.EmailAddress.Contains(input.Filter)
                )
                .OrderByDescending(p => p.CreationTime)
                .ToList();
    
            return new ListResultOutput<RequestListDto>(requests.MapTo<List<RequestListDto>>());
        }
    }
    

    Without disabling the filter, I didn't see any Request objects. Thanks to your suggestion and using the code above, I'm at least getting all the requests now from host. But the Requestor inside Request is null, probably because disabling the filter in the current unit of work doesn't apply there.

    Do you have any suggestions or ideas on how to disable the filter in this case? Maybe somehow recursively or some kind of "always disable filter, if I am host". Thanks!

  • User Avatar
    0
    hikalkan created
    Support Team

    Hi,

    User entity is not 'IMustHaveTenant', it's 'IMayHaveTenant' (see doc: <a class="postlink" href="http://www.aspnetboilerplate.com/Pages/Documents/Zero/User-Management">http://www.aspnetboilerplate.com/Pages/ ... Management</a>). So, you should disable it too. You can disable it in single statement. Example:

    using (CurrentUnitOfWork.DisableFilter(AbpDataFilters.MustHaveTenant, AbpDataFilters.MayHaveTenant))
    {
        ...
    }
    
  • User Avatar
    0
    cashonledger created

    Thank you for your quick response! That was indeed the issue! :D