using System;
using System.Collections.ObjectModel;
using System.ComponentModel.DataAnnotations;
namespace IssueVision.Data.Web
{
/// <summary>
/// User class client-side extensions
/// </summary>
public partial class User
{
/// <summary>
/// Returns each user in the format of "user (FirstName LastName)"
/// </summary>
public string DisplayName
{
get
{
if (this._name == null)
return " ";
else
return this._name + " (" + this._firstName + " " + this._lastName + ")";
}
}
/// <summary>
/// Returns each user type in the format of "Admin" or "User"
/// </summary>
public string DisplayUserType
{
get
{
if (this.UserType == "A")
{
// Admin User
return IssueVisionServiceConstant.UserTypeAdmin;
}
else if (this.UserType == "U")
{
// Normal User
return IssueVisionServiceConstant.UserTypeUser;
}
else
return string.Empty;
}
set
{
if (value != null)
{
if (value.Contains(IssueVisionServiceConstant.UserTypeAdmin))
{
// Admin User
this.UserType = "A";
}
else if (value.Contains(IssueVisionServiceConstant.UserTypeUser))
{
// Normal User
this.UserType = "U";
}
else
this.UserType = String.Empty;
}
}
}
/// <summary>
/// Stores the new password the user entered in the MyProfile UI,
/// even if it is invalid. This way we can validate the new password
/// confirmation adequately all the times
/// </summary>
public string ActualNewPassword { get; set; }
[Display(Name = "NewPasswordConfirmationLabel", ResourceType = typeof(IssueVisionResources))]
[Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ErrorResources))]
[CustomValidation(typeof(User), "CheckNewPasswordConfirmation")]
public string NewPasswordConfirmation
{
get
{
return this._newPasswordConfirmation;
}
set
{
this.ValidateProperty("NewPasswordConfirmation", value);
this._newPasswordConfirmation = value;
this.RaisePropertyChanged("NewPasswordConfirmation");
}
}
private string _newPasswordConfirmation;
/// <summary>
/// Stores the password answer the user entered in the MyProfile UI,
/// even if it is invalid. This way we can validate the password answer
/// confirmation adequately all the times
/// </summary>
public string ActualPasswordAnswer { get; set; }
[Display(Name = "PasswordAnswerConfirmationLabel", ResourceType = typeof(IssueVisionResources))]
[Required(ErrorMessageResourceName = "ValidationErrorRequiredField", ErrorMessageResourceType = typeof(ErrorResources))]
[CustomValidation(typeof(User), "CheckPasswordAnswerConfirmation")]
public string PasswordAnswerConfirmation
{
get
{
return this._passwordAnswerConfirmation;
}
set
{
this.ValidateProperty("PasswordAnswerConfirmation", value);
this._passwordAnswerConfirmation = value;
this.RaisePropertyChanged("PasswordAnswerConfirmation");
}
}
private string _passwordAnswerConfirmation;
/// <summary>
/// Custom validation of whether new password and confirm password match
/// </summary>
/// <param name="newPasswordConfirmation"></param>
/// <param name="validationContext"></param>
/// <returns></returns>
public static ValidationResult CheckNewPasswordConfirmation(string newPasswordConfirmation, ValidationContext validationContext)
{
User currentUser = (User)validationContext.ObjectInstance;
if (!string.IsNullOrEmpty(currentUser.ActualNewPassword) &&
!string.IsNullOrEmpty(newPasswordConfirmation) &&
currentUser.ActualNewPassword != newPasswordConfirmation)
{
return new ValidationResult(ErrorResources.ValidationErrorPasswordConfirmationMismatch, new string[] { "NewPasswordConfirmation" });
}
return ValidationResult.Success;
}
/// <summary>
/// Custom validation of whether password answer and confirm password answer match
/// </summary>
/// <param name="passwordAnswerConfirmation"></param>
/// <param name="validationContext"></param>
/// <returns></returns>
public static ValidationResult CheckPasswordAnswerConfirmation(string passwordAnswerConfirmation, ValidationContext validationContext)
{
User currentUser = (User)validationContext.ObjectInstance;
if (!string.IsNullOrEmpty(currentUser.ActualPasswordAnswer) &&
!string.IsNullOrEmpty(passwordAnswerConfirmation) &&
currentUser.ActualPasswordAnswer != passwordAnswerConfirmation)
{
return new ValidationResult(ErrorResources.ValidationErrorPasswordAnswerConfirmationMismatch, new string[] { "PasswordAnswerConfirmation" });
}
return ValidationResult.Success;
}
/// <summary>
/// Try validate the specified property for User class
/// </summary>
/// <param name="propertyName"></param>
/// <returns></returns>
public bool TryValidateProperty(string propertyName)
{
if (string.IsNullOrEmpty(propertyName))
{
throw new ArgumentNullException("propertyName");
}
if (propertyName == "Name" || propertyName == "FirstName"
|| propertyName == "LastName" || propertyName == "Email"
|| propertyName == "Password" || propertyName == "NewPassword"
|| propertyName == "NewPasswordConfirmation" || propertyName == "PasswordQuestion"
|| propertyName == "PasswordAnswer" || propertyName == "PasswordAnswerConfirmation"
|| propertyName == "UserType")
{
ValidationContext context = new ValidationContext(this, null, null) { MemberName = propertyName };
var validationResults = new Collection<ValidationResult>();
if (propertyName == "Name")
return Validator.TryValidateProperty(this.Name, context, validationResults);
else if (propertyName == "FirstName")
return Validator.TryValidateProperty(this.FirstName, context, validationResults);
else if (propertyName == "LastName")
return Validator.TryValidateProperty(this.LastName, context, validationResults);
else if (propertyName == "Email")
return Validator.TryValidateProperty(this.Email, context, validationResults);
else if (propertyName == "Password")
return Validator.TryValidateProperty(this.Password, context, validationResults);
else if (propertyName == "NewPassword")
return Validator.TryValidateProperty(this.NewPassword, context, validationResults);
else if (propertyName == "NewPasswordConfirmation")
return Validator.TryValidateProperty(this.NewPasswordConfirmation, context, validationResults);
else if (propertyName == "PasswordQuestion")
return Validator.TryValidateProperty(this.PasswordQuestion, context, validationResults);
else if (propertyName == "PasswordAnswer")
return Validator.TryValidateProperty(this.PasswordAnswer, context, validationResults);
else if (propertyName == "PasswordAnswerConfirmation")
return Validator.TryValidateProperty(this.PasswordAnswerConfirmation, context, validationResults);
else if (propertyName == "UserType")
return Validator.TryValidateProperty(this.UserType, context, validationResults);
}
return false;
}
}
}