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();
     }
 }
Esempio n. 2
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();
     }
 }
Esempio n. 3
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();
         }
     }
 }
        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();
        }
Esempio n. 5
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();
            }
        }
Esempio n. 6
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();
         }
     }
 }
Esempio n. 7
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();
            }
        }
Esempio n. 8
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();
     }
 }
Esempio n. 9
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();
        }
Esempio n. 10
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);
     }
 }
Esempio n. 11
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);
            }
        }
Esempio n. 12
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();
            }
        }
Esempio n. 13
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"));
        }