Project

General

Profile

« Previous | Next » 

Revision 40309

Added by Nikon Gasparis over 8 years ago

implemented methods to reset user password and update user profile

View differences:

modules/uoa-repository-manager-gui/trunk/src/main/java/eu/dnetlib/repo/manager/server/UserServiceImpl.java
12 12
import javax.servlet.ServletConfig;
13 13
import javax.servlet.ServletException;
14 14
import java.util.ArrayList;
15
import java.util.List;
15 16
import java.util.regex.Pattern;
16 17

  
17 18
/**
......
151 152

  
152 153
    @Override
153 154
    public void updateUser(UserProfile userProfile) throws UserAccessException {
155
        try {
156
            LOGGER.info("Editing user " + userProfile.getUsername());
157
            Pattern rfc2822 = Pattern.compile("^[a-z0-9!#$%&'*+/=?^_`{|}~-]+(?:\\.[a-z0-9!#$%&'*+/=?^_`{|}~-]+)*@(?:[a-z0-9](?:[a-z0-9-]*[a-z0-9])?\\.)+[a-z0-9](?:[a-z0-9-]*[a-z0-9])?$");
158
            if (!rfc2822.matcher(userProfile.getEmail().trim().toLowerCase()).matches()) {
159
                throw new UserAccessException("login.notValidEmail", UserAccessException.ErrorCode.INVALID_EMAIL_FORMAT);
160
            }
154 161

  
162
            String currentEmail = this.userAPI.getEmailFromUsername(userProfile.getUsername());
163
            if (!userProfile.getEmail().equalsIgnoreCase(currentEmail)) {
164
                if (this.userAPI.userExists(userProfile.getEmail())) {
165
                    throw new UserAccessException("login.mailAlreadyExists", UserAccessException.ErrorCode.MAIL_ALREADY_EXISTS);
166
                }
167
            }
168

  
169
            this.userAPI.editUser(userProfile);
170

  
171
        } catch (Exception e) {
172
            LOGGER.error("Error while editing user " + userProfile.getUsername(), e);
173
            if (e instanceof UserAccessException)
174
                throw (UserAccessException) e;
175
            else
176
                throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
177
//            reportException(e);
178
        }
155 179
    }
156 180

  
157 181
    @Override
158
    public void resendPassword(String email) throws UserAccessException {
182
    public void prepareResetPassword(String email) throws UserAccessException {
159 183

  
184
        try {
185
            LOGGER.debug("Sending password recovery to user " + email);
186
            if (!this.userAPI.userExists(email)) {
187
                throw new UserAccessException("login.userNotExists", UserAccessException.ErrorCode.INVALID_USERNAME);
188
            }
189
            List<String> to = new ArrayList<String>();
190
            to.add(email);
191
            String securityCode = this.userAPI.prepareResetPassword(email);
192
            emailer.sendMail(to, "forgotPassword.mail.Subject", "forgotPassword.mail.Body1" + ": " + this.repoManagerBaseUrl + "?securityCode=" + securityCode + "#resetPassword" + "\n\n" + "forgotPassword.mail.Body2" + ": " + securityCode, false, null);
193

  
194
        } catch (Exception e) {
195
            LOGGER.error("Error while sending password recovery to user " + email, e);
196
            if (e instanceof UserAccessException)
197
                throw (UserAccessException) e;
198
            else
199
                throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
200
            //    reportException(e);
201
        }
160 202
    }
161 203

  
162 204
    @Override
205
    public void resetPassword(String securityCode, String password) throws UserAccessException {
206
        try {
207
            LOGGER.debug("Reseting password with security code " + securityCode);
208

  
209
            if (securityCode.length() == 0) {
210
                throw new UserAccessException("resetPassword.wrongSecurityCode", UserAccessException.ErrorCode.WRONG_SECURITY_CODE);
211
            }
212

  
213
            this.userAPI.resetPassword(securityCode, password);
214

  
215
        } catch (Exception e) {
216
            LOGGER.error("Error while reseting password with security code " + securityCode);
217
            if (e instanceof UserAccessException)
218
                throw (UserAccessException) e;
219
            else
220
                throw new UserAccessException("login.generalError", UserAccessException.ErrorCode.GENERAL_ERROR);
221
            //    reportException(e);
222
        }
223
    }
224

  
225
    @Override
163 226
    public void resendActivation(String email) throws UserAccessException {
164 227

  
165 228
    }
modules/uoa-repository-manager-gui/trunk/src/main/java/eu/dnetlib/repo/manager/shared/UserAccessException.java
15 15
        NOT_ACTIVATED,
16 16
        ACTIVATION_ERROR,
17 17
        LDAP_ERROR,
18
        USERNAME_ALREADY_EXISTS, MAIL_ALREADY_EXISTS, GENERAL_ERROR, ALREADY_ACTIVATED, INVALID_EMAIL_FORMAT, INCORRECT_CAPTCHA
18
        USERNAME_ALREADY_EXISTS, MAIL_ALREADY_EXISTS, GENERAL_ERROR, ALREADY_ACTIVATED, INVALID_EMAIL_FORMAT, WRONG_SECURITY_CODE, INCORRECT_CAPTCHA
19 19
    }
20 20

  
21 21
    private ErrorCode errorCode = null;
modules/uoa-repository-manager-gui/trunk/src/main/java/eu/dnetlib/repo/manager/client/UserService.java
21 21

  
22 22
    UserProfile getUserByEmail(String email) throws UserAccessException;
23 23

  
24
    void resendPassword(String email) throws UserAccessException;
24
    void prepareResetPassword(String email) throws UserAccessException;
25 25

  
26
    void resetPassword(String securityCode, String password) throws UserAccessException;
27

  
26 28
    void resendActivation(String email) throws UserAccessException;
27 29

  
28 30
}

Also available in: Unified diff