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

@SharedLocalizer["Loading"]

} else {


@SharedLocalizer["Cancel"]

@Localizer["Roles"] @Localizer["Effective"] @Localizer["Expiry"]  
@context.Role.Name @Utilities.UtcAsLocalDate(context.EffectiveDate) @Utilities.UtcAsLocalDate(context.ExpiryDate)

} @code { private int userid; private string name = string.Empty; private List roles; private int roleid = -1; private DateTime? _effectivedate = null; private DateTime? _expirydate = null; private List userroles; public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Edit; protected override async Task OnInitializedAsync() { try { userid = Int32.Parse(PageState.QueryString["id"]); User user = await UserService.GetUserAsync(userid, PageState.Site.SiteId); name = user.DisplayName; if (UserSecurity.IsAuthorized(PageState.User, RoleNames.Host)) { roles = await RoleService.GetRolesAsync(PageState.Site.SiteId, true); roles.RemoveAll(item => item.Name == RoleNames.Everyone || item.Name == RoleNames.Unauthenticated); } else { roles = await RoleService.GetRolesAsync(PageState.Site.SiteId); } await GetUserRoles(); } catch (Exception ex) { await logger.LogError(ex, "Error Loading Roles {Error}", ex.Message); AddModuleMessage(Localizer["Error.LoadRole"], MessageType.Error); } } private async Task GetUserRoles() { try { userroles = await UserRoleService.GetUserRolesAsync(PageState.Site.SiteId, userid); } catch (Exception ex) { await logger.LogError(ex, "Error Loading User Roles {UserId} {Error}", userid, ex.Message); AddModuleMessage(Localizer["Error.User.LoadRole"], MessageType.Error); } } private async Task SaveUserRole() { try { if (roleid != -1) { if (!Utilities.ValidateEffectiveExpiryDates(_effectivedate, _expirydate)) { AddModuleMessage(SharedLocalizer["Message.EffectiveExpiryDateError"], MessageType.Warning); return; } 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 = Utilities.UtcAsLocalDate(_effectivedate); userrole.ExpiryDate = Utilities.UtcAsLocalDate(_expirydate); await UserRoleService.AddUserRoleAsync(userrole); } await logger.LogInformation("User Assigned To Role {UserRole}", userrole); AddModuleMessage(Localizer["Success.User.AssignRole"], MessageType.Success); await GetUserRoles(); StateHasChanged(); } else { AddModuleMessage(Localizer["Message.Required.Role"], MessageType.Warning); } } catch (Exception ex) { await logger.LogError(ex, "Error Saving User Roles {UserId} {Error}", userid, 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["Success.User.Remove"], 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); } } }