1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 package com.hack23.cia.service.impl.action.application;
20
21 import java.util.ArrayList;
22 import java.util.Collection;
23 import java.util.Date;
24 import java.util.UUID;
25
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.springframework.beans.factory.annotation.Autowired;
29 import org.springframework.security.access.annotation.Secured;
30 import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
31 import org.springframework.security.core.authority.SimpleGrantedAuthority;
32 import org.springframework.security.core.context.SecurityContextHolder;
33 import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
34 import org.springframework.security.crypto.password.PasswordEncoder;
35 import org.springframework.stereotype.Service;
36 import org.springframework.transaction.annotation.Propagation;
37 import org.springframework.transaction.annotation.Transactional;
38
39 import com.hack23.cia.model.internal.application.system.impl.ApplicationConfiguration;
40 import com.hack23.cia.model.internal.application.system.impl.ApplicationEventGroup;
41 import com.hack23.cia.model.internal.application.system.impl.ApplicationOperationType;
42 import com.hack23.cia.model.internal.application.system.impl.ConfigurationGroup;
43 import com.hack23.cia.model.internal.application.user.impl.UserAccount;
44 import com.hack23.cia.model.internal.application.user.impl.UserAccount_;
45 import com.hack23.cia.model.internal.application.user.impl.UserRole;
46 import com.hack23.cia.service.api.action.application.CreateApplicationEventRequest;
47 import com.hack23.cia.service.api.action.application.CreateApplicationEventResponse;
48 import com.hack23.cia.service.api.action.application.RegisterUserRequest;
49 import com.hack23.cia.service.api.action.application.RegisterUserResponse;
50 import com.hack23.cia.service.api.action.common.ServiceResponse.ServiceResult;
51 import com.hack23.cia.service.data.api.ApplicationConfigurationService;
52 import com.hack23.cia.service.data.api.UserDAO;
53 import com.hack23.cia.service.impl.action.common.AbstractBusinessServiceImpl;
54 import com.hack23.cia.service.impl.action.common.BusinessService;
55
56
57
58
59 @Service
60 @Transactional(propagation = Propagation.REQUIRED)
61 public final class RegisterUserService extends
62 AbstractBusinessServiceImpl<RegisterUserRequest, RegisterUserResponse>
63 implements BusinessService<RegisterUserRequest, RegisterUserResponse> {
64
65
66 private static final Logger LOGGER = LoggerFactory
67 .getLogger(RegisterUserService.class);
68
69
70
71 @Autowired
72 private BusinessService<CreateApplicationEventRequest, CreateApplicationEventResponse> createApplicationEventService;
73
74
75 @Autowired
76 private ApplicationConfigurationService applicationConfigurationService;
77
78
79 @Autowired
80 private UserDAO userDAO;
81
82
83 private final PasswordEncoder passwordEncoder = new BCryptPasswordEncoder();
84
85
86
87
88 public RegisterUserService() {
89 super(RegisterUserRequest.class);
90 }
91
92 @Secured({"ROLE_ANONYMOUS"})
93 @Override
94 public RegisterUserResponse processService(
95 final RegisterUserRequest serviceRequest) {
96
97 final CreateApplicationEventRequest eventRequest = new CreateApplicationEventRequest();
98 eventRequest.setEventGroup(ApplicationEventGroup.USER);
99 eventRequest.setApplicationOperation(ApplicationOperationType.CREATE);
100 eventRequest.setActionName(RegisterUserRequest.class.getSimpleName());
101 eventRequest.setSessionId(serviceRequest.getSessionId());
102 eventRequest.setElementId(serviceRequest.getEmail());
103
104 final ApplicationConfiguration registeredUsersGetAdminConfig = applicationConfigurationService.checkValueOrLoadDefault("Registered User All get Role Admin", "Registered User All get Role Admin", ConfigurationGroup.AUTHORIZATION, RegisterUserService.class.getSimpleName(), "Register User Service", "Responsible for create of useraccounts", "registered.users.get.admin", "true");
105
106 final UserAccount userNameExist = userDAO.findFirstByProperty(UserAccount_.username, serviceRequest.getUsername());
107 final UserAccount userEmailExist = userDAO.findFirstByProperty(UserAccount_.email, serviceRequest.getEmail());
108
109 RegisterUserResponse response;
110 if (userEmailExist == null && userNameExist == null) {
111 final UserAccount userAccount = new UserAccount();
112 userAccount.setCountry(serviceRequest.getCountry());
113 userAccount.setEmail(serviceRequest.getEmail());
114 userAccount.setUsername(serviceRequest.getUsername());
115 userAccount.setUserId(UUID.randomUUID().toString());
116 userAccount.setUserpassword(passwordEncoder.encode(userAccount.getUserId()+".uuid"+ serviceRequest.getUserpassword()));
117 userAccount.setNumberOfVisits(1);
118 userAccount.setUserType(serviceRequest.getUserType());
119 userAccount.setCreatedDate(new Date());
120 userDAO.persist(userAccount);
121
122 if ("true".equals(registeredUsersGetAdminConfig.getPropertyValue())) {
123 userAccount.setUserRole(UserRole.ADMIN);
124 } else {
125 userAccount.setUserRole(UserRole.USER);
126 }
127
128 final Collection<SimpleGrantedAuthority> authorities = new ArrayList<>();
129
130 if (UserRole.ADMIN == userAccount.getUserRole()) {
131 authorities.add(new SimpleGrantedAuthority("ROLE_ADMIN"));
132 } else if (UserRole.USER == userAccount.getUserRole()) {
133 authorities.add(new SimpleGrantedAuthority("ROLE_USER"));
134 }
135
136 SecurityContextHolder.getContext().setAuthentication(new UsernamePasswordAuthenticationToken(userAccount, userAccount.getUserpassword(), authorities));
137
138 eventRequest.setUserId(userAccount.getUserId());
139 response = new RegisterUserResponse(ServiceResult.SUCCESS);
140 } else {
141 response = new RegisterUserResponse(ServiceResult.FAILURE);
142 }
143
144 eventRequest.setApplicationMessage(response.getResult().toString());
145 createApplicationEventService.processService(eventRequest);
146 LOGGER.info("Event: {}",eventRequest);
147 return response;
148 }
149
150 }