21
21
22
22
import java .util .List ;
23
23
24
+ import javax .persistence .EntityManager ;
25
+
24
26
import org .junit .Before ;
25
27
import org .junit .Test ;
26
28
import org .junit .runner .RunWith ;
@@ -46,10 +48,15 @@ public class SecurityIntegrationTests {
46
48
@ Autowired UserRepository userRepository ;
47
49
@ Autowired BusinessObjectRepository businessObjectRepository ;
48
50
@ Autowired SecureBusinessObjectRepository secureBusinessObjectRepository ;
51
+ @ Autowired EntityManager em ;
49
52
50
53
User tom ;
51
54
User olli ;
52
55
User admin ;
56
+
57
+ UsernamePasswordAuthenticationToken olliAuth ;
58
+ UsernamePasswordAuthenticationToken tomAuth ;
59
+ UsernamePasswordAuthenticationToken adminAuth ;
53
60
54
61
BusinessObject object1 ;
55
62
BusinessObject object2 ;
@@ -65,12 +72,16 @@ public void setup() {
65
72
object1 = businessObjectRepository .save (new BusinessObject ("object1" , olli ));
66
73
object2 = businessObjectRepository .save (new BusinessObject ("object2" , olli ));
67
74
object3 = businessObjectRepository .save (new BusinessObject ("object3" , tom ));
75
+
76
+ olliAuth = new UsernamePasswordAuthenticationToken (olli , "x" );
77
+ tomAuth = new UsernamePasswordAuthenticationToken (tom , "x" );
78
+ adminAuth = new UsernamePasswordAuthenticationToken (admin , "x" , singleton (new SimpleGrantedAuthority ("ROLE_ADMIN" )));
68
79
}
69
80
70
81
@ Test
71
82
public void findBusinessObjectsForCurrentUserShouldReturnOnlyBusinessObjectsWhereCurrentUserIsOwner () {
72
83
73
- SecurityContextHolder .getContext ().setAuthentication (new UsernamePasswordAuthenticationToken ( tom , "x" ) );
84
+ SecurityContextHolder .getContext ().setAuthentication (tomAuth );
74
85
75
86
List <BusinessObject > businessObjects = secureBusinessObjectRepository .findBusinessObjectsForCurrentUser ();
76
87
@@ -88,8 +99,7 @@ public void findBusinessObjectsForCurrentUserShouldReturnOnlyBusinessObjectsWher
88
99
@ Test
89
100
public void findBusinessObjectsForCurrentUserShouldReturnAllObjectsForAdmin () {
90
101
91
- SecurityContextHolder .getContext ().setAuthentication (
92
- new UsernamePasswordAuthenticationToken (admin , "x" , singleton (new SimpleGrantedAuthority ("ROLE_ADMIN" ))));
102
+ SecurityContextHolder .getContext ().setAuthentication (adminAuth );
93
103
94
104
List <BusinessObject > businessObjects = secureBusinessObjectRepository .findBusinessObjectsForCurrentUser ();
95
105
@@ -100,14 +110,14 @@ public void findBusinessObjectsForCurrentUserShouldReturnAllObjectsForAdmin() {
100
110
@ Test
101
111
public void findBusinessObjectsForCurrentUserByIdShouldReturnOnlyBusinessObjectsWhereCurrentUserIsOwner () {
102
112
103
- SecurityContextHolder .getContext ().setAuthentication (new UsernamePasswordAuthenticationToken ( tom , "x" ) );
113
+ SecurityContextHolder .getContext ().setAuthentication (tomAuth );
104
114
105
115
List <BusinessObject > businessObjects = secureBusinessObjectRepository .findBusinessObjectsForCurrentUserById ();
106
116
107
117
assertThat (businessObjects , hasSize (1 ));
108
118
assertThat (businessObjects , contains (object3 ));
109
119
110
- SecurityContextHolder .getContext ().setAuthentication (new UsernamePasswordAuthenticationToken ( olli , "x" ) );
120
+ SecurityContextHolder .getContext ().setAuthentication (olliAuth );
111
121
112
122
businessObjects = secureBusinessObjectRepository .findBusinessObjectsForCurrentUserById ();
113
123
@@ -118,13 +128,30 @@ public void findBusinessObjectsForCurrentUserByIdShouldReturnOnlyBusinessObjects
118
128
@ Test
119
129
public void findBusinessObjectsForCurrentUserByIdShouldReturnAllObjectsForAdmin () {
120
130
121
- UsernamePasswordAuthenticationToken auth = new UsernamePasswordAuthenticationToken (admin , "x" ,
122
- singleton (new SimpleGrantedAuthority ("ROLE_ADMIN" )));
123
- SecurityContextHolder .getContext ().setAuthentication (auth );
131
+ SecurityContextHolder .getContext ().setAuthentication (adminAuth );
124
132
125
133
List <BusinessObject > businessObjects = secureBusinessObjectRepository .findBusinessObjectsForCurrentUserById ();
126
134
127
135
assertThat (businessObjects , hasSize (3 ));
128
136
assertThat (businessObjects , contains (object1 , object2 , object3 ));
129
137
}
138
+
139
+ @ Test
140
+ public void customUpdateStatementShouldAllowToUseSecurityContextInformationViaSpelParameters () {
141
+
142
+ SecurityContextHolder .getContext ().setAuthentication (adminAuth );
143
+
144
+ //Detaching items to get them out of the query cache in order to see the updated values.
145
+ em .detach (object1 );
146
+ em .detach (object2 );
147
+ em .detach (object3 );
148
+
149
+ secureBusinessObjectRepository .modifiyDataWithRecordingSecurityContext ();
150
+
151
+ for (BusinessObject bo : businessObjectRepository .findAll ()) {
152
+
153
+ assertThat (bo .getLastModifiedDate (), is (notNullValue ()));
154
+ assertThat (bo .getLastModifiedByUsername (), is ("admin" ));
155
+ }
156
+ }
130
157
}
0 commit comments