@namespace Oqtane.Modules.Admin.UserProfile @using System.Net @using System.Text.RegularExpressions; @inherits ModuleBase @inject NavigationManager NavigationManager @inject IUserService UserService @inject IProfileService ProfileService @inject ISettingService SettingService @inject INotificationService NotificationService @inject IFileService FileService @inject IFolderService FolderService @inject IJSRuntime jsRuntime @inject IServiceProvider ServiceProvider @inject IStringLocalizer Localizer @inject IStringLocalizer SharedLocalizer @if (_initialized) { @if (PageState.User != null && photo != null) { @displayname } else {
}
@if (allowtwofactor) {
}

@foreach (Profile profile in profiles) { var p = profile; if (!p.IsPrivate || UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin)) { if (p.Category != category) {
@p.Category
category = p.Category; }
@if (!string.IsNullOrEmpty(p.Options)) { @if (!string.IsNullOrEmpty(p.Autocomplete)) { } else { } } else { @if (p.Rows == 1) { if (!string.IsNullOrEmpty(p.Autocomplete)) { @if (p.IsRequired) { } else { } } else { @if (p.IsRequired) { } else { } } } else { if (!string.IsNullOrEmpty(p.Autocomplete)) { @if (p.IsRequired) { } else { } } else { @if (p.IsRequired) { } else { } } } }
} }



@if (filter == "to") { @if (notifications.Any()) {
    @Localizer["From"] @Localizer["Subject"] @Localizer["Received"]
@if (context.IsRead) { @(string.IsNullOrEmpty(context.FromDisplayName) ? SharedLocalizer["System"] : context.FromDisplayName) @context.Subject @string.Format("{0:dd-MMM-yyyy HH:mm:ss}", @context.CreatedOn) } else { @(string.IsNullOrEmpty(context.FromDisplayName) ? SharedLocalizer["System"] : context.FromDisplayName) @context.Subject @string.Format("{0:dd-MMM-yyyy HH:mm:ss}", @context.CreatedOn) } @{ string input = "___"; if (context.Body.Contains(input)) { context.Body = context.Body.Split(input)[0]; context.Body = context.Body.Replace("\n", ""); context.Body = context.Body.Replace("\r", ""); } notificationSummary = context.Body.Length > 100 ? (context.Body.Substring(0, 97) + "...") : context.Body; } @if (context.IsRead) { @notificationSummary } else { @notificationSummary }

} else {
@Localizer["NoNotificationsReceived.Text"]
} } else { @if (notifications.Any()) {
@Localizer["To"] @Localizer["Subject"] @Localizer["Sent"]
@if (context.IsRead) { @context.ToDisplayName @context.Subject @string.Format("{0:dd-MMM-yyyy HH:mm:ss}", @context.CreatedOn) } else { @context.ToDisplayName @context.Subject @string.Format("{0:dd-MMM-yyyy HH:mm:ss}", @context.CreatedOn) } @{ string input = "___"; if (context.Body.Contains(input)) { context.Body = context.Body.Split(input)[0]; context.Body = context.Body.Replace("\n", ""); context.Body = context.Body.Replace("\r", ""); } notificationSummary = context.Body.Length > 100 ? (context.Body.Substring(0, 97) + "...") : context.Body; } @if (context.IsRead) { @notificationSummary } else { @notificationSummary }

} else {
@Localizer["NoNotificationsSent.Text"]
} }


} @code { private bool _initialized = false; private string _passwordrequirements; private string username = string.Empty; private string _password = string.Empty; private string _passwordtype = "password"; private string _togglepassword = string.Empty; private string confirm = string.Empty; private bool allowtwofactor = false; private string twofactor = "False"; private string email = string.Empty; private string displayname = string.Empty; private FileManager filemanager; private int folderid = -1; private int photofileid = -1; private File photo = null; private string _ImageFiles = string.Empty; private List profiles; private Dictionary userSettings; private string category = string.Empty; private string filter = "to"; private List notifications; private string notificationSummary = string.Empty; public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.View; protected override async Task OnInitializedAsync() { try { _passwordrequirements = await UserService.GetPasswordRequirementsAsync(PageState.Site.SiteId); _togglepassword = SharedLocalizer["ShowPassword"]; allowtwofactor = (SettingService.GetSetting(PageState.Site.Settings, "LoginOptions:TwoFactor", "false") == "true"); profiles = await ProfileService.GetProfilesAsync(ModuleState.SiteId); if (PageState.User != null) { username = PageState.User.Username; twofactor = PageState.User.TwoFactorRequired.ToString(); email = PageState.User.Email; displayname = PageState.User.DisplayName; if (string.IsNullOrEmpty(email)) { AddModuleMessage(Localizer["Message.User.NoEmail"], MessageType.Warning); } // get user folder var folder = await FolderService.GetFolderAsync(ModuleState.SiteId, PageState.User.FolderPath); if (folder != null) { folderid = folder.FolderId; } if (PageState.User.PhotoFileId != null) { photofileid = PageState.User.PhotoFileId.Value; photo = await FileService.GetFileAsync(photofileid); } else { photofileid = -1; photo = null; } userSettings = PageState.User.Settings; var sitesettings = await SettingService.GetSiteSettingsAsync(PageState.Site.SiteId); _ImageFiles = SettingService.GetSetting(userSettings, "ImageFiles", Constants.ImageFiles); _ImageFiles = (string.IsNullOrEmpty(_ImageFiles)) ? Constants.ImageFiles : _ImageFiles; await LoadNotificationsAsync(); _initialized = true; } else { AddModuleMessage(Localizer["Message.User.NoLogIn"], MessageType.Warning); } } catch (Exception ex) { await logger.LogError(ex, "Error Loading User Profile {Error}", ex.Message); AddModuleMessage(Localizer["Error.Profile.Load"], MessageType.Error); } } private async Task LoadNotificationsAsync() { notifications = await NotificationService.GetNotificationsAsync(PageState.Site.SiteId, filter, PageState.User.UserId); notifications = notifications.Where(item => item.DeletedBy != PageState.User.Username).ToList(); } private string GetProfileValue(string SettingName, string DefaultValue) { string value = SettingService.GetSetting(userSettings, SettingName, DefaultValue); if (value.Contains("]")) { value = value.Substring(value.IndexOf("]") + 1); } return value; } private async Task Save() { try { if (username != string.Empty && email != string.Empty) { if (_password == confirm) { if (ValidateProfiles()) { var user = PageState.User; user.Username = username; user.Password = _password; user.TwoFactorRequired = bool.Parse(twofactor); user.Email = email; user.DisplayName = (displayname == string.Empty ? username : displayname); user.PhotoFileId = filemanager.GetFileId(); if (user.PhotoFileId == -1) { user.PhotoFileId = null; } if (user.PhotoFileId != null) { photofileid = user.PhotoFileId.Value; photo = await FileService.GetFileAsync(photofileid); } else { photofileid = -1; photo = null; } user = await UserService.UpdateUserAsync(user); if (user != null) { await SettingService.UpdateUserSettingsAsync(userSettings, PageState.User.UserId); await logger.LogInformation("User Profile Saved"); if (!string.IsNullOrEmpty(PageState.ReturnUrl)) { NavigationManager.NavigateTo(PageState.ReturnUrl); } else // legacy behavior { AddModuleMessage(Localizer["Success.Profile.Update"], MessageType.Success); StateHasChanged(); } } else { AddModuleMessage(Localizer["Message.Password.Complexity"], MessageType.Error); } } } else { AddModuleMessage(Localizer["Message.Password.Invalid"], MessageType.Warning); } } else { AddModuleMessage(Localizer["Message.Required.ProfileInfo"], MessageType.Warning); } await ScrollToPageTop(); } catch (Exception ex) { await logger.LogError(ex, "Error Saving User Profile {Error}", ex.Message); AddModuleMessage(Localizer["Error.Profile.Save"], MessageType.Error); } } private async Task Logout() { await logger.LogInformation("User Logout Everywhere For Username {Username}", PageState.User?.Username); var url = NavigateUrl(""); // home page if (PageState.Runtime == Shared.Runtime.Hybrid) { if (PageState.User != null) { // hybrid apps utilize an interactive logout await UserService.LogoutUserEverywhereAsync(PageState.User); var authstateprovider = (IdentityAuthenticationStateProvider)ServiceProvider.GetService(typeof(IdentityAuthenticationStateProvider)); authstateprovider.NotifyAuthenticationChanged(); NavigationManager.NavigateTo(url, true); } } else { // post to the Logout page to complete the logout process var fields = new { __RequestVerificationToken = SiteState.AntiForgeryToken, returnurl = url, everywhere = true }; var interop = new Interop(jsRuntime); await interop.SubmitForm(Utilities.TenantUrl(PageState.Alias, "/pages/logout/"), fields); } } private bool ValidateProfiles() { foreach (Profile profile in profiles) { var value = GetProfileValue(profile.Name, string.Empty); if (string.IsNullOrEmpty(value) && !string.IsNullOrEmpty(profile.DefaultValue)) { userSettings = SettingService.SetSetting(userSettings, profile.Name, profile.DefaultValue); } if (!profile.IsPrivate || UserSecurity.IsAuthorized(PageState.User, RoleNames.Admin)) { if (profile.IsRequired && string.IsNullOrEmpty(value)) { AddModuleMessage(string.Format(SharedLocalizer["ProfileRequired"], profile.Title), MessageType.Warning); return false; } if (!string.IsNullOrEmpty(profile.Validation)) { Regex regex = new Regex(profile.Validation); bool valid = regex.Match(value).Success; if (!valid) { AddModuleMessage(string.Format(SharedLocalizer["ProfileInvalid"], profile.Title), MessageType.Warning); return false; } } } } return true; } private void Cancel() { NavigationManager.NavigateTo(PageState.ReturnUrl); } private void ProfileChanged(ChangeEventArgs e, string SettingName) { var value = (string)e.Value; userSettings = SettingService.SetSetting(userSettings, SettingName, value); } private async Task Delete(Notification Notification) { try { if (!Notification.IsDeleted) { Notification.IsDeleted = true; await NotificationService.UpdateNotificationAsync(Notification); } else { await NotificationService.DeleteNotificationAsync(Notification.NotificationId); } await logger.LogInformation("Notification Deleted {Notification}", Notification); await LoadNotificationsAsync(); StateHasChanged(); } catch (Exception ex) { await logger.LogError(ex, "Error Deleting Notification {Notification} {Error}", Notification, ex.Message); AddModuleMessage(ex.Message, MessageType.Error); } } private async void FilterChanged(ChangeEventArgs e) { filter = (string)e.Value; await LoadNotificationsAsync(); StateHasChanged(); } private async Task DeleteAllNotifications() { try { ShowProgressIndicator(); foreach(var Notification in notifications) { if (!Notification.IsDeleted) { Notification.IsDeleted = true; await NotificationService.UpdateNotificationAsync(Notification); } else { await NotificationService.DeleteNotificationAsync(Notification.NotificationId); } await logger.LogInformation("Notification Deleted {Notification}", Notification); } await logger.LogInformation("Notifications Permanently Deleted"); await LoadNotificationsAsync(); HideProgressIndicator(); StateHasChanged(); } catch (Exception ex) { await logger.LogError(ex, "Error Deleting Notifications {Error}", ex.Message); AddModuleMessage(ex.Message, MessageType.Error); HideProgressIndicator(); } } private void TogglePassword() { if (_passwordtype == "password") { _passwordtype = "text"; _togglepassword = SharedLocalizer["HidePassword"]; } else { _passwordtype = "password"; _togglepassword = SharedLocalizer["ShowPassword"]; } } }