예제 #1
0
        private static bool SetSession(UserDataFetcher UDF)
        {
            /* Session. unix in file.
             * we will hash it.
             * send it to server.
             * __________________
             * check procedure:
             * if hash matches -> proceed
             * does not match -> invalid session
             */
            if (!InputValidator.ValidateId(UDF.GetId()))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.USER_NOT_FOUND));
            }
            //-----timestamp
            long unix = DataFetcher.GetServerTimeStamp();

            UserDataPusher.PushSessionFileLoggedIn(unix);
            string hashedUnix = timestampHasher.Hash(unix.ToString(), DataFetcher.GetDeviceIdentifier());

            UserDataPusher.UpdateUserSession(UDF, unix, hashedUnix);
            //System.Windows.Forms.MessageBox.Show($"hashedUnix: {hashedUnix} ({hashedUnix.Length})");

            return(true);

            /*
             * if (UDF.GetCurrentUserTimeStamp().IsTimeStampOlderThan(7 * 24)) //^extension
             *  //user has not been logged in for a whole week
             *  return true;
             * return true;
             */
        }
예제 #2
0
 private void ConfirmEditingUserProfileButton_Click(object sender, EventArgs e)
 {
     confirmEditingUserProfileButton.Visible = false;
     userProfileInfoBox.ReadOnly             = true;
     UserDataPusher.UpdateUserProfileInfo(userProfileInfoBox.Text);
     CurrentUser.profileInfo = userProfileInfoBox.Text;
 }
예제 #3
0
 private static bool SetIsLoggedIn(UserDataFetcher UDF, bool IKnowThatThisFunctionMustGoAfterSetCurrentUser = true)
 {
     if (!InputValidator.ValidateId(UDF.GetId()))
     {
         return(error.SetErrorAndReturnFalse(ErrorCode.USER_NOT_FOUND));
     }
     if (CurrentUser.isLoggedIn)
     {
         UserDataPusher.PushSessionFileIsLoggedOut(false);
         return(true);
     }
     return(false);
 }
예제 #4
0
 private void UserProfileUpForTeachingCheckbox_CheckedChanged(object sender, EventArgs e)
 {
     if (userProfileUpForTeachingCheckbox.Checked)
     {
         UserDataPusher.UpdateIfUserUpForTeaching(1);
         CurrentUser.upForTeaching = 1;
     }
     else
     {
         UserDataPusher.UpdateIfUserUpForTeaching(0);
         CurrentUser.upForTeaching = 0;
     }
 }
예제 #5
0
        public static bool Register(UserDataFetcher UDF, string username, string email, string password, string passwordRepeat)
        {
            error.no = ErrorCode.OK;
            //valid username password
            if (!InputValidator.ValidateUsername(username))
            {
                return(error.SetErrorAndReturnFalse(
                           InputValidator.error.no | ErrorCode.INVALID_USERNAME));
            }
            if (!InputValidator.ValidatePassword(password, passwordRepeat))
            {
                return(error.SetErrorAndReturnFalse(
                           InputValidator.error.no | ErrorCode.INVALID_PASSWORD)); //#trigger pass
            }
            //valid email
            System.Net.Mail.MailAddress mail;
            if (!InputValidator.ValidateEmail(email, out mail))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_EMAIL));
            }

            //taken email username
            if (!InputValidator.CheckEmailNotTaken(UDF, mail))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.EMAIL_TAKEN));
            }
            if (!InputValidator.CheckUsernameNotTaken(UDF, username))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.USERNAME_TAKEN));
            }

            //hash and retrieve used salt
            string hashedPass = hasher.Hash(password, saveUsedSalt: true); //^named argument
            string usedSalt   = hasher.GetLastUsedSaltAndForgetIt();

            //push user
            UserDataPusher.PushNewUser(username, mail, hashedPass, usedSalt);

            //was it successful?
            if (InputValidator.CheckUsernameNotTaken(UDF, username))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.PUSH_ERROR));
            }
            return(true);
        }
예제 #6
0
        public bool Write(string commentToShow)
        {
            error.Clear();
            //validate
            //user
            if (!CurrentUser.isLoggedIn)
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_SESSION));
            }
            if (!InputValidator.ValidateId(CurrentUser.id))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.USER_NOT_FOUND));
            }
            //good text
            if (!InputValidator.ValidateForumProblemCommentText(commentToShow))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_TEXT_FIELD | InputValidator.error.no));
            }
            //valid as comment
            //first get timestamp
            long    unix    = DataFetcher.GetServerTimeStamp();
            Comment comment = new Comment(0, CurrentUser.id, forumPost.id, commentToShow, unix);

            //push
            if (!UserDataPusher.PushNewForumProblemComment(comment))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.PUSH_ERROR));
            }

            //retrieve id
            this.lastComment = DataFetcher.GetCommentFromTemplate(comment); //changes only id

            comments.Add(this.lastComment);


            //this.LoadLast(this.commentsPanel);

            if (SuccessfullyAddedCommentEvent != null)
            {
                SuccessfullyAddedCommentEvent.Invoke(this, new SuccessfullyAddedCommentEventArgs(commentToShow, unix)); //invokes event if it has subscribers
            }

            return(true);
        }
예제 #7
0
        public static bool NewProblem(String name, Subjects.Subject subject, String description, ForumContent FC)
        {
            error.no = ErrorCode.OK;
            //user id
            if (!InputValidator.ValidateId(CurrentUser.id))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_SESSION | ErrorCode.USER_NOT_FOUND));
            }
            //problem name
            if (!InputValidator.ValidateForumProblemName(name))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_NAME | InputValidator.error.no));
            }
            //chosen dropdown item
            if (subject == null)
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_CHOSEN_ITEM));
            }
            //problem description
            if (!InputValidator.ValidateForumProblemDescription(description))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_TEXT_FIELD | InputValidator.error.no));
            }
            //create forum post
            ForumPost forumPost;

            try
            {
                //#watch out. A forum post with ID 0 !
                forumPost = new ForumPost(0, subject.id, name, description, CurrentUser.id);
            }
            catch (Exception)
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.UNKNOWN));
            }
            FC.Add(forumPost);
            //push ok
            if (!UserDataPusher.PushNewForumProblem(forumPost))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.PUSH_ERROR));
            }
            return(true);
        }
예제 #8
0
 private void ConfirmAddInterestButton_Click(object sender, EventArgs e)
 {
     //if user doest already have choosen interest, add it
     if (!CurrentUser.interests.Contains(chooseInterestComboBox.Text) && CurrentUser.isLoggedIn != null)
     {
         if (CurrentUser.isLoggedIn)
         {
             CurrentUser.interests.Add(chooseInterestComboBox.Text);
             UserDataPusher.PushNewInterest(chooseInterestComboBox.Text);
             //show interest in profile
             Label interestsLabel = new Label();
             interestsLabel.AutoSize = true;
             interestsLabel.Text     = chooseInterestComboBox.Text;
             userInterestsFLP.Controls.Add(interestsLabel);
         }
     }
     else
     {
         MessageBox.Show("You already have this interest!");
     }
 }
예제 #9
0
        public static bool UpvoteProblem(ForumPost forumPost)
        {
            error.Clear();
            if (CurrentUser.isLoggedIn == false)
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_SESSION));
            }
            bool upVoted = false; //so far

            try
            {
                upVoted = UserDataPusher.UpvoteForumProblem(forumPost); //exception root
                if (!upVoted)
                {
                    return(error.SetErrorAndReturnFalse(ErrorCode.PUSH_ERROR | ErrorCode.UNKNOWN));
                }
                //upvoted
                //previous votes number
                int votes = DataFetcher.GetForumPostVotes(forumPost);
                //set votes to previous+1
                votes += 1;
                if (!DataPusher.SetForumPostVotes(forumPost, votes))
                {
                    return(error.SetErrorAndReturnFalse(ErrorCode.PUSH_ERROR));
                }
                return(true);
            }
            catch (MySql.Data.MySqlClient.MySqlException myEx)
            { //^exception handling
                if (myEx.Number == (uint)MySql.Data.MySqlClient.MySqlErrorCode.DuplicateKeyEntry)
                {
                    throw new exceptions.DoneBefore();
                }
                else
                {
                    throw myEx;
                }
            }
        }
예제 #10
0
        public static string messageToOutterWorld     = ""; //#delete me
        public static bool LogIn(UserDataFetcher UDF, string username, string password)
        {
            string salt;

            if (!InputValidator.ValidatePassword(password))
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.WRONG_PASSWORD | InputValidator.error.no));
            }
            //is it username?
            if (InputValidator.ValidateUsername(username))
            {
                //-yes. get salt
                salt = UDF.GetSalt(username);
            }
            //was it too short?
            else if (InputValidator.error.no != ErrorCode.TOO_SHORT)
            {
                //-yes. is it email?
                System.Net.Mail.MailAddress email;
                string Email = username;
                if (InputValidator.ValidateEmail(Email, out email))
                {
                    //--yes. get salt
                    salt = UDF.GetSalt(email);
                }
                //--no. return false but before set error
                else
                {
                    return(error.SetErrorAndReturnFalse(ErrorCode.INVALID_EMAIL | ErrorCode.INVALID_USERNAME));
                }
            }
            //it was not username nor email. Let the input validator say what was the problem
            else
            {
                return(error.SetErrorAndReturnFalse(InputValidator.error.no | ErrorCode.INVALID_USERNAME));
            }

            //we have a salt
            //or maybe we should have it

            if (salt == null)
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.UNKNOWN));
            }

            if (salt.Length < 1)
            {
                return(error.SetErrorAndReturnFalse(ErrorCode.USER_NOT_FOUND));
            }

            //let us hash password

            password = hasher.Hash(password, salt);

            //and finally check it

            if (InputValidator.CheckPasswordMatch(UDF, password))
            {
                error.no = ErrorCode.OK;
                //set log in timestamp
                UserDataPusher.PushSessionFileUser(username);
                if (SetSession(UDF))
                {
                    Auth.SetCurrentUser(username, UDF);
                    SetIsLoggedIn(UDF);
                    return(true);
                }
                return(false);
            }

            error.no = ErrorCode.WRONG_PASSWORD;
            return(false);
        }//logIn
예제 #11
0
 public static bool SetLoggedOut()
 {
     //always true because we do not care if user whether user is valid or not
     UserDataPusher.PushSessionFileIsLoggedOut(true);
     return(true);
 }