示例#1
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();
                }
            }
        }
示例#2
0
        public AccessId Login(string username, string password)
        {
            FileAccess access = new FileAccess(adminFile);

            using (var db = new App_Context())
            {
                var salt = db.Users.Where(i => i.Username == username).Select(p => p.Salt).SingleOrDefault();

                byte[] hash;

                if (salt != null)
                {
                    hash = Password.Hash(password, Convert.FromBase64String(salt));
                    var dbpass = Convert.ToBase64String(hash);

                    var result = db.Users.Where(u => u.Username == username && u.Password == dbpass).ToList();

                    if (result.Count != 0)
                    {
                        foreach (User item in result)
                        {
                            access.WriteLogFile(username, password);
                            return(item.AccessId);
                        }
                    }
                }
            }

            access.WriteLogFile(username, password);
            return(0);
        }
示例#3
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);
        }
示例#4
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();
                }
            }
        }
示例#5
0
        public void ViewUser()
        {
            using (var db = new App_Context())
            {
                var result = db.Users.ToList();

                foreach (var item in result)
                {
                    Console.WriteLine($"User Id: {item.Id}" + $"\nUsername:{item.Username}" + $"\nAccess level:{item.AccessId}" + $"\nEmail:{item.Email}\n");
                }
            }
        }
示例#6
0
 public bool ExistUser(string username)
 {
     using (var db = new App_Context())
     {
         var result = db.Users.Where(i => i.Username == username).SingleOrDefault();
         if (result == null)
         {
             return(true);
         }
     }
     return(false);
 }
示例#7
0
 //Update user log
 public void WriteLogFile(int id)
 {
     using (var db = new App_Context())
     {
         var user = db.Users.Where(p => p.Id == id).ToList();
         foreach (var item in user)
         {
             string[] file = new string[] { $"==== User Updated ====\r\nAt:{DateTime.Now} with Username:{item.Username}" };
             File.AppendAllLines(_path, file);
         }
     }
 }
示例#8
0
        public void ViewData()
        {
            using (var db = new App_Context())
            {
                var allMessages = db.Messages.ToList();

                foreach (var item in allMessages)
                {
                    Console.WriteLine($"Message id:{item.Id}" + $"\nMessage:{item.Message}" + $"\nSender:{item.SenderName}" + $"\nReceiver:{item.ReceiverName}" + $"\nShipping date:{item.DateTime}\n");
                }
            }
        }
示例#9
0
 public int NumberOfMess(string username)
 {
     using (var db = new App_Context())
     {
         var mess = db.Messages.Where(m => m.ReceiverName == username).ToList();
         if (mess.Count > 0)
         {
             int number = mess.Count;
             return(number);
         }
     }
     return(0);
 }
示例#10
0
        static bool ReceiverExist(string receiver)
        {
            using (App_Context db = new App_Context())
            {
                var exist = db.Users.Where(i => i.Username == receiver).FirstOrDefault();

                if (exist != null)
                {
                    return(true);
                }
            }
            return(false);
        }
示例#11
0
        static bool IsAdmin(int id)
        {
            using (var db = new App_Context())
            {
                var result = db.Users.Where(i => i.Id == id).Select(p => p.Username);

                foreach (var item in result)
                {
                    if (item.Equals("admin"))
                    {
                        return(true);
                    }
                }
            }
            return(false);
        }
示例#12
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();
                }
            }
        }
示例#13
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();
                        }
                    }
                }
            }
        }
示例#14
0
        public void ViewMyMessage(string username)
        {
            using (var db = new App_Context())
            {
                var myMessage = db.Messages.Where(i => i.ReceiverName == username).Select(m => new { m.Message, m.SenderName }).ToList();

                if (myMessage.Count > 0)
                {
                    Console.WriteLine(String.Format("{0," + Console.WindowWidth / 2 + "}", "You have the following messages"));
                    foreach (var item in myMessage)
                    {
                        Console.WriteLine($"{item.SenderName} - {item.Message}");
                    }
                }
                else
                {
                    Console.WriteLine("You have not any massage in your mail box.");
                }
            }
        }
示例#15
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();
                }
            }
        }
示例#16
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();
                    }
                }
            }
        }
示例#17
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();
                }
            }
        }