Commit 73c004dbee4aea36ccaee71b4f936fb377cc3c37
1 parent
e0e8eef8
TP presque terminé. Plus que 4 tests à résoudre.
Showing
9 changed files
with
150 additions
and
18 deletions
Show diff stats
src/main/java/fr/plil/sio/persistence/api/Group.java
src/main/java/fr/plil/sio/persistence/jdbc/GroupRepository.java
src/main/java/fr/plil/sio/persistence/jdbc/GroupRepositoryJdbc.java
... | ... | @@ -93,7 +93,7 @@ public class GroupRepositoryJdbc implements GroupRepository { |
93 | 93 | } |
94 | 94 | try { |
95 | 95 | stmt = dataSource.getConnection().createStatement(); |
96 | - stmt.execute("DELETE FROM USER_T WHERE GROUP_U = " + id); | |
96 | + stmt.execute("DELETE FROM USER_T WHERE GROUP_ID = " + id); | |
97 | 97 | stmt.execute("DELETE FROM GROUP_T WHERE GROUP_ID = " + id); |
98 | 98 | } catch (SQLException e) { |
99 | 99 | throw new UnsupportedOperationException("sql exception", e); |
... | ... | @@ -110,4 +110,56 @@ public class GroupRepositoryJdbc implements GroupRepository { |
110 | 110 | } |
111 | 111 | } |
112 | 112 | } |
113 | + | |
114 | + @Override | |
115 | + public void removeRight(Long right, Long group){ | |
116 | + Statement stmt = null; | |
117 | + ResultSet rs = null; | |
118 | + if(right==null || group==null){ | |
119 | + throw new IllegalArgumentException(); | |
120 | + } | |
121 | + try { | |
122 | + stmt = dataSource.getConnection().createStatement(); | |
123 | + stmt.execute("DELETE FROM LINK_T WHERE GROUP_ID = " + group + " AND RIGHT_ID = " + right); | |
124 | + } catch (SQLException e) { | |
125 | + throw new UnsupportedOperationException("sql exception", e); | |
126 | + } finally { | |
127 | + try { | |
128 | + if (rs != null) { | |
129 | + rs.close(); | |
130 | + } | |
131 | + if (stmt != null) { | |
132 | + stmt.close(); | |
133 | + } | |
134 | + } catch (SQLException e) { | |
135 | + throw new UnsupportedOperationException("sql exception during close", e); | |
136 | + } | |
137 | + } | |
138 | + } | |
139 | + @Override | |
140 | + public void addRight(Long right, Long group){ | |
141 | + Statement stmt = null; | |
142 | + ResultSet rs = null; | |
143 | + if(right==null || group==null){ | |
144 | + throw new IllegalArgumentException(); | |
145 | + } | |
146 | + try { | |
147 | + stmt = dataSource.getConnection().createStatement(); | |
148 | + stmt.execute("INSERT INTO LINK_T(RIGHT_ID, GROUP_ID) VALUES (" + group + "," + right + ")"); | |
149 | + } catch (SQLException e) { | |
150 | + throw new UnsupportedOperationException("sql exception", e); | |
151 | + } finally { | |
152 | + try { | |
153 | + if (rs != null) { | |
154 | + rs.close(); | |
155 | + } | |
156 | + if (stmt != null) { | |
157 | + stmt.close(); | |
158 | + } | |
159 | + } catch (SQLException e) { | |
160 | + throw new UnsupportedOperationException("sql exception during close", e); | |
161 | + } | |
162 | + } | |
163 | + } | |
164 | + | |
113 | 165 | } | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/GroupServiceJdbc.java
... | ... | @@ -13,6 +13,12 @@ public class GroupServiceJdbc implements GroupService { |
13 | 13 | |
14 | 14 | @Autowired |
15 | 15 | private GroupRepository groupRepository; |
16 | + | |
17 | + @Autowired | |
18 | + private RightRepository rightRepository; | |
19 | + | |
20 | + @Autowired | |
21 | + private UserRepository userRepository; | |
16 | 22 | |
17 | 23 | @Override |
18 | 24 | public Group create(String name) { |
... | ... | @@ -64,28 +70,44 @@ public class GroupServiceJdbc implements GroupService { |
64 | 70 | throw new IllegalArgumentException("group cannot be null"); |
65 | 71 | } |
66 | 72 | |
73 | + if(userRepository.isUserHasRight(groupRepository.findByName(groupName).getId(), right.getId())){ | |
74 | + return false; | |
75 | + } | |
76 | + | |
67 | 77 | Group g = groupRepository.findByName(groupName); |
68 | - List<Right> list = g.getRights(); | |
69 | - list.add(right); | |
70 | -// g.setRights(list); | |
78 | + | |
79 | + g.getRights().add(right); | |
80 | + | |
81 | + groupRepository.addRight(g.getId(), right.getId()); | |
71 | 82 | |
72 | 83 | return true; |
73 | 84 | } |
74 | 85 | |
75 | 86 | @Override |
76 | 87 | public boolean removeRight(String groupName, Right right) { |
88 | + if(groupName==null){ | |
89 | + throw new IllegalArgumentException("Groupname cannot be null"); | |
90 | + } | |
77 | 91 | if(right==null){ |
78 | 92 | throw new IllegalArgumentException("right cannot be null"); |
79 | 93 | } |
80 | 94 | Group group = groupRepository.findByName(groupName); |
81 | 95 | if (group != null) { |
82 | - throw new IllegalStateException("a group with the same name already exists"); | |
96 | + throw new IllegalArgumentException("a group with the same name already exists"); | |
83 | 97 | } |
98 | + | |
99 | + groupRepository.removeRight(group.getId(), right.getId()); | |
100 | + | |
101 | + group.getRights().remove(right); | |
102 | + | |
84 | 103 | return true; |
85 | 104 | } |
86 | 105 | |
87 | - public List<Right> getListRight(String groupName){ | |
106 | + public List<Right> addListRight(String groupName, Right right){ | |
88 | 107 | Group g = groupRepository.findByName(groupName); |
108 | + g.getRights().add(right); | |
89 | 109 | return g.getRights(); |
90 | 110 | } |
111 | + | |
112 | + | |
91 | 113 | } | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/RightRepositoryJdbc.java
... | ... | @@ -21,8 +21,8 @@ import org.springframework.stereotype.Repository; |
21 | 21 | @Repository |
22 | 22 | public class RightRepositoryJdbc implements RightRepository { |
23 | 23 | |
24 | - | |
25 | 24 | private GroupRepository groupRepository; |
25 | + | |
26 | 26 | private RightRepository rightRepository; |
27 | 27 | |
28 | 28 | private static final Logger logger = LoggerFactory.getLogger(GroupRepository.class); |
... | ... | @@ -140,7 +140,7 @@ public class RightRepositoryJdbc implements RightRepository { |
140 | 140 | return false; |
141 | 141 | } |
142 | 142 | if(findOne(id)==null){ |
143 | - return false; | |
143 | + throw new IllegalArgumentException("droit inexistant"); | |
144 | 144 | } |
145 | 145 | try { |
146 | 146 | stmt = dataSource.getConnection().createStatement(); | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/RightServiceJdbc.java
... | ... | @@ -51,7 +51,9 @@ public class RightServiceJdbc implements RightService { |
51 | 51 | if(right==null){ |
52 | 52 | throw new IllegalArgumentException("name cannot be null"); |
53 | 53 | } |
54 | - | |
54 | + if(rightRepository.findByName(right.getName()).size()==0){ | |
55 | + throw new IllegalArgumentException("right not in database"); | |
56 | + } | |
55 | 57 | return rightRepository.delete(right.getId()); |
56 | 58 | } |
57 | 59 | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserRepository.java
src/main/java/fr/plil/sio/persistence/jdbc/UserRepositoryJdbc.java
... | ... | @@ -33,7 +33,7 @@ public class UserRepositoryJdbc implements UserRepository{ |
33 | 33 | ResultSet rs = null; |
34 | 34 | try { |
35 | 35 | stmt = dataSource.getConnection().createStatement(); |
36 | - rs = stmt.executeQuery("SELECT USER_ID, NAME_U, U.GROUP_ID, NAME_C FROM USER_T U JOIN GROUP_T G ON U.GROUP_ID=G.GROUP_ID"); | |
36 | + rs = stmt.executeQuery("SELECT USER_ID, NAME_U, U.GROUP_ID, NAME_C FROM USER_T U JOIN GROUP_T G ON U.GROUP_ID=G.GROUP_ID WHERE NAME_U = \'" + name + "\'"); | |
37 | 37 | if (rs.next()) { |
38 | 38 | logger.debug("found group " + name); |
39 | 39 | User user = new User(); |
... | ... | @@ -123,4 +123,33 @@ public class UserRepositoryJdbc implements UserRepository{ |
123 | 123 | } |
124 | 124 | } |
125 | 125 | } |
126 | + | |
127 | + @Override | |
128 | + public boolean isUserHasRight(Long group, Long right){ | |
129 | + Statement stmt = null; | |
130 | + ResultSet rs = null; | |
131 | + try { | |
132 | + stmt = dataSource.getConnection().createStatement(); | |
133 | + rs = stmt.executeQuery("SELECT * FROM LINK_T WHERE RIGHT_ID = " + right + " AND GROUP_ID = " + group ); | |
134 | + if (rs.next()) { | |
135 | + return true; | |
136 | + } else { | |
137 | + return false; | |
138 | + } | |
139 | + } catch (SQLException e) { | |
140 | + throw new UnsupportedOperationException("sql exception", e); | |
141 | + } finally { | |
142 | + try { | |
143 | + if (rs != null) { | |
144 | + rs.close(); | |
145 | + } | |
146 | + if (stmt != null) { | |
147 | + stmt.close(); | |
148 | + } | |
149 | + } catch (SQLException e) { | |
150 | + throw new UnsupportedOperationException("sql exception during close", e); | |
151 | + | |
152 | + } | |
153 | + } | |
154 | + } | |
126 | 155 | } | ... | ... |
src/main/java/fr/plil/sio/persistence/jdbc/UserServiceJdbc.java
... | ... | @@ -4,6 +4,12 @@ import fr.plil.sio.persistence.api.Right; |
4 | 4 | import fr.plil.sio.persistence.api.Group; |
5 | 5 | import fr.plil.sio.persistence.api.User; |
6 | 6 | import fr.plil.sio.persistence.api.UserService; |
7 | +import java.sql.ResultSet; | |
8 | +import java.sql.SQLException; | |
9 | +import java.sql.Statement; | |
10 | +import javax.sql.DataSource; | |
11 | +import org.slf4j.Logger; | |
12 | +import org.slf4j.LoggerFactory; | |
7 | 13 | import org.springframework.beans.factory.annotation.Autowired; |
8 | 14 | import org.springframework.stereotype.Service; |
9 | 15 | |
... | ... | @@ -15,25 +21,35 @@ public class UserServiceJdbc implements UserService { |
15 | 21 | |
16 | 22 | @Autowired |
17 | 23 | private GroupRepository groupRepository; |
24 | + | |
25 | + @Autowired | |
26 | + private RightRepository rightRepository; | |
18 | 27 | |
28 | + @Autowired | |
29 | + private DataSource dataSource; | |
30 | + | |
31 | + private static final Logger logger = LoggerFactory.getLogger(UserRepository.class); | |
32 | + | |
19 | 33 | @Override |
20 | 34 | public User create(String name, String groupName) { |
21 | 35 | if(name==null || groupName==null){ |
22 | 36 | throw new IllegalArgumentException("name cannot be null"); |
23 | 37 | } |
38 | + Group group = new Group(); | |
39 | + group = groupRepository.findByName(groupName); | |
24 | 40 | User user = userRepository.findByName(name); |
41 | + if(group==null){ | |
42 | + throw new IllegalArgumentException("group does not exist"); | |
43 | + } | |
25 | 44 | if(user!=null){ |
26 | - throw new IllegalArgumentException("a user with the same name already exists"); | |
45 | + throw new IllegalStateException("a user with the same name already exists"); | |
27 | 46 | } |
28 | 47 | user = new User(); |
29 | 48 | user.setName(name); |
30 | - Group group = new Group(); | |
31 | - group = groupRepository.findByName(groupName); | |
49 | + | |
32 | 50 | if(group!=null) |
33 | 51 | user.setGroup(group); |
34 | - else{ | |
35 | - throw new IllegalArgumentException("group trouve avec le find by name null"); | |
36 | - } | |
52 | + | |
37 | 53 | userRepository.save(user, group); |
38 | 54 | return user; |
39 | 55 | } |
... | ... | @@ -67,8 +83,9 @@ public class UserServiceJdbc implements UserService { |
67 | 83 | if(right==null){ |
68 | 84 | throw new IllegalArgumentException("right cannot be null"); |
69 | 85 | } |
70 | - else{ | |
71 | - return false; | |
86 | + if(rightRepository.findByName(right.getName()).size()==0){ | |
87 | + throw new IllegalArgumentException("right not in database"); | |
72 | 88 | } |
89 | + return userRepository.isUserHasRight(userRepository.findByName(userName).getId(), right.getId()); | |
73 | 90 | } |
74 | 91 | } | ... | ... |