View Javadoc
1   package fr.ifremer.quadrige3.core.service.administration.user;
2   
3   /*-
4    * #%L
5    * Quadrige3 Core :: Quadrige3 Client Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2017 Ifremer
10   * %%
11   * This program is free software: you can redistribute it and/or modify
12   * it under the terms of the GNU Affero General Public License as published by
13   * the Free Software Foundation, either version 3 of the License, or
14   * (at your option) any later version.
15   * 
16   * This program is distributed in the hope that it will be useful,
17   * but WITHOUT ANY WARRANTY; without even the implied warranty of
18   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19   * GNU General Public License for more details.
20   * 
21   * You should have received a copy of the GNU Affero General Public License
22   * along with this program.  If not, see <http://www.gnu.org/licenses/>.
23   * #L%
24   */
25  import fr.ifremer.quadrige3.core.dao.administration.user.DepartmentExtendDao;
26  import fr.ifremer.quadrige3.core.dao.administration.user.DepartmentJdbcDao;
27  import fr.ifremer.quadrige3.core.dao.administration.user.QuserExtendDao;
28  import fr.ifremer.quadrige3.core.dao.administration.user.QuserJdbcDao;
29  import fr.ifremer.quadrige3.core.dao.referential.*;
30  import fr.ifremer.quadrige3.core.dao.technical.Assert;
31  import fr.ifremer.quadrige3.core.security.Encryption;
32  import fr.ifremer.quadrige3.core.vo.administration.user.DepartmentVO;
33  import fr.ifremer.quadrige3.core.vo.administration.user.LightQuserVO;
34  import fr.ifremer.quadrige3.core.vo.administration.user.PrivilegeVO;
35  import fr.ifremer.quadrige3.core.vo.administration.user.QuserVO;
36  import org.apache.commons.collections4.CollectionUtils;
37  import org.apache.commons.lang3.ArrayUtils;
38  import org.apache.commons.lang3.StringUtils;
39  import org.springframework.beans.factory.annotation.Autowired;
40  import org.springframework.context.annotation.Lazy;
41  import org.springframework.stereotype.Service;
42  
43  import javax.annotation.Resource;
44  import java.util.List;
45  
46  /**
47   * <p>
48   * UserServiceImpl class.
49   * </p>
50   * 
51   */
52  @Service("userService")
53  @Lazy
54  public class UserServiceImpl implements UserService {
55  
56  	// NOTE : some beans are set to 'not required'.
57  	// This is because quadrige3-synchro-server not have it
58  	@Autowired(required = false)
59  	protected QuserExtendDao quserDao;
60  
61  	@Autowired(required = false)
62  	protected DepartmentExtendDao departmentDao;
63  
64  	@Autowired(required = false)
65  	protected StatusDao statusDao;
66  
67  	@Autowired(required = false)
68  	protected PrivilegeDao privilegeDao;
69  
70  	@Resource
71  	private QuserJdbcDao quserJdbcDao;
72  
73  	@Resource
74  	private DepartmentJdbcDao departmentJdbcDao;
75  
76  	/** {@inheritDoc} */
77  	@Override
78  	public QuserVO getUserById(int userId) {
79  		return quserJdbcDao.getUserById(userId);
80  	}
81  
82  	/** {@inheritDoc} */
83  	@Override
84  	public QuserVO getUserWithDepartmentAndPrivilegesById(int userId) {
85  		QuserVO user = quserJdbcDao.getUserById(userId);
86  
87  		// Load user's department
88  		if (user.getDepId() != null && user.getDepartment() == null) {
89  			DepartmentVO department = departmentJdbcDao.getDepartmentById(user.getDepId());
90  			user.setDepartment(department);
91  		}
92  
93  		// Load user's privileges
94  		List<PrivilegeVO> userPrivileges = quserJdbcDao.getPrivilegesByUserId(userId);
95  		if (CollectionUtils.isNotEmpty(userPrivileges)) {
96  			user.setPrivileges(userPrivileges.toArray(new PrivilegeVO[userPrivileges.size()]));
97  		}
98  
99  		return user;
100 	}
101 
102 	/** {@inheritDoc} */
103 	@Override
104 	public LightQuserVO getLightUserById(int quserId) {
105 		return quserJdbcDao.getLightUserById(quserId);
106 	}
107 
108 	/** {@inheritDoc} */
109 	@Override
110 	public void save(QuserVO user, boolean saveOtherLinkedEntities) {
111 		Assert.notNull(user);
112 
113 		// make sure the user status exists
114 		if (saveOtherLinkedEntities && user.getStatusCd() != null) {
115 			makeSureStatusExists(user.getStatusCd());
116 		}
117 
118 		// Save the department if exists
119 		if (saveOtherLinkedEntities && user.getDepartment() != null) {
120 			DepartmentVO department = user.getDepartment();
121 
122 			// make sure the department status exists
123 			if (department.getStatusCd() != null) {
124 				makeSureStatusExists(department.getStatusCd());
125 			}
126 
127 			// Update the department
128 			department = departmentDao.save(department);
129 
130 			// Refresh the user's department
131 			user.setDepartment(department);
132 			if (department != null) {
133 				user.setDepId(department.getDepId());
134 			} else {
135 				user.setDepId(null);
136 			}
137 		}
138 
139 		// Make sure all privileges exists
140 		if (saveOtherLinkedEntities && ArrayUtils.isNotEmpty(user.getPrivileges())) {
141 			for (PrivilegeVO privilegeVO : user.getPrivileges()) {
142 				makeSurePrivilegeExists(privilegeVO);
143 			}
144 		}
145 
146 		// Save the user
147 		quserDao.save(user);
148 
149 	}
150 
151 	/** {@inheritDoc} */
152 	@Override
153 	public boolean hasPrivilege(int quserId, String privilegeCode) {
154 		Assert.notNull(privilegeCode);
155 
156 		List<String> privilegeCodes = quserDao.getPrivilegeCodesByUserId(quserId);
157 
158 		return !CollectionUtils.isEmpty(privilegeCodes) && privilegeCodes.contains(privilegeCode);
159 
160 	}
161 
162 	@Override
163 	public boolean isLocalUserWithNoPassword(String login) {
164 		Assert.notBlank(login);
165 		return quserDao.isLocalUserWithNoPassword(login);
166 	}
167 
168 	@Override
169 	public Boolean isLoginExtranet(String login) {
170 		Assert.notBlank(login);
171 		return quserDao.isLoginExtranet(login);
172 	}
173 
174 	@Override
175 	public boolean hasPassword(String login) {
176 		Assert.notBlank(login);
177 		return quserDao.hasPassword(login);
178 	}
179 
180 	@Override
181 	public void resetPassword(String login) {
182 		Assert.notBlank(login);
183 		quserDao.resetPassword(login);
184 	}
185 
186 	@Override
187 	public void updatePasswordByUserId(int userId, String password) {
188 		Assert.notBlank(password);
189 		quserDao.updatePasswordByUserId(userId, Encryption.sha(password));
190 	}
191 
192 	/* -- protected methods -- */
193 
194 	/**
195 	 * <p>
196 	 * makeSureStatusExists.
197 	 * </p>
198 	 * 
199 	 * @param statusCode
200 	 *            a {@link java.lang.String} object.
201 	 */
202 	protected void makeSureStatusExists(String statusCode) {
203 		getStatusAndMakeSureExists(statusCode);
204 	}
205 
206 	/**
207 	 * <p>
208 	 * getStatusAndMakeSureExists.
209 	 * </p>
210 	 * 
211 	 * @param statusCode
212 	 *            a {@link java.lang.String} object.
213 	 * @return a {@link fr.ifremer.quadrige3.core.dao.referential.Status} object.
214 	 */
215 	protected Status getStatusAndMakeSureExists(String statusCode) {
216 		Status status = statusDao.load(statusCode);
217 
218 		// Status exists: fine !
219 		if (status != null) {
220 			return status;
221 		}
222 
223 		// Create a new entity
224 		status = Status.Factory.newInstance();
225 
226 		// code
227 		status.setStatusCd(statusCode);
228 
229 		// name (from the enum, if exists)
230 		StatusCode statusEnum = StatusCode.valueOf(statusCode);
231 		status.setStatusNm(statusEnum.name());
232 
233 		// Create the entity
234 		return statusDao.create(status);
235 	}
236 
237 	/**
238 	 * <p>
239 	 * makeSurePrivilegeExists.
240 	 * </p>
241 	 * 
242 	 * @param privilegeVO
243 	 *            a {@link fr.ifremer.quadrige3.core.vo.administration.user.PrivilegeVO} object.
244 	 */
245 	protected void makeSurePrivilegeExists(PrivilegeVO privilegeVO) {
246 		Assert.notNull(privilegeVO);
247 		Assert.notNull(privilegeVO.getPrivilegeCd());
248 
249 		Privilege privilege = privilegeDao.load(privilegeVO.getPrivilegeCd());
250 
251 		// Privilege exists: fine !
252 		if (privilege != null) {
253 			return;
254 		}
255 
256 		// Create a new entity
257 		privilege = Privilege.Factory.newInstance();
258 
259 		// code
260 		privilege.setPrivilegeCd(privilegeVO.getPrivilegeCd());
261 
262 		// name
263 		if (StringUtils.isNotBlank(privilegeVO.getPrivilegeNm())) {
264 			privilege.setPrivilegeNm(privilegeVO.getPrivilegeNm());
265 		} else {
266 			privilege.setPrivilegeNm(privilegeVO.getPrivilegeCd());
267 		}
268 
269 		// status
270 		String statusCode = privilegeVO.getStatusCd();
271 		if (StringUtils.isBlank(statusCode)) {
272 			statusCode = StatusCode.ENABLE.value();
273 		}
274 		Status status = getStatusAndMakeSureExists(statusCode);
275 		privilege.setStatus(status);
276 
277 		// Create the entity
278 		privilegeDao.create(privilege);
279 	}
280 
281 }