/// <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); }
public static int GetAliasCount(CSSDataContext db, string username) { Login login = Login.FindLoginByUsernameOrCallsign(db, username); return(GetAliasCount(db, login)); }