public static bool IsMachineInformationFromAVirtualMachine(CSSDataContext db, MachineInformation machineInformation, Login login) { var virtualMachineRecord = machineInformation.MachineValues.FirstOrDefault(p => db.VirtualMachineMarkers.Count(q => System.Data.Linq.SqlClient.SqlMethods.Like(p.Value, q.IdentifierMask) && (DeviceType) q.RecordTypeId == p.Type) > 0); if (virtualMachineRecord != null) { Log.Write(db, Enumerations.LogType.AuthenticationServer, "LoginID: " + login.Id + ", Name: " + login.Username + ", Virtual Machine Detected: name: " + virtualMachineRecord.Name + ", type: " + virtualMachineRecord.Type + ", value: " + virtualMachineRecord.Value + "."); return true; } return false; }
public static int GetAliasLimit(CSSDataContext db, Login login) { int aliasLimit = DefaultAliasLimit; if (login.HasRole(Common.Enumerations.RoleType.Administrator) == true || login.HasRole(Common.Enumerations.RoleType.SuperAdministrator) == true) aliasLimit = AdminAliasLimit; else if (login.HasRole(Common.Enumerations.RoleType.ZoneLeader) == true) aliasLimit = ZoneLeaderAliasLimit; else if (login.HasRole(Common.Enumerations.RoleType.Moderator) == true) aliasLimit = ModeratorAliasLimit; return aliasLimit; }
public static void NewMessage(CSSDataContext db, string subject, string message, Alias sender, Login recipient, DateTime sendDate) { db.PersonalMessages.InsertOnSubmit(new PersonalMessage() { Subject = subject, Message = message, SenderAliasId = sender.Id, LoginId = recipient.Id, DateCreated = DateTime.Now, DateExpires = DateTime.Now.AddYears(1), //Leaving at one year until discussion DateToSend = sendDate, DateViewed = null }); db.SubmitChanges(); }
public static int GetAliasCount(CSSDataContext db, Login login) { int aliasLimit = GetAliasLimit(db, login); int currentAliasCount = 0; if (login.Identity != null) { // If you want the count to be based on this login + all linked logins, uncomment this line. //currentAliasCount = login.Identity.Logins.SelectMany(p => p.Aliases).Count(); currentAliasCount = login.Aliases.Count(); if (currentAliasCount >= aliasLimit) return 0; } return aliasLimit - currentAliasCount; }
public static bool TryGetAuthenticatedLogin(CSSDataContext db, string username, string password, out Login login, out LoginStatus loginStatus) { loginStatus = LoginStatus.Authenticated; login = Login.FindLoginByUsernameOrCallsign(db, username); if (login == null) loginStatus = LoginStatus.InvalidCredentials; else if (login.IsBanned) loginStatus = LoginStatus.AccountLocked; else { CssMembershipProvider provider = new CssMembershipProvider(); if (provider.ValidateUser(login.Username, password) == false) loginStatus = LoginStatus.InvalidCredentials; else loginStatus = LoginStatus.Authenticated; } return loginStatus == LoginStatus.Authenticated; }
private Ban CreateBan(Login user, BanType banType) { TimeSpan? duration = Ban.CalculateDuration(user.Identity, banType); DateTime expirationDate; if (duration == TimeSpan.MaxValue) expirationDate = SqlDateTime.MaxValue.Value; else expirationDate = DateTime.Now.Add(duration.Value); return new Ban() { BannedByLoginId = user.Id, BanType = banType, BanTypeId = banType.Id, DateCreated = DateTime.Now, DateExpires = expirationDate, InEffect = true, Login = user }; }
private void attach_Logins(Login entity) { this.SendPropertyChanging(); entity.Identity = this; }
private void detach_Logins(Login entity) { this.SendPropertyChanging(); entity.Identity = null; }
partial void UpdateLogin(Login instance);
partial void DeleteLogin(Login instance);
/// <summary> /// Retrieve an unused ActiveKey for this Login. /// </summary> private static ActiveKey RetrieveUnusedKey(CSSDataContext db, Login login) { var random = new Random(); var earliest = DateTime.Now.AddHours(-ActiveKey.PreferredMinLifetime); var availableKeys = db.AvailableKey(login.Id) .Where(p => p.DateCreated > earliest && p.IsValid == true); var length = availableKeys.Count(); if (length == 0) return null; //Find a key (at random) which this Login has not already used. var index = random.Next(0, length - 1); var keyresult = availableKeys.Skip(index).Take(1).FirstOrDefault(); return db.ActiveKeys.FirstOrDefault(p => p.Id == keyresult.Id); }
partial void InsertLogin(Login 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 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(); } }
public static bool IsMachineInformationFromAVirtualMachine(CSSDataContext db, MachineInformation machineInformation, Login login) { var virtualMachineRecord = machineInformation.MachineValues.FirstOrDefault(p => db.VirtualMachineMarkers.Count(q => System.Data.Linq.SqlClient.SqlMethods.Like(p.Value, q.IdentifierMask) && (DeviceType)q.RecordTypeId == p.Type) > 0); if (virtualMachineRecord != null) { Log.Write(db, Enumerations.LogType.AuthenticationServer, "LoginID: " + login.Id + ", Name: " + login.Username + ", Virtual Machine Detected: name: " + virtualMachineRecord.Name + ", type: " + virtualMachineRecord.Type + ", value: " + virtualMachineRecord.Value + "."); return(true); } return(false); }
private void CreateTestData(out Login login1, out Login login2, out Login login3) { using (var db = new CSSDataContext()) { ClearDataForCallsign("User1"); ClearDataForCallsign("User2"); ClearDataForCallsign("User3"); //ClearDataForCallsign("User4"); login1 = CreateUser("User1", "Password1", "*****@*****.**", 10); login2 = CreateUser("User2", "Password2", "*****@*****.**", 20); login3 = CreateUser("User3", "Password3", "*****@*****.**", 13); // User1 and 3 should have some duplicate IPs //login4 = CreateUser("User4", "Password4", "*****@*****.**"); BanType banType = db.BanTypes.FirstOrDefault(); if (banType == null) { var banClass = new BanClass() { Name = "Auto" }; db.BanClasses.InsertOnSubmit(banClass); db.SubmitChanges(); banType = new BanType() { //BanClass = banClass, BanClassId = banClass.Id, BaseTimeInMinutes = 30, Description = "Harassment / Threats", IsIncremental = true, RocNumber = 1 }; db.BanTypes.InsertOnSubmit(banType); db.SubmitChanges(); } db.Bans.InsertOnSubmit(new Ban() { BannedByLoginId = 1, //BanType = banType, BanTypeId = banType.Id, DateCreated = DateTime.Now, DateExpires = DateTime.Now.AddDays(1), InEffect = true, Reason = "Pork Muffins!", LoginId = login2.Id }); db.SubmitChanges(); db.Bans.InsertOnSubmit(new Ban() { BannedByLoginId = 1, //BanType = banType, BanTypeId = banType.Id, DateCreated = DateTime.Now.AddDays(-30), DateExpires = DateTime.Now.AddDays(-29), InEffect = false, Reason = "Old ban.", LoginId = login2.Id, }); db.SubmitChanges(); MachineRecordType machineRecordType = db.MachineRecordTypes.FirstOrDefault(); if (machineRecordType == null) { machineRecordType = new MachineRecordType() { Id = 1, Name = "Network" }; db.MachineRecordTypes.InsertOnSubmit(machineRecordType); machineRecordType = new MachineRecordType() { Id = 2, Name = "HardDisk" }; machineRecordType = new MachineRecordType() { Id = 3, Name = "EDID" }; machineRecordType = new MachineRecordType() { Id = 4, Name = "Serial" }; machineRecordType = new MachineRecordType() { Id = 5, Name = "Misc" }; db.MachineRecordTypes.InsertOnSubmit(machineRecordType); db.SubmitChanges(); } db.MachineRecords.InsertOnSubmit(new MachineRecord() { DeviceType = Allegiance.CommunitySecuritySystem.Common.Envelopes.AuthInfo.DeviceType.HardDisk, Identifier = "1234567890", LoginId = login3.Id, RecordTypeId = (int)Allegiance.CommunitySecuritySystem.Common.Envelopes.AuthInfo.DeviceType.HardDisk }); db.SubmitChanges(); db.MachineRecords.InsertOnSubmit(new MachineRecord() { DeviceType = Allegiance.CommunitySecuritySystem.Common.Envelopes.AuthInfo.DeviceType.Serial, Identifier = "ABCDEFGHIJKLMNOP", LoginId = login3.Id, RecordTypeId = (int)Allegiance.CommunitySecuritySystem.Common.Envelopes.AuthInfo.DeviceType.Serial }); db.SubmitChanges(); Poll poll = db.Polls.FirstOrDefault(); PollOption pollOption1 = null; if (poll == null) { poll = new Poll() { DateCreated = DateTime.Now, DateExpires = DateTime.Now.AddDays(30), Question = "This is the question.", LastRecalculation = DateTime.Now }; db.Polls.InsertOnSubmit(poll); db.SubmitChanges(); pollOption1 = new PollOption() { Option = "Option 1", PollId = poll.Id, VoteCount = 0 }; db.PollOptions.InsertOnSubmit(pollOption1); db.SubmitChanges(); } else { pollOption1 = db.PollOptions.First(); } db.PollVotes.InsertOnSubmit(new PollVote() { LoginId = login3.Id, PollOptionId = pollOption1.Id }); db.SubmitChanges(); } }
/// <summary> /// Check if the selected alias is available for this Login Account, /// if the alias is not previously associated with this Login, and it /// is available, create it. /// </summary> public static CheckAliasResult ValidateUsage(CSSDataContext db, Login login, bool allowCreate, string legacyPassword, ref string callsignWithTags, out Alias alias) { alias = null; //Parse Callsign var match = Regex.Match(callsignWithTags, string.Concat(@"^(?<token>\W)?(?<callsign>[a-z]\w+)(@(?<tag>\w+))?$"), RegexOptions.Compiled | RegexOptions.IgnoreCase); var token = match.Groups["token"].Value; var callsign = match.Groups["callsign"].Value; var tag = match.Groups["tag"].Value; if (callsign.Length < MinAliasLength || callsign.Length > MaxAliasLength) return CheckAliasResult.Unavailable; if (BadWords.ContainsBadWord(callsign) == true) return CheckAliasResult.ContainedBadWord; alias = db.Alias.FirstOrDefault(p => p.Callsign == callsign); IEnumerable<Group_Alias_GroupRole> group_roles = null; //Check if this callsign is not empty if (string.IsNullOrEmpty(callsign)) return CheckAliasResult.Unavailable; //Validate that alias is a member of group associated with this tag if (!string.IsNullOrEmpty(tag)) { if (alias == null) return CheckAliasResult.Unavailable; group_roles = alias.Group_Alias_GroupRoles .Where(p => p.Group.Tag == tag); if (group_roles.Count() == 0) return CheckAliasResult.Unavailable; //Override input tag tag = group_roles.Select(p => p.Group.Tag).FirstOrDefault(); } //Validate that the alias has the role which allows him to use this tag with this group if (!string.IsNullOrEmpty(token)) { if (alias == null) return CheckAliasResult.Unavailable; var tokenChar = token[0]; //User has selected a @Tag if (!string.IsNullOrEmpty(tag)) { if (group_roles.Any(p => p.GroupRole.Token == tokenChar) == false) return CheckAliasResult.Unavailable; } //User has not selected a @Tag else { group_roles = alias.Group_Alias_GroupRoles .Where(p => p.GroupRole.Token == tokenChar && !p.Group.IsSquad); if (group_roles.Count() == 0) return CheckAliasResult.Unavailable; } } //Check if we can create this alias if (alias == null) { if (login != null) { // Check that the user has not used up all their aliases. if (GetAliasCount(db, login) <= 0) return CheckAliasResult.AliasLimit; } CheckAliasResult result = CheckAliasResult.Available; if(Settings.Default.UseAsgsForLegacyCallsignCheck == true) result = ValidateLegacyCallsignUsage(callsign, legacyPassword); if (result != CheckAliasResult.Available) return result; if (allowCreate && login != null) { alias = new Alias() { Callsign = callsign, DateCreated = DateTime.Now, IsDefault = false, IsActive = true }; login.Aliases.Add(alias); db.SubmitChanges(); } else //Alias does not exist, and we cannot create it. return CheckAliasResult.Available; } //Check if the user has this alias else if(login != null) { int aliasID = alias.Id; if (!login.Identity.Logins.SelectMany(p => p.Aliases).Any(p => p.Id == aliasID)) return CheckAliasResult.Unavailable; } //Override input callsign callsignWithTags = string.Format("{0}{1}{2}", token, alias.Callsign, (!string.IsNullOrEmpty(tag)) ? "@" + tag : string.Empty); return CheckAliasResult.Registered; }