Esempio n. 1
0
        public bool Register(string username, string password, string email)
        {
            FileAccess access = new FileAccess(adminFile);

            using (var db = new App_Context())
            {
                var isNull = ExistUser(username);

                if (isNull)
                {
                    var salt = Password.GetSalt();
                    var hash = Password.Hash(password, salt);

                    User user = new User {
                        Username = username,
                        Password = Convert.ToBase64String(hash),
                        Salt     = Convert.ToBase64String(salt),
                        Email    = email,
                        AccessId = AccessId.Simple
                    };

                    db.Users.Add(user);
                    db.SaveChanges();

                    access.WriteLogFile(username, password, email);
                    return(true);
                }
            }
            return(false);
        }
Esempio n. 2
0
        public void UpdateEmail(int id, string email)
        {
            FileAccess access = new FileAccess(adminFile);

            using (var db = new App_Context())
            {
                var result = db.Users.Find(id);

                if (result != null)
                {
                    result.Email = email;
                    access.WriteLogFile(id);
                    db.SaveChanges();
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.WriteLine("Update succesfully!");
                    Console.ResetColor();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("The Id doesn't match.Update failed.");
                    Console.ResetColor();
                }
            }
        }
Esempio n. 3
0
        public void UpdateUser(int id, AccessId accessid)
        {
            FileAccess access = new FileAccess(adminFile);

            using (var db = new App_Context())
            {
                var  result     = db.Users.Find(id);
                bool isTheAdmin = IsAdmin(id);
                if (result != null && !isTheAdmin)
                {
                    result.AccessId = accessid;
                    access.WriteLogFile(id);
                    db.SaveChanges();
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.WriteLine("Update succesfully!");
                    Console.ResetColor();
                }
                else if (result != null && isTheAdmin)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("Invalid Action.You try to change the Admin privilages.");
                    Console.ResetColor();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("The Id doesn't match.Update failed.");
                    Console.ResetColor();
                }
            }
        }
Esempio n. 4
0
        public void DeleteUser(User user)
        {
            using (var db = new App_Context())
            {
                var result = db.Users.Find(user.Id);
                IList <MessageDTO> messageDTOs = db.Messages.Where(m => m.ReceiverName == result.Username || m.SenderName == result.Username).ToList();

                string[] file;
                if (messageDTOs.Count > 0)
                {
                    db.Messages.RemoveRange(messageDTOs);
                    db.SaveChanges();
                }

                bool isTheAdmin = IsAdmin(user.Id);

                if (result != null && !isTheAdmin)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("The User will delete permanently.Do you want to continue? Y/N");
                    Console.ResetColor();
                    string x = Console.ReadLine();
                    if (x == "Y" || x == "y")
                    {
                        file = new string[] { $"====  User Deleted ====\r\nAt:{DateTime.Now} with Username:{result.Username}" };
                        File.AppendAllLines(adminFile, file);
                        db.Users.Remove(result);
                        db.SaveChanges();
                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                        Console.WriteLine("The user deleted succesfully.");
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.WriteLine("Deletion of the user has been canceled");
                        Console.ResetColor();
                    }
                }
                else if (result != null && isTheAdmin)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("Invalid Action.You try to delete the Administrator.");
                    Console.ResetColor();
                }
            }
        }
Esempio n. 5
0
        public void CreateMessage(string message, string sender, string receiver)
        {
            FileAccess fileAccess = new FileAccess(path);

            fileAccess.WriteMessage(sender, receiver, message);

            using (var db = new App_Context())
            {
                IList <User> sendDetails  = db.Users.Where(i => i.Username == sender).ToList();
                IList <User> recevDetails = db.Users.Where(r => r.Username == receiver).ToList();

                if (recevDetails.Count == 0)
                {
                    Console.ForegroundColor = ConsoleColor.DarkYellow;
                    Console.WriteLine("The recipient you choose does not exist.");
                    Console.ResetColor();
                }
                else
                {
                    foreach (User item in sendDetails)
                    {
                        foreach (User item_2 in recevDetails)
                        {
                            MessageDTO messageDTO = new MessageDTO
                            {
                                Message      = message,
                                SenderName   = sender,
                                ReceiverName = receiver,
                                DateTime     = DateTime.Now,
                                Sender       = item,
                                Receiver     = item_2
                            };

                            db.Messages.Add(messageDTO);
                            Console.ForegroundColor = ConsoleColor.DarkGreen;
                            Console.WriteLine("The message was send it successfully.");
                            Console.ResetColor();
                            db.SaveChanges();
                        }
                    }
                }
            }
        }
Esempio n. 6
0
        public void EditMessage(int messId, string message)
        {
            using (var db = new App_Context())
            {
                var _messId = db.Messages.Find(messId);

                if (_messId != null)
                {
                    _messId.Message  = message;
                    _messId.DateTime = DateTime.Now;
                    db.SaveChanges();
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.WriteLine("Update succesfully!");
                    Console.ResetColor();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("Update Failed.");
                    Console.ResetColor();
                }
            }
        }
Esempio n. 7
0
        public void UpdateUser(int id, string pass)
        {
            FileAccess access = new FileAccess(adminFile);

            using (var db = new App_Context())
            {
                var result = db.Users.Find(id);

                bool isTheAdmin = IsAdmin(id);

                if (result != null && !isTheAdmin)
                {
                    var salt = Password.GetSalt();
                    var hash = Password.Hash(pass, salt);
                    result.Password = Convert.ToBase64String(hash);
                    result.Salt     = Convert.ToBase64String(salt);
                    access.WriteLogFile(id);
                    db.SaveChanges();
                    Console.ForegroundColor = ConsoleColor.DarkGreen;
                    Console.WriteLine("Update succesfully!");
                    Console.ResetColor();
                }
                else if (result != null && isTheAdmin)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("Invalid Action.You try to change the Admin privilages.");
                    Console.ResetColor();
                }
                else
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("The Id doesn't match.Update failed.");
                    Console.ResetColor();
                }
            }
        }
Esempio n. 8
0
        public void DeleteMessage(int messageId)
        {
            bool value = false;

            using (var db = new App_Context())
            {
                var message = db.Messages.Find(messageId);

                if (message != null)
                {
                    value = true;
                }

                if (value)
                {
                    Console.ForegroundColor = ConsoleColor.DarkRed;
                    Console.WriteLine("The message will delete permanently.Do you want to continue? Y/N");
                    Console.ResetColor();
                    string x = Console.ReadLine();
                    if (x == "Y" || x == "y")
                    {
                        db.Messages.Remove(message);
                        db.SaveChanges();
                        Console.ForegroundColor = ConsoleColor.DarkGreen;
                        Console.WriteLine("The message delete it.");
                        Console.ResetColor();
                    }
                    else
                    {
                        Console.ForegroundColor = ConsoleColor.DarkGray;
                        Console.WriteLine("Deleting aborted.");
                        Console.ResetColor();
                    }
                }
            }
        }