Example #1
0
        /// <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 System.Web.Security.MembershipCreateStatus TryCreateIdentity(CSSDataContext db, string username, string password, string email, out Identity createdIdentity)
        {
            createdIdentity = null;

            if (BadWords.ContainsBadWord(username) == true)
            {
                return(System.Web.Security.MembershipCreateStatus.UserRejected);
            }

            foreach (char letter in username.ToArray())
            {
                if (Char.IsLetterOrDigit(letter) == false && letter != '_')
                {
                    return(System.Web.Security.MembershipCreateStatus.UserRejected);
                }
            }

            if (username.Length < 3 || username.Length > 17)
            {
                return(System.Web.Security.MembershipCreateStatus.UserRejected);
            }

            Identity newIdentity = new Identity();

            newIdentity.DateLastLogin             = DateTime.Now;
            newIdentity.LastGlobalMessageDelivery = DateTime.Now;

            // See if the login belongs to this user.
            var login = Login.FindLoginByUsernameOrCallsign(db, username, password);

            if (login == null)
            {
                // See if the login belongs to any user.
                if (db.Logins.FirstOrDefault(p => p.Username == username) != null)
                {
                    return(System.Web.Security.MembershipCreateStatus.DuplicateUserName);
                }

                if (db.Alias.FirstOrDefault(p => p.Callsign == username) != null)
                {
                    return(System.Web.Security.MembershipCreateStatus.DuplicateUserName);
                }

                // login is available, create a new one.
                login = new Login()
                {
                    Username    = username,
                    Password    = PasswordHash.CreateHash(password),
                    Email       = email,
                    DateCreated = DateTime.Now
                };
            }

            string callsignWithTags    = username;
            Alias  existingAlias       = null;
            var    aliasValidateResult = Alias.ValidateUsage(db, login, false, password, ref callsignWithTags, out existingAlias);

            if (aliasValidateResult == CheckAliasResult.InvalidLegacyPassword)
            {
                return(System.Web.Security.MembershipCreateStatus.InvalidPassword);
            }

            else if (Alias.ValidateUsage(db, login, false, password, ref callsignWithTags, out existingAlias) != CheckAliasResult.Available)
            {
                return(System.Web.Security.MembershipCreateStatus.InvalidUserName);
            }

            List <Alias> aliases = Alias.ListAliases(db, username);

            // Make sure no one else has this username as an alias.
            if (aliases.Count > 0)
            {
                return(System.Web.Security.MembershipCreateStatus.DuplicateUserName);
            }

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

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

            db.Identities.InsertOnSubmit(newIdentity);

            // Must be done by the caller.
            //db.SubmitChanges();

            createdIdentity = newIdentity;

            return(System.Web.Security.MembershipCreateStatus.Success);
        }
        /// <summary>
        /// Find the account associated with the input alias.
        /// </summary>
        private static Login FindLoginByCallsign(CSSDataContext db, string callsign)
        {
            string cleanCallsign = Alias.GetCallsignFromStringWithTokensAndTags(db, callsign);

            return(db.Logins.FirstOrDefault(p => p.Aliases.Any(q => q.Callsign == cleanCallsign)));
        }