View Javadoc
1   package fr.ifremer.reefdb.service.administration.user;
2   
3   /*
4    * #%L
5    * Reef DB :: Core
6    * $Id:$
7    * $HeadURL:$
8    * %%
9    * Copyright (C) 2014 - 2015 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  
26  import com.google.common.collect.Lists;
27  import fr.ifremer.quadrige3.core.dao.administration.user.PrivilegeCode;
28  import fr.ifremer.quadrige3.core.dao.technical.Assert;
29  import fr.ifremer.quadrige3.core.security.Encryption;
30  import fr.ifremer.quadrige3.core.service.technical.CacheService;
31  import fr.ifremer.reefdb.dao.administration.user.ReefDbQuserDao;
32  import fr.ifremer.reefdb.dto.ReefDbBeans;
33  import fr.ifremer.reefdb.dto.configuration.filter.person.PersonCriteriaDTO;
34  import fr.ifremer.reefdb.dto.referential.PersonDTO;
35  import fr.ifremer.reefdb.dto.referential.PrivilegeDTO;
36  import fr.ifremer.reefdb.service.StatusFilter;
37  import org.springframework.beans.factory.annotation.Autowired;
38  import org.springframework.stereotype.Service;
39  
40  import javax.annotation.Resource;
41  import java.util.Collection;
42  import java.util.List;
43  
44  /**
45   * <p>UserServiceImpl class.</p>
46   *
47   */
48  @Service("reefDbUserService")
49  public class UserServiceImpl extends fr.ifremer.quadrige3.core.service.administration.user.UserServiceImpl implements UserService {
50  
51      @Autowired
52      protected CacheService cacheService;
53  
54      @Resource(name = "reefDbQuserDao")
55      protected ReefDbQuserDao quserDao;
56  
57      /** {@inheritDoc} */
58      @Override
59      public Integer getDepartmentIdByUserId(int userId) {
60          return quserDao.getDepartmentIdByUserId(userId);
61      }
62  
63      /** {@inheritDoc} */
64      @Override
65      public Boolean isLoginExtranet(String login) {
66          Assert.notBlank(login);
67          return quserDao.isLoginExtranet(login);
68      }
69  
70      /** {@inheritDoc} */
71      @Override
72      public boolean hasPassword(String login) {
73          Assert.notBlank(login);
74          return quserDao.hasPassword(login);
75      }
76  
77      /** {@inheritDoc} */
78      @Override
79      public boolean isLocalUserWithNoPassword(String login) {
80          Assert.notBlank(login);
81          return quserDao.isLocalUserWithNoPassword(login);
82      }
83  
84      /** {@inheritDoc} */
85      @Override
86      public void updatePasswordByUserId(int personId, String password) {
87          Assert.notBlank(password);
88          quserDao.updatePasswordByUserId(personId, Encryption.sha(password));
89      }
90  
91      /** {@inheritDoc} */
92      @Override
93      public List<PersonDTO> getActiveUsers() {
94          return quserDao.getAllUsers(StatusFilter.ACTIVE.toStatusCodes());
95      }
96  
97      /** {@inheritDoc} */
98      @Override
99      public void resetPassword(String login) {
100         Assert.notBlank(login);
101         quserDao.resetPassword(login);
102     }
103 
104     /** {@inheritDoc} */
105     @Override
106     public List<PersonDTO> searchUser(PersonCriteriaDTO searchCriteria) {
107         StatusFilter statusFilter = StatusFilter.toLocalOrNational(searchCriteria.getLocal());
108         List<String> statusCodes = statusFilter.intersect(searchCriteria.getStatus());
109         Integer departmentId = searchCriteria.getDepartment() == null ?
110                 null : searchCriteria.getDepartment().getId();
111         String privilegeCode = searchCriteria.getPrivilege() == null ?
112                 null : searchCriteria.getPrivilege().getCode();
113         return quserDao.findUsersByCriteria(
114                 searchCriteria.getName(),
115                 searchCriteria.getFirstName(),
116                 searchCriteria.isStrictName(),
117                 searchCriteria.getLogin(),
118                 departmentId,
119                 privilegeCode, statusCodes);
120     }
121 
122     /** {@inheritDoc} */
123     @Override
124     public List<PrivilegeDTO> getAllPrivileges() {
125         return quserDao.getAllPrivileges();
126     }
127 
128     /** {@inheritDoc} */
129     @Override
130     public List<PrivilegeDTO> getAvailablePrivileges() {
131         List<PrivilegeDTO> availablePrivileges = Lists.newArrayList();
132         for (PrivilegeDTO privilege : getAllPrivileges()) {
133             if (PrivilegeCode.LOCAL_ADMINISTRATOR.getValue().equals(privilege.getCode())
134                     || PrivilegeCode.QUALIFIER.getValue().equals(privilege.getCode())) {
135                 availablePrivileges.add(privilege);
136             }
137         }
138         return availablePrivileges;
139     }
140 
141     /** {@inheritDoc} */
142     @Override
143     public Collection<PrivilegeDTO> getPrivilegesByUser(Integer userId) {
144         return quserDao.getPrivilegesByUserId(userId);
145     }
146 
147     /** {@inheritDoc} */
148     @Override
149     public void saveUsers(List<PersonDTO> users) {
150         for (PersonDTO user : users) {
151             if (user.isDirty()) {
152                 quserDao.saveTemporaryUser(user);
153                 user.setDirty(false);
154             }
155         }
156     }
157 
158     /** {@inheritDoc} */
159     @Override
160     public void deleteUsers(List<Integer> personIds) {
161         quserDao.deleteTemporaryUser(personIds);
162     }
163 
164     /** {@inheritDoc} */
165     @Override
166     public void replaceUser(PersonDTO source, PersonDTO target, boolean delete) {
167 
168         Assert.notNull(source);
169         Assert.notNull(target);
170 
171         Integer sourceQuserId = source.getId();
172         Integer targetQuserId = target.getId();
173         Assert.isTrue(sourceQuserId < 0, String.format("Can't replace a user with a positive id %s", sourceQuserId));
174         Assert.isTrue(ReefDbBeans.isLocalStatus(source.getStatus()), String.format("Can't replace a matrix with non local status %s", sourceQuserId));
175 
176         if (!sourceQuserId.equals(targetQuserId)) {
177 
178             // replace occurences
179             quserDao.replaceTemporaryUserFks(sourceQuserId, targetQuserId, delete);
180 
181         }
182     }
183 
184     /** {@inheritDoc} */
185     @Override
186     public boolean isUserUsedInProgram(int userId) {
187         return quserDao.isUserUsedInProgram(userId);
188     }
189 
190     /** {@inheritDoc} */
191     @Override
192     public boolean isUserUsedInRules(int userId) {
193         return quserDao.isUserUsedInRules(userId);
194     }
195 
196     /** {@inheritDoc} */
197     @Override
198     public boolean isUserUsedInData(int userId) {
199         return quserDao.isUserUsedInData(userId);
200     }
201 
202     /** {@inheritDoc} */
203     @Override
204     public boolean isUserUsedInValidatedData(int userId) {
205         return quserDao.isUserUsedInValidatedData(userId);
206     }
207 
208 }