package art.servers.transactionsserver.model.access; import art.library.interop.InteropParameters; import art.library.interop.InteropResponse; import art.library.interop.serialization.Serialization; import art.library.interop.serialization.SerializationException; import art.library.model.devices.Device; import art.library.model.devices.user.User; import art.library.model.devices.user.UserIdentification; import art.library.model.devices.user.UserInformation; import art.library.model.devices.user.UserPermission; import art.library.net.ldap.LDAP; import art.library.utils.licence.Licence; import art.library.utils.password.PasswordGenerator; import art.servers.transactionsserver.Shared; import art.servers.transactionsserver.controller.ListenerImplementation; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.List; import java.util.logging.Level; import java.util.logging.Logger; import java.util.stream.Collectors; public class ModelUsers { public ModelUsers() throws Exception { } // /** * @param parameters InteropParameters *

name: "language" class: String *

name: "lGroup" class: String[] * @return InteropResponse * UserIdentification[] * @throws SerializationException */ @SuppressWarnings("unchecked") public InteropResponse getUsersIdentification(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); List result = new ArrayList<>(); try { String[] lgroup = (String[])parameters.getParameterValue("lGroup"); for (Device device : Shared.model.getDevices(lgroup)) { try { User user = (User)device; UserIdentification userIdentification = new UserIdentification(); userIdentification.group = user.information.group; try { userIdentification.username = Licence.decrypt(user.username); } catch (Exception e) { userIdentification.username = user.username; } userIdentification.role = user.getDeviceInformation().role; result.add(userIdentification); } catch (Exception e) { } } Collections.sort(result, (UserIdentification o1, UserIdentification o2) -> (o1.group+o1.username).compareTo((o2.group+o2.username))); } catch (Exception e) { e.printStackTrace(); throw new SerializationException(e.getMessage()); } return new InteropResponse(result.toArray(new UserIdentification[result.size()])); } /** * @param parameters InteropParameters *

name: "language" class: String *

name: "lGroup" class: String[] * @return InteropResponse * UserInformation[] * @throws SerializationException */ @SuppressWarnings("unchecked") public InteropResponse getUsersInformation(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); List result = new ArrayList<>(); try { String bodyContent = (String) parameters.getParameterValue("bodyContent"); List ldevice = new ArrayList<>(); if (bodyContent != null) { String[] lusername = (String[]) Serialization.deserialize(String[].class, bodyContent); ldevice.addAll(Shared.model.getDevicesFromIdentifier(lusername)); }else{ String[] lgroup = (String[])parameters.getParameterValue("lGroup"); ldevice.addAll(Arrays.asList(Shared.model.getDevices(lgroup))); } result.addAll(ldevice.stream().map((w) -> { UserInformation information = Serialization.clone(w.getDeviceInformation()); try { information.setIdentifier(Licence.decrypt(information.getIdentifier())); } catch (Exception ex) { Logger.getLogger(ModelUsers.class.getName()).log(Level.SEVERE, null, ex); } return information; }).collect(Collectors.toList())); Collections.sort(result, (UserInformation o1, UserInformation o2) -> (o1.group+o1.getIdentifier()).compareTo((o2.group+o2.getIdentifier()))); } catch (Exception e) { throw new SerializationException(e.getMessage()); } return new InteropResponse(result.toArray(new UserInformation[result.size()])); } // /** * @param parameters InteropParameters *

name: "get" class: getUserLdap *

name: "language" class: String *

name: "username" class: String * @return InteropResponse * UserInformation * @throws SerializationException */ public InteropResponse getUserLdap(InteropParameters parameters) throws SerializationException { String language = (String)parameters.getParameterValue("language"); try { Shared.println("ModelUsers", "1.GetUserLDAP: " + Shared.getConfiguration().lldapServer); if ((Shared.getConfiguration().lldapServer == null) || (Shared.getConfiguration().lldapServer.length <= 0)) { return new InteropResponse(Shared.getMessage("Information:") + " " + Shared.getMessage("art.servers.transactionsserver.ldap.json not defined")); } String username = (String)parameters.getParameterValue("username"); String usernameDecrypt; try { usernameDecrypt = Licence.decrypt(username); } catch (Exception e) { usernameDecrypt = username; username = Licence.encrypt(username); } Shared.println("ModelUsers", "2.GetUserLDAP username: " + usernameDecrypt + " - " + username); LDAP ldap = new LDAP(Arrays.asList(Shared.getConfiguration().lldapServer)); Shared.println("ModelUsers", "GetUserLdap: LDAP: " + ldap); if (ldap.userExists(usernameDecrypt)) { Shared.println("ModelUsers", "GetUserLdap: LDAP user exists: " + usernameDecrypt); art.library.net.ldap.User ldapUser = ldap.getUser(usernameDecrypt); //GET ARTIC USER User user = null; try { user = (User) Shared.getModel().getDevice(username); } catch (Exception e) { Shared.println("ModelUsers", "GetUserLdap: LDAP user exists, user ARTIC ex: " + e.toString()); UserInformation userInformation = new UserInformation(); userInformation.setIdentifier(username); userInformation.group = ldapUser.department; userInformation.name = ldapUser.displayName; userInformation.company = ldapUser.company; userInformation.phone = ldapUser.telephoneNumber; userInformation.address = ldapUser.userPrincipalName; userInformation.description = ldapUser.employeeID; userInformation.password = Licence.encrypt(PasswordGenerator.generatePassword(8)); List lOrganizationUnit = new ArrayList<>(); if (ldapUser.getOrganizationalUnitsMembership().size() > 0) { lOrganizationUnit = ldapUser.getOrganizationalUnitsMembership().stream().distinct().collect(Collectors.toList()); } lOrganizationUnit.add("default"); StringBuilder sb = new StringBuilder(); lOrganizationUnit.stream().map(w -> (" "+ sb.append(w))); List lorganizationUnitEncrypt = lOrganizationUnit.stream().map(w -> { try { return Licence.encrypt(w); } catch (Exception ex) { Logger.getLogger(ModelUsers.class.getName()).log(Level.SEVERE, null, ex); } return w; }).collect(Collectors.toList()); //GET USER PERMISSIONS FROM ORGANIZATION USERS InteropParameters parametersGetUserPermission = new InteropParameters(); parametersGetUserPermission.addBodycontent(lorganizationUnitEncrypt); for (String organization : lOrganizationUnit) { Shared.println("ModelUSers", "GetUserLdap - Organization: " + organization); } Shared.getModel().addDevices(new UserInformation[]{userInformation}); user = (User) Shared.getModel().getDevice(username); InteropResponse response = Shared.getModel().modelPermissions.getUserPermission(parametersGetUserPermission); // Object[] object = response.getValue(); try { UserPermission userPermission = response.getResponse(); Shared.println("ModelUSers", "GetUserLdap - UserPermission: DeviceFeature: " + userPermission.lPermissionDeviceFeature.size() + " - Feature: " + userPermission.lPermissionFeature.size() + " - Read: " + userPermission.lPermissionDeviceRead.size() + " - Write: " + userPermission.lPermissionDeviceWrite.size()); user.getDeviceConfiguration().permissions = userPermission; User[] luser = new User[1]; luser[0] = user; InteropParameters parametersPermissions = new InteropParameters(); parametersPermissions.addParameter("language", Shared.getLanguage()); parametersPermissions.addBodycontent(luser); ((ListenerImplementation)Shared.controllerListener.getListenerImplementation()).addDevices(parametersPermissions); } catch (Exception ex) { Shared.printstack("ModelUsers", ex); } } ldap.close(); //we return String username that means user is correct. UserIdentification userIdentification = new UserIdentification(); userIdentification.username = user.username; userIdentification.password = user.getDeviceInformation().password; return new InteropResponse(userIdentification); } Shared.println("ModelUsers", "GetUserLdap: LDAP user NOT exists: " + usernameDecrypt); } catch (Exception e) { Shared.printstack("ModelUsers", e); } //We return null, that means there is no ldap user for that username requested. return new InteropResponse((UserIdentification)null); } }