--- /dev/null
+/*******************************************************************************
+ * Copyright � Igor Barma, Alexandre Desoubeaux, Christian Martel, Eric Brun, Mathieu Amblard, Gwenael Gevet, Pierre Guillot, 2012
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Alexandre Lefevre, Marc Salvat 2014-2016
+ * Copyright Alexandre Desoubeaux, Christian Martel, Cedric Lecarpentier, Marc Salvat, Marc Suarez, Harifetra Ramamonjy 2017
+ *
+ * This file is part of the work and learning management system Pentila Nero.
+ *
+ * Pentila Nero is free software. You can redistribute it and/or modify since
+ * you respect the terms of either (at least one of the both license) :
+ * - under the terms of the GNU Affero General Public License as
+ * published by the Free Software Foundation, either version 3 of the
+ * License, or (at your option) any later version.
+ * - the CeCILL-C as published by CeCILL-C; either version 1 of the
+ * License, or any later version
+ * - the GNU Lesser General Public License as published by the
+ * Free Software Foundation, either version 3 of the license,
+ * or (at your option) any later version.
+ *
+ * There are special exceptions to the terms and conditions of the
+ * licenses as they are applied to this software. View the full text of
+ * the exception in file LICENSE-PROJECT.txt in the directory of this software
+ * distribution.
+ *
+ * Pentila Nero is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+ * Licenses for more details.
+ *
+ * You should have received a copy of the GNU Affero General Public License
+ * and the CeCILL-C and the GNU Lesser General Public License along with
+ * Pentila Nero. If not, see :
+ * <http://www.gnu.org/licenses/> and
+ * <http://www.cecill.info/licences.fr.html>.
+ ******************************************************************************/
+package com.pentila.entSavoie;
+
+import java.util.ArrayList;
+import java.util.HashMap;
+import java.util.HashSet;
+import java.util.List;
+import java.util.Locale;
+import java.util.Map;
+import java.util.Set;
+
+import com.liferay.portal.NoSuchRoleException;
+import com.liferay.portal.kernel.bean.PortletBeanLocatorUtil;
+import com.liferay.portal.kernel.dao.orm.Disjunction;
+import com.liferay.portal.kernel.dao.orm.DynamicQuery;
+import com.liferay.portal.kernel.dao.orm.DynamicQueryFactoryUtil;
+import com.liferay.portal.kernel.dao.orm.RestrictionsFactoryUtil;
+import com.liferay.portal.kernel.exception.PortalException;
+import com.liferay.portal.kernel.exception.SystemException;
+import com.liferay.portal.kernel.log.Log;
+import com.liferay.portal.kernel.log.LogFactoryUtil;
+import com.liferay.portal.kernel.util.LocalizationUtil;
+import com.liferay.portal.model.Role;
+import com.liferay.portal.model.RoleConstants;
+import com.liferay.portal.model.User;
+import com.liferay.portal.service.CompanyLocalServiceUtil;
+import com.liferay.portal.service.GroupLocalServiceUtil;
+import com.liferay.portal.service.RoleLocalServiceUtil;
+import com.pentila.entSavoie.UserProfile;
+import com.pentila.entSavoie.userManagement.service.ClpSerializer;
+
+import static com.pentila.entSavoie.ENTRolesConstants.*;
+
+/**
+ * @author Lancelot SIX
+ *
+ * Permet de gérer d'avoir une API simplifiée vers les roles specifiques a
+ * l'ent (à savoir roles enseignant, parent, prof, ...)
+ *
+ */
+public class ENTRolesUtil {
+
+ private static Log logger = LogFactoryUtil.getLog(ENTRolesUtil.class);
+
+
+ /**
+ * Verify if the user in parameter is local admin
+ * @param pUser User to verify if it is a local adin
+ * @return true if the user is a local admin, false otherwise (case of exception for exemple)
+ */
+ public static boolean isLocalAdmin(User pUser){
+ Role adminGrpRole;
+ try {
+ adminGrpRole = ENTRoleUtilFactory.getInstance(pUser.getCompanyId()).getRole(ENTRolesConstants.GROUP_ADMIN);
+ return RoleLocalServiceUtil.hasUserRole(pUser.getUserId(), adminGrpRole.getRoleId());
+ } catch (Exception e){
+ logger.error(e);
+ }
+ return false;
+ }
+
+ public ENTRolesUtil(long companyId)
+ {
+ setCompanyId(companyId);
+ }
+
+ public void setCompanyId(long companyId) {
+ this._companyId = companyId;
+ }
+
+ public Role getRole(String roleName) throws PortalException, SystemException {
+ return getRole(roleName, "Role genere automatiquement");
+ }
+
+ public Role getRole(String roleName, String description) throws PortalException, SystemException {
+ Role desiredRole = null;
+ try {
+ desiredRole = RoleLocalServiceUtil.getRole(_companyId, roleName);
+ } catch (NoSuchRoleException e) {
+ Map<Locale, String> descriptionMap =
+ LocalizationUtil.getLocalizationMap(description);
+
+ //Le role n'existe pas, on le cree
+ desiredRole = RoleLocalServiceUtil.addRole(
+ CompanyLocalServiceUtil.getCompany(_companyId).
+ getDefaultUser().getUserId(),
+ _companyId, roleName, new HashMap<Locale, String>(), descriptionMap,
+ RoleConstants.TYPE_REGULAR);
+ }
+ return desiredRole;
+ }
+
+ /**
+ * Retourne l'ensemble des roles ENT de l'utilisateur
+ *
+ * @param u
+ * @return
+ */
+ public Role[] getENTRoles(User u) {
+ Set<Role> userRoles = new HashSet<Role>();
+ try {
+ for (Role userRole : u.getRoles()) {
+ if (isNationalRole(userRole)) {
+ userRoles.add(userRole);
+ }
+ }
+ } catch (SystemException e) {
+ logger.error(e);
+ }
+ return userRoles.toArray(new Role[0]);
+ }
+
+ /**
+ * @param entPersonsProfils Un code national tel que present dans le ldap
+ * @return Le nom du role correspondant dans l'ENT
+ */
+ public String transformCodeNationalBrut(String entPersonsProfils) {
+ return entPersonsProfils;
+ }
+
+ /**
+ * Determine si oui ou non le role donne est une role correspondant
+ * a un role de la nomenclature nationale ou de la version etendue
+ *
+ * @param r
+ * @return
+ */
+ public boolean isNationalRole(Role r) {
+ final String roleName = r.getName();
+ for (String code : CODES_NATIONAUX) {
+ if (code.equals(roleName)) {
+ return true;
+ }
+ }
+ for (String code : CODES_ADITIONNELS) {
+ if (code.equals(roleName)) {
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * Permet de transformer une valeur extraite du LDAP correspondant au champ
+ * ENTPersonFonctions en un couple {nom_role, description_role}.<br />
+ *
+ * Pour rappel, cette attribut est de la forme suivante:
+ * <idEtab>$<code_fonction>$<fonction>$<code_spe>$<spe>
+ *
+ * Retourne null en cas de probleme (malformation du champ).
+ *
+ * @param attributeVal
+ * @return
+ */
+ public String[] translateENTPersonFonctionsToRoleName(final String attributeVal) {
+ if (attributeVal==null) return null;
+ String[] extraction = attributeVal.split("\\$");
+ if (extraction.length != 5) return null;
+ final String code = transformeCodeFonctionEnRoleName(extraction[1]);
+ if (code==null) return null;
+ return new String[]{
+ code,
+ extraction[2]};
+ }
+
+
+ /**
+ * Cette fonction permet de faire la translation entre un code fonction
+ * et un role ENT. Retourne null si le code est inconne
+ *
+ * Le Voici les significations des codes:
+ * ADF -> PERSONNELS ADMINISTRATIFS
+ * AED -> ASSISTANT D'EDUCATION
+ * ASE -> ASSISTANT ETRANGER
+ * DIR -> DIRECTION
+ * DOC -> DOCUMENTATION
+ * EDU -> EDUCATION
+ * ENS -> ENSEIGNEMENT
+ * LAB -> PERSONNELS DE LABORATOIRE
+ * MDS -> PERSONNELS MEDICO-SOCIAUX
+ * ORI -> ORIENTATION
+ *
+ * @param code
+ * @return
+ */
+ private String transformeCodeFonctionEnRoleName(final String code) {
+ if (code.equals("ADF")) {
+ return ENTRolesConstants.NATIONAL_20;
+ } else if (code.equals("AED")) {
+ return ENTRolesConstants.NATIONAL_21;
+ } else if (code.equals("ASE")) {
+ return ENTRolesConstants.NATIONAL_22;
+ } else if (code.equals("DIR")) {
+ return ENTRolesConstants.NATIONAL_23;
+ } else if (code.equals("DOC")) {
+ return ENTRolesConstants.NATIONAL_24;
+ } else if (code.equals("EDU")) {
+ return ENTRolesConstants.NATIONAL_25;
+ } else if (code.equals("ENS")) {
+ return ENTRolesConstants.NATIONAL_26;
+ } else if (code.equals("LAB")) {
+ return ENTRolesConstants.NATIONAL_27;
+ } else if (code.equals("TEC")) {
+ return ENTRolesConstants.NATIONAL_27;
+ } else if (code.equals("MDS")) {
+ return ENTRolesConstants.NATIONAL_28;
+ } else if (code.equals("ORI")) {
+ return ENTRolesConstants.NATIONAL_29;
+ }
+ return null;
+ }
+
+ public String getUserTypeByRole(User user) throws PortalException, SystemException{
+ String result = "";
+ List<Role> lrole = user.getRoles();
+
+ Role[] personnels = { RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_5),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_6),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_20),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_21),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_22),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_25),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_27),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_28)};
+
+ Role[] personnelsDirection = {RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_4),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_23)};
+
+ Role[] enseignants = {RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_3),
+ RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_26)};
+
+
+ for (Role r : enseignants){
+ if(lrole.contains(r)){
+ result = "enseignant";
+ break;
+ }
+ }
+ if(result.isEmpty()){
+ for (Role r : personnelsDirection){
+ if(lrole.contains(r)){
+ result = "personnelDirection";
+ break;
+ }
+ }
+ }
+ if(result.isEmpty()){
+ if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_24))){
+ result = "documentaliste";
+ }
+ }
+
+ if(result.isEmpty()){
+ if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_25))){
+ result = "CPE";
+ }
+ }
+
+ if (result.isEmpty()){
+ if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_2))){
+ result = "parent";
+ }
+ }
+ if(result.isEmpty()){
+ if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_1))){
+ result = "eleve";
+ }
+ }
+ if(result.isEmpty()){
+ if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_29))){
+ result = "conseillerOrientation";
+ }
+ }
+ if(result.isEmpty()){
+ if(lrole.contains(RoleLocalServiceUtil.getRole(user.getCompanyId(), ENTRolesConstants.NATIONAL_7))){
+ result = "personnelRectorat";
+ }
+ }
+ if(result.isEmpty()){
+ for (Role r : personnels){
+ if(lrole.contains(r)){
+ result = "personnel";
+ break;
+ }
+ }
+ }
+
+
+ return result;
+ }
+
+ /**
+ * this method verify if a role (with his strin representation) is contains in a tab of String.
+ * @param pRoleName the role (as string) to search
+ * @param pRoleStringList the list where we search the role
+ * @return trye if the role is find false otherwise
+ */
+ public static boolean isRoleStringContainsInRoleStringList(String pRoleName, String[] pRoleStringList){
+ if(pRoleStringList == null || pRoleStringList.length == 0){
+ return false;
+ }
+
+ for(String aRoleStringFromList : pRoleStringList){
+ if(pRoleName.equals(aRoleStringFromList)){
+ return true;
+ }
+ }
+
+ return false;
+ }
+
+ /**
+ * This method verify if the role in parameter (as string) as right to access at all classes in etab.
+ * The roles allow are contains on ENTRolesConstants.ROLES_ACCESS_TO_ALL_CLASS and group_admin
+ * @param pRoleName the r�le to compare
+ * @return true if role is allow to acces to all classes in etab
+ */
+ public static boolean isAllowedToHaveAllEtabsClasses(String pRoleName){
+ // if role is group admin it is allowed
+ if(pRoleName.equals(ENTRolesConstants.GROUP_ADMIN)){
+ return true;
+ }
+ // if role is contains is roles allowed return true
+ for(String[] roles : ENTRolesConstants.ROLES_ACCESS_TO_ALL_CLASS){
+ if(isRoleStringContainsInRoleStringList(pRoleName,roles)){
+ return true;
+ }
+ }
+ return false;
+ }
+
+ /**
+ * This methode checks if the user has visibility on all roles.
+ * For now, only student can't search other role than their own
+ * @param user The user currently searching for other users
+ * @return a boolean that indicates if the user has visibility on all roles
+ */
+ public static boolean hasSearchOnAllRoles(User user) {
+ try {
+ //Only Student haven't got the right to search on all roles for now
+ if (!RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(), ENTRolesConstants.NATIONAL_1, false)) {
+ return true;
+ }
+ } catch (Exception e) {
+ logger.error(e);
+ }
+ return false;
+ }
+
+ /**
+ * This method returns all the roles on which the user can perform a search
+ * For now, only student can't search other role than their own
+ * @param user The user currently searching for other users
+ * @return a Role List which are the role visible for the user received
+ */
+ public static List<Role> getSearchableRoles(User user) {
+ ENTRolesUtil ru = ENTRoleUtilFactory.getInstance(user.getCompanyId());
+ List<Role> visibleRoles = new ArrayList<Role>();
+
+ // Student can only search for other students, if it's an other user all roles are allowed in search
+ try {
+ if (!RoleLocalServiceUtil.hasUserRole(user.getUserId(), user.getCompanyId(), ENTRolesConstants.NATIONAL_1, false)) {
+
+ for(String roleName : ENTRolesConstants.getCodesFiltered(user.getCompanyId())) {
+ visibleRoles.add(ru.getRole(roleName));
+ }
+ }
+ else {
+ visibleRoles.add(ru.getRole(ENTRolesConstants.NATIONAL_1));
+ }
+ } catch (Exception e) {
+ logger.error(e);
+ }
+
+ return visibleRoles;
+ }
+
+ /**
+ * Methods which return the list of role from roleId
+ * Don't use to edit ROLE
+ * If you want to get a pre define role prefer to use : ENTRoleUtilFactory.getInstance(companyId).getRole(MyRoleName);
+ * @param listRoleId the role id we search for
+ * @return
+ * @throws SystemException
+ */
+ @SuppressWarnings("unchecked")
+ public static List<Role> getRoleFromRoleIds(List<Long> listRoleId) throws SystemException{
+
+ ClassLoader classLoader = (ClassLoader)PortletBeanLocatorUtil.locate(ClpSerializer.getServletContextName(),"portletClassLoader");
+ DynamicQuery dynamicQuery = DynamicQueryFactoryUtil.forClass(Role.class,classLoader);
+ Disjunction disjunctionORoleId = RestrictionsFactoryUtil.disjunction();
+
+ for(Long roleId : listRoleId){
+ disjunctionORoleId.add(RestrictionsFactoryUtil.eq("roleId", roleId));
+ }
+ dynamicQuery.add(disjunctionORoleId);
+ List<Role> roleToReturn = new ArrayList<Role>();
+ roleToReturn.addAll(GroupLocalServiceUtil.dynamicQuery(dynamicQuery));
+
+ return roleToReturn;
+ }
+
+ /**
+ * Get the User Profile
+ * @param user
+ * @return
+ * @throws SystemException
+ * @throws NoSuchRoleException
+ */
+ public static int getUserProfile(User user) throws SystemException,NoSuchRoleException {
+ List<Role> roleList = RoleLocalServiceUtil.getUserRoles(user.getUserId());
+ int userProfile = 0;
+ for (Role role : roleList) {
+ if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_STUDENT)) {
+ userProfile = UserProfile.STUDENT.getId();
+ break;
+ }
+ else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_PARENT)) {
+ userProfile = UserProfile.PARENT.getId();
+ break;
+ }
+ else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_TEACHER)) {
+ userProfile = UserProfile.TEACHER.getId();
+ break;
+ }
+ else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_DIRECTOR)) {
+ userProfile = UserProfile.DIRECTOR.getId();
+ break;
+ }
+ else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_PERSONNAL)) {
+ userProfile = UserProfile.PERSONNAL.getId();
+ break;
+ }
+ else if (ENTRolesConstants.isRoleContainsInRoleNameArray(role, ENTRolesConstants.LIST_ROLES_ACADEMIC)) {
+ userProfile = UserProfile.OTHER.getId();
+ break;
+ }
+ }
+ if (userProfile == 0) {
+ throw new NoSuchRoleException("User "+user.getFullName()+" with id "+user.getUserId()+" doesn't have a valid role");
+ } else {
+ return userProfile;
+ }
+
+ }
+
+ private long _companyId;
+}