149 lines
5.8 KiB
C#
149 lines
5.8 KiB
C#
using System;
|
|
using System.Collections.Generic;
|
|
using System.ComponentModel.DataAnnotations;
|
|
using System.Linq;
|
|
using System.Web;
|
|
using EnVisage.Code;
|
|
using System.Web.Mvc;
|
|
using EnVisage.Code.BLL;
|
|
|
|
namespace EnVisage.Models
|
|
{
|
|
[Bind(Include = "Id,UserName,FirstName,LastName,Email,Phone,PreferredResourceAllocation,PreferredTotalsDisplaying,ShowAutomaticViews,Discriminator,CompaniesWatcher,CompaniesContributor,WorkFlowRoles")]
|
|
public class UserModel :IBaseModel<AspNetUser>, IValidatableObject
|
|
{
|
|
public UserModel()
|
|
{
|
|
|
|
this.PreferredTotalsDisplaying = true;
|
|
this.PasswordResetRequests = new HashSet<PasswordResetRequest>();
|
|
this.User2Team = new HashSet<User2Team>();
|
|
this.AspNetRoles = new HashSet<AspNetRole>();
|
|
this.CompaniesWatcher = new List<Guid>();
|
|
this.CompaniesContributor = new List<Guid>();
|
|
this.WorkFlowRoles = new List<Guid>();
|
|
|
|
}
|
|
|
|
public UserModel(AspNetUser dbObj)
|
|
{
|
|
if (dbObj == null)
|
|
{
|
|
Id = Guid.Empty;
|
|
|
|
return;
|
|
}
|
|
|
|
if (!string.IsNullOrEmpty(dbObj.Id))
|
|
{
|
|
Id = Guid.Parse(dbObj.Id);
|
|
|
|
}
|
|
else
|
|
{
|
|
|
|
Id = Guid.Empty;
|
|
}
|
|
UserName = dbObj.UserName;
|
|
FirstName = dbObj.FirstName;
|
|
LastName = dbObj.LastName;
|
|
Email = dbObj.Email;
|
|
PasswordHash = dbObj.PasswordHash;
|
|
Phone = dbObj.PhoneNumber;
|
|
PreferredResourceAllocation = dbObj.PreferredResourceAllocation;
|
|
ShowAutomaticViews = dbObj.ShowAutomaticViews;
|
|
OverUnderCoefficient = dbObj.OverUnderCoefficient;
|
|
AspNetRoles = dbObj.AspNetRoles;
|
|
Discriminator = dbObj.Discriminator;
|
|
PasswordResetRequests = dbObj.PasswordResetRequests;
|
|
PreferredTotalsDisplaying = dbObj.PreferredTotalsDisplaying;
|
|
SecurityStamp = dbObj.SecurityStamp;
|
|
Type = dbObj.Type;
|
|
User2Team = dbObj.User2Team;
|
|
|
|
}
|
|
[Display(Name = "Workflow Roles")]
|
|
public List<Guid> WorkFlowRoles { get; set; }
|
|
[Display(Name = "User Name")]
|
|
[Required]
|
|
[Remote("IsUnique", "User", HttpMethod = "POST", AdditionalFields = "Id", ErrorMessage = "The user name entered is already in use please try a different user name")]
|
|
// [RegularExpression(@"^[a-zA-Z0-9_.@\s]+$",
|
|
[RegularExpression(@"^[^ ]*",
|
|
ErrorMessage = "Please make sure that the user name does not contain a space.")]
|
|
public string UserName { get; set; }
|
|
public Guid Id { get; set; }
|
|
public string PasswordHash { get; set; }
|
|
public string SecurityStamp { get; set; }
|
|
public string Discriminator { get; set; }
|
|
[Required]
|
|
[Display(Name = "Email Address")]
|
|
[EmailAddress(ErrorMessage = "Invalid Email Address")]
|
|
[StringLength(512)]
|
|
public string Email { get; set; }
|
|
[Display(Name = "Phone Number")]
|
|
[StringLength(80)]
|
|
public string Phone { get; set; }
|
|
public short Type { get; set; }
|
|
public bool PreferredResourceAllocation { get; set; }
|
|
public bool PreferredTotalsDisplaying { get; set; }
|
|
[Display(Name = "Automatic Views for my Business Units")]
|
|
public bool ShowAutomaticViews { get; set; }
|
|
public decimal OverUnderCoefficient { get; set; }
|
|
[Required]
|
|
[Display(Name = "First Name")]
|
|
[StringLength(40)]
|
|
public string FirstName { get; set; }
|
|
[Required]
|
|
[Display(Name = "Last Name")]
|
|
[StringLength(40)]
|
|
public string LastName { get; set; }
|
|
[Display(Name = "Watcher to Business Units")]
|
|
public List<Guid> CompaniesWatcher { get; set; }
|
|
[Display(Name = "Contributor to Business Units")]
|
|
public List<Guid> CompaniesContributor { get; set; }
|
|
|
|
public virtual ICollection<PasswordResetRequest> PasswordResetRequests { get; set; }
|
|
public virtual ICollection<User2Team> User2Team { get; set; }
|
|
public virtual ICollection<AspNetRole> AspNetRoles { get; set; }
|
|
|
|
|
|
/// <summary>
|
|
/// Copies data from model to DAL object.
|
|
/// </summary>
|
|
/// <param name="dbObj">A target DAL object.</param>
|
|
public void CopyTo(AspNetUser dbObj)
|
|
{
|
|
if (dbObj == null)
|
|
throw new ArgumentNullException();
|
|
string _ID = Id.ToString();
|
|
if (Id == Guid.Empty)
|
|
_ID = null;
|
|
if (string.IsNullOrEmpty(dbObj.Id) && !string.IsNullOrEmpty(_ID))
|
|
dbObj.Id = Id.ToString();
|
|
dbObj.UserName = UserName;
|
|
dbObj.FirstName = FirstName;
|
|
dbObj.LastName = LastName;
|
|
dbObj.Email = Email;
|
|
dbObj.PasswordHash = PasswordHash;
|
|
dbObj.PhoneNumber = Phone;
|
|
dbObj.PreferredResourceAllocation = PreferredResourceAllocation;
|
|
dbObj.ShowAutomaticViews = ShowAutomaticViews;
|
|
dbObj.AspNetRoles = AspNetRoles;
|
|
dbObj.Discriminator = Discriminator;
|
|
dbObj.PasswordResetRequests = PasswordResetRequests;
|
|
dbObj.PreferredTotalsDisplaying = PreferredTotalsDisplaying;
|
|
dbObj.OverUnderCoefficient = Convert.ToDecimal(OverUnderCoefficient);
|
|
dbObj.SecurityStamp = SecurityStamp;
|
|
dbObj.Type = Type;
|
|
dbObj.User2Team = User2Team;
|
|
|
|
}
|
|
|
|
public IEnumerable<ValidationResult> Validate(ValidationContext validationContext)
|
|
{
|
|
if (AccountManager.UserByEmailExists(Email, Id.ToString()))
|
|
yield return new ValidationResult(string.Format(Constants.ERROR_DUPLICATE_EMAIL), new[] { "Email" });
|
|
}
|
|
|
|
}
|
|
} |