226 lines
9.3 KiB
Plaintext
226 lines
9.3 KiB
Plaintext
@namespace Oqtane.Modules.Admin.Users
|
|
@inherits ModuleBase
|
|
@inject NavigationManager NavigationManager
|
|
@inject IUserService UserService
|
|
@inject IProfileService ProfileService
|
|
@inject ISettingService SettingService
|
|
@inject IStringLocalizer<Add> Localizer
|
|
@inject IStringLocalizer<SharedResources> SharedLocalizer
|
|
|
|
<TabStrip>
|
|
<TabPanel Name="Identity" ResourceKey="Identity">
|
|
@if (profiles != null)
|
|
{
|
|
<div class="container">
|
|
<div class="row mb-1 align-items-center">
|
|
<Label Class="col-sm-3" For="username" HelpText="A unique username for a user. Note that this field can not be modified once it is saved." ResourceKey="Username"></Label>
|
|
<div class="col-sm-9">
|
|
<input id="username" class="form-control" @bind="@username" />
|
|
</div>
|
|
</div>
|
|
<div class="row mb-1 align-items-center">
|
|
<Label Class="col-sm-3" For="password" HelpText="The user's password. Please choose a password which is sufficiently secure." ResourceKey="Password"></Label>
|
|
<div class="col-sm-9">
|
|
<div class="input-group">
|
|
<input id="password" type="@_passwordtype" class="form-control" @bind="@_password" autocomplete="new-password" required />
|
|
<button type="button" class="btn btn-secondary" @onclick="@TogglePassword">@_togglepassword</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="row mb-1 align-items-center">
|
|
<Label Class="col-sm-3" For="confirm" HelpText="Please enter the password again to confirm it matches with the value above" ResourceKey="Confirm"></Label>
|
|
<div class="col-sm-9">
|
|
<div class="input-group">
|
|
<input id="confirm" type="@_passwordtype" class="form-control" @bind="@confirm" autocomplete="new-password" required />
|
|
<button type="button" class="btn btn-secondary" @onclick="@TogglePassword">@_togglepassword</button>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
<div class="row mb-1 align-items-center">
|
|
<Label Class="col-sm-3" For="email" HelpText="The email address where the user will receive notifications" ResourceKey="Email"></Label>
|
|
<div class="col-sm-9">
|
|
<input id="email" class="form-control" @bind="@email" />
|
|
</div>
|
|
</div>
|
|
<div class="row mb-1 align-items-center">
|
|
<Label Class="col-sm-3" For="displayname" HelpText="The full name of the user" ResourceKey="DisplayName"></Label>
|
|
<div class="col-sm-9">
|
|
<input id="displayname" class="form-control" @bind="@displayname" />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
|
|
}
|
|
</TabPanel>
|
|
<TabPanel Name="Profile" ResourceKey="Profile">
|
|
@if (profiles != null)
|
|
{
|
|
<div class="container">
|
|
<div class="row mb-1 align-items-center">
|
|
@foreach (Profile profile in profiles)
|
|
{
|
|
var p = profile;
|
|
if (p.Category != category)
|
|
{
|
|
<div class="col text-center pb-2">
|
|
<strong>@p.Category</strong>
|
|
</div>
|
|
category = p.Category;
|
|
}
|
|
<div class="row mb-1 align-items-center">
|
|
<Label Class="col-sm-3" For="@p.Name" HelpText="@p.Description">@p.Title</Label>
|
|
<div class="col-sm-9">
|
|
@if (p.IsRequired)
|
|
{
|
|
<input id="@p.Name" class="form-control" maxlength="@p.MaxLength" value="@GetProfileValue(p.Name, p.DefaultValue)" required @onchange="@(e => ProfileChanged(e, p.Name))" />
|
|
}
|
|
else
|
|
{
|
|
<input id="@p.Name" class="form-control" maxlength="@p.MaxLength" value="@GetProfileValue(p.Name, p.DefaultValue)" @onchange="@(e => ProfileChanged(e, p.Name))" />
|
|
}
|
|
</div>
|
|
</div>
|
|
}
|
|
|
|
</div>
|
|
</div>
|
|
}
|
|
</TabPanel>
|
|
</TabStrip>
|
|
<br />
|
|
<br />
|
|
<button type="button" class="btn btn-success" @onclick="SaveUser">@SharedLocalizer["Save"]</button>
|
|
<NavLink class="btn btn-secondary" href="@NavigateUrl()">@SharedLocalizer["Cancel"]</NavLink>
|
|
|
|
@code {
|
|
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 string email = string.Empty;
|
|
private string displayname = string.Empty;
|
|
private List<Profile> profiles;
|
|
private Dictionary<string, string> settings;
|
|
private string category = string.Empty;
|
|
|
|
public override SecurityAccessLevel SecurityAccessLevel => SecurityAccessLevel.Edit;
|
|
|
|
protected override async Task OnInitializedAsync()
|
|
{
|
|
try
|
|
{
|
|
_togglepassword = SharedLocalizer["ShowPassword"];
|
|
profiles = await ProfileService.GetProfilesAsync(ModuleState.SiteId);
|
|
settings = new Dictionary<string, string>();
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await logger.LogError(ex, "Error Loading User Profile {Error}", ex.Message);
|
|
AddModuleMessage(Localizer["Error.Profile.Load"], MessageType.Error);
|
|
}
|
|
}
|
|
|
|
private string GetProfileValue(string SettingName, string DefaultValue)
|
|
{
|
|
string value = SettingService.GetSetting(settings, SettingName, DefaultValue);
|
|
if (value.Contains("]"))
|
|
{
|
|
value = value.Substring(value.IndexOf("]") + 1);
|
|
}
|
|
return value;
|
|
}
|
|
|
|
private async Task SaveUser()
|
|
{
|
|
try
|
|
{
|
|
if (username != string.Empty && _password != string.Empty && confirm != string.Empty && email != string.Empty && ValidateProfiles())
|
|
{
|
|
if (_password == confirm)
|
|
{
|
|
var user = await UserService.GetUserAsync(username, PageState.Site.SiteId);
|
|
if (user == null)
|
|
{
|
|
user = new User();
|
|
user.SiteId = PageState.Site.SiteId;
|
|
user.Username = username;
|
|
user.Password = _password;
|
|
user.Email = email;
|
|
user.DisplayName = string.IsNullOrWhiteSpace(displayname) ? username : displayname;
|
|
user.PhotoFileId = null;
|
|
|
|
user = await UserService.AddUserAsync(user);
|
|
|
|
if (user != null)
|
|
{
|
|
await SettingService.UpdateUserSettingsAsync(settings, user.UserId);
|
|
await logger.LogInformation("User Created {User}", user);
|
|
NavigationManager.NavigateTo(NavigateUrl());
|
|
}
|
|
else
|
|
{
|
|
await logger.LogError("Error Adding User {Username} {Email}", username, email);
|
|
AddModuleMessage(Localizer["Error.User.AddCheckPass"], MessageType.Error);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
AddModuleMessage(Localizer["Message.Username.Exists"], MessageType.Warning);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
AddModuleMessage(Localizer["Message.Password.NoMatch"], MessageType.Warning);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
AddModuleMessage(Localizer["Message.Required.ProfileInfo"], MessageType.Warning);
|
|
}
|
|
}
|
|
catch (Exception ex)
|
|
{
|
|
await logger.LogError(ex, "Error Adding User {Username} {Email} {Error}", username, email, ex.Message);
|
|
AddModuleMessage(Localizer["Error.User.Add"], MessageType.Error);
|
|
}
|
|
}
|
|
|
|
private bool ValidateProfiles()
|
|
{
|
|
bool valid = true;
|
|
foreach (Profile profile in profiles)
|
|
{
|
|
if (string.IsNullOrEmpty(SettingService.GetSetting(settings, profile.Name, string.Empty)) && !string.IsNullOrEmpty(profile.DefaultValue))
|
|
{
|
|
settings = SettingService.SetSetting(settings, profile.Name, profile.DefaultValue);
|
|
}
|
|
if (profile.IsRequired && string.IsNullOrEmpty(SettingService.GetSetting(settings, profile.Name, string.Empty)))
|
|
{
|
|
valid = false;
|
|
}
|
|
}
|
|
return valid;
|
|
}
|
|
|
|
private void ProfileChanged(ChangeEventArgs e, string SettingName)
|
|
{
|
|
var value = (string)e.Value;
|
|
settings = SettingService.SetSetting(settings, SettingName, value);
|
|
}
|
|
|
|
private void TogglePassword()
|
|
{
|
|
if (_passwordtype == "password")
|
|
{
|
|
_passwordtype = "text";
|
|
_togglepassword = SharedLocalizer["HidePassword"];
|
|
}
|
|
else
|
|
{
|
|
_passwordtype = "password";
|
|
_togglepassword = SharedLocalizer["ShowPassword"];
|
|
}
|
|
}
|
|
}
|