partial void DeleteIdentity(Identity instance);
 partial void UpdateIdentity(Identity instance);
 partial void InsertIdentity(Identity instance);
        public static Login CreateUser(string user, string password, string email, int ipBaseIndex)
        {
            using (var db = new CSSDataContext())
            {
                var existingAlias = Alias.GetAliasByCallsign(db, user);
                if (existingAlias != null)
                {
                    db.Group_Alias_GroupRoles.DeleteAllOnSubmit(existingAlias.Login.Aliases.SelectMany(p => p.Group_Alias_GroupRoles));
                    db.PollVotes.DeleteAllOnSubmit(existingAlias.Login.Identity.Logins.SelectMany(p => p.PollVotes));
                    db.GroupMessage_Alias.DeleteAllOnSubmit(existingAlias.Login.Aliases.SelectMany(p => p.GroupMessage_Alias));
                    db.PersonalMessages.DeleteAllOnSubmit(existingAlias.Login.Aliases.SelectMany(p => p.PersonalMessages));

                    var loginsToDelete = existingAlias.Login.Identity.Logins.ToList();
                    var identityToDelete = existingAlias.Login.Identity;
                    //List<Identity> identiesToDelete = existingAlias.Login.Identity.Logins.SelectMany(p => p.Identity).ToList();

                    db.MachineRecords.DeleteAllOnSubmit(existingAlias.Login.Identity.MachineRecords);
                    db.SubmitChanges();

                    db.Alias.DeleteAllOnSubmit(existingAlias.Login.Identity.Logins.SelectMany(p  => p.Aliases));
                    db.SubmitChanges();

                    db.Logins.DeleteAllOnSubmit(loginsToDelete);
                    db.SubmitChanges();

                    db.LogIPs.DeleteAllOnSubmit(identityToDelete.LogIPs);
                    db.Identities.DeleteOnSubmit(identityToDelete);
                    db.SubmitChanges();
                }

                var identity = new Identity()
                {
                    DateLastLogin = DateTime.Now,
                    LastGlobalMessageDelivery = DateTime.Now
                };

                var login = new Login()
                {
                    Username    = user,
                    Password    = PasswordHash.CreateHash(password),
                    Email       = email,
                    DateCreated = DateTime.Now,
                };

                var alias = new Alias()
                {
                    Callsign    = user,
                    DateCreated = DateTime.Now,
                    IsDefault   = true,
                };

                login.Aliases.Add(alias);
                identity.Logins.Add(login);

                db.Identities.InsertOnSubmit(identity);

                for (int i = 0; i < 5; i++)
                {
                    identity.LogIPs.Add(new LogIP()
                    {
                        IPAddress = "192.168.1." + (ipBaseIndex + i).ToString(),
                        LastAccessed = DateTime.Now
                    });
                }

                db.SubmitChanges();

                return login;
            }
        }
        //public Login BanningLogin
        //{
        //    get { this.
        //}
        /// <summary>
        /// Calculate how long a person's ban will be based on the reason and who they are.
        /// http://web.archive.org/web/20070129051419/http://www.allegacademy.org/proposedbantime.shtml
        /// </summary>
        public static TimeSpan? CalculateDuration(Identity user, BanType banType)
        {
            TimeSpan? duration  = TimeSpan.MinValue;
            var days = banType.BanClass.Id == (int) Enumerations.BanClassType.Major ? 180 : 90;

            var recentBans = from b in user.Bans
                             where b.DateCreated > DateTime.Now.AddDays(-days)
                                && b.BanType != null
                                && b.BanType.BanClassId == banType.BanClassId
                             select b;

            var sameOffenses = from b in recentBans
                               where b.BanTypeId == banType.Id
                               select b;

            //Get # of the same infractions; Adding 1 for this instance
            var likeInfractions = sameOffenses.Count() + 1;
            var minutes = banType.BaseTimeInMinutes;

            //2nd or more infraction
            if(likeInfractions >= 2)
            {
                if (banType.BanClassId == (int)Enumerations.BanClassType.Minor)
                    minutes = (int)Math.Pow(banType.BaseTimeInMinutes, 2);
                else //Major
                    minutes = banType.BaseTimeInMinutes * 4;

                //Third or more infraction
                if(likeInfractions >= 3)
                {
                    if (banType.BanClassId == (int)Enumerations.BanClassType.Minor)
                        minutes *= 8;
                    else //Major
                        minutes *= 5;
                }
            }

            duration = TimeSpan.FromMinutes(minutes);

            //Calculate number of total infractions for this class; Adding 1 for this instance
            var totalInfractions = recentBans.Count() + 1;

            //If this is a minor ban, check if user has >= 6 minor bans
            if (banType.BanClassId == (int)Enumerations.BanClassType.Minor && (totalInfractions % 6 == 0))
            {
                switch (totalInfractions)
                {
                    case 6: //First-time habitual offender = 10 days
                        duration = TimeSpan.FromDays(10);
                        break;

                    case 12: //Second-time habitual offender = 30 days
                        duration = TimeSpan.FromDays(30);
                        break;

                    default: //Third or more time habitual offender = 90 days
                        duration = TimeSpan.FromDays(90);
                        break;
                }
            }

            //If this is a major ban, check if user has >= 4 major bans
            else if (banType.BanClassId == (int)Enumerations.BanClassType.Major && (totalInfractions % 4 == 0))
            {
                switch (totalInfractions)
                {
                    case 4: //First-time habitual offender = 30 days
                        duration = TimeSpan.FromDays(30);
                        break;

                    case 8: //Second-time habitual offender = 60 days
                        duration = TimeSpan.FromDays(60);
                        break;

                    default: //Third or more time habitual offender, results in permanent ban
                        duration = null;
                        break;
                }
            }

            // Check if the ban is permanent.
            if (banType.InfractionsBeforePermanentBan != null && totalInfractions > banType.InfractionsBeforePermanentBan.Value)
            {
                duration = TimeSpan.MaxValue;
            }

            return duration;
        }
        public static void CreateUser(string user, string password, string email)
        {
            using (var db = new CSSDataContext())
            {
                var identity = new Identity()
                {
                    DateLastLogin = DateTime.Now,
                    LastGlobalMessageDelivery = DateTime.Now
                };

                var login = new Login()
                {
                    Username    = user,
                    Password    = Hash(password),
                    Email       = email,
                    DateCreated = DateTime.Now,
                };

                var alias = new Alias()
                {
                    Callsign    = user,
                    DateCreated = DateTime.Now,
                    IsDefault   = true,
                    IsActive = true
                };

                login.Aliases.Add(alias);
                identity.Logins.Add(login);
                login.Lobby_Logins.Add(new Lobby_Login() { Lobby = db.Lobbies.First(p => p.Name == "Production") });

                db.Identities.InsertOnSubmit(identity);
                db.SubmitChanges();
            }
        }