Alejandro Acuña
2025-04-29 d1d736e487d9eb104dcae9def948066037afd2f0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
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
    {
    }
 
    //<editor-fold defaultstate="collapsed" desc="Users">    
     /**
     * @param parameters <code>InteropParameters</code>
     * <p>name: "language" class: <code>String</code>
     * <p>name: "lGroup" class: <code>String[]</code>
     * @return <code>InteropResponse</code> 
     * <code>UserIdentification[]</code>
     * @throws SerializationException 
     */
    @SuppressWarnings("unchecked")
    public InteropResponse getUsersIdentification(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
        
        List<UserIdentification> 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 <code>InteropParameters</code>
     * <p>name: "language" class: <code>String</code>
     * <p>name: "lGroup" class: <code>String[]</code>
     * @return <code>InteropResponse</code> 
     * <code>UserInformation[]</code>
     * @throws SerializationException 
     */
    @SuppressWarnings("unchecked")
    public InteropResponse getUsersInformation(InteropParameters parameters) throws SerializationException
    {
        String language = (String)parameters.getParameterValue("language");
        
        List<UserInformation> result = new ArrayList<>();
        try
        {
            String bodyContent = (String) parameters.getParameterValue("bodyContent");
            List<Device> 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()]));
    }
    //</editor-fold>
    
        
     /**
     * @param parameters <code>InteropParameters</code>
     * <p>name: "get" class: <code>getUserLdap</code>
     * <p>name: "language" class: <code>String</code>
     * <p>name: "username" class: <code>String</code>
     * @return <code>InteropResponse</code> 
     * <code>UserInformation</code>
     * @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<String> 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<String> 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);
    }
}