2012-06-16 15:59:59 -03:00
|
|
|
/*
|
2012-10-04 20:48:01 -03:00
|
|
|
* Copyright [2012] [wisemapping]
|
2012-06-16 15:59:59 -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
|
|
|
|
* "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
|
|
|
|
*
|
|
|
|
* http://www.wisemapping.org/license
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package com.wisemapping.service;
|
|
|
|
|
|
|
|
import com.wisemapping.dao.UserManager;
|
|
|
|
import com.wisemapping.exceptions.WiseMappingException;
|
|
|
|
import com.wisemapping.mail.NotificationService;
|
2013-03-17 18:51:33 -03:00
|
|
|
import com.wisemapping.model.*;
|
2012-07-17 01:20:47 -03:00
|
|
|
import org.apache.velocity.app.VelocityEngine;
|
2012-06-16 15:59:59 -03:00
|
|
|
import org.jetbrains.annotations.NotNull;
|
2012-07-13 21:07:51 -03:00
|
|
|
import org.springframework.context.MessageSource;
|
|
|
|
import org.springframework.context.i18n.LocaleContextHolder;
|
2012-07-17 01:20:47 -03:00
|
|
|
import org.springframework.ui.velocity.VelocityEngineUtils;
|
2012-06-16 15:59:59 -03:00
|
|
|
|
2012-07-13 21:07:51 -03:00
|
|
|
import java.io.IOException;
|
2012-07-17 01:20:47 -03:00
|
|
|
import java.util.*;
|
2012-06-16 15:59:59 -03:00
|
|
|
|
|
|
|
public class UserServiceImpl
|
|
|
|
implements UserService {
|
|
|
|
private UserManager userManager;
|
|
|
|
private MindmapService mindmapService;
|
|
|
|
private NotificationService notificationService;
|
2012-07-13 21:07:51 -03:00
|
|
|
private MessageSource messageSource;
|
2012-07-17 01:20:47 -03:00
|
|
|
private VelocityEngine velocityEngine;
|
2012-07-13 21:07:51 -03:00
|
|
|
|
2012-06-16 15:59:59 -03:00
|
|
|
|
2012-06-23 16:15:59 -03:00
|
|
|
@Override
|
2012-06-16 15:59:59 -03:00
|
|
|
public void activateAccount(long code)
|
|
|
|
throws InvalidActivationCodeException {
|
|
|
|
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);
|
|
|
|
notificationService.activateAccount(user);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-23 16:15:59 -03:00
|
|
|
@Override
|
2012-06-16 15:59:59 -03:00
|
|
|
public void resetPassword(@NotNull String email)
|
2013-03-17 18:51:33 -03:00
|
|
|
throws InvalidUserEmailException, InvalidAuthSchemaException {
|
2012-06-16 15:59:59 -03:00
|
|
|
final User user = userManager.getUserBy(email);
|
|
|
|
if (user != null) {
|
2013-03-17 18:51:33 -03:00
|
|
|
|
2013-03-17 23:17:55 -03:00
|
|
|
if (user.getAuthenticationType() != AuthenticationType.DATABASE) {
|
|
|
|
throw new InvalidAuthSchemaException("Could not change password for " + user.getAuthenticationType().getCode());
|
2013-03-17 18:51:33 -03:00
|
|
|
}
|
|
|
|
|
2012-06-16 15:59:59 -03:00
|
|
|
// Generate a random password ...
|
|
|
|
final String password = randomstring(8, 10);
|
|
|
|
user.setPassword(password);
|
2012-06-20 13:28:45 -03:00
|
|
|
updateUser(user);
|
2012-06-16 15:59:59 -03:00
|
|
|
|
|
|
|
// Send an email with the new temporal password ...
|
|
|
|
notificationService.resetPassword(user, password);
|
|
|
|
} 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-06-23 16:15:59 -03:00
|
|
|
@Override
|
2012-06-16 15:59:59 -03:00
|
|
|
public void deleteUser(@NotNull User user) {
|
|
|
|
userManager.deleteUser(user);
|
|
|
|
}
|
|
|
|
|
2012-06-23 16:15:59 -03:00
|
|
|
@Override
|
|
|
|
public void auditLogin(@NotNull User user) {
|
2012-07-13 21:07:51 -03:00
|
|
|
if (user == null) {
|
2012-07-06 21:21:56 -03:00
|
|
|
throw new IllegalArgumentException("User can not be null");
|
|
|
|
}
|
2012-06-23 16:15:59 -03:00
|
|
|
final AccessAuditory accessAuditory = new AccessAuditory();
|
|
|
|
accessAuditory.setUser(user);
|
|
|
|
accessAuditory.setLoginDate(Calendar.getInstance());
|
|
|
|
userManager.auditLogin(accessAuditory);
|
|
|
|
}
|
|
|
|
|
2013-03-17 18:51:33 -03:00
|
|
|
@NotNull
|
2013-02-17 21:00:08 -03:00
|
|
|
public User createUser(@NotNull User user, boolean emailConfirmEnabled, boolean welcomeEmail) throws WiseMappingException {
|
2012-06-16 15:59:59 -03:00
|
|
|
final UUID uuid = UUID.randomUUID();
|
|
|
|
user.setCreationDate(Calendar.getInstance());
|
|
|
|
user.setActivationCode(uuid.getLeastSignificantBits());
|
|
|
|
|
|
|
|
if (emailConfirmEnabled) {
|
|
|
|
user.setActivationDate(null);
|
|
|
|
|
|
|
|
} else {
|
|
|
|
user.setActivationDate(Calendar.getInstance());
|
|
|
|
}
|
|
|
|
|
|
|
|
Collaborator col = userManager.getCollaboratorBy(user.getEmail());
|
2013-02-17 21:00:08 -03:00
|
|
|
|
2012-06-16 15:59:59 -03:00
|
|
|
if (col != null) {
|
|
|
|
userManager.createUser(user, col);
|
|
|
|
} else {
|
|
|
|
userManager.createUser(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
//create welcome map
|
2012-08-16 18:28:09 -03:00
|
|
|
final Mindmap mindMap = buildTutorialMindmap(user.getFirstname());
|
2012-07-13 21:07:51 -03:00
|
|
|
mindmapService.addMindmap(mindMap, user);
|
2012-06-16 15:59:59 -03:00
|
|
|
|
2012-07-17 01:20:47 -03:00
|
|
|
|
2012-06-16 15:59:59 -03:00
|
|
|
// Send registration email.
|
|
|
|
if (emailConfirmEnabled) {
|
|
|
|
notificationService.sendRegistrationEmail(user);
|
2013-02-17 21:00:08 -03:00
|
|
|
} else if (welcomeEmail) {
|
2012-06-20 13:28:45 -03:00
|
|
|
// Send a welcome email ..
|
|
|
|
notificationService.newAccountCreated(user);
|
2012-06-16 15:59:59 -03:00
|
|
|
}
|
2012-06-20 13:28:45 -03:00
|
|
|
|
2012-06-16 15:59:59 -03:00
|
|
|
return user;
|
|
|
|
}
|
|
|
|
|
2012-08-16 18:28:09 -03:00
|
|
|
public Mindmap buildTutorialMindmap(@NotNull String firstName) {
|
2012-07-13 21:07:51 -03:00
|
|
|
//To change body of created methods use File | Settings | File Templates.
|
2012-08-23 20:29:07 -03:00
|
|
|
final Locale locale = LocaleContextHolder.getLocale();
|
2012-08-15 21:28:51 -03:00
|
|
|
Mindmap result = new Mindmap();
|
2012-07-17 01:20:47 -03:00
|
|
|
final Map<String, Object> model = new HashMap<String, Object>();
|
|
|
|
model.put("messages", messageSource);
|
|
|
|
model.put("noArgs", new Object[]{});
|
|
|
|
model.put("locale", locale);
|
|
|
|
|
|
|
|
final String mapXml = VelocityEngineUtils.mergeTemplateIntoString(velocityEngine, "/samples/tutorial.vm", model);
|
2012-07-13 21:07:51 -03:00
|
|
|
|
|
|
|
try {
|
2012-07-17 01:20:47 -03:00
|
|
|
result.setXmlStr(mapXml);
|
2012-07-15 02:04:53 -03:00
|
|
|
result.setTitle(messageSource.getMessage("WELCOME", null, locale) + " " + firstName);
|
2012-07-13 21:07:51 -03:00
|
|
|
result.setDescription("");
|
|
|
|
|
|
|
|
} catch (IOException e) {
|
2012-07-17 01:20:47 -03:00
|
|
|
e.printStackTrace();
|
2012-07-13 21:07:51 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2012-06-23 16:15:59 -03:00
|
|
|
@Override
|
2012-06-16 15:59:59 -03:00
|
|
|
public void changePassword(@NotNull User user) {
|
2012-06-20 13:28:45 -03:00
|
|
|
notificationService.passwordChanged(user);
|
2012-06-16 15:59:59 -03:00
|
|
|
userManager.updateUser(user);
|
|
|
|
}
|
|
|
|
|
2012-06-23 16:15:59 -03:00
|
|
|
@Override
|
2012-06-16 15:59:59 -03:00
|
|
|
public User getUserBy(String email) {
|
|
|
|
return userManager.getUserBy(email);
|
|
|
|
}
|
|
|
|
|
2012-06-23 16:15:59 -03:00
|
|
|
@Override
|
2012-06-16 15:59:59 -03:00
|
|
|
public User getUserBy(long id) {
|
|
|
|
return userManager.getUserBy(id);
|
|
|
|
}
|
|
|
|
|
2012-06-23 16:15:59 -03:00
|
|
|
@Override
|
2012-06-16 15:59:59 -03:00
|
|
|
public void updateUser(@NotNull User user) {
|
|
|
|
userManager.updateUser(user);
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setUserManager(@NotNull UserManager userManager) {
|
|
|
|
this.userManager = userManager;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setMindmapService(@NotNull MindmapService mindmapService) {
|
|
|
|
this.mindmapService = mindmapService;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void setNotificationService(NotificationService notificationService) {
|
|
|
|
this.notificationService = notificationService;
|
|
|
|
}
|
2012-07-13 21:07:51 -03:00
|
|
|
|
|
|
|
public void setMessageSource(@NotNull MessageSource messageSource) {
|
|
|
|
this.messageSource = messageSource;
|
|
|
|
}
|
2012-07-17 01:20:47 -03:00
|
|
|
|
|
|
|
public void setVelocityEngine(VelocityEngine velocityEngine) {
|
|
|
|
this.velocityEngine = velocityEngine;
|
|
|
|
}
|
2013-02-17 21:00:08 -03:00
|
|
|
|
|
|
|
@Override
|
|
|
|
public User getCasUserBy(String uid) {
|
|
|
|
// TODO Auto-generated method stub
|
|
|
|
return null;
|
|
|
|
}
|
2012-06-16 15:59:59 -03:00
|
|
|
}
|