Ejemplo n.º 1
0
        //Login to Unlogged Register User with valid user name and pass.
        public Tuple <bool, string> Login(string username, string pass, bool isGuest = false)
        {
            Logger.logSensitive(this, System.Reflection.MethodBase.GetCurrentMethod());
            if (isGuest)
            {
                string Uname = addGuest();
                return(new Tuple <bool, string>(true, Uname));
                //No need to Insert Guest to DB
            }
            Tuple <bool, string> ans = check_args(username, pass);

            if (!ans.Item1)
            {
                return(ans);
            }
            string sha1 = SB.CalcSha1(pass);
            string temp_pass_hash;

            if (!Users_And_Hashes.TryGetValue(username, out temp_pass_hash))
            {
                return(new Tuple <bool, string>(false, "No such User: "******"\n"));
            }
            if (Users_And_Hashes.ContainsKey(username) && temp_pass_hash == sha1)
            {
                User tUser;
                if (!users.TryGetValue(username, out tUser))
                {
                    return(new Tuple <bool, string>(false, "Error occured User is not in the users_DB but is registered: " + username + " \n"));
                }
                if (tUser.LoggedStatus())
                {
                    return(new Tuple <bool, string>(false, "The user: "******" is already logged in\n"));
                }
                tUser.LogIn();
                //Update LogginStatus
                Statistics.Instance.InserRecord(username, DateTime.Now);
                DbManager.Instance.UpdateUserLogInStatus(tUser.getUserName(), false);
                Active_users.Add(tUser.getUserName(), tUser);
                if (tUser.HasPendingMessages())
                {
                    List <NotifyData> messages = tUser.GetPendingMessages();
                    foreach (NotifyData msg in messages)
                    {
                        //Remove From DB Message
                        DbNotifyData dbMsg = DbManager.Instance.GetDbNotification(username, msg.Context);
                        DbManager.Instance.DeleteSingleMessage(dbMsg);
                        //Try to send the message and if not recieved it will be enetred to DB Again inside Notify
                        Publisher.Instance.Notify(tUser.getUserName(), msg);
                        //tUser.RemovePendingMessage(msg);
                    }
                    tUser.RemoveAllPendingMessages();
                }
                DbManager.Instance.SaveChanges();
                return(new Tuple <bool, string>(true, username + " Logged int\n"));
            }
            DbManager.Instance.SaveChanges();
            return(new Tuple <bool, string>(false, "Wrong Credentials\n"));
        }