public static void Ban(this SqlConnection connection, IEnumerable <User> users)
 {
     foreach (var user in users)
     {
         connection.Ban(user);
     }
 }
Ejemplo n.º 2
0
        private static void UserLogin(Command command, Socket currentUser)
        {
            try
            {
                var nickname = command.GetCommandParameterByName("Nickname");
                var password = command.GetCommandParameterByName("Password");

                string response;

                if (_onlineUsers.ContainsKey(nickname.Text))
                {
                    response = "Already logged in.";
                }
                else
                {
                    if (command.Parameters.Count != 2)
                    {
                        response = "Invalid input, enter -h for help.";
                    }
                    else
                    {
                        using (SqlConnection connection = new SqlConnection(connectionString))
                        {
                            var user = connection.GetByNickname(nickname.Text);

                            if (user.BannedUntil > DateTime.Now)
                            {
                                response = $"You cannot log in until {user.BannedUntil}";
                            }
                            else
                            {
                                if (connection.Login(user, password.Text))
                                {
                                    _onlineUsers.TryAdd(nickname.Text, currentUser);

                                    response = "Successfully logged in";
                                }
                                else
                                {
                                    user.FailedLoginAttempts++;

                                    connection.UpdateFailedLoginAttempts(user, user.FailedLoginAttempts);

                                    if (user.FailedLoginAttempts >= 3)
                                    {
                                        connection.Ban(user);
                                        _bannedUsers.TryAdd(user.Nickname, currentUser);
                                        response = "Three failed attempts for log in, you have been banned, try again later.";
                                    }
                                    else
                                    {
                                        response = "Unsuccessful login.";
                                    }
                                }
                            }
                        }
                    }
                }

                byte[] bytes = Encoding.ASCII.GetBytes(response);
                currentUser.Send(bytes);
            }
            catch (ArgumentNullException e)
            {
                Console.WriteLine(e.Message);
            }
        }