66import java .util .Iterator ;
77import java .util .Set ;
88
9+ import fr .gael .dhus .service .exception .RequiredFieldMissingException ;
910import org .apache .commons .logging .Log ;
1011import org .apache .commons .logging .LogFactory ;
1112import org .springframework .beans .factory .annotation .Autowired ;
2526import fr .gael .dhus .api .stub .stub_share .exceptions .UserBadEncryptionException ;
2627import fr .gael .dhus .api .stub .stub_share .exceptions .UserBadOldPasswordException ;
2728import fr .gael .dhus .api .stub .stub_share .exceptions .UserPasswordConfirmationException ;
28- import fr .gael .dhus .database .dao .UserDao ;
2929import fr .gael .dhus .database .object .User ;
3030import fr .gael .dhus .database .object .User .PasswordEncryption ;
31- import fr .gael .dhus .messaging .mail .MailServer ;
3231import fr .gael .dhus .service .UserService ;
3332import fr .gael .dhus .service .exception .RootNotModifiableException ;
3433import fr .gael .dhus .spring .context .ApplicationContextProvider ;
35- import fr .gael .dhus .system .config .ConfigurationManager ;
3634
3735@ RestController
3836public class StubUserController {
3937 private static Log logger = LogFactory .getLog (StubUserController .class );
4038
41- @ Autowired
42- private UserDao userDao ;
4339
4440 @ Autowired
4541 private UserService userService ;
4642
47- @ Autowired
48- private ConfigurationManager cfgManager ;
49-
50- @ Autowired
51- private MailServer mailer ;
52-
5343 private User getUserFromPrincipal (Principal principal ) {
5444 User user = ((User ) ((UsernamePasswordAuthenticationToken ) principal )
5545 .getPrincipal ());
@@ -67,27 +57,14 @@ public User getUserProfile(Principal principal,
6757 @ RequestMapping (value = "/stub/users/{userid}" , method = RequestMethod .PUT )
6858 public int updateUserProfile (Principal principal ,
6959 @ RequestBody UserRequestBody body ,
70- @ PathVariable (value = "userid" ) String userid ) {
60+ @ PathVariable (value = "userid" ) String userid ) throws RequiredFieldMissingException , RootNotModifiableException {
7161 logger .info ("******** updateUserProfile()" );
7262 int responseCode = 0 ;
7363 User user = body .getUser ();
7464 logger .info ("******** called body.getUser" );
7565 PasswordModel passwordModel = body .getPasswordModel ();
7666 User u = getUserFromPrincipal (principal );
77- try {
78- logger .info ("******** inside try" );
79- checkRoot (u );
80- } catch (RootNotModifiableException e ) {
81- logger .info ("******** caught RootNotModifiableException" );
82- responseCode = 1001 ;
83- logger .error ("Root cannot be modified: " + e .getMessage ());
84- e .printStackTrace ();
85- return responseCode ;
86- } catch (Exception ex ) {
87- logger .info ("******** caught generic exception" );
88- logger .error (ex .getMessage ());
89- }
90- logger .error ("******** fields check" );
67+
9168 // check user fields. set only not empty fields
9269 if (user .getEmail () != null && !user .getEmail ().isEmpty ())
9370 u .setEmail (user .getEmail ());
@@ -146,12 +123,11 @@ public int updateUserProfile(Principal principal,
146123 throw new UserPasswordConfirmationException (
147124 "Confirmation password value doesn't match." );
148125 }
149- // logger.info("new pwd: " +
150- // passwordModel.getConfirmPassword());RootNotModifiableException
151- u .setPassword (passwordModel .getConfirmPassword ());
126+ userService .selfChangePassword (u .getId (),passwordModel .getOldPassword (),passwordModel .getConfirmPassword ());
152127 }
153128 logger .info ("******** update user" );
154- userDao .update (u );
129+
130+ userService .selfUpdateUser (u );
155131 return responseCode ;
156132 }
157133
@@ -160,68 +136,18 @@ public int forgotPassword(@RequestBody User user)
160136 throws RootNotModifiableException {
161137 int responseCode = 0 ;
162138
163- if (userDao .isRootUser (user )) {
164- logger .error ("Root cannot be modified" );
165- responseCode = 1001 ;
166- return responseCode ;
167- }
168- User checked = userService .resolveUser (userDao .getByName (user
169- .getUsername ()));
170- if (checked == null
171- || !checked .getEmail ().toLowerCase ()
172- .equals (user .getEmail ().toLowerCase ())) {
173- logger .error ("No user can be found for this "
174- + "username/mail combination" );
175- responseCode = 1002 ;
176- return responseCode ;
177- }
178-
179- String message = "Dear " + getUserWelcome (checked ) + ",\n \n "
180- + "Please follow this link to set a new password in the "
181- + cfgManager .getNameConfiguration ().getShortName ()
182- + " system:\n "
183- + cfgManager .getServerConfiguration ().getExternalUrl ()
184- + "#/home/r="
185- + userDao .computeUserCodeForPasswordReset (checked ) + "\n \n "
186- + "For help requests please write to: "
187- + cfgManager .getSupportConfiguration ().getMail () + "\n \n "
188- + "Kind regards.\n "
189- + cfgManager .getSupportConfiguration ().getName () + ".\n "
190- + cfgManager .getServerConfiguration ().getExternalUrl ();
191-
192- //logger.info("Forgot password mail message: " + message);
193- String subject = "User password reset" ;
194-
195139 try {
196- mailer . send ( checked . getEmail (), null , null , subject , message );
140+ userService . forgotPassword ( "#/home/r=" , user );
197141 } catch (Exception e ) {
198- logger .error ("Cannot send email to " + checked . getEmail () , e );
142+ logger .error ("Reset password of user: " + user + " failed" , e );
199143 responseCode = 1003 ;
200144 return responseCode ;
201145 }
202- logger .info ("responseCode: " + responseCode );
146+ logger .info ("responseCode: " + responseCode );
203147
204148 return responseCode ;
205149 }
206150
207- private void checkRoot (User user ) throws RootNotModifiableException {
208- if (userDao .isRootUser (user )) {
209- logger .error ("Root cannot be modified" );
210- throw new RootNotModifiableException ("Root cannot be modified" );
211- }
212- }
213-
214- private String getUserWelcome (User u ) {
215- String firstname = u .getUsername ();
216- String lastname = "" ;
217- if (u .getFirstname () != null && !u .getFirstname ().trim ().isEmpty ()) {
218- firstname = u .getFirstname ();
219- if (u .getLastname () != null && !u .getLastname ().trim ().isEmpty ())
220- lastname = " " + u .getLastname ();
221- }
222- return firstname + lastname ;
223- }
224-
225151 /**
226152 * Uploaded products ids list of logged user
227153 *
0 commit comments