Base solution for your next web application
Open Closed

Displaying only docs assigned to current user #9773


User avatar
0
hrod761 created

.NET Core 3.1 Angular v9.0.1

Hello,

here the scenario, our tenants will have their client login to view their invoices via the app, we created a role "ClientCustomers" for them. we also created 2 new entities as follows:

ClientAccounts: This table will hold the list of accounts and the respectice userid associated with that account (one client can have multiple accounts) ClientAccountsInvoices: This table hold the invoices details and is linked to the ClientAccounts via ClientAccount.Id.

how can I show the current user, only those records from the ClientAccountsInvoices that belong to him/her?

Thanks,


3 Answer(s)
  • User Avatar
    0
    zony created
    Support Team

    Hi hrod761, I think you can inject into ʻIAbpSession and get the current user Id through the ʻUserId property. When querying ClientAccountsInvoices, query the corresponding database table through the IQuerable association. Fake code:

    public async Task TestDemoAsync()
    {
        var currentUserId = AbpSession.UserId;
        if (currentUserId == null)
        {
            throw new UserFriendlyException("User not login.");
        }
    
        var currentUserAccounts = ClientAccountsRep.GetAll().Where(ca => ca.UserId = currentUserId);
        var currentUserInvoices = await ClientAccountsInvoicesRep.GetAll()
            .Join(currentUserAccounts, cai => cai.ClientAccount.Id, ca => ca.UserId, (cai, ca) => cai)
            .ToListAsync();
        var currentUserAccountsList = await currentUserAccounts.ToListAsync();
    }
    
  • User Avatar
    1
    hrod761 created

    Thanks for the suggestion, i'll give that a try and report back.

  • User Avatar
    0
    ismcagdas created
    Support Team

    Hi @hrod761

    Please reopen if that doesn't work for you.