/// <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); }
/// <summary> /// If the recipient is null, then the message will be sent to all users. /// </summary> /// <param name="db"></param> /// <param name="subject"></param> /// <param name="message"></param> /// <param name="recipient"></param> /// <param name="sendDate"></param> /// <param name="sender"></param> static public void NewMessage(CSSDataContext db, string subject, string message, string recipient, DateTime sendDate, Alias sender) { Group group = null; if (recipient != null) { group = db.Groups.FirstOrDefault(g => g.Name == recipient); if (group == null) { throw new Exception("Could not find associated group."); } } var msg = new GroupMessage() { Subject = subject, Message = message, Group = group, DateCreated = DateTime.Now, DateExpires = DateTime.Now.AddYears(1), //Leaving at one year until discussion DateToSend = sendDate, SenderAliasId = sender.Id }; if (recipient != null) { var aliases = db.Group_Alias_GroupRoles .Where(g => g.GroupId == group.Id); foreach (var alias in aliases) { msg.GroupMessage_Alias.Add(new GroupMessage_Alias() { AliasId = alias.AliasId, DateViewed = null }); } } db.GroupMessages.InsertOnSubmit(msg); db.SubmitChanges(); }
public static CheckAliasResult ValidateUsage(CSSDataContext db, ref string callsignWithTags, out Alias alias) { return(ValidateUsage(db, null, false, null, ref callsignWithTags, out alias)); }