Skip to content

Commit 95f1cab

Browse files
committed
Code Cleanup
* Fix issues of using fully qualified names when the classes are already imported * Fix an issue where null check is done first in Login class * use equalsIgnoreCase() instead of != in FileManagement class
1 parent bc58227 commit 95f1cab

File tree

8 files changed

+33
-29
lines changed

8 files changed

+33
-29
lines changed

Source/Cataphract/API/Dragon/AccountCreate.java

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import Cataphract.API.Build;
2626
import Cataphract.API.IOStreams;
2727
import Cataphract.API.Minotaur.Cryptography;
28+
import Cataphract.API.Minotaur.PolicyCheck;
2829

2930
/**
3031
* A class to create new user accounts on the system. Can be restricted by policy "account_create"
@@ -126,7 +127,7 @@ public AccountCreate(String username) throws Exception
126127
{
127128
//Store the current username locally to _currentUsername
128129
_currentUsername = username;
129-
_isCurrentUserAdmin = new Cataphract.API.Dragon.Login(_currentUsername).checkPrivilegeLogic();
130+
_isCurrentUserAdmin = new Login(_currentUsername).checkPrivilegeLogic();
130131
}
131132

132133
/**
@@ -137,7 +138,7 @@ public AccountCreate(String username) throws Exception
137138
public final void accountCreateLogic()throws Exception
138139
{
139140
// Check the policy if account creation is allowed in the policy file, can be bypassed by the accounts with administrator privileges
140-
if(new Cataphract.API.Minotaur.PolicyCheck().retrievePolicyValue("account_create").equals("on") || _isCurrentUserAdmin)
141+
if(new PolicyCheck().retrievePolicyValue("account_create").equals("on") || _isCurrentUserAdmin)
141142
{
142143
//If the authentication check fails, exit from the module
143144
if(! authenticateCurrentUser())
@@ -204,11 +205,11 @@ private final boolean authenticateCurrentUser()throws Exception
204205
try
205206
{
206207
//Display the name of the user currently logged in
207-
IOStreams.println("Username: " + new Cataphract.API.Dragon.Login(_currentUsername).getNameLogic());
208+
IOStreams.println("Username: " + new Login(_currentUsername).getNameLogic());
208209

209-
new Cataphract.API.Minotaur.Cryptography();
210+
new Cryptography();
210211
//challenge the database for the provided credentials, and store the status
211-
authenticationStatus = new Cataphract.API.Dragon.Login(_currentUsername).authenticationLogic(Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("Password: "))), Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("SecurityKey: "))));
212+
authenticationStatus = new Login(_currentUsername).authenticationLogic(Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("Password: "))), Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("SecurityKey: "))));
212213
}
213214
catch(Exception e)
214215
{
@@ -293,10 +294,10 @@ private final boolean setAccountUsername()throws Exception
293294
}
294295
else
295296
{
296-
_newAccountUsername = Cataphract.API.Minotaur.Cryptography.stringToSHA3_256(_newAccountUsername);
297+
_newAccountUsername = Cryptography.stringToSHA3_256(_newAccountUsername);
297298

298299
// check if the entered username exists in the database already
299-
if(new Cataphract.API.Dragon.Login(_newAccountUsername).checkUserExistence())
300+
if(new Login(_newAccountUsername).checkUserExistence())
300301
{
301302
IOStreams.printError("Username has already been enrolled! Please try again with another username.");
302303
_newAccountUsername = "";
@@ -503,7 +504,7 @@ private final void addAccountToDatabase()
503504
public final void createDefaultAdministratorAccount()throws Exception
504505
{
505506
// Check if the Administrator account exists already. If not, then create the Administrator account
506-
if(!new Cataphract.API.Dragon.Login("Administrator").checkUserExistence())
507+
if(!new Login("Administrator").checkUserExistence())
507508
{
508509
// Set the parameters for the Administrator account
509510
_newAccountAdmin = true;

Source/Cataphract/API/Dragon/AccountDelete.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public AccountDelete(String currentUsername) throws Exception
5454
{
5555
// Set the current username
5656
_currentUsername = currentUsername;
57-
//_isCurrentUserAdmin = new Cataphract.API.Dragon.Login(currentUsername).checkPrivilegeLogic();
57+
//_isCurrentUserAdmin = new Login(currentUsername).checkPrivilegeLogic();
5858
_isCurrentUserAdmin = new Login(currentUsername).checkUserExistence();
5959
}
6060

@@ -69,7 +69,7 @@ public void deleteUserAccount() throws Exception
6969
Build.viewBuildInfo();
7070

7171
// Check the policy if account deletion is allowed in the policy file, can be bypassed by the accounts with administrator privileges
72-
if(new Cataphract.API.Minotaur.PolicyCheck().retrievePolicyValue("account_delete").equals("on") || new Cataphract.API.Dragon.Login(_currentUsername).checkPrivilegeLogic())
72+
if(new Cataphract.API.Minotaur.PolicyCheck().retrievePolicyValue("account_delete").equals("on") || new Login(_currentUsername).checkPrivilegeLogic())
7373
{
7474
// Check login credentials
7575
if (!login())

Source/Cataphract/API/Dragon/AccountModify.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,7 @@ public AccountModify(String user)throws Exception
7777
public final void accountModifyLogic()throws Exception
7878
{
7979
// Check the policy if account modification is allowed in the policy file, can be bypassed by the accounts with administrator privileges
80-
if(new Cataphract.API.Minotaur.PolicyCheck().retrievePolicyValue("account_modify").equals("on") || new Cataphract.API.Dragon.Login(_currentUsername).checkPrivilegeLogic())
80+
if(new Cataphract.API.Minotaur.PolicyCheck().retrievePolicyValue("account_modify").equals("on") || new Login(_currentUsername).checkPrivilegeLogic())
8181
{
8282
// Check login credentials
8383
if(!login())

Source/Cataphract/API/Dragon/Login.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public class Login
4848
public Login(String username) throws Exception
4949
{
5050
// If username is empty or null, assign a default user
51-
_username = (username.equals("") || username == null) ? "DEFAULT USER" : username;
51+
_username = (username == null || username.equals("")) ? "DEFAULT USER" : username;
5252
}
5353

5454
/**

Source/Cataphract/API/Minotaur/PolicyManager.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ public final void policyEditorLogic()throws Exception
7979
IOStreams.printError("Authentication Failure. Exiting...");
8080
else
8181
//Check if the policy management is enabled for users. If disabled, check if the user is admin and override the policy.
82-
if((new Cataphract.API.Minotaur.PolicyCheck().retrievePolicyValue("policy").equalsIgnoreCase("on")) || _isUserAdmin)
82+
if((new PolicyCheck().retrievePolicyValue("policy").equalsIgnoreCase("on")) || _isUserAdmin)
8383
//Call the policy editor if conditions are met.
8484
policyEditor();
8585
else

Source/Cataphract/API/Wraith/FileManagement.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ public FileManagement(String username)throws Exception
5959
// Set username to the global variable
6060
_username = username;
6161
// Set the name of the user to the global variable
62-
_name = new Cataphract.API.Dragon.Login(username).getNameLogic();
62+
_name = new Login(username).getNameLogic();
6363
// Initialize the present working directory
6464
_presentWorkingDirectory = "./Users/Cataphract/" + _username + "/";
6565
}
@@ -77,7 +77,7 @@ public FileManagement(String username)throws Exception
7777
private final boolean login()throws Exception
7878
{
7979
IOStreams.println("> Username: " + _name);
80-
return new Cataphract.API.Dragon.Login(_username).authenticationLogic(Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("> Password: "))), Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("> Security Key: ")))) ;
80+
return new Login(_username).authenticationLogic(Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("> Password: "))), Cryptography.stringToSHA3_256(String.valueOf(console.readPassword("> Security Key: ")))) ;
8181
}
8282

8383
/*****************************************
@@ -440,7 +440,7 @@ public void fileManagementLogic(String scriptFileName)throws Exception
440440
String scriptLine;
441441

442442
// Read the script file, line by line
443-
while ((scriptLine = br.readLine()) != "<EndGrinch>")
443+
while (!(scriptLine = br.readLine()).equalsIgnoreCase("<EndGrinch>"))
444444
{
445445
// Check if the line is a comment or is blank in the script file and skip the line
446446
if (scriptLine.startsWith("#") || scriptLine.equalsIgnoreCase(""))
@@ -474,7 +474,7 @@ else if (scriptLine.equalsIgnoreCase("End Script"))
474474
private void grinchInterpreter(String command)throws Exception
475475
{
476476
// Split the command string into an array of command arguments
477-
String[] commandArray = Cataphract.API.Anvil.splitStringToArray(command);
477+
String[] commandArray = Anvil.splitStringToArray(command);
478478

479479
// Switch statement to handle different commands
480480
switch (commandArray[0].toLowerCase())

Source/Cataphract/Core/Loader.java

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@
3333

3434
//Import the required Cataphract APIs
3535
import Cataphract.API.IOStreams;
36+
import Cataphract.API.Astaroth.Calendar;
37+
import Cataphract.API.Astaroth.Time;
3638
import Cataphract.API.Minotaur.Cryptography;
3739
import Cataphract.API.Anvil;
3840
import Cataphract.API.Build;
@@ -87,10 +89,10 @@ public static void main(String[] args)throws Exception
8789
throw new Exception();
8890

8991
case "astaroth":
90-
IOStreams.println(String.valueOf(new Cataphract.API.Astaroth.Time().getUnixEpoch()));
91-
IOStreams.println(String.valueOf(new Cataphract.API.Astaroth.Time().getDateTimeUsingSpecifiedFormat("dd-MMMM-yyyy \nEEEE HH:mm:ss")));
92-
new Cataphract.API.Astaroth.Calendar().printCalendar(0,0);
93-
new Cataphract.API.Astaroth.Calendar().printCalendar(8, 2077);
92+
IOStreams.println(String.valueOf(new Time().getUnixEpoch()));
93+
IOStreams.println(String.valueOf(new Time().getDateTimeUsingSpecifiedFormat("dd-MMMM-yyyy \nEEEE HH:mm:ss")));
94+
new Calendar().printCalendar(0,0);
95+
new Calendar().printCalendar(8, 2077);
9496
System.exit(0);
9597
break;
9698

@@ -594,7 +596,7 @@ protected Setup()
594596
*/
595597
private void displaySetupProgress()
596598
{
597-
Cataphract.API.Build.viewBuildInfo();
599+
Build.viewBuildInfo();
598600
IOStreams.println("[ -- Program Setup Checklist -- ]");
599601
IOStreams.println("[*] Show Program Prerequisites : " + (prereqInfoStatus?"COMPLETED":"PENDING"));
600602
IOStreams.println("[*] Initialize Directories : " + (initDirs?"COMPLETED":"PENDING"));
@@ -614,12 +616,12 @@ boolean setupCataphract()throws Exception
614616
boolean returnValue = false;
615617

616618
//A brief introduction to Cataphract and why it is being setup //
617-
Cataphract.API.Build.clearScreen();
619+
Build.clearScreen();
618620

619621
/**
620622
* Display a message to give a brief introduction to Cataphract and the reason why it is being setup
621623
*/
622-
String oobeIntroduction = Cataphract.API.Build._Branding + """
624+
String oobeIntroduction = Build._Branding + """
623625
624626
Welcome to Cataphract!
625627
@@ -635,7 +637,7 @@ boolean setupCataphract()throws Exception
635637
636638
If the current user is a System Administrator,\u00A0""";
637639

638-
Cataphract.API.IOStreams.confirmReturnToContinue(oobeIntroduction, ".\nSetup> ");
640+
IOStreams.confirmReturnToContinue(oobeIntroduction, ".\nSetup> ");
639641

640642
//User needs to accept EULA
641643
showAndAcceptEULA();
@@ -654,7 +656,7 @@ boolean setupCataphract()throws Exception
654656

655657
//Show the set of actions undertaken to the user before restarting
656658
displaySetupProgress();
657-
Cataphract.API.IOStreams.confirmReturnToContinue("Setup complete! ", ".\nSetup> ");
659+
IOStreams.confirmReturnToContinue("Setup complete! ", ".\nSetup> ");
658660

659661

660662
returnValue = prereqInfoStatus & initAdminAccount & initDB & initDirs & initPolicies;
@@ -769,7 +771,7 @@ private void initializeDatabase()
769771
catch(Exception e)
770772
{
771773
//Catch any exceptions caught during runtime and pass it on to the ExceptionHandler class
772-
new Cataphract.API.ExceptionHandler().handleException(e);
774+
new ExceptionHandler().handleException(e);
773775
}
774776

775777
//Update the Database Initialization to COMPLETE or failed

Source/Cataphract/Core/SycoraxKernel.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333

3434
import Cataphract.API.Minotaur.Cryptography;
3535
import Cataphract.API.Minotaur.PolicyCheck;
36+
import Cataphract.API.Minotaur.PolicyManager;
3637

3738
public class SycoraxKernel
3839
{
@@ -103,7 +104,7 @@ private void commandProcessor(String input)throws Exception
103104
break;
104105

105106
case "policymgmt":
106-
new Cataphract.API.Minotaur.PolicyManager().policyEditorLogic();
107+
new PolicyManager().policyEditorLogic();
107108
break;
108109

109110
case "grinch":
@@ -236,7 +237,7 @@ private boolean anvilScriptEngine(String scriptFileName)throws Exception
236237
IOStreams.printError("The name of the script file cannot be be blank.");
237238
else
238239
{
239-
if(new Cataphract.API.Minotaur.PolicyCheck().retrievePolicyValue("script").equals("on") && _isUserAdmin)
240+
if(new PolicyCheck().retrievePolicyValue("script").equals("on") && _isUserAdmin)
240241
{
241242
scriptFileName = "./Users/Cataphract/" + _username + "/" + scriptFileName;
242243
//Check if the script file specified exists.

0 commit comments

Comments
 (0)