Commit 485cb3e1 authored by Geo Perez's avatar Geo Perez

Fixing another issue with error messages

parent 72ea39f9
......@@ -2,20 +2,28 @@
{
using System;
using Models;
using Microsoft.Extensions.Options;
// Sonar-Codacy thought we needed a static method here; and suggested dual default nulls was pointless.
internal class DebugPasswordChangeProvider : IPasswordChangeProvider
{
ApiErrorItem IPasswordChangeProvider.PerformPasswordChange(ChangePasswordModel model)
private readonly AppSettings _options;
public DebugPasswordChangeProvider(IOptions<AppSettings> options)
{
_options = options.Value;
}
public ApiErrorItem PerformPasswordChange(ChangePasswordModel model)
{
var username = model.Username.Substring(0, model.Username.IndexOf("@", StringComparison.Ordinal));
switch (username)
{
case "error":
return new ApiErrorItem { ErrorCode = ApiErrorCode.Generic, Message = "Error" };
return new ApiErrorItem { ErrorCode = ApiErrorCode.Generic, Message = _options.ClientSettings.Alerts.ErrorCaptcha };
case "notfound":
return new ApiErrorItem { ErrorCode = ApiErrorCode.UserNotFound, Message = "Invalid Username or Password" };
return new ApiErrorItem { ErrorCode = ApiErrorCode.UserNotFound, Message = _options.ClientSettings.Alerts.ErrorInvalidUser };
default:
return null;
}
......
......@@ -38,7 +38,7 @@ namespace Unosquare.PassCore.Web.Helpers
// Check if the user principal exists
if (userPrincipal == null)
{
return new ApiErrorItem { ErrorCode = ApiErrorCode.UserNotFound, Message = _options.ClientSettings.Alerts.ErrorInvalidUserOrPassword };
return new ApiErrorItem { ErrorCode = ApiErrorCode.UserNotFound, Message = _options.ClientSettings.Alerts.ErrorInvalidUser };
}
// Check if password change is allowed
......@@ -46,6 +46,18 @@ namespace Unosquare.PassCore.Web.Helpers
{
return new ApiErrorItem { ErrorCode = ApiErrorCode.ChangeNotPermitted, Message = _options.ClientSettings.Alerts.ErrorPasswordChangeNotAllowed };
}
// Verify user is not a member of an excluded group
if (_options.ClientSettings.CheckRestrictedAdGroups)
{
foreach (var userPrincipalAuthGroup in userPrincipal.GetAuthorizationGroups())
{
if (_options.ClientSettings.RestrictedADGroups.Contains(userPrincipalAuthGroup.Name))
{
return new ApiErrorItem { ErrorCode = ApiErrorCode.ChangeNotPermitted, Message = _options.ClientSettings.Alerts.ErrorPasswordChangeNotAllowed };
}
}
}
// Validate user credentials
if (principalContext.ValidateCredentials(model.Username, model.CurrentPassword) == false)
......@@ -65,18 +77,6 @@ namespace Unosquare.PassCore.Web.Helpers
}
}
// Verify user is not a member of an excluded group
if (_options.ClientSettings.CheckRestrictedAdGroups)
{
foreach (var userPrincipalAuthGroup in userPrincipal.GetAuthorizationGroups())
{
if (_options.ClientSettings.RestrictedADGroups.Contains(userPrincipalAuthGroup.Name))
{
return new ApiErrorItem { ErrorCode = ApiErrorCode.ChangeNotPermitted, Message = _options.ClientSettings.Alerts.ErrorPasswordChangeNotAllowed };
}
}
}
// Change the password via 2 different methods. Try SetPassword if ChangePassword fails.
try
{
......
......@@ -66,7 +66,7 @@ namespace Unosquare.PassCore.Web.Models
public string ErrorPasswordChangeNotAllowed { get; set; }
public string SuccessAlertBody { get; set; }
public string SuccessAlertTitle { get; set; }
public string ErrorInvalidUserOrPassword { get; set; }
public string ErrorInvalidUser { get; set; }
public string ErrorCaptcha { get; set; }
}
......
......@@ -59,7 +59,7 @@
"ErrorPasswordChangeNotAllowed": "You are not allowed to change your password. Please contact your system administrator.",
"ErrorInvalidCredentials": "You need to provide the correct current password.",
"ErrorInvalidDomain": "You have supplied an invalid domain to logon to.",
"ErrorInvalidUserOrPassword": "Invalid username or password.",
"ErrorInvalidUser": "We could not find your user account.",
"ErrorCaptcha": "Could not verify you are not a robot"
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment