public static void DeleteUser(User deletingUser)
 {
     if (Menus.HorizontalMenu("\n\n\tAre you sure you want to delete this user?", new List <string> {
         "Yes", "No"
     }).Contains('Y'))
     {
         using (var database = new DatabaseStuff())
         {
             // When deleting a user you have to erase their emails and chat messages as well
             database.Users.Remove(database.Users.Single(i => i.UserName == deletingUser.UserName));
             var deletingMessages = database.Messages.Where(i => i.ReceiverId == deletingUser.Id || i.SenderId == deletingUser.Id);
             foreach (Message deletingMessage in deletingMessages)
             {
                 database.Messages.Remove(deletingMessage);
             }
             var deletingChatMessages = database.Chat.Where(i => i.SenderId == deletingUser.Id);
             foreach (ChatMessage deletingChatMessage in deletingChatMessages)
             {
                 database.Chat.Remove(deletingChatMessage);
             }
             database.SaveChanges();
         }
         Console.Clear();
         Console.WriteLine("\n\n\tUser successfully DELETED\n\n\tOK");
         Console.ReadKey();
     }
 }
Ejemplo n.º 2
0
        static void Main(string[] args)
        {
            Console.Title = "Chat@Bootcamp";
            // If database is empty create the 2 basic users firsthand.
            using (var database = new DatabaseStuff())
            {
                if (!database.Users.Any())
                {
                    try
                    {
                        Console.WriteLine("\n\n\tTrying to create database. Please wait..");
                        UserManager newAdmin = new UserManager("admin", "aDmI3$", true);
                        UserManager newGuest = new UserManager("guest", "guest", true);
                    }
                    catch (Exception e) { Console.WriteLine(e); }
                }
            }
            // Create the main object for managing user's functions
            UserManager activeUserManager;

            while (true)
            {
                // Everytime a user logs out the active user manager get's another object.
                activeUserManager = MenuFunctions.LoginScreen();
                MainMenu(activeUserManager);
            }
        }
Ejemplo n.º 3
0
 private bool UserNameAlreadyExists(string userName)
 {
     using (var database = new DatabaseStuff())
     {
         return(database.Users.Any(i => i.UserName == userName));
     }
 }
 public static void CreateNewUser()
 {
     using (var database = new DatabaseStuff())
     {
         UserManager newUser = new UserManager(true);
     }
 }
Ejemplo n.º 5
0
 public static void CommunicationHistory(UserManager activeUserManager)
 {
     using (var database = new DatabaseStuff())
     {
         List <Message> allMessages = database.Messages.Include("Sender").Include("Receiver").ToList();
         if (allMessages.Count == 0)
         {
             PrintNoContent("No messages yet");
             return;
         }
         Console.Clear();
         Console.WriteLine(StringsFormatted.History + '\n');
         try
         {
             foreach (Message message in allMessages)
             {
                 Console.WriteLine($"\t{message.TimeSent.ToString("dd/MM HH:mm")} " +
                                   $"{message.Sender.UserName} sent '{message.Title}' " +
                                   $"to {message.Receiver.UserName}");
             }
         }
         catch (Exception e) { PrintException(e); }
         Console.Write("\n\n\tOK");
         Console.ReadKey();
     }
 }
Ejemplo n.º 6
0
 private void SetAccessibility(bool isNewUser)
 {
     if (isNewUser)
     {
         if (UserName == "admin")
         {
             UserAccess = Accessibility.administrator;
         }
         else if (UserName == "guest")
         {
             UserAccess = Accessibility.guest;
         }
         else
         {
             UserAccess = Accessibility.user;
         }
     }
     else
     {
         using (var database = new DatabaseStuff())
         {
             UserAccess = database.Users.Single(u => u.UserName == UserName).UserAccess;
         }
     }
 }
        public static User SelectUser(UserManager activeUserManager)
        {
            List <string> selectUserItems = new List <string>();

            using (var database = new DatabaseStuff())
            {
                List <User> availableUsers = database.Users.ToList();

                try
                {
                    foreach (User user in availableUsers)
                    {
                        if (user.UserName != activeUserManager.UserName)
                        {
                            selectUserItems.Add(user.UserName);
                        }
                    }
                }
                catch (Exception e) { Console.WriteLine(e); }
                if (selectUserItems.Count == 0)
                {
                    Console.WriteLine("\n\n\tNo users in database!");
                    Console.ReadKey();
                    return(null);
                }
                selectUserItems.Add("Back");
                string selectedUserName = Menus.VerticalMenu(StringsFormatted.SelectUser, selectUserItems);
                if (selectedUserName.Contains("Back"))
                {
                    return(null);
                }
                Console.Clear();
                return(database.Users.Single(i => i.UserName == selectedUserName));
            }
        }
Ejemplo n.º 8
0
 private bool IDmatched(string password)
 {
     using (var database = new DatabaseStuff())
     {
         string passHash  = database.Users.Single(i => i.UserName == UserName).Password;
         string givenPass = PasswordHandling.PasswordToHash(PasswordHandling.ConvertToSecureString(password), UserName);
         return(givenPass == passHash);
     }
 }
Ejemplo n.º 9
0
        public static void MainMenu(UserManager activeUserManager)
        {
            while (true)
            {
                using (var database = new DatabaseStuff())
                {
                    // Probe the database for the nuber of unread messages in chat and unread mail
                    int unreadChatMessages = database.Users.Include("UnreadChatMessages").Single(c => c.UserName == activeUserManager.UserName).UnreadChatMessages.Count;

                    // Create the Menu items common to all users
                    List <string> mainMenuItems = new List <string> {
                        $"Chat ({unreadChatMessages})", "Personal Messages", "Log Out", "Exit"
                    };

                    // Add more options for User and Administrator access.
                    if (activeUserManager.UserAccess == Accessibility.administrator)
                    {
                        mainMenuItems.Insert(2, "Manage Users");
                        mainMenuItems.Insert(3, "Messages History");
                    }
                    else if (activeUserManager.UserAccess == Accessibility.user)
                    {
                        mainMenuItems.Insert(2, "Messages History");
                    }

                    // Acquire the choice of function from the user using a vertical menu
                    string userChoice = Menus.VerticalMenu(StringsFormatted.MainMenu, mainMenuItems);

                    // Check what the user chose and act accordingly
                    if (userChoice.Contains("Chat"))
                    {
                        MenuFunctions.ShowChat(activeUserManager);
                    }
                    else if (userChoice.Contains("Personal Messages"))
                    {
                        MenuFunctions.PersonalMessages(activeUserManager);
                    }
                    else if (userChoice.Contains("Messages History"))
                    {
                        MenuFunctions.CommunicationHistory(activeUserManager);
                    }
                    else if (userChoice.Contains("Manage Users"))
                    {
                        MenuFunctions.ManageUsers(activeUserManager);
                    }
                    else if (userChoice.Contains("Log Out"))
                    {
                        return;
                    }
                    else if (userChoice.Contains("Exit"))
                    {
                        Environment.Exit(0);
                    }
                }
            }
        }
Ejemplo n.º 10
0
 public static void DeleteChatMessage(int chosenMessageID)
 {
     if (Menus.HorizontalMenu("\n\n\tAre you sure you want to delete the selected messages?", new List <string> {
         "Yes", "No"
     }).Contains('Y'))
     {
         using (var database = new DatabaseStuff())
         {
             database.Chat.Remove(database.Chat.Single(c => c.Id == chosenMessageID));
             database.SaveChanges();
         }
     }
 }
Ejemplo n.º 11
0
 public static void UpdateChatMessage(int chosenMessageID)
 {
     Console.Clear();
     using (var database = new DatabaseStuff())
     {
         ChatMessage editedMessage = database.Chat.Single(c => c.Id == chosenMessageID);
         Console.Write("\n\n\tOLD TEXT: " + editedMessage.Text + "\n\n\tNEW TEXT: * ");
         editedMessage.Text = "* " + Console.ReadLine();
         Console.WriteLine("\n\n\tSAVE");
         Console.ReadKey(true);
         database.SaveChanges();
     }
 }
Ejemplo n.º 12
0
        public static void ChangeUserPermissions(User changingUser)
        {
            List <string> manageUserItems;

            if (changingUser.UserAccess == Accessibility.administrator)
            {
                manageUserItems = new List <string>()
                {
                    "downgrade to USER", "downgrade to GUEST", "Back"
                };
            }
            else if (changingUser.UserAccess == Accessibility.user)
            {
                manageUserItems = new List <string>()
                {
                    "upgrade to ADMINISTRATOR", "downgrade to GUEST", "Back"
                };
            }
            else
            {
                manageUserItems = new List <string>()
                {
                    "upgrade to ADMINISTRATOR", "upgrade to USER", "Back"
                };
            }
            string changeOfAccess = Menus.VerticalMenu($"\n\n\t{changingUser.UserName} is {changingUser.UserAccess}, how do you want to change his permissions?", manageUserItems);

            using (var database = new DatabaseStuff())
            {
                User changedUser = database.Users.Single(i => i.UserName == changingUser.UserName);
                if (changeOfAccess.Contains("ADMINISTRATOR"))
                {
                    changedUser.UserAccess = Accessibility.administrator;
                }
                else if (changeOfAccess.Contains("USER"))
                {
                    changedUser.UserAccess = Accessibility.user;
                }
                else if (changeOfAccess.Contains("GUEST"))
                {
                    changedUser.UserAccess = Accessibility.guest;
                }
                else
                {
                    return;
                }
                database.SaveChanges();
            }
            Console.Write($"\n\n\tYou did {changeOfAccess}, the user: {changingUser.UserName}\n\n\tOK");
            Console.ReadKey();
        }
Ejemplo n.º 13
0
        public void ClearUnreadChat()
        {
            using (var database = new DatabaseStuff())
            {
                TheUser = database.Users.Single(u => u.UserName == UserName);
                List <ChatMessage> UnreadChatMessages = database.Chat.Include("UnreadUsers").Where(c => c.SenderId != TheUser.Id).ToList();

                foreach (ChatMessage message in UnreadChatMessages)
                {
                    message.UnreadUsers.Remove(TheUser);
                }
                database.SaveChanges();
            }
        }
Ejemplo n.º 14
0
        public static int SelectChatMessage()
        {
            Console.Clear();
            string        chat         = StringsFormatted.Chat + "\n\n";
            List <string> chatMessages = new List <string>();

            using (var database = new DatabaseStuff())
            {
                List <ChatMessage> DBChatMessages = database.Chat.Include("Sender").ToList();
                foreach (ChatMessage chatMessage in DBChatMessages)
                {
                    chatMessages.Add($"{chatMessage.Id}. {chatMessage.TimeSent.ToString("dd/MM HH:mm")}   {chatMessage.Sender.UserName}: {chatMessage.Text}");
                }
            }
            return(int.Parse(Menus.VerticalMenu(chat, chatMessages).Split('.').First()));
        }
Ejemplo n.º 15
0
 public static void DeleteMessage(Message receivedMessage)
 {
     if (Menus.HorizontalMenu("\n\n\tAre you sure you want to delete this message?", new List <string> {
         "Yes", "No"
     }).Contains('Y'))
     {
         using (var database = new DatabaseStuff())
         {
             database.Messages.Remove(database.Messages.Single(i => i.Id == receivedMessage.Id));
             database.SaveChanges();
             Console.Clear();
             Console.WriteLine("\n\n\tMessage successfully DELETED\n\n\tOK");
             Console.ReadKey();
         }
     }
 }
Ejemplo n.º 16
0
        public static void PersonalMessages(UserManager activeUserManager)
        {
            while (true)
            {
                using (var database = new DatabaseStuff())
                {
                    // Probe the database for the sent and received personal messages
                    List <Message> receivedMessages = database.Messages.Include("Sender").Where(message => message.Receiver.Id == activeUserManager.TheUser.Id).ToList();
                    List <Message> sentMessages     = database.Messages.Include("Receiver").Where(message => message.Sender.Id == activeUserManager.TheUser.Id).ToList();

                    int countReceivedMessages       = receivedMessages.Count;
                    int countUnreadReceivedMessages = receivedMessages.Count(message => message.IsRead == false);
                    int countSentMessages           = sentMessages.Count;
                    int countUnreadSentMessages     = sentMessages.Count(message => message.IsRead == false);

                    string inbox = $"Inbox ({countUnreadReceivedMessages}/{countReceivedMessages})";
                    string sent  = $"Sent  ({countUnreadSentMessages}/{countSentMessages})";


                    // Create the Message Menu items common to all users
                    List <string> messageMenuItems = new List <string> {
                        $"Create NEW", $"{inbox}", $"{sent}", "Back"
                    };

                    // Acquire the choice of function from the user using a vertical menu
                    string userChoice = Menus.VerticalMenu(StringsFormatted.PersonalMessages, messageMenuItems);

                    if (userChoice.Contains("Create"))
                    {
                        PersonalMessageFunctions.SendEmail(activeUserManager);
                    }
                    else if (userChoice.Contains("Inbox"))
                    {
                        PersonalMessageFunctions.PresentAndManipulateMessage(activeUserManager, receivedMessages);
                    }
                    else if (userChoice.Contains("Sent"))
                    {
                        PersonalMessageFunctions.PresentAndManipulateMessage(activeUserManager, sentMessages, Received: false);
                    }
                    else
                    {
                        return;
                    }
                }
            }
        }
Ejemplo n.º 17
0
        public static void AddReplyToChat(UserManager activeUserManager, string chat)
        {
            using (var database = new DatabaseStuff())
            {
                // Access the active user from Database
                User thisUser = database.Users.Single(u => u.UserName == activeUserManager.UserName);
                // Rewrite the whole chat with the username added at the bottom
                Console.Clear();
                Console.Write(chat + "\n\n\t" + activeUserManager.UserName + ": ");

                // Collect all the other users in a list
                var    unreadUsers = database.Users.Where(u => u.UserName != thisUser.UserName).ToList();
                string replyText   = Console.ReadLine();
                // Create the new chat message
                database.Chat.Add(new ChatMessage(thisUser, replyText, unreadUsers));
                database.SaveChanges();
            }
        }
Ejemplo n.º 18
0
 public static void DeleteAllChatMessages()
 {
     if (Menus.HorizontalMenu("\n\n\tAre you sure you want to delete ALL chat messages?", new List <string> {
         "Yes", "No"
     }).Contains('Y'))
     {
         using (var database = new DatabaseStuff())
         {
             // When deleting all the contents of a table the ExecuteSqlCommand
             // is used for execution speed purposes.
             database.Database.ExecuteSqlCommand("DELETE FROM ChatMessages");
             database.SaveChanges();
         }
         Console.Clear();
         Console.WriteLine("\n\n\tCHAT successfully DELETED\n\n\tOK");
         Console.ReadKey();
     }
 }
Ejemplo n.º 19
0
        public static void ForwardMessage(UserManager activeUserManager, Message forwardMessage)
        {
            User receiver = ManageUserFunctions.SelectUser(activeUserManager);

            if (receiver is null)
            {
                return;
            }
            string  forwardTitle     = "FW: " + forwardMessage.Title;
            string  forwardBody      = forwardMessage.Body;
            Message forwardedMessage = new Message(activeUserManager.TheUser.Id, receiver.Id, forwardTitle, forwardBody);

            using (var database = new DatabaseStuff())
            {
                database.Messages.Add(forwardedMessage);
                database.SaveChanges();
            }
            Console.Clear();
            Console.WriteLine($"\n\n\tMessage successfully forwarded to {receiver.UserName}\n\n\tOK");
            Console.ReadKey();
        }
Ejemplo n.º 20
0
 public UserManager(string userName, string password, bool isNewUser = false)
 {
     LoginTries = 1;
     if (IsWrongUserName(userName, isNewUser))
     {
         AskUserName(isNewUser);
     }
     else
     {
         UserName = userName;
     }
     LoginTries = 1;
     if (IsWrongPassword(password, isNewUser))
     {
         AskPassword(isNewUser);
     }
     else
     {
         Password = PasswordHandling.PasswordToHash(PasswordHandling.ConvertToSecureString(password), UserName);
     }
     SetAccessibility(isNewUser);
     // If is new user create a user to database
     using (var database = new DatabaseStuff())
     {
         if (isNewUser)
         {
             try
             {
                 database.Users.Add(new User(UserName, Password, UserAccess));
                 database.SaveChanges();
             }
             catch (Exception e)
             {
                 Console.WriteLine(e.Message + " User could not get created");
             }
         }
         TheUser = database.Users.Single(u => u.UserName == UserName);
     }
 }
Ejemplo n.º 21
0
        public static void UpdateEmail(UserManager activeUserManager, Message editMessage)
        {
            Console.Write("\n\n\n\n\tNew Title: ");
            string newMessageTitle = Console.ReadLine();

            Console.Write("\n\tNew Body: ");
            string newMessageBody = Console.ReadLine();

            using (var database = new DatabaseStuff())
            {
                try
                {
                    Message newMessage = database.Messages.Single(m => m.Id == editMessage.Id);
                    newMessage.Title  = newMessageTitle;
                    newMessage.Body   = newMessageBody;
                    newMessage.IsRead = false;
                    database.SaveChanges();
                    Console.Write($"\n\n\tEmail updated successfully\n\n\tOK");
                }
                catch (Exception e) { MenuFunctions.PrintException(e); }
                Console.ReadKey(true);
            }
        }
Ejemplo n.º 22
0
        public static Message SelectMessage(UserManager activeUserManager, List <Message> Messages, bool Received)
        {
            List <string> selectMessageItems = new List <string>();
            string        receiverName;

            using (var database = new DatabaseStuff())
            {
                int UserId = database.Users.Single(i => i.UserName == activeUserManager.UserName).Id;

                foreach (Message message in Messages)
                {
                    if (Received)
                    {
                        receiverName = message.Sender.UserName;
                    }
                    else
                    {
                        receiverName = message.Receiver.UserName;
                    }

                    selectMessageItems.Add(CustomizeAppearanceOfMessage(message, receiverName, Received));
                }

                selectMessageItems.Add("Back");
                string oMessage = Menus.VerticalMenu(StringsFormatted.OpenMessage, selectMessageItems);

                if (oMessage.Contains("Back"))
                {
                    return(null);
                }

                int messageID = int.Parse(oMessage.Split('|')[1]);

                Console.Clear();
                return(database.Messages.Single(i => i.Id == messageID));
            }
        }
Ejemplo n.º 23
0
        public static void SendEmail(UserManager activeUserManager, User receiver = null, string replyTitle = "")
        {
            string title = "\n\n\tTitle: " + replyTitle;

            // NULL Receiver means it's NOT a REPLY it's just a NEW message.
            if (receiver is null)
            {
                receiver = ManageUserFunctions.SelectUser(activeUserManager);
            }
            if (receiver is null)
            {
                return;
            }

            Console.WriteLine(StringsFormatted.SendEmail);
            Console.Write(title);

            string MessageTitle = (title.Contains("RE")) ? replyTitle : Console.ReadLine();

            Console.Write("\n\tBody: ");
            string MessageBody = Console.ReadLine();

            using (var database = new DatabaseStuff())
            {
                int     senderId = database.Users.Single(i => i.UserName == activeUserManager.UserName).Id;
                Message email    = new Message(senderId, receiver.Id, MessageTitle, MessageBody);
                try
                {
                    database.Messages.Add(email);
                    database.SaveChanges();
                    Console.Write($"\n\n\tEmail sent successfully to {receiver.UserName}\n\n\tOK");
                }
                catch (Exception e) { MenuFunctions.PrintException(e); }
                Console.ReadKey();
            }
        }
Ejemplo n.º 24
0
 public static UserManager LoginScreen()
 {
     while (true)
     {
         string userChoice = Menus.HorizontalMenu(StringsFormatted.Welcome, new List <string> {
             "Sign Up", "Log In"
         });
         using (var database = new DatabaseStuff())
         {
             if (userChoice == "Log In")
             {
                 try
                 { return(SuccessfullLogin(isNewUser: false)); }
                 catch (Exception e) { PrintException(e); }
             }
             else
             {
                 try
                 { return(SuccessfullLogin(isNewUser: true)); }
                 catch (Exception e) { PrintException(e); }
             }
         }
     }
 }
Ejemplo n.º 25
0
        public static void PresentAndManipulateMessage(UserManager activeUserManager, List <Message> Messages, bool Received = true)
        {
            string userChoice;

            do
            {
                if (Messages.Count == 0)
                {
                    MenuFunctions.PrintNoContent("No Messages to View");
                    return;
                }

                Message selectedMessage = SelectMessage(activeUserManager, Messages, Received);

                if (selectedMessage is null)
                {
                    return;
                }

                string presentedMessage = StringsFormatted.ReadEmails
                                          + $"\n\n\tTitle: {selectedMessage.Title}"
                                          + $"\n\n\tBody:  {selectedMessage.Body}\n\n";

                List <string> messageOptions = new List <string> {
                    "Forward", "Delete", "Back"
                };

                if (Received)
                {
                    messageOptions.Insert(0, "Reply");
                }
                else
                {
                    messageOptions.Insert(0, "Edit");
                }

                userChoice = Menus.HorizontalMenu(presentedMessage, messageOptions);

                using (var database = new DatabaseStuff())
                {
                    Message readMessage = database.Messages.Single(m => m.Id == selectedMessage.Id);
                    if (Received)
                    {
                        readMessage.IsRead = true;
                    }
                    database.SaveChanges();
                    if (userChoice.Contains("Forward"))
                    {
                        ForwardMessage(activeUserManager, selectedMessage);
                    }
                    else if (userChoice.Contains("Reply"))
                    {
                        User toBeReplied = database.Users.Single(u => u.Id == readMessage.Sender.Id);
                        SendEmail(activeUserManager, toBeReplied, "RE: " + readMessage.Title);
                    }
                    else if (userChoice.Contains("Edit"))
                    {
                        UpdateEmail(activeUserManager, selectedMessage);
                    }
                    else if (userChoice.Contains("Delete"))
                    {
                        DeleteMessage(selectedMessage);
                        return;
                    }
                }
            }while (!userChoice.Contains("Back"));
        }
Ejemplo n.º 26
0
        public static void ShowChat(UserManager activeUserManager)
        {
            while (true)
            {
                Console.Clear();
                string chat = StringsFormatted.Chat + "\n\n";
                using (var database = new DatabaseStuff())
                {
                    // Access the active user from Database
                    User activeUser = database.Users.Single(u => u.UserName == activeUserManager.UserName);

                    // Get all the chat messages ordered increasingly and
                    // access the users haven't read them yet.
                    List <ChatMessage> chatMessages = database.Chat.Include("UnreadUsers").OrderBy(i => i.Id).ToList();

                    bool firstNewMessage = true;

                    foreach (ChatMessage message in chatMessages)
                    {
                        if (message.UnreadUsers.Contains(activeUser) && firstNewMessage)
                        {
                            chat           += "\n\t__________________________NEW__________________________\n";
                            firstNewMessage = false;
                        }
                        chat += "\n\t" + message.TimeSent.ToString("dd/MM HH:mm") + "   " +
                                (database.Users.Single(i => i.Id == message.SenderId).UserName.ToString()
                                 + ":").PadRight(15) + message.Text + '\n';
                    }
                    // Since by the next point the user read the new messages
                    activeUserManager.ClearUnreadChat();


                    List <string> chatOptions = new List <string> {
                        "Reply", "Back"
                    };
                    if (activeUser.UserAccess == Accessibility.administrator && database.Chat.Any())
                    {
                        chatOptions.Insert(1, "Edit");
                        chatOptions.Insert(2, "Delete All");
                    }
                    string userChoice = Menus.HorizontalMenu(chat, chatOptions);

                    // Does the user wish to leave?
                    if (userChoice.Contains("Back"))
                    {
                        break;
                    }

                    // Or edit the chat Messages
                    else if (userChoice.Contains("Edit"))
                    {
                        ChatFunctions.EditChatMessages();
                    }
                    // Maybe delete them all?
                    else if (userChoice.Contains("Delete All"))
                    {
                        ChatFunctions.DeleteAllChatMessages();
                    }
                    // Or add a reply
                    else
                    {
                        ChatFunctions.AddReplyToChat(activeUserManager, chat);
                    }
                }
            }
        }