@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 ITimeZoneService TimeZoneService @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 List _timezones; 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 string _timezoneid = string.Empty; 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); _timezones = await TimeZoneService.GetTimeZonesAsync(); if (PageState.User != null) { _username = PageState.User.Username; _twofactor = PageState.User.TwoFactorRequired.ToString(); _email = PageState.User.Email; _displayname = PageState.User.DisplayName; _timezoneid = PageState.User.TimeZoneId; 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.TimeZoneId = _timezoneid; 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"]; } } }