Added admin actions.

This commit is contained in:
Andrew Lalis 2021-02-21 12:56:13 +01:00
parent d5546a5320
commit 84709cc0d4
5 changed files with 70 additions and 4 deletions

View File

@ -1,8 +1,11 @@
package nl.andrewlalis.erme;
import com.formdev.flatlaf.FlatLightLaf;
import nl.andrewlalis.erme.util.Hash;
import nl.andrewlalis.erme.view.EditorFrame;
import java.nio.charset.StandardCharsets;
public class EntityRelationMappingEditor {
public static final String VERSION = "1.2.0";
@ -10,7 +13,19 @@ public class EntityRelationMappingEditor {
if (!FlatLightLaf.install()) {
System.err.println("Could not install FlatLight Look and Feel.");
}
final EditorFrame frame = new EditorFrame();
final boolean includeAdminActions = shouldIncludeAdminActions(args);
if (includeAdminActions) {
System.out.println("Admin actions have been enabled.");
}
final EditorFrame frame = new EditorFrame(includeAdminActions);
frame.setVisible(true);
}
private static boolean shouldIncludeAdminActions(String[] args) {
if (args.length < 1) {
return false;
}
byte[] pw = args[0].getBytes(StandardCharsets.UTF_8);
return Hash.matches(pw, "admin_hash.txt");
}
}

View File

@ -0,0 +1,50 @@
package nl.andrewlalis.erme.util;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Arrays;
public class Hash {
public static boolean matches(byte[] password, String resourceFile) {
MessageDigest md;
try {
md = MessageDigest.getInstance("SHA-256");
} catch (NoSuchAlgorithmException e) {
e.printStackTrace();
return false;
}
byte[] passwordHash = md.digest(password);
InputStream is = Hash.class.getClassLoader().getResourceAsStream(resourceFile);
if (is == null) {
System.err.println("Could not obtain input stream to admin_hash.txt");
return false;
}
char[] buffer = new char[64];
try (BufferedReader br = new BufferedReader(new InputStreamReader(is))) {
if (br.read(buffer) != buffer.length) {
System.err.println("Incorrect number of characters read from hash file.");
return false;
}
} catch (IOException e) {
e.printStackTrace();
return false;
}
String hashHex = String.valueOf(buffer);
byte[] hash = hexStringToByteArray(hashHex);
return Arrays.equals(passwordHash, hash);
}
private static byte[] hexStringToByteArray(String s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
data[i / 2] = (byte) ((Character.digit(s.charAt(i), 16) << 4)
+ Character.digit(s.charAt(i+1), 16));
}
return data;
}
}

View File

@ -9,10 +9,10 @@ import java.awt.*;
* The main JFrame for the editor.
*/
public class EditorFrame extends JFrame {
public EditorFrame() {
public EditorFrame(boolean includeAdminActions) {
super("ER-Mapping Editor");
this.setContentPane(new DiagramPanel(new MappingModel()));
this.setJMenuBar(new EditorMenuBar());
this.setJMenuBar(new EditorMenuBar(includeAdminActions));
this.setMinimumSize(new Dimension(400, 400));
this.setPreferredSize(new Dimension(800, 800));
this.pack();

View File

@ -12,7 +12,7 @@ import javax.swing.*;
* The menu bar that's visible atop the application.
*/
public class EditorMenuBar extends JMenuBar {
public EditorMenuBar() {
public EditorMenuBar(boolean includeAdminActions) {
this.add(this.buildFileMenu());
this.add(this.buildEditMenu());
this.add(this.buildHelpMenu());

View File

@ -0,0 +1 @@
cfdabe75d984e5a92fb491dadc9091419d9587c049246356a488e83a75505bce