|
|
@ -1,15 +1,36 @@ |
|
|
|
import java.util.ArrayList; |
|
|
|
import java.io.FileInputStream; |
|
|
|
import java.io.FileOutputStream; |
|
|
|
import java.io.ObjectInputStream; |
|
|
|
import java.io.ObjectOutputStream; |
|
|
|
|
|
|
|
/** |
|
|
|
* Credential Repository for handling user credentials |
|
|
|
* @author Claudia Metzler |
|
|
|
*/ |
|
|
|
|
|
|
|
public class CredentialRepository implements CredentialRepositoryInterface{ |
|
|
|
|
|
|
|
private int idCounter = 0; |
|
|
|
private ArrayList<Credential> credentials; |
|
|
|
private CredentialList credentials; |
|
|
|
|
|
|
|
/** |
|
|
|
* Konstruktor |
|
|
|
* |
|
|
|
* Initializes instance variable ArrayList |
|
|
|
*/ |
|
|
|
public CredentialRepository() |
|
|
|
{ |
|
|
|
this.credentials = new ArrayList<Credential>(); |
|
|
|
this.credentials = new CredentialList(); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Create a new Set of User credentials |
|
|
|
* @param name |
|
|
|
* @param password |
|
|
|
* @return size of repositoy - arraylist for checking |
|
|
|
*/ |
|
|
|
|
|
|
|
public int createNewCredential(String name, String password) |
|
|
|
{ |
|
|
|
try { |
|
|
@ -23,6 +44,12 @@ public class CredentialRepository implements CredentialRepositoryInterface{ |
|
|
|
return this.getListSize(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function to return a specific Credential via Name input |
|
|
|
* @param needle [ aka to-search-for ] |
|
|
|
* @return usercredential | NULL |
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
public Credential getCredentialsViaName(String needle) { |
|
|
|
|
|
|
@ -38,6 +65,12 @@ public class CredentialRepository implements CredentialRepositoryInterface{ |
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Function to return a specific Credential via ID |
|
|
|
* @param id |
|
|
|
* @return usercredential | NULL |
|
|
|
*/ |
|
|
|
|
|
|
|
@Override |
|
|
|
public Credential getCredentialsViaId(int id) { |
|
|
|
|
|
|
@ -51,13 +84,11 @@ public class CredentialRepository implements CredentialRepositoryInterface{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
The next 3 functions assume you already have successfully |
|
|
|
pulled a credential from the repository and now want to edit it. |
|
|
|
Thus, these functions require you to pass the desired credential's index |
|
|
|
/** |
|
|
|
* Function to update userpassword, assuming we already know the userid |
|
|
|
* @param index |
|
|
|
* @param newPassword |
|
|
|
*/ |
|
|
|
|
|
|
|
|
|
|
|
@Override |
|
|
|
public void updatePassword(int index, String newPassword) { |
|
|
|
|
|
|
@ -71,6 +102,12 @@ public class CredentialRepository implements CredentialRepositoryInterface{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/** |
|
|
|
* Function to update username analoge to updatePassword |
|
|
|
* @param index |
|
|
|
* @param newUsername |
|
|
|
*/ |
|
|
|
public void updateUsername(int index, String newUsername){ |
|
|
|
|
|
|
|
try{ |
|
|
@ -82,6 +119,11 @@ public class CredentialRepository implements CredentialRepositoryInterface{ |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Deletion function for credentials |
|
|
|
* Will immediately delete - double check for user permission |
|
|
|
* @param index |
|
|
|
*/ |
|
|
|
@Override |
|
|
|
public void delete(int index) { |
|
|
|
|
|
|
@ -89,28 +131,68 @@ public class CredentialRepository implements CredentialRepositoryInterface{ |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
/* |
|
|
|
this function will be required in the future for catching the left-shift in the arrayList |
|
|
|
after one entry was deleted. welp, maybe it won't. can't tell and i've no time atm to |
|
|
|
finish this |
|
|
|
/** |
|
|
|
* Function for serialization of all Credentials |
|
|
|
* @param fileName |
|
|
|
*/ |
|
|
|
|
|
|
|
public void serializeObject(String fileName) |
|
|
|
{ |
|
|
|
if(fileName.equals("")) return; |
|
|
|
|
|
|
|
private int mapIndexToListCounter(int needle) { |
|
|
|
for(int c = 0; c<this.credentials.size(); c++) |
|
|
|
try { |
|
|
|
FileOutputStream file = new FileOutputStream(this.getWorkingDirectory() + fileName + ".claud11"); |
|
|
|
ObjectOutputStream out = new ObjectOutputStream(file); |
|
|
|
out.writeObject(this.credentials); |
|
|
|
} catch (Exception fail) { |
|
|
|
System.err.println("Serialization failed!"); |
|
|
|
fail.printStackTrace(); |
|
|
|
} |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Function to load serialized Objects from hard drive by path |
|
|
|
* @param fileName |
|
|
|
*/ |
|
|
|
public void deserializeObjects(String fileName) |
|
|
|
{ |
|
|
|
if(fileName.equals("")) return; |
|
|
|
|
|
|
|
try { |
|
|
|
FileInputStream file = new FileInputStream(this.getWorkingDirectory() + fileName + ".claud11"); |
|
|
|
ObjectInputStream in = new ObjectInputStream(file); |
|
|
|
|
|
|
|
this.credentials = (CredentialList)in.readObject(); |
|
|
|
|
|
|
|
in.close(); |
|
|
|
file.close(); |
|
|
|
|
|
|
|
} catch(Exception fail) |
|
|
|
{ |
|
|
|
if(this.credentials.get(c).getId() == needle) |
|
|
|
{ |
|
|
|
return c; |
|
|
|
} |
|
|
|
System.err.println("Loading of CredentialRepository failed"); |
|
|
|
} |
|
|
|
return -1; |
|
|
|
} |
|
|
|
*/ |
|
|
|
public int getListSize() |
|
|
|
|
|
|
|
/** |
|
|
|
* helper function to check list size after insertion / deletion |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
|
|
|
|
private int getListSize() |
|
|
|
{ |
|
|
|
return this.credentials.size(); |
|
|
|
} |
|
|
|
|
|
|
|
/** |
|
|
|
* Helper function for serialization |
|
|
|
* @return |
|
|
|
*/ |
|
|
|
|
|
|
|
private String getWorkingDirectory() |
|
|
|
{ |
|
|
|
return System.getProperty("user.dir"); |
|
|
|
} |
|
|
|
|
|
|
|
} |
|
|
|
|
|
|
|
|