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

A few fixes for client users #49

Merged
merged 2 commits into from
Jan 12, 2024
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ public OperatingSystemItemController(
#endregion

#region Endpoints
// TODO: Limit based on role and tenant.
/// <summary>
///
/// </summary>
Expand All @@ -64,6 +63,7 @@ public OperatingSystemItemController(
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OperatingSystemItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Operating System Item" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Only 60 second caching

public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand All @@ -87,7 +87,6 @@ public IActionResult Find()
}
}

// TODO: Limit based on role and tenant.
/// <summary>
///
/// </summary>
Expand Down
14 changes: 7 additions & 7 deletions src/api/Areas/Dashboard/Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,15 +51,16 @@ public OrganizationController(
#endregion

#region Endpoints
// TODO: Limit based on role and tenant.
/// <summary>
///
/// Find all organizations that match the specified query filter.
/// Only returns organizations the current user has access to.
/// </summary>
/// <returns></returns>
[HttpGet(Name = "GetOrganizations-Dashboard")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<OrganizationModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Organization" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand All @@ -69,7 +70,7 @@ public IActionResult Find()
var isHSB = this.User.HasClientRole(ClientRole.HSB);
if (isHSB)
{
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
var result = _service.Find(filter);
return new JsonResult(result.Select(o => new OrganizationModel(o)));
}
else
Expand All @@ -78,14 +79,13 @@ public IActionResult Find()
var user = _authorization.GetUser();
if (user == null) return Forbid();

var result = _service.FindForUser(user.Id, filter.GeneratePredicate(), filter.Sort);
var result = _service.FindForUser(user.Id, filter);
return new JsonResult(result.Select(o => new OrganizationModel(o)));
}
}

// TODO: Limit based on role and tenant.
/// <summary>
///
/// Get the organization for the specified 'id'.
/// </summary>
/// <param name="id"></param>
/// <returns></returns>
Expand All @@ -109,7 +109,7 @@ public IActionResult GetForId(int id)
var user = _authorization.GetUser();
if (user == null) return Forbid();

var entity = _service.FindForUser(user.Id, (o) => o.Id == id, o => o.Id).FirstOrDefault();
var entity = _service.FindForUser(user.Id, new HSB.Models.Filters.OrganizationFilter() { Id = id }).FirstOrDefault();
if (entity == null) return Forbid();
return new JsonResult(new OrganizationModel(entity));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ public ServerItemController(
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<ServerItemModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Server Item" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand Down
3 changes: 1 addition & 2 deletions src/api/Areas/Dashboard/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,6 @@ public TenantController(
#endregion

#region Endpoints
// TODO: Limit based on role and tenant.
/// <summary>
///
/// </summary>
Expand All @@ -60,6 +59,7 @@ public TenantController(
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(IEnumerable<TenantModel>), (int)HttpStatusCode.OK)]
[SwaggerOperation(Tags = new[] { "Tenant" })]
[ResponseCache(VaryByQueryKeys = new[] { "*" }, Location = ResponseCacheLocation.Client, Duration = 60)]
public IActionResult Find()
{
var uri = new Uri(this.Request.GetDisplayUrl());
Expand All @@ -83,7 +83,6 @@ public IActionResult Find()
}
}

// TODO: Limit based on role and tenant.
/// <summary>
///
/// </summary>
Expand Down
4 changes: 3 additions & 1 deletion src/api/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ public static void Main(string[] args)
.AllowAnyMethod(); ;
});
}
});
})
.AddResponseCaching();

var app = builder.Build();

Expand All @@ -93,6 +94,7 @@ public static void Main(string[] args)
app.UseCors("CorsPolicy");

app.UseMiddleware(typeof(LogRequestMiddleware));
app.UseResponseCaching();

app.UseAuthentication();
app.UseAuthorization();
Expand Down
15 changes: 4 additions & 11 deletions src/libs/dal/Services/IOrganizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,10 @@ namespace HSB.DAL.Services;

public interface IOrganizationService : IBaseService<Organization>
{
IEnumerable<Organization> FindForUser<T>(
long userId,
System.Linq.Expressions.Expression<Func<Organization, bool>> predicate,
System.Linq.Expressions.Expression<Func<Organization, T>>? sort = null,
int? take = null,
int? skip = null);
IEnumerable<Organization> Find(
Models.Filters.OrganizationFilter filter);

public IEnumerable<Organization> FindForUser(
IEnumerable<Organization> FindForUser(
long userId,
System.Linq.Expressions.Expression<Func<Organization, bool>> predicate,
string[] sort,
int? take = null,
int? skip = null);
Models.Filters.OrganizationFilter filter);
}
55 changes: 27 additions & 28 deletions src/libs/dal/Services/OrganizationService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,26 +18,21 @@ public OrganizationService(HSBContext dbContext, ClaimsPrincipal principal, ISer
#endregion

#region Methods
public IEnumerable<Organization> FindForUser<T>(
long userId,
System.Linq.Expressions.Expression<Func<Organization, bool>> predicate,
System.Linq.Expressions.Expression<Func<Organization, T>>? sort = null,
int? take = null,
int? skip = null)
public IEnumerable<Organization> Find(
Models.Filters.OrganizationFilter filter)
{
var query = (from org in this.Context.Organizations
join uo in this.Context.UserOrganizations on org.Id equals uo.OrganizationId
where uo.UserId == userId
select org)
.Where(predicate)
.Where(filter.GeneratePredicate())
.Distinct();

if (sort != null)
query = query.OrderBy(sort);
if (take.HasValue)
query = query.Take(take.Value);
if (skip.HasValue)
query = query.Skip(skip.Value);
if (filter.Sort?.Any() == true)
query = query.OrderByProperty(filter.Sort);
else query = query.OrderBy(si => si.Name);
if (filter.Quantity.HasValue)
query = query.Take(filter.Quantity.Value);
if (filter.Page.HasValue && filter.Quantity.HasValue && filter.Page > 1)
query = query.Skip(filter.Page.Value * filter.Quantity.Value);

return query
.AsNoTracking()
Expand All @@ -46,25 +41,29 @@ join uo in this.Context.UserOrganizations on org.Id equals uo.OrganizationId

public IEnumerable<Organization> FindForUser(
long userId,
System.Linq.Expressions.Expression<Func<Organization, bool>> predicate,
string[] sort,
int? take = null,
int? skip = null)
Models.Filters.OrganizationFilter filter)
{
var userOrganizationQuery = from uo in this.Context.UserOrganizations
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed issue where a user is only linked to a tenant, but not to an organization

where uo.UserId == userId
select uo.OrganizationId;
var tenantOrganizationQuery = from tOrg in this.Context.TenantOrganizations
join ut in this.Context.UserTenants on tOrg.TenantId equals ut.TenantId
where ut.UserId == userId
select tOrg.OrganizationId;

var query = (from org in this.Context.Organizations
join uo in this.Context.UserOrganizations on org.Id equals uo.OrganizationId
where uo.UserId == userId
where userOrganizationQuery.Contains(org.Id) || tenantOrganizationQuery.Contains(org.Id)
select org)
.Where(predicate)
.Where(filter.GeneratePredicate())
.Distinct();

if (sort?.Any() == true)
query = query.OrderByProperty(sort);
if (take.HasValue)
query = query.Take(take.Value);
if (skip.HasValue)
query = query.Skip(skip.Value);
if (filter.Sort?.Any() == true)
query = query.OrderByProperty(filter.Sort);
else query = query.OrderBy(si => si.Name);
if (filter.Quantity.HasValue)
query = query.Take(filter.Quantity.Value);
if (filter.Page.HasValue && filter.Quantity.HasValue && filter.Page > 1)
query = query.Skip(filter.Page.Value * filter.Quantity.Value);

return query
.AsNoTracking()
Expand Down
9 changes: 4 additions & 5 deletions src/libs/dal/Services/TenantService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.ChangeTracking;
using HSB.DAL.Extensions;
using System.Linq.Expressions;

namespace HSB.DAL.Services;

Expand All @@ -27,8 +26,8 @@ public IEnumerable<Tenant> FindForUser<T>(
int? skip = null)
{
var query = (from t in this.Context.Tenants
join usert in this.Context.UserTenants on t.Id equals usert.TenantId
where usert.UserId == userId
join ut in this.Context.UserTenants on t.Id equals ut.TenantId
where ut.UserId == userId
select t)
.Where(predicate);

Expand All @@ -52,8 +51,8 @@ public IEnumerable<Tenant> FindForUser(
int? skip = null)
{
var query = (from t in this.Context.Tenants
join usert in this.Context.UserTenants on t.Id equals usert.TenantId
where usert.UserId == userId
join ut in this.Context.UserTenants on t.Id equals ut.TenantId
where ut.UserId == userId
select t)
.Where(predicate);

Expand Down
4 changes: 4 additions & 0 deletions src/libs/models/Filters/OrganizationFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ namespace HSB.Models.Filters;
public class OrganizationFilter : PageFilter
{
#region Properties
public int? Id { get; set; }
public string? Name { get; set; }

public bool? IsEnabled { get; set; }
Expand All @@ -29,6 +30,7 @@ public OrganizationFilter(Dictionary<string, Microsoft.Extensions.Primitives.Str
{
var filter = new Dictionary<string, Microsoft.Extensions.Primitives.StringValues>(queryParams, StringComparer.OrdinalIgnoreCase);

this.Id = filter.GetIntNullValue(nameof(this.Id));
this.Name = filter.GetStringValue(nameof(this.Name));
this.IsEnabled = filter.GetBoolNullValue(nameof(this.IsEnabled));
this.ServiceNowKey = filter.GetStringValue(nameof(this.ServiceNowKey));
Expand All @@ -45,6 +47,8 @@ public OrganizationFilter(Dictionary<string, Microsoft.Extensions.Primitives.Str
public ExpressionStarter<Entities.Organization> GeneratePredicate()
{
var predicate = PredicateBuilder.New<Entities.Organization>();
if (this.Id != null)
predicate = predicate.And((u) => u.Id == this.Id);
if (this.Name != null)
predicate = predicate.And((u) => EF.Functions.Like(u.Name, $"%{this.Name}%"));
if (this.IsEnabled != null)
Expand Down
Loading