Skip to content

Commit cdfc33c

Browse files
MVC pattern + DAO OK ---> controller, DAO, template done
1 parent b65b41a commit cdfc33c

File tree

7 files changed

+177
-15
lines changed

7 files changed

+177
-15
lines changed

Diff for: pom.xml

+10
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,16 @@
6161
<artifactId>spring-boot-autoconfigure</artifactId>
6262
<version>2.1.3.RELEASE</version>
6363
</dependency>
64+
<dependency>
65+
<groupId>org.webjars</groupId>
66+
<artifactId>Semantic-UI</artifactId>
67+
<version>2.4.1</version>
68+
</dependency>
69+
<dependency>
70+
<groupId>org.springframework.boot</groupId>
71+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
72+
</dependency>
73+
6474
</dependencies>
6575

6676
<build>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.iat.tpldapapachedirectory.dao;
2+
3+
import com.iat.tpldapapachedirectory.configuration.GlobalProperties;
4+
import com.iat.tpldapapachedirectory.model.User;
5+
import com.iat.tpldapapachedirectory.service.ConnectionService;
6+
import com.iat.tpldapapachedirectory.service.LdapQueries;
7+
import org.apache.directory.api.ldap.model.cursor.CursorException;
8+
import org.apache.directory.api.ldap.model.entry.Entry;
9+
import org.apache.directory.api.ldap.model.exception.LdapException;
10+
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.stereotype.Component;
13+
14+
import java.io.IOException;
15+
import java.util.ArrayList;
16+
17+
@Component
18+
public class UserDao {
19+
20+
// documentation : https://directory.apache.org/api/user-guide/6.12-entry.html
21+
22+
@Autowired
23+
LdapQueries ldapQueries;
24+
25+
@Autowired
26+
ConnectionService connectionService;
27+
28+
@Autowired
29+
GlobalProperties globalProperties;
30+
31+
public ArrayList<User> getAllAdmByIdAndName() throws IOException, LdapException, CursorException {
32+
LdapNetworkConnection connection = connectionService.openConnection();
33+
ArrayList<Entry> entries = ldapQueries.findAllAdm(connection, globalProperties.getDomain());
34+
ArrayList<User> admins = new ArrayList<>();
35+
for (Entry entry: entries) {
36+
// TODO : vérifier si le design pattern utilisé ici (LdapQueries + UserDao + UserController) est efficient
37+
User user = new User();
38+
user.setCategory(entry.get("radiusTunnelPrivateGroupId").get().toString());
39+
user.setName(entry.get("sn").get().toString());
40+
admins.add(user);
41+
}
42+
return admins;
43+
}
44+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
package com.iat.tpldapapachedirectory.model;
2+
3+
4+
import org.springframework.stereotype.Component;
5+
6+
import java.util.List;
7+
8+
@Component
9+
public class User {
10+
11+
private String name; // sn
12+
private String category; // radiusTunnelPrivateGroup
13+
14+
public String getName() {
15+
return name;
16+
}
17+
18+
public void setName(String name) {
19+
this.name = name;
20+
}
21+
22+
public String getCategory() {
23+
return category;
24+
}
25+
26+
public void setCategory(String category) {
27+
this.category = category;
28+
}
29+
}

Diff for: src/main/java/com/iat/tpldapapachedirectory/service/ConnectionService.java

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package com.iat.tpldapapachedirectory.service;
22

3-
import com.iat.tpldapapachedirectory.LdapQueries;
43
import com.iat.tpldapapachedirectory.configuration.GlobalProperties;
54
import org.apache.directory.api.ldap.model.exception.LdapException;
65
import org.apache.directory.ldap.client.api.LdapConnection;

Diff for: src/main/java/com/iat/tpldapapachedirectory/LdapQueries.java renamed to src/main/java/com/iat/tpldapapachedirectory/service/LdapQueries.java

+24-14
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,26 @@
1-
package com.iat.tpldapapachedirectory;
1+
package com.iat.tpldapapachedirectory.service;
22

3+
import com.iat.tpldapapachedirectory.model.User;
34
import org.apache.directory.api.ldap.model.cursor.CursorException;
45
import org.apache.directory.api.ldap.model.cursor.EntryCursor;
56
import org.apache.directory.api.ldap.model.entry.*;
67
import org.apache.directory.api.ldap.model.exception.LdapException;
78
import org.apache.directory.api.ldap.model.message.SearchScope;
89
import org.apache.directory.ldap.client.api.LdapConnection;
10+
import org.springframework.beans.factory.annotation.Autowired;
911
import org.springframework.stereotype.Component;
1012

1113
import java.io.IOException;
14+
import java.util.ArrayList;
15+
import java.util.List;
1216

1317
@Component
1418
public class LdapQueries {
1519

1620
/**
17-
* Performs search using a search of all persons in a LDAP domain (SearchScope.SearchScope.SUBTREE)
21+
* Performs search using a search of all persons in a LDAP model (SearchScope.SearchScope.SUBTREE)
1822
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
19-
* @param domain : a LDAP domain
23+
* @param domain : a LDAP model
2024
*/
2125
public void findAllPersons(LdapConnection connection, String domain) throws LdapException, CursorException, IOException {
2226

@@ -40,9 +44,11 @@ public void findAllPersons(LdapConnection connection, String domain) throws Ldap
4044
/**
4145
* Performs search using a search of all persons in a LDAP organizational unit (SearchScope.ONELEVEL)
4246
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
43-
* @param domain : a LDAP domain
47+
* @param domain : a LDAP model
4448
*/
45-
public void findAllAdm(LdapConnection connection, String domain) throws LdapException, CursorException, IOException {
49+
public ArrayList<Entry> findAllAdm(LdapConnection connection, String domain) throws LdapException, CursorException, IOException {
50+
51+
ArrayList<Entry> entries = new ArrayList<>();
4652

4753
// Root : ou=adm ; parse : all adm, only adm (one level)
4854
EntryCursor cursor = connection.search( "ou=adm, "+domain+"", "(objectclass=*)", SearchScope.ONELEVEL, "*" );
@@ -52,18 +58,22 @@ public void findAllAdm(LdapConnection connection, String domain) throws LdapExce
5258
{
5359
Entry entry = cursor.get();
5460
System.out.println(entry);
61+
entries.add(entry);
5562
nbrEntries +=1;
5663

5764
}
5865

5966
System.out.println("NOMBRE TOTAL D'ENTREES : " + nbrEntries);
67+
6068
cursor.close();
69+
70+
return entries;
6171
}
6272

6373
/**
6474
* Add an entry (a person) to the LDAP server
6575
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
66-
* @param domain : a LDAP domain
76+
* @param domain : a LDAP model
6777
* @param cn : a LDAP common name
6878
* @param sn : a LDAP surname
6979
*/
@@ -85,7 +95,7 @@ public void addPerson(LdapConnection connection, String domain, String cn, Strin
8595
/**
8696
* Delete the entry (a person) with the given distinguished name to the LDAP server
8797
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
88-
* @param domain : a LDAP domain
98+
* @param domain : a LDAP model
8999
* @param cn : a LDAP common name
90100
*/
91101
public void deletePerson(LdapConnection connection, String domain, String cn) throws Exception {
@@ -97,7 +107,7 @@ public void deletePerson(LdapConnection connection, String domain, String cn) th
97107
* Applies all the modifications (add attributes + values = ModificationOperation.ADD_ATTRIBUTE)...
98108
* ... to the entry (a person) specified by its distinguished name
99109
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
100-
* @param domain : a LDAP domain
110+
* @param domain : a LDAP model
101111
* @param cn : a LDAP common name
102112
* @param attributeId1 : first attribute to add
103113
* @param value1 : first value to add
@@ -119,7 +129,7 @@ public void addAttributesToPerson(LdapConnection connection, String domain, Stri
119129
* Applies all the modifications (remove attributes = ModificationOperation.REMOVE_ATTRIBUTE)...
120130
* ... to the entry (a person) specified by its distinguished name
121131
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
122-
* @param domain : a LDAP domain
132+
* @param domain : a LDAP model
123133
* @param cn : a LDAP common name
124134
* @param attributeId1 : first attribute to remove
125135
* @param attributeId2 : second attribute to remove
@@ -137,7 +147,7 @@ public void removeAttributesToPerson(LdapConnection connection, String domain, S
137147
* Applies all the modifications (replace attributes + values = ModificationOperation.REPLACE_ATTRIBUTE)...
138148
* ... to the entry (a person) specified by its distinguished name
139149
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
140-
* @param domain : a LDAP domain
150+
* @param domain : a LDAP model
141151
* @param cn : a LDAP common name
142152
* @param attributeId1 : first attribute to replace
143153
* @param value1 : first value to replace
@@ -168,9 +178,9 @@ public void moveAndRenamePerson(LdapConnection connection, String oldDn, String
168178
}
169179

170180
/**
171-
* Performs search using a search of all organizational unit in a LDAP domain (SearchScope.SearchScope.ONELEVEL)
181+
* Performs search using a search of all organizational unit in a LDAP model (SearchScope.SearchScope.ONELEVEL)
172182
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
173-
* @param domain : a LDAP domain
183+
* @param domain : a LDAP model
174184
*/
175185
public void findAllOu(LdapConnection connection, String domain) throws LdapException, CursorException, IOException {
176186

@@ -193,7 +203,7 @@ public void findAllOu(LdapConnection connection, String domain) throws LdapExcep
193203
/**
194204
* Add an entry (an organizational unit) to the LDAP server
195205
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
196-
* @param domain : a LDAP domain
206+
* @param domain : a LDAP model
197207
* @param ou : an organizational unit
198208
*/
199209
public void addOu(LdapConnection connection, String domain, String ou) throws LdapException {
@@ -210,7 +220,7 @@ public void addOu(LdapConnection connection, String domain, String ou) throws Ld
210220
/**
211221
* Delete the entry (an organizational unit) with the given distinguished name to the LDAP server
212222
* @param connection : an instance of LdapNetworkConnection (interface LdapConnection)
213-
* @param domain : a LDAP domain
223+
* @param domain : a LDAP model
214224
*/
215225
public void deleteOu(LdapConnection connection, String domain,String ou) throws Exception {
216226

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
package com.iat.tpldapapachedirectory.web;
2+
3+
import com.iat.tpldapapachedirectory.configuration.GlobalProperties;
4+
import com.iat.tpldapapachedirectory.dao.UserDao;
5+
import com.iat.tpldapapachedirectory.model.User;
6+
import com.iat.tpldapapachedirectory.service.LdapQueries;
7+
import com.iat.tpldapapachedirectory.service.ConnectionService;
8+
import org.apache.directory.api.ldap.model.cursor.CursorException;
9+
import org.apache.directory.api.ldap.model.exception.LdapException;
10+
import org.apache.directory.ldap.client.api.LdapNetworkConnection;
11+
import org.springframework.beans.factory.annotation.Autowired;
12+
import org.springframework.stereotype.Controller;
13+
import org.springframework.ui.Model;
14+
import org.springframework.web.bind.annotation.GetMapping;
15+
16+
import java.io.IOException;
17+
18+
@Controller
19+
public class UserController {
20+
21+
@Autowired
22+
ConnectionService connectionService;
23+
24+
@Autowired
25+
GlobalProperties globalProperties;
26+
27+
@Autowired
28+
UserDao userDao;
29+
30+
@GetMapping("/admList")
31+
public String admList(Model model) throws IOException, LdapException, CursorException {
32+
Iterable<User> admins = userDao.getAllAdmByIdAndName();
33+
model.addAttribute("admins", admins);
34+
model.addAttribute("msg", "Ceci est un test");
35+
return "admList";
36+
}
37+
38+
}

Diff for: src/main/resources/templates/admList.html

+32
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
<!DOCTYPE html>
2+
<html xmlns:th="http://www.thymeleaf.org" lang="en">
3+
<head>
4+
<meta charset="UTF-8">
5+
<title>Liste des Admins</title>
6+
</head>
7+
<body>
8+
<div>
9+
<h2 th:text="'Liste des Admins'">Liste des Admins</h2>
10+
<h3 th:text="${msg}">msg</h3>
11+
<table border="1">
12+
<thead>
13+
<tr>
14+
<th>Nom</th>
15+
<th>Catégorie</th>
16+
</tr>
17+
</thead>
18+
<tbody>
19+
<tr th:each="admin : ${admins}">
20+
<td>
21+
<a th:text="${admin.name}"></a>
22+
</td>
23+
<td>
24+
<a th:text="${admin.category}"></a>
25+
</td>
26+
27+
</tr>
28+
</tbody>
29+
</table>
30+
</div>
31+
</body>
32+
</html>

0 commit comments

Comments
 (0)