Skip to content
Snippets Groups Projects
Commit 8e3acfbe authored by George Peppard's avatar George Peppard
Browse files

actually add code

parent 645b28e5
No related branches found
No related tags found
No related merge requests found
......@@ -36,6 +36,10 @@ The `-u` and `-p` parameters are required, as is the username to query
(obviously). The information above can be displayed at any time by
running the software with the `-?` or `--help` arguments.
If you do not want to give the program your password in plain text (you shouldn't)
then you can use an encrypted form by prefixing it with `{crypt}` and
encrypting it by following [these instructions](https://www.openldap.org/faq/data/cache/344.html).
## License and limitation of liability
This program is free software: you can redistribute it and/or modify
......@@ -58,3 +62,13 @@ To put it briefly, I was not sure how the University would react to me
using their LDAP server for this purpose (although one could argue that
if they didn't want me to use it, then I should not have access!), hence
the project can only be viewed internally on the University's Git service.
## Things that could be improved
As there is no reason anyone would actually use this software, there is
no real reason to modify it in the future. The following is a list of things
that could be improved to make the software better.
- Right now it is not possible to specify the LDAP server hostname or port.
This means if the University decides to change their hostname or port, the
program will break.
......@@ -13,8 +13,8 @@
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<maven.compiler.source>1.17</maven.compiler.source>
<maven.compiler.target>1.17</maven.compiler.target>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
</properties>
<dependencies>
......@@ -58,8 +58,17 @@
<version>2.22.1</version>
</plugin>
<plugin>
<artifactId>maven-jar-plugin</artifactId>
<version>3.0.2</version>
<artifactId>maven-assembly-plugin</artifactId>
<configuration>
<archive>
<manifest>
<mainClass>com.gpeppard.ecsuserfetcher.Main</mainClass>
</manifest>
</archive>
<descriptorRefs>
<descriptorRef>jar-with-dependencies</descriptorRef>
</descriptorRefs>
</configuration>
</plugin>
<plugin>
<artifactId>maven-install-plugin</artifactId>
......@@ -78,16 +87,20 @@
<artifactId>maven-project-info-reports-plugin</artifactId>
<version>3.0.0</version>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-javadoc-plugin</artifactId>
<version>3.3.1</version>
<configuration>
<show>private</show>
</configuration>
</plugin>
</plugins>
</pluginManagement>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<configuration>
<source>17</source>
<target>17</target>
</configuration>
</plugin>
</plugins>
</build>
......
package com.gpeppard.ecsuserfetcher;
import com.gpeppard.ecsuserfetcher.models.Person;
import org.apache.directory.api.ldap.model.cursor.CursorException;
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
import org.apache.directory.api.ldap.model.entry.Entry;
import org.apache.directory.api.ldap.model.exception.LdapException;
import org.apache.directory.api.ldap.model.exception.LdapInvalidAttributeValueException;
import org.apache.directory.api.ldap.model.message.SearchScope;
import org.apache.directory.ldap.client.api.LdapConnection;
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
import java.io.IOException;
/**
* A single instance of the program.
*
* @author George Peppard <gjp1g21@soton.ac.uk>
* @author George Peppard {@literal <gjp1g21@soton.ac.uk>}
*/
public class EcsUserFetcher {
private final String ldapUsername;
......@@ -12,9 +24,9 @@ public class EcsUserFetcher {
private final boolean detailed;
/**
* Constructs a new instance of SCWebScraper.
* Constructs a new instance of EcsUserFetcher.
*
* We do not want everything to be static so instead, we use this object instead to hold (or
* <p>We do not want everything to be static so instead, we use this object instead to hold (or
* call) the majority of program code.
*
* @param ldapUsername the LDAP username of the user accessing the data
......@@ -29,10 +41,146 @@ public class EcsUserFetcher {
this.detailed = detailed;
}
/** Run the application. */
public void run() {
Person person = fetchUser(query);
if (person == null) {
System.err.println("There was an error whilst trying to fetch this user.");
return;
}
outputUserInformation(person);
}
/**
* Run the application.
* Fetches a {@link Person} from the LDAP server.
*
* @param mailNickname the mailNickname attribute of the person you want to search for
* @return the Person who has been returned, or {@code null} if they do not exist or an error occurs
*/
public void run() {
private Person fetchUser(String mailNickname) {
/* Connect to the LDAP server and bind credentials */
LdapConnection connection = new LdapNetworkConnection("ldap.soton.ac.uk", 389);
try {
connection.bind(ldapUsername, ldapPassword);
} catch (LdapException e) {
System.err.println("Failed to connect to the LDAP server: " + e.getMessage());
System.err.println("Are your credentials correct?");
try {
connection.close();
} catch(IOException ex) {
ex.printStackTrace();
}
return null;
}
/* Search for the user by mailNickname */
EntryCursor cur;
try {
cur = connection.search(
"OU=fp,OU=fp,OU=ek,OU=User,DC=soton,DC=ac,DC=uk", "(mailNickname=" + query + ")", SearchScope.SUBTREE, "*");
} catch(LdapException e) {
try {
connection.close();
} catch(IOException ex) {
ex.printStackTrace();
}
System.err.println("An error occurred whilst searching for the user: " + e.getMessage());
return null;
}
/* Check to make sure this object exists */
try {
if (!cur.next()) {
System.err.println("There were no results for that email nickname. Does the user exist?");
try {
connection.close();
} catch(IOException ex) {
ex.printStackTrace();
}
return null;
}
} catch(LdapException | CursorException e) {
/* this should never happen */
e.printStackTrace();
return null;
}
/* Get first (there should only ever be one) object from server and then close connection */
Entry entry;
try {
entry = cur.get();
} catch(CursorException e) {
/* this should never happen so we just print the stack trace */
e.printStackTrace();
try {
connection.close();
} catch(IOException ex) {
ex.printStackTrace();
}
return null;
}
try {
connection.unBind();
connection.close();
} catch (LdapException | IOException e) {
System.err.println("An exception occurred when closing the LDAP connection: " + e.getMessage());
}
/* Move information from the LDAP object into a new Person */
Person person;
try {
person = new Person(
entry.get("mailNickname").getString(),
entry.get("givenName").getString(),
entry.get("sn").getString(),
entry.get("title").getString(),
entry.get("department").getString()
);
} catch (LdapInvalidAttributeValueException e) {
System.err.println("Could not get required attributes for this person. Is it an actual person?");
return null;
}
/* Return the Person */
return person;
}
/**
* Outputs the user information to the console.
*/
private void outputUserInformation(Person person) {
if (detailed) {
outputDetailedUserInformation(person);
} else {
outputBriefUserInformation(person);
}
}
/**
* Outputs the user name to the console.
*/
private void outputBriefUserInformation(Person person) {
System.out.println("User: " + person.getMailName());
System.out.println("Full name: " + person.getGivenName() + " " + person.getSurname());
}
/**
* Outputs detailed user information to the console.
*/
private void outputDetailedUserInformation(Person person) {
System.out.println("User: " + person.getMailName());
System.out.println("Full name: " + person.getGivenName() + " " + person.getSurname());
System.out.println("Position title: " + person.getJobTitle());
System.out.println("Department: " + person.getDepartment());
}
}
......@@ -5,7 +5,7 @@ import org.apache.commons.cli.*;
/**
* Main application class.
*
* @author George Peppard <gjp1g21@soton.ac.uk>
* @author George Peppard {@literal <gjp1g21@soton.ac.uk>}
*/
public class Main {
/**
......
package com.gpeppard.ecsuserfetcher.models;
/**
* A single person whose data is stored in the LDAP server.
*
* @author George Peppard {@literal <gjp1g21@soton.ac.uk>}
*/
public class Person {
private final String mailName;
private final String givenName;
private final String surname;
private final String jobTitle;
private final String department;
/**
* Constructs a new instance of {@code Person}.
*
* @param mailName the mail nickname of the person
* @param givenName the first name of the person
* @param surname the surname of the person
* @param jobTitle the position title of the person
* @param department the department of the person
*/
public Person(String mailName, String givenName, String surname, String jobTitle, String department) {
this.mailName = mailName;
this.givenName = givenName;
this.surname = surname;
this.jobTitle = jobTitle;
this.department = department;
}
/**
* Gets the mail nickname of the person.
*
* @return the mail nickname of the person
*/
public String getMailName() {
return mailName;
}
/**
* Gets the first name of the person.
*
* @return the first name of the person
*/
public String getGivenName() {
return givenName;
}
/**
* Gets the surname of the person.
*
* @return the surname of the person
*/
public String getSurname() {
return surname;
}
/**
* Gets the position title of the person.
*
* @return the position title of the person
*/
public String getJobTitle() {
return jobTitle;
}
/**
* Gets the department of the person.
*
* @return the department of the person
*/
public String getDepartment() {
return department;
}
}
package com.gpeppard.ecsuserfetcher.models;
public class User {
}
package com.gpeppard;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
/**
* Unit test for simple App.
*/
public class AppTest
{
/**
* Rigorous Test :-)
*/
@Test
public void shouldAnswerWithTrue()
{
assertTrue( true );
}
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment