Skip to content

Commit

Permalink
Merge pull request #1280 from sbwalker/dev
Browse files Browse the repository at this point in the history
user and role management improvements
  • Loading branch information
sbwalker committed Apr 24, 2021
2 parents a3449f5 + d42c7a5 commit 60f3868
Show file tree
Hide file tree
Showing 6 changed files with 35 additions and 21 deletions.
6 changes: 5 additions & 1 deletion Oqtane.Client/Modules/Admin/Roles/Users.razor
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,16 @@ else
<Pager Items="@userroles">
<Header>
<th>@Localizer["Users"]</th>
<th>@Localizer["Effective"]</th>
<th>@Localizer["Expiry"]</th>
<th>&nbsp;</th>
</Header>
<Row>
<td>@context.User.DisplayName</td>
<td>@context.EffectiveDate</td>
<td>@context.ExpiryDate</td>
<td>
<ActionDialog Header="Remove User" Message="@Localizer["Are You Sure You Wish To Remove {0} From This Role?", context.User.DisplayName]" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteUserRole(context.UserRoleId))" ResourceKey="DeleteUserRole" />
<ActionDialog Header="Remove User" Message="@Localizer["Are You Sure You Wish To Remove {0} From This Role?", context.User.DisplayName]" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteUserRole(context.UserRoleId))" Disabled="@(context.Role.IsAutoAssigned)" ResourceKey="DeleteUserRole" />
</td>
</Row>
</Pager>
Expand Down
5 changes: 1 addition & 4 deletions Oqtane.Client/Modules/Admin/Users/Index.razor
Original file line number Diff line number Diff line change
Expand Up @@ -36,10 +36,7 @@ else
<ActionLink Action="Edit" Parameters="@($"id=" + context.UserId.ToString())" ResourceKey="EditUser" />
</td>
<td>
@if (context.Role.Name != RoleNames.Host)
{
<ActionDialog Header="Delete User" Message="@Localizer["Are You Sure You Wish To Delete {0}?", context.User.DisplayName]" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteUser(context))" ResourceKey="DeleteUser" />
}
<ActionDialog Header="Delete User" Message="@Localizer["Are You Sure You Wish To Delete {0}?", context.User.DisplayName]" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteUser(context))" Disabled="@(context.Role.Name == RoleNames.Host)" ResourceKey="DeleteUser" />
</td>
<td>
<ActionLink Action="Roles" Parameters="@($"id=" + context.UserId.ToString())" ResourceKey="Roles" />
Expand Down
10 changes: 6 additions & 4 deletions Oqtane.Client/Modules/Admin/Users/Roles.razor
Original file line number Diff line number Diff line change
Expand Up @@ -59,15 +59,16 @@ else
<Pager Items="@userroles">
<Header>
<th>@Localizer["Roles"]</th>
<th>@Localizer["Effective"]</th>
<th>@Localizer["Expiry"]</th>
<th>&nbsp;</th>
</Header>
<Row>
<td>@context.Role.Name</td>
<td>@context.EffectiveDate</td>
<td>@context.ExpiryDate</td>
<td>
@if (context.Role.Name != RoleNames.Registered && (context.Role.Name != RoleNames.Host || userid != PageState.User.UserId))
{
<ActionDialog Header="Remove Role" Message="@Localizer["Are You Sure You Wish To Remove This User From The {0} Role?", context.Role.Name]" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteUserRole(context.UserRoleId))" ResourceKey="DeleteUserRole" />
}
<ActionDialog Header="Remove Role" Message="@Localizer["Are You Sure You Wish To Remove This User From The {0} Role?", context.Role.Name]" Action="Delete" Security="SecurityAccessLevel.Admin" Class="btn btn-danger" OnClick="@(async () => await DeleteUserRole(context.UserRoleId))" Disabled="@(context.Role.IsAutoAssigned || (context.Role.Name == RoleNames.Host && userid == PageState.User.UserId))" ResourceKey="DeleteUserRole" />
</td>
</Row>
</Pager>
Expand Down Expand Up @@ -95,6 +96,7 @@ else
if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host))
{
roles = await RoleService.GetRolesAsync(PageState.Site.SiteId, true);
roles = roles.Where(item => item.Name != RoleNames.Everyone).ToList();
}
else
{
Expand Down
10 changes: 7 additions & 3 deletions Oqtane.Server/Controllers/RoleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ public IEnumerable<Role> Get(string siteid, string global)
{
if (string.IsNullOrEmpty(global))
{
global = "false";
global = "False";
}
return _roles.GetRoles(int.Parse(siteid), bool.Parse(global));
}
Expand Down Expand Up @@ -72,8 +72,12 @@ public Role Put(int id, [FromBody] Role role)
[Authorize(Roles = RoleNames.Admin)]
public void Delete(int id)
{
_roles.DeleteRole(id);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Role Deleted {RoleId}", id);
var role = _roles.GetRole(id);
if (!role.IsSystem)
{
_roles.DeleteRole(id);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "Role Deleted {RoleId}", id);
}
}
}
}
12 changes: 6 additions & 6 deletions Oqtane.Server/Controllers/UserRoleController.cs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,16 @@ public UserRole Post([FromBody] UserRole userRole)
var role = _roles.GetRole(userRole.RoleId);
if (ModelState.IsValid && (User.IsInRole(RoleNames.Host) || role.Name != RoleNames.Host))
{
userRole = _userRoles.AddUserRole(userRole);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "User Role Added {UserRole}", userRole);

if (role.Name == RoleNames.Host)
{
// host roles can only exist at global level - remove all site specific user roles
_userRoles.DeleteUserRoles(userRole.UserId);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "User Roles Deleted For UserId {UserId}", userRole.UserId);
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Roles Deleted For UserId {UserId}", userRole.UserId);
}

userRole = _userRoles.AddUserRole(userRole);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "User Role Added {UserRole}", userRole);

_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.User, userRole.UserId);
}
return userRole;
Expand Down Expand Up @@ -98,10 +98,10 @@ public void Delete(int id)
// add site specific user roles to preserve user access
var role = _roles.GetRoles(_tenants.GetAlias().SiteId).FirstOrDefault(item => item.Name == RoleNames.Registered);
userRole = _userRoles.AddUserRole(new UserRole { UserId = userRole.UserId, RoleId = role.RoleId, EffectiveDate = null, ExpiryDate = null });
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Role Added {UserRole}", userRole);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "User Role Added {UserRole}", userRole);
role = _roles.GetRoles(_tenants.GetAlias().SiteId).FirstOrDefault(item => item.Name == RoleNames.Admin);
userRole = _userRoles.AddUserRole(new UserRole { UserId = userRole.UserId, RoleId = role.RoleId, EffectiveDate = null, ExpiryDate = null });
_logger.Log(LogLevel.Information, this, LogFunction.Delete, "User Role Added {UserRole}", userRole);
_logger.Log(LogLevel.Information, this, LogFunction.Create, "User Role Added {UserRole}", userRole);
}

_syncManager.AddSyncEvent(_tenants.GetTenant().TenantId, EntityNames.User, userRole.UserId);
Expand Down
13 changes: 10 additions & 3 deletions Oqtane.Server/Repository/RoleRepository.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
using System.Collections.Generic;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using Oqtane.Models;
Expand All @@ -16,12 +16,19 @@ public RoleRepository(TenantDBContext context)

public IEnumerable<Role> GetRoles(int siteId)
{
return _db.Role.Where(item => item.SiteId == siteId);
return GetRoles(siteId, false);
}

public IEnumerable<Role> GetRoles(int siteId, bool includeGlobalRoles)
{
return _db.Role.Where(item => item.SiteId == siteId || item.SiteId == null);
if (includeGlobalRoles)
{
return _db.Role.Where(item => item.SiteId == siteId || item.SiteId == null);
}
else
{
return _db.Role.Where(item => item.SiteId == siteId);
}
}

public Role AddRole(Role role)
Expand Down

0 comments on commit 60f3868

Please sign in to comment.