using System; using System.Collections.Generic; using System.Linq; using System.Web; using System.Web.Mvc; using System.ComponentModel.DataAnnotations; using Sleis.Validation.Attribute; using Sleis.Utility; using Sleis.ViewModels; namespace Sleis.Models { public class UserModel : BaseIdentityModel { private bool secQuestionsValid = true; public static readonly string SESSION_USER_KEY = "USER_STATE"; [Required(ErrorMessage=Constants.RequiredErrorMessage), RegularExpression(Constants.EmailFormatRegEx, ErrorMessage = Constants.FormatErrorMessage)] public virtual string Email { get; set; } [Required(ErrorMessage = Constants.RequiredErrorMessage)] public virtual string FullName { get; set; } public virtual SleisUserStatusType Status { get; set; } public virtual SleisUserTypeType Type { get; set; } public virtual List Roles { get; set; } public virtual List AllRoles { get; set; } public virtual List ContactPoints { get; set; } public virtual List ContactSecAnswers { get; set; } public virtual List Facilities { get; set; } public virtual string Title { get; set; } public virtual string Org { get; set; } public virtual bool IsElectronicSigVerified { get; set; } public virtual DateTime? LastLogin { get; set; } public virtual int FailedPasswordAttemptCount { get; set; } public virtual int FailedSecurityQuestionAnswerCount { get; set; } public virtual string AssignedRoles { get { Roles.Sort(); return RoleUtility.ToDescriptionString(Roles); } } public virtual bool IsAppAdmin { get { return Roles != null && Roles.Contains(AppUserRoleType.AgencyAdmin); } } public virtual string StatusName { get { return EnumUtility.GetDefaultDescriptionAsString(new SleisUserStatusType[] { Status }); } } public UserModel() { InitMode(); } internal UserModel(int id) : base(id) { InitMode(); } private void InitMode() { Roles = new List(); ContactPoints = new List(); ContactSecAnswers = new List(); } public virtual bool IsAgencyUser() { return this.Type == SleisUserTypeType.Agency; } public virtual bool HasFiveUniqueQuestionsAndAnswers() { int qCount = 0; int aCount = 0; List questions = new List(); if (secQuestionsValid == false) return false; foreach (UserAnswerModel answer in ContactSecAnswers) { if (!questions.Contains(answer.Question) && !string.IsNullOrWhiteSpace(answer.Question)) { ++qCount; questions.Add(answer.Question); } if (!string.IsNullOrWhiteSpace(answer.Answer)) ++aCount; } return (aCount >= 5 && qCount >= 5); } public virtual void InvalidateSecurityQuestions() { secQuestionsValid = false; } /// /// Returns true if current user is either an agency editor or facility editor. /// /// public virtual bool IsEditor() { return AllRoles.Exists(x => x == AppUserRoleType.AgencyEditor || x == AppUserRoleType.FacilityEditor || x == AppUserRoleType.AgencyAdmin || x == AppUserRoleType.FacilityAdmin); } public override IEnumerable Validate(ValidationContext ctx) { List errs = new List(); if (Type.Equals(SleisUserTypeType.Undefined)) { errs.Add(new ValidationResult("Type not defined", new string[] { "Type" })); } if (ContactSecAnswers.Count > 0 && !IsAgencyUser()) { List questions = new List(); int qIndex = 0; foreach (UserAnswerModel answer in ContactSecAnswers) { if (questions.Contains(answer.Question)) { string[] propName = new string[] { String.Format("ContactSecAnswers[{0}].Question", qIndex) }; errs.Add(new ValidationResult("Electronic Signature Challenge Questions must be unique in your profile.", propName)); } else { questions.Add(answer.Question); } qIndex++; } } return errs; } } }