2010-12-13 11:07:20 -03:00
|
|
|
/*
|
2011-01-23 20:34:05 -03:00
|
|
|
* Copyright [2011] [wisemapping]
|
2010-12-13 11:07:20 -03:00
|
|
|
*
|
2011-01-23 21:03:12 -03:00
|
|
|
* Licensed under WiseMapping Public License, Version 1.0 (the "License").
|
|
|
|
* It is basically the Apache License, Version 2.0 (the "License") plus the
|
2011-01-23 20:34:05 -03:00
|
|
|
* "powered by wisemapping" text requirement on every single page;
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the license at
|
2010-12-13 11:07:20 -03:00
|
|
|
*
|
2011-01-23 20:34:05 -03:00
|
|
|
* http://www.wisemapping.org/license
|
2010-12-13 11:07:20 -03:00
|
|
|
*
|
2011-01-23 20:34:05 -03:00
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
2010-12-13 11:07:20 -03:00
|
|
|
*/
|
|
|
|
|
2009-06-07 18:59:43 +00:00
|
|
|
package com.wisemapping.service;
|
|
|
|
|
|
|
|
import com.wisemapping.dao.UserManager;
|
|
|
|
import com.wisemapping.exceptions.WiseMappingException;
|
|
|
|
import com.wisemapping.mail.Mailer;
|
2012-02-21 14:22:43 -03:00
|
|
|
import com.wisemapping.model.Collaborator;
|
2009-06-07 18:59:43 +00:00
|
|
|
import com.wisemapping.model.User;
|
2010-12-13 11:07:20 -03:00
|
|
|
import org.apache.log4j.Logger;
|
2012-02-21 17:41:51 -03:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2009-06-07 18:59:43 +00:00
|
|
|
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
public class UserServiceImpl
|
|
|
|
implements UserService {
|
|
|
|
private UserManager userManager;
|
|
|
|
private MindmapService mindmapService;
|
|
|
|
private Mailer mailer;
|
2010-12-13 11:07:20 -03:00
|
|
|
final static Logger logger = Logger.getLogger("org.wisemapping.service");
|
2009-06-07 18:59:43 +00:00
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
public void activateAccount(long code)
|
2011-03-12 19:10:44 -03:00
|
|
|
throws InvalidActivationCodeException {
|
2009-06-07 18:59:43 +00:00
|
|
|
final User user = userManager.getUserByActivationCode(code);
|
|
|
|
if (user == null || user.isActive()) {
|
|
|
|
throw new InvalidActivationCodeException("Invalid Activation Code");
|
|
|
|
} else {
|
|
|
|
final Calendar now = Calendar.getInstance();
|
|
|
|
user.setActivationDate(now);
|
|
|
|
userManager.updateUser(user);
|
|
|
|
final Map<String, User> model = new HashMap<String, User>();
|
|
|
|
model.put("user", user);
|
|
|
|
mailer.sendEmail(mailer.getRegistrationEmail(), user.getEmail(), "WiseMapping : Active account", model, "activationAccountMail.vm");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public User reloadUser(final User user) {
|
|
|
|
return this.getUserBy(user.getId());
|
|
|
|
}
|
|
|
|
|
|
|
|
public void sendEmailPassword(String email)
|
|
|
|
throws InvalidUserEmailException {
|
|
|
|
final User user = userManager.getUserBy(email);
|
|
|
|
if (user != null) {
|
|
|
|
final Map<String, Object> model = new HashMap<String, Object>();
|
|
|
|
final String password = randomstring(8, 10);
|
|
|
|
user.setPassword(password);
|
|
|
|
changePassword(user);
|
|
|
|
model.put("user", user);
|
|
|
|
model.put("password", password);
|
2011-03-12 19:10:44 -03:00
|
|
|
|
2009-06-07 18:59:43 +00:00
|
|
|
mailer.sendEmail(mailer.getRegistrationEmail(), user.getEmail(), "WiseMapping : Recovery Password", model, "recoveryMail.vm");
|
|
|
|
} else {
|
|
|
|
throw new InvalidUserEmailException("The email '" + email + "' does not exists.");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
private String randomstring(int lo, int hi) {
|
|
|
|
int n = rand(lo, hi);
|
|
|
|
byte b[] = new byte[n];
|
|
|
|
for (int i = 0; i < n; i++)
|
|
|
|
b[i] = (byte) rand('@', 'Z');
|
|
|
|
return new String(b);
|
|
|
|
}
|
|
|
|
|
|
|
|
private int rand(int lo, int hi) {
|
|
|
|
java.util.Random rn = new java.util.Random();
|
|
|
|
int n = hi - lo + 1;
|
|
|
|
int i = rn.nextInt() % n;
|
|
|
|
if (i < 0)
|
|
|
|
i = -i;
|
|
|
|
return lo + i;
|
|
|
|
}
|
|
|
|
|
2012-02-21 17:41:51 -03:00
|
|
|
public void deleteUser(@NotNull User user){
|
|
|
|
userManager.deleteUser(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public User createUser(@NotNull User user, boolean emailConfirmEnabled) throws WiseMappingException {
|
2009-06-07 18:59:43 +00:00
|
|
|
final UUID uuid = UUID.randomUUID();
|
|
|
|
user.setCreationDate(Calendar.getInstance());
|
|
|
|
user.setActivationCode(uuid.getLeastSignificantBits());
|
|
|
|
|
2011-03-12 19:10:44 -03:00
|
|
|
if (emailConfirmEnabled) {
|
|
|
|
user.setActivationDate(null);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
user.setActivationDate(Calendar.getInstance());
|
2009-06-07 18:59:43 +00:00
|
|
|
}
|
2011-03-12 19:10:44 -03:00
|
|
|
|
2012-02-21 14:22:43 -03:00
|
|
|
Collaborator col = userManager.getCollaboratorBy(user.getEmail());
|
2011-03-12 19:10:44 -03:00
|
|
|
if (col != null) {
|
|
|
|
userManager.createUser(user, col);
|
|
|
|
} else {
|
2009-06-07 18:59:43 +00:00
|
|
|
userManager.createUser(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
//create welcome map
|
|
|
|
mindmapService.addWelcomeMindmap(user);
|
|
|
|
|
2011-03-12 19:10:44 -03:00
|
|
|
// Send registration email.
|
|
|
|
if (emailConfirmEnabled) {
|
|
|
|
sendRegistrationEmail(user);
|
|
|
|
}
|
2012-02-21 17:41:51 -03:00
|
|
|
return user;
|
2011-03-12 19:10:44 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
private void sendRegistrationEmail(User user) {
|
2009-06-07 18:59:43 +00:00
|
|
|
final Map<String, Object> model = new HashMap<String, Object>();
|
|
|
|
model.put("user", user);
|
2011-03-12 19:10:44 -03:00
|
|
|
|
|
|
|
|
2009-06-07 18:59:43 +00:00
|
|
|
final String activationUrl = "http://wisemapping.com/c/activation.htm?code=" + user.getActivationCode();
|
2011-03-12 19:10:44 -03:00
|
|
|
logger.info("create User - acrivationUrl: " + activationUrl);
|
2009-06-07 18:59:43 +00:00
|
|
|
model.put("emailcheck", activationUrl);
|
|
|
|
mailer.sendEmail(mailer.getRegistrationEmail(), user.getEmail(), "Welcome to Wisemapping!", model,
|
|
|
|
"confirmationMail.vm");
|
|
|
|
mailer.sendEmail(mailer.getRegistrationEmail(), mailer.getRegistrationEmail(), "Wisemapping : New User", model,
|
|
|
|
"activeUserAccountMail.vm");
|
|
|
|
}
|
|
|
|
|
|
|
|
public void changePassword(User user) {
|
|
|
|
userManager.updateUser(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public User getUserBy(String email) {
|
|
|
|
return userManager.getUserBy(email);
|
|
|
|
}
|
|
|
|
|
|
|
|
public User getUserByUsername(String username) {
|
|
|
|
return userManager.getUserByUsername(username);
|
|
|
|
}
|
|
|
|
|
|
|
|
public User getUserBy(long id) {
|
|
|
|
return userManager.getUserBy(id);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void updateUser(User user) {
|
|
|
|
userManager.updateUser(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUserManager(UserManager userManager) {
|
|
|
|
this.userManager = userManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMailer(Mailer mailer) {
|
|
|
|
this.mailer = mailer;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMindmapService(MindmapService mindmapService) {
|
|
|
|
this.mindmapService = mindmapService;
|
|
|
|
}
|
|
|
|
}
|