8
8
import org .springframework .beans .factory .annotation .Autowired ;
9
9
import org .springframework .beans .factory .annotation .Value ;
10
10
import org .springframework .data .domain .Pageable ;
11
+ import org .springframework .security .core .Authentication ;
11
12
import org .springframework .security .core .context .SecurityContextHolder ;
12
13
import org .springframework .stereotype .Controller ;
13
14
import org .springframework .ui .Model ;
28
29
import org .springframework .security .authentication .AuthenticationManager ;
29
30
import org .springframework .security .authentication .BadCredentialsException ;
30
31
import org .springframework .security .authentication .UsernamePasswordAuthenticationToken ;
31
- import org .springframework .security .core .Authentication ;
32
32
import org .springframework .web .bind .annotation .RequestParam ;
33
33
import org .springframework .data .domain .PageRequest ; // Add this import statement
34
34
import org .springframework .data .domain .Page ; // Add this import statement
@@ -65,6 +65,9 @@ public class AppController {
65
65
@ Autowired
66
66
private AuthenticationManager authenticationManager ;
67
67
68
+ @ Autowired
69
+ private UserRepository userRepository ;
70
+
68
71
// private static final Logger logger = Logger.getLogger(AppController.class.getName());
69
72
70
73
@ Value ("${enableSearchFeature}" )
@@ -74,6 +77,18 @@ public boolean getEnableSearchFeature() {
74
77
return this .enableSearchFeature ;
75
78
}
76
79
80
+ // Add MFA status to all pages that need it
81
+ private void addUserInfoToModel (Model model , Principal principal ) {
82
+ if (principal != null ) {
83
+ String username = principal .getName ();
84
+ User user = userRepository .findByUsername (username );
85
+ if (user != null ) {
86
+ model .addAttribute ("mfaEnabled" , user .isMfaEnabled ());
87
+ model .addAttribute ("username" , username );
88
+ }
89
+ }
90
+ }
91
+
77
92
private String handleSale (Sale sale , HttpSession session , RedirectAttributes redirectAttributes , Runnable action ) {
78
93
sale .setEditing (true ); // set isEditing to true
79
94
action .run ();
@@ -89,7 +104,7 @@ private String handleSale(Sale sale, HttpSession session, RedirectAttributes red
89
104
}
90
105
91
106
@ RequestMapping ("/" )
92
- public String viewHomePage (Model model , Principal principal , @ RequestParam (defaultValue = "0" ) int page , HttpSession session ) {
107
+ public String viewHomePage (Model model , Principal principal , @ RequestParam (defaultValue = "0" ) int page , HttpSession session ) {
93
108
String lastSearchQuery = (String ) session .getAttribute ("lastSearchQuery" );
94
109
if (lastSearchQuery != null && !lastSearchQuery .isEmpty ()) {
95
110
session .setAttribute ("lastSearchQuery" , null ); // set lastSearchQuery to null
@@ -103,37 +118,67 @@ public String viewHomePage(Model model , Principal principal, @RequestParam(defa
103
118
model .addAttribute ("listSale" , salePage .getContent ());
104
119
model .addAttribute ("currentPage" , page );
105
120
model .addAttribute ("totalPages" , salePage .getTotalPages ());
121
+
122
+ // Add user info including MFA status
123
+ addUserInfoToModel (model , principal );
124
+
106
125
return "index" ;
107
126
}
108
127
109
128
@ RequestMapping ("/new" )
110
- public ModelAndView showNewForm () {
129
+ public ModelAndView showNewForm (Principal principal ) {
111
130
ModelAndView mav = new ModelAndView ("new_form" );
112
131
Sale sale = new Sale ();
113
132
mav .addObject ("sale" , sale );
114
133
mav .addObject ("currentDate" , LocalDate .now ());
115
134
mav .addObject ("enableSearchFeature" , enableSearchFeature );
135
+
136
+ // Add user info including MFA status
137
+ if (principal != null ) {
138
+ String username = principal .getName ();
139
+ User user = userRepository .findByUsername (username );
140
+ if (user != null ) {
141
+ mav .addObject ("mfaEnabled" , user .isMfaEnabled ());
142
+ mav .addObject ("username" , username );
143
+ }
144
+ }
145
+
116
146
return mav ;
117
147
}
118
148
119
149
@ RequestMapping ("/edit/{serialNumber}" )
120
- public ModelAndView showEditForm (@ PathVariable (name = "serialNumber" ) String serialNumber ) {
150
+ public ModelAndView showEditForm (@ PathVariable (name = "serialNumber" ) String serialNumber , Principal principal ) {
121
151
ModelAndView mav = new ModelAndView ("edit_form" );
122
152
Sale sale = dao .get (serialNumber );
123
153
sale .setEditing (true );
124
154
mav .addObject ("sale" , sale );
125
155
mav .addObject ("enableSearchFeature" , enableSearchFeature );
156
+
157
+ // Add user info including MFA status
158
+ if (principal != null ) {
159
+ String username = principal .getName ();
160
+ User user = userRepository .findByUsername (username );
161
+ if (user != null ) {
162
+ mav .addObject ("mfaEnabled" , user .isMfaEnabled ());
163
+ mav .addObject ("username" , username );
164
+ }
165
+ }
166
+
126
167
return mav ;
127
168
}
128
169
129
170
@ RequestMapping ("/search" )
130
- public String search (@ ModelAttribute ("q" ) String query , Model model , HttpSession session ) {
171
+ public String search (@ ModelAttribute ("q" ) String query , Model model , HttpSession session , Principal principal ) {
131
172
List <Sale > listSale = dao .search (query );
132
173
model .addAttribute ("listSale" , listSale );
133
174
134
175
boolean enableSearchFeature = true ;
135
176
model .addAttribute ("enableSearchFeature" , enableSearchFeature );
136
177
session .setAttribute ("lastSearchQuery" , query ); // save the last search query in the session
178
+
179
+ // Add user info including MFA status
180
+ addUserInfoToModel (model , principal );
181
+
137
182
return "search" ;
138
183
}
139
184
0 commit comments