/* Cfour (C++ Common Console Classes)
 * Copyright (C) 2001 Jeffrey Bakker
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 *
 *
 *  Author:              Jeffrey Bakker
 *  Filename:            security.cpp
 *  File version:        1.1
 *
 *  ===========================================================================
 *
 *  Created on:  July 1st, 2001
 *
 *
 *  Modified on: July 6th, 2001
 *
 *             - Using a new class LogPass as a data member, rather than
 *               using two data members, log, and pass.
 *
 *             - Changed all methods that directly access the log and pass
 *               so they access the LogPass methods.
 *
 *             - These changes will help make the Security class to be
 *               able to use multiple login and pass accounts.
 *
 *  ===========================================================================
 *
 *  Remark:
 *
 *  This is a tight implementation of the Security class. Most methods call
 *  other methods which may also call methods from Account, a Private member
 *  of type LogPass class.
 *
 *  For example, the login() method directly calls only two methods, yet it
 *  also indirectly calls SIX other methods to do the subroutines.
 *
 *  ===========================================================================
 *
 *  string getLogin():
 *
 *   Description: private method that returns the Login name.
 *
 *   Input:
 *    - takes 0 parameters
 *
 *   Output:
 *    - returns the Login (string)
 *
 *  ===========================================================================
 *
 *  string getPass():
 *
 *   Description: private method that returns the Password.
 *
 *   Input:
 *    - takes 0 parameters
 *
 *   Output:
 *    - returns the Password (string)
 *
 *  ===========================================================================
 *
 *  bool ask_login():
 *
 *   Description: asks the user for the login, then passes it to other
 *                methods for verification.
 *
 *   Input:
 *    - takes 0 parameters
 *
 *   Output:
 *    - returns true if login is correct, false otherwise
 *
 *  ===========================================================================
 *
 *  bool ask_pass():
 *
 *   Description: asks the user for the password, then passes it to other
 *                methods for verification. the keystrokes are masked.
 *
 *   Input:
 *    - takes 0 parameters
 *
 *   Output:
 *    - returns true if password is correct, false otherwise
 *
 *  ===========================================================================
 *
 *  bool verify_login(string L):
 *
 *   Description: verifies whether or not the Logins match. If not, a failure
 *                message will be displayed.
 *
 *   Input:
 *
 *    Parameter 1
 *    - Name:        L
 *    - Type:        string
 *    - Description: the Login to be checked
 *
 *   Output:
 *    - returns true if login is correct, false otherwise
 *
 *  ===========================================================================
 *
 *  bool verify_pass(string P):
 *
 *   Description: verifies whether or not the Passwords match. If not, a
 *                failure message will be displayed.
 *
 *   Input:
 *
 *    Parameter 1
 *    - Name:        P
 *    - Type:        string
 *    - Description: the Password to be checked
 *
 *   Output:
 *    - returns true if Password is correct, false otherwise
 *
 *  ===========================================================================
 *
 *  bool Login():
 *
 *   Description: a routine which calls ask_login() and ask_pass() to
 *                retrieve the log/pass from the user, and verify them.
 *
 *   Input:
 *    - takes 0 parameters
 *
 *   Output:
 *    - returns true if BOTH Login & Password are correct, false otherwise
 *
 *  ===========================================================================
 *
 *  void reset_LP():
 *
 *   Description: allows user to change the Login and Password. This method
 *                will require the user to provide the current Login and
 *                Password in order to let the uuser change it.
 *
 *   Input:
 *    - takes 0 parameters
 *
 *   Output:
 *    - returns nothing
 *
 *  ===========================================================================
 * _______. ..
 */


#include "security.h" 
#include "cfconsole.h" 

//-----------------------------------------------------------------------------
Security::Security() {

 Account.set_log("admin");
 Account.set_pass("please");
 mask = '*';
 rate = 1;
}
//-----------------------------------------------------------------------------
// methods to retrieve log/pass -----------------------------------------------
string Security::getLogin() const {return Account.get_log();}
string Security::getPass()  const {return Account.get_pass();}
//-----------------------------------------------------------------------------

// methods that ask user for log/pass -----------------------------------------
// ask user for login ---------------------------------------------------------
bool Security::ask_login() {

 string L;

 cout << "\nEnter login:    ";
 cin  >> L;

 cin.get();

 if(verify_login(L)) {return true;}
 else return false;
}
// ask user to input password -------------------------------------------------
bool Security::ask_pass() {


 string P;

 cout << "\nEnter password: ";
 cin  >> P;

 cin.get();

 if(verify_login(P)) {return true;}
 else return false;
}
//-----------------------------------------------------------------------------

// log/pass verification methods ----------------------------------------------
// verify whether login is correct --------------------------------------------
bool Security::verify_login(string L) const {

 if (Account.vlogin(L)) {return true;}
 else {
  cout << "\a\nLogin Failed: invalid login.\n";
  Console::pause();
  return false;
 }
}
// verify whether password is correct -----------------------------------------
bool Security::verify_pass(string P) const {

 if (Account.vpassword(P)) {return true;}
 else {
  cout << "\a\nLogin Failed: invalid password.\n";
  Console::pause();
  return false;
 }
}
//-----------------------------------------------------------------------------

// get log/pass from the user (to provide access) -----------------------------
bool Security::Login() {

 if(ask_login()) {
  if(ask_pass()) {return true;}
 }

 return false;
}
//-----------------------------------------------------------------------------
// modify the mask character and frequency ------------------------------------
void Security::set_mask(char m, int r) {
 mask = m; rate = r;
}
// ----------------------------------------------------------------------------
// allows user to change login and passowrd (log/pass is required) ------------
void Security::reset_LP() {

 string newlogin;
 string newpass;

 cout << "\nEnter the current L/P.\n";

 if(ask_login()) {
 
  if(ask_pass()) {
 
   cout << "\nEnter the new login:    ";
   cin  >> newlogin;

   cout << "\nEnter the new password: ";
   cin  >> newpass;

   Account.set_log(newlogin);
   Account.set_pass(newpass);

   cout << "\nLogin/Pass changed.\n";
  }
  else cout << "\a\nInvalid password.\n";
 }
 else cout << "\a\nInvalid login.\n";

}
//-----------------------------------------------------------------------------