Skip to content

Commit

Permalink
HOSTSD-228 Add user admin (#61)
Browse files Browse the repository at this point in the history
Add organization admin
  • Loading branch information
Fosol authored Jan 26, 2024
1 parent 7384385 commit 38c99d8
Show file tree
Hide file tree
Showing 58 changed files with 1,184 additions and 610 deletions.
18 changes: 12 additions & 6 deletions src/api/Areas/Admin/Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ public IActionResult Find()
var uri = new Uri(this.Request.GetDisplayUrl());
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
var filter = new HSB.Models.Filters.OrganizationFilter(query);
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new OrganizationModel(ci)));
var result = _service.Find(filter);
return new JsonResult(result.Select(ci => new OrganizationModel(ci, true)));
}

/// <summary>
Expand All @@ -75,7 +75,7 @@ public IActionResult GetForId(int id)

if (organization == null) return new NoContentResult();

return new JsonResult(new OrganizationModel(organization));
return new JsonResult(new OrganizationModel(organization, true));
}

/// <summary>
Expand All @@ -93,7 +93,10 @@ public IActionResult Add(OrganizationModel model)
var entity = model.ToEntity();
_service.Add(entity);
_service.CommitTransaction();
return CreatedAtAction(nameof(GetForId), new { id = entity.Id }, new OrganizationModel(entity));

var result = _service.FindForId(model.Id, true);
if (result == null) return new BadRequestObjectResult(new ErrorResponseModel("Organization does not exist"));
return CreatedAtAction(nameof(GetForId), new { id = result.Id }, new OrganizationModel(result, true));
}

/// <summary>
Expand All @@ -111,7 +114,10 @@ public IActionResult Update(OrganizationModel model)
var entity = model.ToEntity();
_service.Update(entity);
_service.CommitTransaction();
return new JsonResult(new OrganizationModel(entity));

var result = _service.FindForId(model.Id, true);
if (result == null) return new BadRequestObjectResult(new ErrorResponseModel("Organization does not exist"));
return new JsonResult(new OrganizationModel(result, true));
}

/// <summary>
Expand All @@ -129,7 +135,7 @@ public IActionResult Remove(OrganizationModel model)
var entity = model.ToEntity() ?? throw new NoContentException();
_service.Remove(entity);
_service.CommitTransaction();
return new JsonResult(new OrganizationModel(entity));
return new JsonResult(new OrganizationModel(entity, false));
}
#endregion
}
16 changes: 11 additions & 5 deletions src/api/Areas/Admin/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ public IActionResult Find()
var query = Microsoft.AspNetCore.WebUtilities.QueryHelpers.ParseQuery(uri.Query);
var filter = new HSB.Models.Filters.TenantFilter(query);
var result = _service.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new TenantModel(ci)));
return new JsonResult(result.Select(ci => new TenantModel(ci, true)));
}

/// <summary>
Expand All @@ -75,7 +75,7 @@ public IActionResult GetForId(int id)

if (tenant == null) return new NoContentResult();

return new JsonResult(new TenantModel(tenant));
return new JsonResult(new TenantModel(tenant, true));
}

/// <summary>
Expand All @@ -93,7 +93,10 @@ public IActionResult Add(TenantModel model)
var entity = model.ToEntity();
_service.Add(entity);
_service.CommitTransaction();
return CreatedAtAction(nameof(GetForId), new { id = entity.Id }, new TenantModel(entity));

var result = _service.FindForId(model.Id, true);
if (result == null) return new BadRequestObjectResult(new ErrorResponseModel("Tenant does not exist"));
return CreatedAtAction(nameof(GetForId), new { id = result.Id }, new TenantModel(result, true));
}

/// <summary>
Expand All @@ -111,7 +114,10 @@ public IActionResult Update(TenantModel model)
var entity = model.ToEntity();
_service.Update(entity);
_service.CommitTransaction();
return new JsonResult(new TenantModel(entity));

var result = _service.FindForId(model.Id, true);
if (result == null) return new BadRequestObjectResult(new ErrorResponseModel("Tenant does not exist"));
return new JsonResult(new TenantModel(result, true));
}

/// <summary>
Expand All @@ -129,7 +135,7 @@ public IActionResult Remove(TenantModel model)
var entity = model.ToEntity() ?? throw new NoContentException();
_service.Remove(entity);
_service.CommitTransaction();
return new JsonResult(new TenantModel(entity));
return new JsonResult(new TenantModel(entity, true));
}
#endregion
}
19 changes: 13 additions & 6 deletions src/api/Areas/Admin/Controllers/UserController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -75,15 +75,16 @@ public IActionResult Find()
/// Get user for the specified 'id'.
/// </summary>
/// <param name="id"></param>
/// <param name="includePermissions"></param>
/// <returns></returns>
[HttpGet("{id}", Name = "GetUser-SystemAdmin")]
[Produces(MediaTypeNames.Application.Json)]
[ProducesResponseType(typeof(UserModel), (int)HttpStatusCode.OK)]
[ProducesResponseType((int)HttpStatusCode.NoContent)]
[SwaggerOperation(Tags = new[] { "User" })]
public IActionResult GetForId(int id)
public IActionResult GetForId(int id, bool includePermissions)
{
var result = _userService.FindForId(id);
var result = _userService.FindForId(id, includePermissions);
if (result == null) return new NoContentResult();
return new JsonResult(new UserModel(result));
}
Expand All @@ -102,10 +103,12 @@ public IActionResult Add(UserModel model)
{
var user = (Entities.User)model;
if (String.IsNullOrWhiteSpace(user.Key) || user.Key == Guid.Empty.ToString()) user.Key = Guid.NewGuid().ToString();
var result = _userService.Add(user);
var entry = _userService.Add(user);
_userService.CommitTransaction();
var entity = result.Entity;
return CreatedAtAction(nameof(GetForId), new { id = entity.Id }, new UserModel(entity));

var result = _userService.FindForId(model.Id, true);
if (result == null) return new BadRequestObjectResult(new ErrorResponseModel("User does not exist"));
return CreatedAtAction(nameof(GetForId), new { id = result.Id }, new UserModel(result));
}

/// <summary>
Expand Down Expand Up @@ -133,9 +136,13 @@ public async Task<IActionResult> UpdateAsync(UserModel model)
}
roles = roles.Distinct().ToList();

// TODO: Only update if roles changed
await _cssHelper.UpdateUserRolesAsync(model.Key.ToString(), roles.ToArray());
_userService.CommitTransaction();
return new JsonResult(new UserModel(entry.Entity));

var result = _userService.FindForId(model.Id, true);
if (result == null) return new BadRequestObjectResult(new ErrorResponseModel("User does not exist"));
return new JsonResult(new UserModel(result));
}

/// <summary>
Expand Down
8 changes: 4 additions & 4 deletions src/api/Areas/Dashboard/Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public IActionResult Find()
if (isHSB)
{
var result = _service.Find(filter);
return new JsonResult(result.Select(o => new OrganizationModel(o)));
return new JsonResult(result.Select(o => new OrganizationModel(o, true)));
}
else
{
Expand All @@ -80,7 +80,7 @@ public IActionResult Find()
if (user == null) return Forbid();

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

Expand All @@ -101,7 +101,7 @@ public IActionResult GetForId(int id)
{
var entity = _service.FindForId(id);
if (entity == null) return new NoContentResult();
return new JsonResult(new OrganizationModel(entity));
return new JsonResult(new OrganizationModel(entity, true));
}
else
{
Expand All @@ -111,7 +111,7 @@ public IActionResult GetForId(int id)

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));
return new JsonResult(new OrganizationModel(entity, true));
}
}
#endregion
Expand Down
12 changes: 6 additions & 6 deletions src/api/Areas/Dashboard/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -70,16 +70,16 @@ public IActionResult Find()
if (isHSB)
{
var result = _tenantService.Find(filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new TenantModel(ci)));
return new JsonResult(result.Select(ci => new TenantModel(ci, true)));
}
else
{
// Only return tenants this user belongs to.
var user = _authorization.GetUser();
if (user == null) return Forbid();

var result = _tenantService.FindForUser(user.Id, filter.GeneratePredicate(), filter.Sort);
return new JsonResult(result.Select(ci => new TenantModel(ci)));
var result = _tenantService.FindForUser(user.Id, filter);
return new JsonResult(result.Select(ci => new TenantModel(ci, true)));
}
}

Expand All @@ -100,17 +100,17 @@ public IActionResult GetForId(int id)
{
var entity = _tenantService.FindForId(id);
if (entity == null) return new NoContentResult();
return new JsonResult(new TenantModel(entity));
return new JsonResult(new TenantModel(entity, true));
}
else
{
// Only return tenants this user belongs to.
var user = _authorization.GetUser();
if (user == null) return Forbid();

var entity = _tenantService.FindForUser(user.Id, (t) => t.Id == id, t => t.Id).FirstOrDefault();
var entity = _tenantService.FindForUser(user.Id, new HSB.Models.Filters.TenantFilter() { Id = id }).FirstOrDefault();
if (entity == null) return Forbid();
return new JsonResult(new TenantModel(entity));
return new JsonResult(new TenantModel(entity, true));
}
}
#endregion
Expand Down
10 changes: 5 additions & 5 deletions src/api/Areas/Services/Controllers/OrganizationController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public OrganizationController(IOrganizationService service, ILogger<Organization
public IActionResult Find()
{
var organizations = _service.Find(o => true);
return new JsonResult(organizations.Select(ci => new OrganizationModel(ci)));
return new JsonResult(organizations.Select(ci => new OrganizationModel(ci, true)));
}

/// <summary>
Expand All @@ -70,7 +70,7 @@ public IActionResult GetForId(int id)

if (entity == null) return new NoContentResult();

return new JsonResult(new OrganizationModel(entity));
return new JsonResult(new OrganizationModel(entity, true));
}

/// <summary>
Expand All @@ -92,14 +92,14 @@ public IActionResult AddOrUpdate(OrganizationModel model)
{
_service.Add(entity);
_service.CommitTransaction();
return CreatedAtAction(nameof(GetForId), new { id = entity.Id }, new OrganizationModel(entity));
return CreatedAtAction(nameof(GetForId), new { id = entity.Id }, new OrganizationModel(entity, true));
}
else
{
_service.ClearChangeTracker(); // Remove existing from context.
_service.Update(entity);
_service.CommitTransaction();
return new JsonResult(new OrganizationModel(entity));
return new JsonResult(new OrganizationModel(entity, true));
}
}

Expand All @@ -118,7 +118,7 @@ public IActionResult Update(OrganizationModel model)
var entity = model.ToEntity();
_service.Update(entity);
_service.CommitTransaction();
return new JsonResult(new OrganizationModel(entity));
return new JsonResult(new OrganizationModel(entity, true));
}
#endregion
}
8 changes: 4 additions & 4 deletions src/api/Areas/Services/Controllers/TenantController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public TenantController(ITenantService service, ILogger<TenantController> logger
public IActionResult Find()
{
var tenants = _service.Find(o => true);
return new JsonResult(tenants.Select(ci => new TenantModel(ci)));
return new JsonResult(tenants.Select(ci => new TenantModel(ci, true)));
}

/// <summary>
Expand All @@ -70,7 +70,7 @@ public IActionResult GetForId(int id)

if (tenant == null) return new NoContentResult();

return new JsonResult(new TenantModel(tenant));
return new JsonResult(new TenantModel(tenant, true));
}

/// <summary>
Expand All @@ -88,7 +88,7 @@ public IActionResult Add(TenantModel model)
var entity = model.ToEntity();
_service.Add(entity);
_service.CommitTransaction();
return CreatedAtAction(nameof(GetForId), new { id = entity.Id }, new TenantModel(entity));
return CreatedAtAction(nameof(GetForId), new { id = entity.Id }, new TenantModel(entity, true));
}

/// <summary>
Expand All @@ -106,7 +106,7 @@ public IActionResult Update(TenantModel model)
var entity = model.ToEntity();
_service.Update(entity);
_service.CommitTransaction();
return new JsonResult(new TenantModel(entity));
return new JsonResult(new TenantModel(entity, true));
}
#endregion
}
2 changes: 1 addition & 1 deletion src/dashboard/src/app/api/admin/users/[id]/route.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { dispatch } from '@/app/api/utils';

export async function GET(req: Request, context: { params: any }) {
const url = new URL(req.url);
return await dispatch(`/v1/admin/users/${context.params.id}`);
return await dispatch(`/v1/admin/users/${context.params.id}${url.search}`);
}

export async function PUT(req: Request, context: { params: any }) {
Expand Down
Loading

0 comments on commit 38c99d8

Please sign in to comment.