@namespace Oqtane.Modules.Admin.Roles @inherits ModuleBase @inject IRoleService RoleService @inject IUserRoleService UserRoleService @inject IStringLocalizer Localizer @if (userroles == null) {

@Localizer["Loading"]

} else {
@Localizer["Cancel"]

@Localizer["Users"] @Localizer["Effective"] @Localizer["Expiry"]  
@context.User.DisplayName @context.EffectiveDate @context.ExpiryDate

} @code { private int roleid; private string name = string.Empty; private List users; private int userid = -1; private DateTime? effectivedate = null; private DateTime? expirydate = null; private List userroles; public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Admin; protected override async Task OnInitializedAsync() { try { roleid = Int32.Parse(PageState.QueryString["id"]); Role role = await RoleService.GetRoleAsync(roleid); name = role.Name; users = await UserRoleService.GetUserRolesAsync(PageState.Site.SiteId); users = users .Where(u => u.Role.Name == RoleNames.Registered) .OrderBy(u => u.User.DisplayName) .ToList(); await GetUserRoles(); } catch (Exception ex) { await logger.LogError(ex, "Error Loading Users {Error}", ex.Message); AddModuleMessage(Localizer["Error.User.Load"], MessageType.Error); } } private async Task GetUserRoles() { try { userroles = await UserRoleService.GetUserRolesAsync(PageState.Site.SiteId); userroles = userroles.Where(item => item.RoleId == roleid).ToList(); } catch (Exception ex) { await logger.LogError(ex, "Error Loading User Roles {RoleId} {Error}", roleid, ex.Message); AddModuleMessage(Localizer["Error.User.LoadRole"], MessageType.Error); } } private async Task SaveUserRole() { try { if (userid != -1) { var userrole = userroles.Where(item => item.UserId == userid && item.RoleId == roleid).FirstOrDefault(); if (userrole != null) { userrole.EffectiveDate = effectivedate; userrole.ExpiryDate = expirydate; await UserRoleService.UpdateUserRoleAsync(userrole); } else { userrole = new UserRole(); userrole.UserId = userid; userrole.RoleId = roleid; userrole.EffectiveDate = effectivedate; userrole.ExpiryDate = expirydate; await UserRoleService.AddUserRoleAsync(userrole); } await logger.LogInformation("User Assigned To Role {UserRole}", userrole); AddModuleMessage(Localizer["Success.User.AssignedRole"], MessageType.Success); await GetUserRoles(); StateHasChanged(); } else { AddModuleMessage(Localizer["Message.Required.UserSelect"], MessageType.Warning); } } catch (Exception ex) { await logger.LogError(ex, "Error Saving User Roles {RoleId} {Error}", roleid, ex.Message); AddModuleMessage(Localizer["Error.User.SaveRole"], MessageType.Error); } } private async Task DeleteUserRole(int UserRoleId) { try { await UserRoleService.DeleteUserRoleAsync(UserRoleId); await logger.LogInformation("User Removed From Role {UserRoleId}", UserRoleId); AddModuleMessage(Localizer["Confirm.User.RoleRemoved"], MessageType.Success); await GetUserRoles(); StateHasChanged(); } catch (Exception ex) { await logger.LogError(ex, "Error Removing User From Role {UserRoleId} {Error}", UserRoleId, ex.Message); AddModuleMessage(Localizer["Error.User.RemoveRole"], MessageType.Error); } } }