1 /*******************************************************************************
2 * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
3 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
4 * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
6 * This file is part of the work and learning management system Pentila Nero.
8 * Pentila Nero is free software. You can redistribute it and/or modify since
9 * you respect the terms of either (at least one of the both license) :
10 * - under the terms of the GNU Affero General Public License as
11 * published by the Free Software Foundation, either version 3 of the
12 * License, or (at your option) any later version.
13 * - the CeCILL-C as published by CeCILL-C; either version 1 of the
14 * License, or any later version
15 * - the GNU Lesser General Public License as published by the
16 * Free Software Foundation, either version 3 of the license,
17 * or (at your option) any later version.
19 * There are special exceptions to the terms and conditions of the
20 * licenses as they are applied to this software. View the full text of
21 * the exception in file LICENSE-PROJECT.txt in the directory of this software
24 * Pentila Nero is distributed in the hope that it will be useful,
25 * but WITHOUT ANY WARRANTY; without even the implied warranty of
26 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
27 * Licenses for more details.
29 * You should have received a copy of the GNU Affero General Public License
30 * and the CeCILL-C and the GNU Lesser General Public License along with
31 * Pentila Nero. If not, see :
32 * <http://www.gnu.org/licenses/> and
33 * <http://www.cecill.info/licences.fr.html>.
34 ******************************************************************************/
35 package com.pentila.entSavoie;
37 import java.util.ArrayList;
38 import java.util.HashMap;
39 import java.util.HashSet;
40 import java.util.List;
41 import java.util.Locale;
45 import com.liferay.portal.NoSuchRoleException;
46 import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
47 import com.liferay.portal.kernel.dao.orm.Disjunction;
48 import com.liferay.portal.kernel.dao.orm.DynamicQuery;
49 import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
50 import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
51 import com.liferay.portal.kernel.exception.PortalException;
52 import com.liferay.portal.kernel.exception.SystemException;
53 import com.liferay.portal.kernel.log.Log;
54 import com.liferay.portal.kernel.log.LogFactoryUtil;
55 import com.liferay.portal.kernel.util.LocalizationUtil;
56 import com.liferay.portal.model.Role;
57 import com.liferay.portal.model.RoleConstants;
58 import com.liferay.portal.model.User;
59 import com.liferay.portal.service.CompanyLocalServiceUtil;
60 import com.liferay.portal.service.GroupLocalServiceUtil;
61 import com.liferay.portal.service.RoleLocalServiceUtil;
62 import com.pentila.entSavoie.UserProfile;
63 import com.pentila.entSavoie.userManagement.service.ClpSerializer;
65 import static com.pentila.entSavoie.ENTRolesConstants.*;
68 * @author Lancelot SIX
70 * Permet de gérer d'avoir une API simplifiée vers les roles specifiques a
71 * l'ent (à savoir roles enseignant, parent, prof, ...)
74 public class ENTRolesUtil {
76 private static Log logger = LogFactoryUtil.getLog(ENTRolesUtil.class);
80 * Verify if the user in parameter is local admin
81 * @param pUser User to verify if it is a local adin
82 * @return true if the user is a local admin, false otherwise (case of exception for exemple)
84 public static boolean isLocalAdmin(User pUser){
87 adminGrpRole = ENTRoleUtilFactory.getInstance(pUser.getCompanyId()).getRole(ENTRolesConstants.GROUP_ADMIN);
88 return RoleLocalServiceUtil.hasUserRole(pUser.getUserId(), adminGrpRole.getRoleId());
89 } catch (Exception e){
95 public ENTRolesUtil(long companyId)
97 setCompanyId(companyId);
100 public void setCompanyId(long companyId) {
101 this._companyId = companyId;
104 public Role getRole(String roleName) throws PortalException, SystemException {
105 return getRole(roleName, "Role genere automatiquement");
108 public Role getRole(String roleName, String description) throws PortalException, SystemException {
109 Role desiredRole = null;
111 desiredRole = RoleLocalServiceUtil.getRole(_companyId, roleName);
112 } catch (NoSuchRoleException e) {
113 Map<Locale, String> descriptionMap =
114 LocalizationUtil.getLocalizationMap(description);
116 //Le role n'existe pas, on le cree
117 desiredRole = RoleLocalServiceUtil.addRole(
118 CompanyLocalServiceUtil.getCompany(_companyId).
119 getDefaultUser().getUserId(),
120 _companyId, roleName, new HashMap<Locale, String>(), descriptionMap,
121 RoleConstants.TYPE_REGULAR);
127 * Retourne l'ensemble des roles ENT de l'utilisateur
132 public Role[] getENTRoles(User u) {
133 Set<Role> userRoles = new HashSet<Role>();
135 for (Role userRole : u.getRoles()) {
136 if (isNationalRole(userRole)) {
137 userRoles.add(userRole);
140 } catch (SystemException e) {
143 return userRoles.toArray(new Role[0]);
147 * @param entPersonsProfils Un code national tel que present dans le ldap
148 * @return Le nom du role correspondant dans l'ENT
150 public String transformCodeNationalBrut(String entPersonsProfils) {
151 return entPersonsProfils;
155 * Determine si oui ou non le role donne est une role correspondant
156 * a un role de la nomenclature nationale ou de la version etendue
161 public boolean isNationalRole(Role r) {
162 final String roleName = r.getName();
163 for (String code : CODES_NATIONAUX) {
164 if (code.equals(roleName)) {
168 for (String code : CODES_ADITIONNELS) {
169 if (code.equals(roleName)) {
177 * Permet de transformer une valeur extraite du LDAP correspondant au champ
178 * ENTPersonFonctions en un couple {nom_role, description_role}.<br />
180 * Pour rappel, cette attribut est de la forme suivante:
181 * <idEtab>$<code_fonction>$<fonction>$<code_spe>$<spe>
183 * Retourne null en cas de probleme (malformation du champ).
185 * @param attributeVal
188 public String[] translateENTPersonFonctionsToRoleName(final String attributeVal) {
189 if (attributeVal==null) return null;
190 String[] extraction = attributeVal.split("\\$");
191 if (extraction.length != 5) return null;
192 final String code = transformeCodeFonctionEnRoleName(extraction[1]);
193 if (code==null) return null;
201 * Cette fonction permet de faire la translation entre un code fonction
202 * et un role ENT. Retourne null si le code est inconne
204 * Le Voici les significations des codes:
205 * ADF -> PERSONNELS ADMINISTRATIFS
206 * AED -> ASSISTANT D'EDUCATION
207 * ASE -> ASSISTANT ETRANGER
209 * DOC -> DOCUMENTATION
211 * ENS -> ENSEIGNEMENT
212 * LAB -> PERSONNELS DE LABORATOIRE
213 * MDS -> PERSONNELS MEDICO-SOCIAUX
219 private String transformeCodeFonctionEnRoleName(final String code) {
220 if (code.equals("ADF")) {
221 return ENTRolesConstants.NATIONAL_20;
222 } else if (code.equals("AED")) {
223 return ENTRolesConstants.NATIONAL_21;
224 } else if (code.equals("ASE")) {
225 return ENTRolesConstants.NATIONAL_22;
226 } else if (code.equals("DIR")) {
227 return ENTRolesConstants.NATIONAL_23;
228 } else if (code.equals("DOC")) {
229 return ENTRolesConstants.NATIONAL_24;
230 } else if (code.equals("EDU")) {
231 return ENTRolesConstants.NATIONAL_25;
232 } else if (code.equals("ENS")) {
233 return ENTRolesConstants.NATIONAL_26;
234 } else if (code.equals("LAB")) {
235 return ENTRolesConstants.NATIONAL_27;
236 } else if (code.equals("TEC")) {
237 return ENTRolesConstants.NATIONAL_27;
238 } else if (code.equals("MDS")) {
239 return ENTRolesConstants.NATIONAL_28;
240 } else if (code.equals("ORI")) {
241 return ENTRolesConstants.NATIONAL_29;
246 public String getUserTypeByRole(User user) throws PortalException, SystemException{
248 List<Role> lrole = user.getRoles();
250 Role[] personnels = { RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_5),
251 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_6),
252 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_20),
253 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_21),
254 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_22),
255 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_25),
256 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_27),
257 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_28)};
259 Role[] personnelsDirection = {RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_4),
260 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_23)};
262 Role[] enseignants = {RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_3),
263 RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_26)};
266 for (Role r : enseignants){
267 if(lrole.contains(r)){
268 result = "enseignant";
272 if(result.isEmpty()){
273 for (Role r : personnelsDirection){
274 if(lrole.contains(r)){
275 result = "personnelDirection";
280 if(result.isEmpty()){
281 if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_24))){
282 result = "documentaliste";
286 if(result.isEmpty()){
287 if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_25))){
292 if (result.isEmpty()){
293 if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_2))){
297 if(result.isEmpty()){
298 if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_1))){
302 if(result.isEmpty()){
303 if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_29))){
304 result = "conseillerOrientation";
307 if(result.isEmpty()){
308 if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_7))){
309 result = "personnelRectorat";
312 if(result.isEmpty()){
313 for (Role r : personnels){
314 if(lrole.contains(r)){
315 result = "personnel";
326 * this method verify if a role (with his strin representation) is contains in a tab of String.
327 * @param pRoleName the role (as string) to search
328 * @param pRoleStringList the list where we search the role
329 * @return trye if the role is find false otherwise
331 public static boolean isRoleStringContainsInRoleStringList(String pRoleName, String[] pRoleStringList){
332 if(pRoleStringList == null || pRoleStringList.length == 0){
336 for(String aRoleStringFromList : pRoleStringList){
337 if(pRoleName.equals(aRoleStringFromList)){
346 * This method verify if the role in parameter (as string) as right to access at all classes in etab.
347 * The roles allow are contains on ENTRolesConstants.ROLES_ACCESS_TO_ALL_CLASS and group_admin
348 * @param pRoleName the r�le to compare
349 * @return true if role is allow to acces to all classes in etab
351 public static boolean isAllowedToHaveAllEtabsClasses(String pRoleName){
352 // if role is group admin it is allowed
353 if(pRoleName.equals(ENTRolesConstants.GROUP_ADMIN)){
356 // if role is contains is roles allowed return true
357 for(String[] roles : ENTRolesConstants.ROLES_ACCESS_TO_ALL_CLASS){
358 if(isRoleStringContainsInRoleStringList(pRoleName,roles)){
366 * This methode checks if the user has visibility on all roles.
367 * For now, only student can't search other role than their own
368 * @param user The user currently searching for other users
369 * @return a boolean that indicates if the user has visibility on all roles
371 public static boolean hasSearchOnAllRoles(User user) {
373 //Only Student haven't got the right to search on all roles for now
374 if (!RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(), ENTRolesConstants.NATIONAL_1, false)) {
377 } catch (Exception e) {
384 * This method returns all the roles on which the user can perform a search
385 * For now, only student can't search other role than their own
386 * @param user The user currently searching for other users
387 * @return a Role List which are the role visible for the user received
389 public static List<Role> getSearchableRoles(User user) {
390 ENTRolesUtil ru = ENTRoleUtilFactory.getInstance(user.getCompanyId());
391 List<Role> visibleRoles = new ArrayList<Role>();
393 // Student can only search for other students, if it's an other user all roles are allowed in search
395 if (!RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(), ENTRolesConstants.NATIONAL_1, false)) {
397 for(String roleName : ENTRolesConstants.getCodesFiltered(user.getCompanyId())) {
398 visibleRoles.add(ru.getRole(roleName));
402 visibleRoles.add(ru.getRole(ENTRolesConstants.NATIONAL_1));
404 } catch (Exception e) {
412 * Methods which return the list of role from roleId
413 * Don't use to edit ROLE
414 * If you want to get a pre define role prefer to use : ENTRoleUtilFactory.getInstance(companyId).getRole(MyRoleName);
415 * @param listRoleId the role id we search for
417 * @throws SystemException
419 @SuppressWarnings("unchecked")
420 public static List<Role> getRoleFromRoleIds(List<Long> listRoleId) throws SystemException{
422 ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader");
423 DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Role.class,classLoader);
424 Disjunction disjunctionORoleId = RestrictionsFactoryUtil.disjunction();
426 for(Long roleId : listRoleId){
427 disjunctionORoleId.add(RestrictionsFactoryUtil.eq("roleId", roleId));
429 dynamicQuery.add(disjunctionORoleId);
430 List<Role> roleToReturn = new ArrayList<Role>();
431 roleToReturn.addAll(GroupLocalServiceUtil.dynamicQuery(dynamicQuery));
437 * Get the User Profile
440 * @throws SystemException
441 * @throws NoSuchRoleException
443 public static int getUserProfile(User user) throws SystemException,NoSuchRoleException {
444 List<Role> roleList = RoleLocalServiceUtil.getUserRoles(user.getUserId());
446 for (Role role : roleList) {
447 if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_STUDENT)) {
448 userProfile = UserProfile.STUDENT.getId();
451 else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_PARENT)) {
452 userProfile = UserProfile.PARENT.getId();
455 else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_TEACHER)) {
456 userProfile = UserProfile.TEACHER.getId();
459 else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_DIRECTOR)) {
460 userProfile = UserProfile.DIRECTOR.getId();
463 else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_PERSONNAL)) {
464 userProfile = UserProfile.PERSONNAL.getId();
467 else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_ACADEMIC)) {
468 userProfile = UserProfile.OTHER.getId();
472 if (userProfile == 0) {
473 throw new NoSuchRoleException("User "+user.getFullName()+" with id "+user.getUserId()+" doesn't have a valid role");
480 private long _companyId;