You are on page 1of 5

/*

* To change this license header, choose License Headers in Project Properties.


* To change this template file, choose Tools | Templates
* and open the template in the editor.
*/
package sdev425_2;

import java.awt.Desktop;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.sql.Timestamp;
import java.util.logging.Level;
import java.util.logging.Logger;
import javafx.application.Application;
import javafx.application.Platform;
import javafx.event.ActionEvent;
import javafx.event.EventHandler;
import javafx.geometry.Pos;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.Label;
import javafx.scene.control.PasswordField;
import javafx.scene.control.TextField;
import javafx.scene.layout.GridPane;
import javafx.scene.paint.Color;
import javafx.scene.text.Text;
import javafx.stage.Stage;
import javax.swing.JOptionPane;

/**
*
* @author jim Adopted from Oracle's Login Tutorial Application
* https://docs.oracle.com/javafx/2/get_started/form.htm
*/
public class SDEV425_2 extends Application {

@Override
public void start(Stage primaryStage) {
primaryStage.setTitle("SDEV425 Login");
// Grid Pane divides your window into grids
GridPane grid = new GridPane();
// Align to Center
// Note Position is geometric object for alignment
grid.setAlignment(Pos.CENTER);
// Set gap between the components
// Larger numbers mean bigger spaces
grid.setHgap(10);
grid.setVgap(10);

// Create some text to place in the scene


Text scenetitle = new Text("Welcome. Login to continue.");
// Add text to grid 0,0 span 2 columns, 1 row
grid.add(scenetitle, 0, 0, 2, 1);

// Create Label
Label userName = new Label("User Name:");
// Add label to grid 0,1
grid.add(userName, 0, 1);

// Create Textfield
TextField userTextField = new TextField();
// Add textfield to grid 1,1
grid.add(userTextField, 1, 1);

// Create Label
Label pw = new Label("Password:");
// Add label to grid 0,2
grid.add(pw, 0, 2);

// Create Passwordfield
PasswordField pwBox = new PasswordField();
// Add Password field to grid 1,2
grid.add(pwBox, 1, 2);

// Create Login Button


Button btn = new Button("Login");
// Add button to grid 1,4
grid.add(btn, 1, 4);

final Text actiontarget = new Text();


grid.add(actiontarget, 1, 6);

// Set the Action when button is clicked


btn.setOnAction(new EventHandler<ActionEvent>() {
private int attempts;

@Override
public void handle(ActionEvent e) {
// Authenticate the user
boolean isValid = authenticate(userTextField.getText(),
pwBox.getText());
// If valid clear the grid and Welcome the user
if (isValid) {
grid.setVisible(false);
GridPane grid2 = new GridPane();
// Align to Center
// Note Position is geometric object for alignment
grid2.setAlignment(Pos.CENTER);
// Set gap between the components
// Larger numbers mean bigger spaces
grid2.setHgap(10);
grid2.setVgap(10);
//AC-8 System use notification providing security and
privacy notices
Text scenetitle = new Text("Welcome " +
userTextField.getText() + "!" +
"\n\nUsers are accessing a U.S. Government
information system, Information system usage may be monitored, recorded, and
subject to audit;"
+ "\nUnauthorized use of the information
system is prohibited and subject to criminal and civil penalties;"
+ "\nUse of the information system indicates
consent to monitoring and recording.");
// Add text to grid 0,0 span 2 columns, 1 row
grid2.add(scenetitle, 0, 0, 2, 1);
Scene scene = new Scene(grid2, 500, 400);
primaryStage.setScene(scene);
primaryStage.show();
try {
readFile();
// If Invalid Ask user to try again
} catch (IOException ex) {

Logger.getLogger(SDEV425_2.class.getName()).log(Level.SEVERE, null, ex);


}
} else {
final Text actiontarget = new Text();
grid.add(actiontarget, 1, 6);
actiontarget.setFill(Color.FIREBRICK);
actiontarget.setText("Please try again.");
//attempts added after each unsuccessful login
attempts++;
System.out.println(attempts);
//AC-7 AC-12 The information automatically closes
the session after 3 failed login attempts
if (attempts == 3){
JOptionPane.showMessageDialog(null, "You have
exceeded the limit of unsuccessful login attempts, please try again another
time.");
//AU-3 AU-8 event logged to auditlog
String log = "Event: Login failure limit
exceeded. " + "User: Unknown" + " Date and time: " + new
Timestamp(date.getTime()) + " Location: Handle method";
try {
auditText(log);
}
catch(Exception e3) {

}
Platform.exit();
System.exit(0);
}
}
}

private void auditText(String log) {


throw new UnsupportedOperationException("Not supported
yet."); //To change body of generated methods, choose Tools | Templates.
}
});
// Set the size of Scene
Scene scene = new Scene(grid, 500, 400);
primaryStage.setScene(scene);
primaryStage.show();
}

/**
* @param args the command line arguments
*/
public static void main(String[] args) {
launch(args);
}

/**
* @param text the text being audited
* AU-3 Uses a buffered writer to write audit log messages when called
*/
public static void auditText(String text) throws ClassNotFoundException {
BufferedWriter bw = null;

try {
bw = new BufferedWriter(new FileWriter("auditlog.txt", true));
//create object
bw.write(text); //write audit text into file
bw.newLine(); //new line after text
bw.flush(); //flush after audit text
}
catch(IOException e) {

finally {
if (bw != null)
try {
bw.close();
}
catch(IOException e2) {

}
}
}

/**
* @param user the username entered
* @param pword the password entered
* @return isValid true for authenticated
*/
public boolean authenticate(String user, String pword) {
boolean isValid = false;
if (user.equalsIgnoreCase("sdevadmin")
&& pword.equals("425!pass")) {
isValid = true;
//readFile();
}

return isValid;
}

// public class JavaOpenFile {

public void readFile() throws IOException {


//text file, should be opening in default text editor
File file = new File("C:/Users/solomon
M/Desktop/importantFile/govern.txt");

//first check if Desktop is supported by Platform or not


if(!Desktop.isDesktopSupported()){
System.out.println("Desktop is not supported");
return;
}

Desktop desktop = Desktop.getDesktop();


if(file.exists()) desktop.open(file);

//let's try to open PDF file


//
file = new File("/Users/pankaj/java.pdf");
if(file.exists()) desktop.open(file);
}

private static class date {

private static long getTime() {


throw new UnsupportedOperationException("Not supported yet."); //To
change body of generated methods, choose Tools | Templates.
}

public date() {
}
}

You might also like