Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
40 changes: 31 additions & 9 deletions src/library/assistant/ui/login/LoginController.java
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package library.assistant.ui.login;

import com.jfoenix.controls.JFXButton;
import com.jfoenix.controls.JFXPasswordField;
import com.jfoenix.controls.JFXTextField;
import java.io.IOException;
Expand Down Expand Up @@ -29,27 +30,49 @@ public class LoginController implements Initializable {
private JFXTextField username;
@FXML
private JFXPasswordField password;
@FXML
private JFXButton loginButton;

Preferences preference;

@Override
public void initialize(URL url, ResourceBundle rb) {
preference = Preferences.getPreferences();
if (preference.getUsername() == null || preference.getUsername().isEmpty()) {
loginButton.setText("Create Account");
}
}

@FXML
private void handleLoginButtonAction(ActionEvent event) {
String uname = StringUtils.trimToEmpty(username.getText());
String pword = DigestUtils.shaHex(password.getText());
String pword = password.getText();

if (preference.getUsername() == null || preference.getUsername().isEmpty()) {
if (uname.isEmpty() || pword.isEmpty()) {
username.getStyleClass().add("wrong-credentials");
password.getStyleClass().add("wrong-credentials");
return;
}

preference.setUsername(uname);
preference.setPassword(DigestUtils.shaHex(pword));
Preferences.writePreferenceToFile(preference);

if (uname.equals(preference.getUsername()) && pword.equals(preference.getPassword())) {
closeStage();
loadMain();
LOGGER.log(Level.INFO, "User successfully logged in {}", uname);
}
else {
username.getStyleClass().add("wrong-credentials");
password.getStyleClass().add("wrong-credentials");
LOGGER.log(Level.INFO, "Admin account created for user {}", uname);
} else {
String pwordHex = DigestUtils.shaHex(pword);

if (uname.equals(preference.getUsername()) && pwordHex.equals(preference.getPassword())) {
closeStage();
loadMain();
LOGGER.log(Level.INFO, "User successfully logged in {}", uname);
} else {
username.getStyleClass().add("wrong-credentials");
password.getStyleClass().add("wrong-credentials");
}
}
}

Expand All @@ -70,8 +93,7 @@ void loadMain() {
stage.setScene(new Scene(parent));
stage.show();
LibraryAssistantUtil.setStageIcon(stage);
}
catch (IOException ex) {
} catch (IOException ex) {
LOGGER.log(Level.ERROR, "{}", ex);
}
}
Expand Down
2 changes: 1 addition & 1 deletion src/library/assistant/ui/login/login.fxml
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
<Font size="17.0" />
</font>
</JFXPasswordField>
<JFXButton layoutX="106.0" layoutY="278.0" onAction="#handleLoginButtonAction" prefHeight="40.0" prefWidth="90.0" styleClass="login-button" text="Login" />
<JFXButton fx:id="loginButton" layoutX="106.0" layoutY="278.0" onAction="#handleLoginButtonAction" prefHeight="40.0" prefWidth="90.0" styleClass="login-button" text="Login" />
<JFXButton layoutX="239.0" layoutY="278.0" onAction="#handleCancelButtonAction" prefHeight="40.0" prefWidth="90.0" styleClass="login-button" text="Cancel" />
<FontAwesomeIconView glyphName="LOCK" layoutX="200.0" layoutY="79.0" size="55" />
<FontAwesomeIconView glyphName="USER" layoutX="64.0" layoutY="149.0" size="25" />
Expand Down
2 changes: 0 additions & 2 deletions src/library/assistant/ui/settings/Preferences.java
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,6 @@ public class Preferences {
public Preferences() {
nDaysWithoutFine = 14;
finePerDay = 2;
username = "admin";
setPassword("admin");
}

public int getnDaysWithoutFine() {
Expand Down
38 changes: 38 additions & 0 deletions test/library/assistant/ui/settings/PreferencesTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package library.assistant.ui.settings;

import org.junit.Test;
import static org.junit.Assert.*;
import org.apache.commons.codec.digest.DigestUtils;

public class PreferencesTest {

@Test
public void testDefaultConstructor() {
Preferences prefs = new Preferences();
// The default implementation should not set credentials.
assertNull("Username should be null by default", prefs.getUsername());
assertNull("Password should be null by default", prefs.getPassword());
}

@Test
public void testSetPasswordShort() {
Preferences prefs = new Preferences();
String shortPass = "short";
prefs.setPassword(shortPass);

// "short" length is 5 < 16, so it should be hashed
assertNotEquals(shortPass, prefs.getPassword());
assertEquals(40, prefs.getPassword().length()); // SHA-1 hex is 40 chars
assertEquals(DigestUtils.shaHex(shortPass), prefs.getPassword());
}

@Test
public void testSetPasswordLong() {
Preferences prefs = new Preferences();
String longPass = "1234567890123456"; // 16 chars
prefs.setPassword(longPass);

// >= 16 chars -> plain text
assertEquals(longPass, prefs.getPassword());
}
}
15 changes: 15 additions & 0 deletions test/stubs/library/assistant/alert/AlertMaker.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package library.assistant.alert;

public class AlertMaker {

public static void showSimpleAlert(String title, String content) {
// Stub: do nothing or log to console
System.out.println("[STUB] AlertMaker.showSimpleAlert: " + title + " - " + content);
}

public static void showErrorMessage(Exception ex, String title, String content) {
// Stub: do nothing or log to console
System.out.println("[STUB] AlertMaker.showErrorMessage: " + title + " - " + content);
ex.printStackTrace();
}
}