fix issue adding existing user to a new site

This commit is contained in:
sbwalker 2024-08-29 17:53:11 -04:00
parent d718969cbd
commit 9620c5a98f
4 changed files with 35 additions and 96 deletions

View File

@ -14,7 +14,6 @@
<TabPanel Name="Identity" ResourceKey="Identity"> <TabPanel Name="Identity" ResourceKey="Identity">
@if (profiles != null) @if (profiles != null)
{ {
<ModuleMessage Message="@_passwordrequirements" Type="MessageType.Info" />
<div class="container"> <div class="container">
<div class="row mb-1 align-items-center"> <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> <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>
@ -22,24 +21,6 @@
<input id="username" class="form-control" @bind="@_username" /> <input id="username" class="form-control" @bind="@_username" />
</div> </div>
</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" tabindex="-1">@_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" tabindex="-1">@_togglepassword</button>
</div>
</div>
</div>
<div class="row mb-1 align-items-center"> <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> <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"> <div class="col-sm-9">
@ -123,12 +104,7 @@
@code { @code {
private bool _initialized = false; private bool _initialized = false;
private string _passwordrequirements;
private string _username = string.Empty; 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 _email = string.Empty;
private string _displayname = string.Empty; private string _displayname = string.Empty;
private string _notify = "True"; private string _notify = "True";
@ -142,8 +118,6 @@
{ {
try try
{ {
_passwordrequirements = await UserService.GetPasswordRequirementsAsync(PageState.Site.SiteId);
_togglepassword = SharedLocalizer["ShowPassword"];
profiles = await ProfileService.GetProfilesAsync(ModuleState.SiteId); profiles = await ProfileService.GetProfilesAsync(ModuleState.SiteId);
settings = new Dictionary<string, string>(); settings = new Dictionary<string, string>();
_initialized = true; _initialized = true;
@ -169,39 +143,32 @@
{ {
try try
{ {
if (_username != string.Empty && _password != string.Empty && _confirm != string.Empty && _email != string.Empty) if (_username != string.Empty && _email != string.Empty)
{ {
if (_password == _confirm) if (ValidateProfiles())
{ {
if (ValidateProfiles()) var user = new User();
user.SiteId = PageState.Site.SiteId;
user.Username = _username;
user.Password = ""; // will be auto generated
user.Email = _email;
user.DisplayName = string.IsNullOrWhiteSpace(_displayname) ? _username : _displayname;
user.PhotoFileId = null;
user.SuppressNotification = !bool.Parse(_notify);
user = await UserService.AddUserAsync(user);
if (user != null)
{ {
var user = new User(); await SettingService.UpdateUserSettingsAsync(settings, user.UserId);
user.SiteId = PageState.Site.SiteId; await logger.LogInformation("User Created {User}", user);
user.Username = _username; NavigationManager.NavigateTo(NavigateUrl());
user.Password = _password; }
user.Email = _email; else
user.DisplayName = string.IsNullOrWhiteSpace(_displayname) ? _username : _displayname; {
user.PhotoFileId = null; await logger.LogError("Error Adding User {Username} {Email}", _username, _email);
user.SuppressNotification = !bool.Parse(_notify); AddModuleMessage(Localizer["Error.User.AddCheckPass"], MessageType.Error);
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.Password.NoMatch"], MessageType.Warning);
} }
} }
else else
@ -252,18 +219,4 @@
var value = (string)e.Value; var value = (string)e.Value;
settings = SettingService.SetSetting(settings, SettingName, value); settings = SettingService.SetSetting(settings, SettingName, value);
} }
private void TogglePassword()
{
if (_passwordtype == "password")
{
_passwordtype = "text";
_togglepassword = SharedLocalizer["HidePassword"];
}
else
{
_passwordtype = "password";
_togglepassword = SharedLocalizer["ShowPassword"];
}
}
} }

View File

@ -117,12 +117,6 @@
<resheader name="writer"> <resheader name="writer">
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value> <value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
</resheader> </resheader>
<data name="Error.User.AddCheckPass" xml:space="preserve">
<value>Error Adding User. Please Ensure Password Meets Complexity Requirements And Username And Email Is Not Already In Use.</value>
</data>
<data name="Message.Password.NoMatch" xml:space="preserve">
<value>Passwords Entered Do Not Match</value>
</data>
<data name="Error.User.Add" xml:space="preserve"> <data name="Error.User.Add" xml:space="preserve">
<value>Error Adding User</value> <value>Error Adding User</value>
</data> </data>
@ -133,17 +127,11 @@
<value>Identity</value> <value>Identity</value>
</data> </data>
<data name="Message.Required.ProfileInfo" xml:space="preserve"> <data name="Message.Required.ProfileInfo" xml:space="preserve">
<value>You Must Provide A Username, Password, Email Address And All Required Profile Information</value> <value>You Must Provide A Username, Email Address And All Required Profile Information</value>
</data> </data>
<data name="Message.Username.Exists" xml:space="preserve"> <data name="Message.Username.Exists" xml:space="preserve">
<value>Username Already Exists</value> <value>Username Already Exists</value>
</data> </data>
<data name="Confirm.HelpText" xml:space="preserve">
<value>Please enter the password again to confirm it matches with the value above</value>
</data>
<data name="Confirm.Text" xml:space="preserve">
<value>Confirm Password:</value>
</data>
<data name="DisplayName.HelpText" xml:space="preserve"> <data name="DisplayName.HelpText" xml:space="preserve">
<value>The full name of the user</value> <value>The full name of the user</value>
</data> </data>
@ -156,21 +144,12 @@
<data name="Email.Text" xml:space="preserve"> <data name="Email.Text" xml:space="preserve">
<value>Email:</value> <value>Email:</value>
</data> </data>
<data name="Password.HelpText" xml:space="preserve">
<value>The user's password. Please choose a password which is sufficiently secure.</value>
</data>
<data name="Password.Text" xml:space="preserve">
<value>Password:</value>
</data>
<data name="Username.HelpText" xml:space="preserve"> <data name="Username.HelpText" xml:space="preserve">
<value>A unique username for a user. Note that this field can not be modified once it is saved.</value> <value>A unique username for a user. Note that this field can not be modified once it is saved.</value>
</data> </data>
<data name="Username.Text" xml:space="preserve"> <data name="Username.Text" xml:space="preserve">
<value>Username:</value> <value>Username:</value>
</data> </data>
<data name="Password.Placeholder" xml:space="preserve">
<value>Password</value>
</data>
<data name="Notify.HelpText" xml:space="preserve"> <data name="Notify.HelpText" xml:space="preserve">
<value>Indicate if new users should receive an email notification</value> <value>Indicate if new users should receive an email notification</value>
</data> </data>

View File

@ -147,11 +147,13 @@ namespace Oqtane.Controllers
if (_userPermissions.IsAuthorized(User, user.SiteId, EntityNames.User, -1, PermissionNames.Write, RoleNames.Admin)) if (_userPermissions.IsAuthorized(User, user.SiteId, EntityNames.User, -1, PermissionNames.Write, RoleNames.Admin))
{ {
user.EmailConfirmed = true; user.EmailConfirmed = true;
user.IsAuthenticated = true;
allowregistration = true; allowregistration = true;
} }
else else
{ {
user.EmailConfirmed = false; user.EmailConfirmed = false;
user.IsAuthenticated = false;
allowregistration = _sites.GetSite(user.SiteId).AllowRegistration; allowregistration = _sites.GetSite(user.SiteId).AllowRegistration;
} }

View File

@ -12,6 +12,7 @@ using Oqtane.Enums;
using Oqtane.Infrastructure; using Oqtane.Infrastructure;
using Oqtane.Models; using Oqtane.Models;
using Oqtane.Repository; using Oqtane.Repository;
using Oqtane.Security;
using Oqtane.Shared; using Oqtane.Shared;
namespace Oqtane.Managers namespace Oqtane.Managers
@ -145,13 +146,17 @@ namespace Oqtane.Managers
} }
else else
{ {
var result = await _identitySignInManager.CheckPasswordSignInAsync(identityuser, user.Password, false); succeeded = true;
succeeded = result.Succeeded; if (!user.IsAuthenticated)
if (!succeeded)
{ {
errors = "Password Not Valid For User"; var result = await _identitySignInManager.CheckPasswordSignInAsync(identityuser, user.Password, false);
succeeded = result.Succeeded;
if (!succeeded)
{
errors = "Password Not Valid For User";
}
user.EmailConfirmed = succeeded;
} }
user.EmailConfirmed = succeeded;
} }
if (succeeded) if (succeeded)