forked from idempiere/idempiere
-
Notifications
You must be signed in to change notification settings - Fork 0
/
MRole.java
3385 lines (3178 loc) · 104 KB
/
MRole.java
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
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/******************************************************************************
* Product: Adempiere ERP & CRM Smart Business Solution *
* Copyright (C) 1999-2006 ComPiere, Inc. All Rights Reserved. *
* This program is free software; you can redistribute it and/or modify it *
* under the terms version 2 of the GNU General Public License as published *
* by the Free Software Foundation. This program 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 GNU General Public License for more details. *
* You should have received a copy of the GNU General Public License along *
* with this program; if not, write to the Free Software Foundation, Inc., *
* 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
* For the text or an alternative of this public license, you may reach us *
* ComPiere, Inc., 2620 Augustine Dr. #245, Santa Clara, CA 95054, USA *
* or via info@compiere.org or http://www.compiere.org/license.html *
*****************************************************************************/
package org.compiere.model;
import static org.compiere.model.SystemIDs.USER_SUPERUSER;
import static org.compiere.model.SystemIDs.USER_SYSTEM;
import java.io.Serializable;
import java.lang.reflect.Array;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map.Entry;
import java.util.Properties;
import java.util.logging.Level;
import org.adempiere.exceptions.AdempiereException;
import org.adempiere.process.UUIDGenerator;
import org.compiere.util.CLogger;
import org.compiere.util.DB;
import org.compiere.util.DisplayType;
import org.compiere.util.Env;
import org.compiere.util.Ini;
import org.compiere.util.KeyNamePair;
import org.compiere.util.Msg;
import org.compiere.util.Trace;
import org.compiere.util.Util;
import org.compiere.wf.MWorkflow;
import org.compiere.wf.MWorkflowAccess;
import org.idempiere.cache.ImmutablePOSupport;
import org.idempiere.cache.POCopyCache;
/**
* Role Model.
* Includes AD_User runtime info for Personal Access
* The class is final, so that you cannot overwrite the security rules.
*
* @author Jorg Janke
* @author Karsten Thiemann FR [ 1782412 ]
* @author Carlos Ruiz - globalqss - FR [ 1846929 ] - implement ASP
* @contributor KittiU - FR [ 3062553 ] - Duplicated action in DocAction list for Multiple Role Users
* @version $Id: MRole.java,v 1.5 2006/08/09 16:38:47 jjanke Exp $
*/
public final class MRole extends X_AD_Role implements ImmutablePOSupport
{
/**
*
*/
private static final long serialVersionUID = -8937680640915708588L;
/**
* Get Default (Client) Role
* @return role
*/
public static MRole getDefault ()
{
return getDefault (Env.getCtx(), false);
} // getDefault
/**
* Get/Set Default Role.
* @param ctx context
* @param reload if true forces load
* @return role
* @see org.compiere.util.Login#loadPreferences(KeyNamePair, KeyNamePair, java.sql.Timestamp, String)
*/
public static MRole getDefault (Properties ctx, boolean reload)
{
int AD_Role_ID = Env.getContextAsInt(ctx, Env.AD_ROLE_ID);
int AD_User_ID = Env.getContextAsInt(ctx, Env.AD_USER_ID);
MRole defaultRole = getDefaultRole();
if (reload || defaultRole == null)
{
defaultRole = get (ctx, AD_Role_ID, AD_User_ID, reload);
setDefaultRole(defaultRole);
}
else if (defaultRole.getAD_Role_ID() != AD_Role_ID
|| defaultRole.getAD_User_ID() != AD_User_ID)
{
defaultRole = get (ctx, AD_Role_ID, AD_User_ID, reload);
setDefaultRole(defaultRole);
}
return defaultRole;
} // getDefault
private static void setDefaultRole(MRole defaultRole) {
Env.getCtx().remove(ROLE_KEY);
Env.getCtx().put(ROLE_KEY, defaultRole);
}
private static MRole getDefaultRole() {
return (MRole) Env.getCtx().get(ROLE_KEY);
}
/**
* Get Role for User from cache
* @param ctx context
* @param AD_Role_ID role
* @param AD_User_ID user
* @param reload if true forces load
* @return role
*/
public synchronized static MRole get (Properties ctx, int AD_Role_ID, int AD_User_ID, boolean reload)
{
if (s_log.isLoggable(Level.CONFIG)) s_log.config("AD_Role_ID=" + AD_Role_ID + ", AD_User_ID=" + AD_User_ID + ", reload=" + reload);
String key = AD_Role_ID + "_" + AD_User_ID;
MRole role = (MRole)s_roles.get (key, e -> new MRole(ctx, e));
if (role == null || reload)
{
role = new MRole (ctx, AD_Role_ID, null);
if (AD_Role_ID == SystemIDs.ROLE_SYSTEM)
{
String trxName = null;
role.load(trxName); // special Handling
}
role.setAD_User_ID(AD_User_ID);
role.loadAccess(reload);
s_roles.put (key, role, e -> new MRole(Env.getCtx(), e));
if (s_log.isLoggable(Level.INFO)) s_log.info(role.toString());
}
return role;
} // get
/**
* Get Role (cached).
* Did not set user - so no access loaded
* @param ctx context
* @param AD_Role_ID role
* @return role
*/
public static MRole get (Properties ctx, int AD_Role_ID)
{
return get(ctx, AD_Role_ID, Env.getAD_User_ID(ctx), false); // metas-2009_0021_AP1_G94 - we need to use this method because we need to load/reload all accesses
} // get
/**
* Get Roles Of Client
* @param ctx context
* @return roles of client
*/
public static MRole[] getOfClient (Properties ctx)
{
return getOfClient(ctx, (String)null);
}
/**
* Get Roles Of Client
* @param ctx context
* @param trxName
* @return roles of client
*/
public static MRole[] getOfClient (Properties ctx, String trxName)
{
String sql = "SELECT * FROM AD_Role WHERE AD_Client_ID=?";
ArrayList<MRole> list = new ArrayList<MRole> ();
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, trxName);
pstmt.setInt (1, Env.getAD_Client_ID(ctx));
rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MRole(ctx, rs, trxName));
}
catch (Exception e)
{
s_log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
MRole[] retValue = new MRole[list.size ()];
list.toArray (retValue);
return retValue;
} // getOfClient
/**
* Get Roles With where clause
* @param ctx context
* @param whereClause where clause
* @return roles of client
*/
public static MRole[] getOf (Properties ctx, String whereClause)
{
String sql = "SELECT * FROM AD_Role";
if (whereClause != null && whereClause.length() > 0)
sql += " WHERE " + whereClause;
ArrayList<MRole> list = new ArrayList<MRole> ();
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, null);
rs = pstmt.executeQuery ();
while (rs.next ())
list.add (new MRole(ctx, rs, null));
}
catch (Exception e)
{
s_log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
MRole[] retValue = new MRole[list.size ()];
list.toArray (retValue);
return retValue;
} // getOf
/** Role/User Cache */
private static POCopyCache<String,MRole> s_roles = new POCopyCache<String,MRole>(Table_Name, 5);
/** Log */
private static CLogger s_log = CLogger.getCLogger(MRole.class);
/** Access SQL Read Write */
public static final boolean SQL_RW = true;
/** Access SQL Read Only */
public static final boolean SQL_RO = false;
/** Access SQL Fully Qualified */
public static final boolean SQL_FULLYQUALIFIED = true;
/** Access SQL Not Fully Qualified */
public static final boolean SQL_NOTQUALIFIED = false;
/** The AD_User_ID of the SuperUser */
public static final int SUPERUSER_USER_ID = USER_SUPERUSER;
/** The AD_User_ID of the System Administrator */
public static final int SYSTEM_USER_ID = USER_SYSTEM;
private static final String ROLE_KEY = "org.compiere.model.DefaultRole";
/**
* UUID based Constructor
* @param ctx Context
* @param AD_Role_UU UUID key
* @param trxName Transaction
*/
public MRole(Properties ctx, String AD_Role_UU, String trxName) {
super(ctx, AD_Role_UU, trxName);
if (Util.isEmpty(AD_Role_UU))
setInitialDefaults();
}
/**************************************************************************
* Standard Constructor
* NOTE - This method must not be used when the role is being requested to manage permissions,
* in such case is necessary to use one of the get methods setting the userID
* @param ctx context
* @param AD_Role_ID id
* @param trxName transaction
*/
public MRole (Properties ctx, int AD_Role_ID, String trxName)
{
super (ctx, AD_Role_ID, trxName);
// ID=0 == System Administrator
if (AD_Role_ID == SystemIDs.ROLE_SYSTEM)
setInitialDefaults();
} // MRole
/**
* Set the initial defaults for a new record
*/
private void setInitialDefaults() {
setIsCanExport (true);
setIsCanReport (true);
setIsManual (false);
setIsPersonalAccess (false);
setIsPersonalLock (false);
setIsShowAcct (false);
setIsAccessAllOrgs(false);
setUserLevel (USERLEVEL_Organization);
setPreferenceType(PREFERENCETYPE_Organization);
setIsChangeLog(false);
setOverwritePriceLimit(false);
setIsUseUserOrgAccess(false);
setMaxQueryRecords(0);
setConfirmQueryRecords(0);
}
/**
* Load Constructor
* NOTE - This method must not be used when the role is being requested to manage permissions,
* in such case is necessary to use one of the get methods setting the userID
* @param ctx context
* @param rs result set
* @param trxName transaction
*/
public MRole(Properties ctx, ResultSet rs, String trxName)
{
super(ctx, rs, trxName);
} // MRole
/**
*
* @param copy
*/
public MRole(MRole copy)
{
this(Env.getCtx(), copy);
}
/**
*
* @param ctx
* @param copy
*/
public MRole(Properties ctx, MRole copy)
{
this(ctx, copy, (String) null);
}
/**
*
* @param ctx
* @param copy
* @param trxName
*/
public MRole(Properties ctx, MRole copy, String trxName)
{
this(ctx, 0, trxName);
copyPO(copy);
this.m_AD_User_ID = copy.m_AD_User_ID;
this.m_orgAccess = copy.m_orgAccess != null ? Arrays.copyOf(copy.m_orgAccess, copy.m_orgAccess.length) : null;
this.m_tableAccess = copy.m_tableAccess != null ? Arrays.copyOf(copy.m_tableAccess, copy.m_tableAccess.length): null;
this.m_columnAccess = copy.m_columnAccess != null ? Arrays.copyOf(copy.m_columnAccess, copy.m_columnAccess.length) : null;
this.m_recordAccess = copy.m_recordAccess != null ? Arrays.copyOf(copy.m_recordAccess, copy.m_recordAccess.length) : null;
this.m_recordDependentAccess = copy.m_recordDependentAccess != null ? Arrays.copyOf(copy.m_recordDependentAccess, copy.m_recordDependentAccess.length) : null;
this.m_windowAccess = copy.m_windowAccess != null ? new HashMap<Integer, Boolean>(copy.m_windowAccess) : null;
this.m_processAccess = copy.m_processAccess != null ? new HashMap<Integer, Boolean>(copy.m_processAccess) : null;
this.m_taskAccess = copy.m_taskAccess != null ? new HashMap<Integer, Boolean>(copy.m_taskAccess) : null;
this.m_workflowAccess = copy.m_workflowAccess != null ? new HashMap<Integer, Boolean>(copy.m_workflowAccess) : null;
this.m_formAccess = copy.m_formAccess != null ? new HashMap<Integer, Boolean>(copy.m_formAccess) : null;
this.m_infoAccess = copy.m_infoAccess != null ? new HashMap<Integer, Boolean>(copy.m_infoAccess) : null;
this.m_includedRoles = copy.m_includedRoles != null ? new ArrayList<MRole>(copy.m_includedRoles) : null;
this.m_parent = copy.m_parent != null ? new MRole(ctx, copy.m_parent, trxName) : null;
this.m_includedSeqNo = copy.m_includedSeqNo;
this.m_canAccess_Info_Product = copy.m_canAccess_Info_Product;
}
/**
* Get Confirm Query Records
* @return entered records or 500 (default)
*/
public int getConfirmQueryRecords ()
{
int no = super.getConfirmQueryRecords ();
if (no == 0)
return 500;
return no;
} // getConfirmQueryRecords
/**
* Require Query
* @param noRecords records
* @return true if query required
*/
public boolean isQueryRequire (int noRecords)
{
if (noRecords < 2)
return false;
int max = getMaxQueryRecords();
if (max > 0 && noRecords > max)
return true;
int qu = getConfirmQueryRecords();
return (noRecords > qu);
} // isQueryRequire
/**
* Over max Query
* @param noRecords records
* @return true if over max query
*/
public boolean isQueryMax (int noRecords)
{
int max = getMaxQueryRecords();
return max > 0 && noRecords > max;
} // isQueryMax
/**
* Before Save
* @param newRecord new
* @return true if it can be saved
*/
protected boolean beforeSave(boolean newRecord)
{
if (getAD_Client_ID() == 0)
setUserLevel(USERLEVEL_System);
else if (getUserLevel().equals(USERLEVEL_System))
{
log.saveError("AccessTableNoUpdate", Msg.getElement(getCtx(), "UserLevel"));
return false;
}
return true;
} // beforeSave
/**
* After Save
* @param newRecord new
* @param success success
* @return success
*/
protected boolean afterSave (boolean newRecord, boolean success)
{
if (!success)
return success;
if (newRecord && success)
{
// Add Role to SuperUser
MUserRoles su = new MUserRoles(getCtx(), SUPERUSER_USER_ID, getAD_Role_ID(), get_TrxName());
su.saveEx();
// Add Role to User
if (getCreatedBy() != SUPERUSER_USER_ID && MSysConfig.getBooleanValue(MSysConfig.AUTO_ASSIGN_ROLE_TO_CREATOR_USER, false, getAD_Client_ID()))
{
MUserRoles ur = new MUserRoles(getCtx(), getCreatedBy(), getAD_Role_ID(), get_TrxName());
ur.saveEx();
}
updateAccessRecords();
}
//
else if (is_ValueChanged("UserLevel"))
updateAccessRecords();
// Default Role changed
if (getDefaultRole() != null
&& getDefaultRole().get_ID() == get_ID())
setDefaultRole(this);
return success;
} // afterSave
/**
* Executed after Delete operation.
* @param success true if record deleted
* @return true if delete is a success
*/
protected boolean afterDelete (boolean success)
{
if(success) {
deleteAccessRecords();
}
return success;
} // afterDelete
/**
* Create Access Records
* @return info
*/
public String updateAccessRecords ()
{
return updateAccessRecords(true);
}
/**
* Create Access Records
* @param reset true will reset existing access
* @return info
*/
public String updateAccessRecords (boolean reset)
{
if (isManual())
return "-";
String roleClientOrgUser = getAD_Role_ID() + ","
+ getAD_Client_ID() + "," + getAD_Org_ID() + ",'Y', getDate(),"
+ getUpdatedBy() + ", getDate()," + getUpdatedBy()
+ ",'Y' "; // IsReadWrite
String sqlWindow = "INSERT INTO AD_Window_Access "
+ "(AD_Window_ID, AD_Role_ID,"
+ " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsReadWrite) "
+ "SELECT DISTINCT w.AD_Window_ID, " + roleClientOrgUser
+ "FROM AD_Window w"
+ " INNER JOIN AD_Tab t ON (w.AD_Window_ID=t.AD_Window_ID)"
+ " INNER JOIN AD_Table tt ON (t.AD_Table_ID=tt.AD_Table_ID) "
+ " LEFT JOIN AD_Window_Access wa ON "
+ "(wa.AD_Role_ID=" + getAD_Role_ID()
+ " AND w.AD_Window_ID = wa.AD_Window_ID) "
+ "WHERE w.IsActive = 'Y' AND wa.AD_Window_ID IS NULL AND t.SeqNo=(SELECT MIN(SeqNo) FROM AD_Tab xt " // only check first tab
+ "WHERE xt.AD_Window_ID=w.AD_Window_ID) "
+ "AND tt.AccessLevel IN ";
String sqlProcess = "INSERT INTO AD_Process_Access "
+ "(AD_Process_ID, AD_Role_ID,"
+ " AD_Client_ID, AD_Org_ID, IsActive, Created, CreatedBy, Updated, UpdatedBy, IsReadWrite) "
+ "SELECT DISTINCT p.AD_Process_ID, " + roleClientOrgUser
+ "FROM AD_Process p LEFT JOIN AD_Process_Access pa ON "
+ "(pa.AD_Role_ID=" + getAD_Role_ID()
+ " AND p.AD_Process_ID = pa.AD_Process_ID) "
+ "WHERE p.IsActive = 'Y' AND pa.AD_Process_ID IS NULL AND AccessLevel IN ";
String sqlForm = "INSERT INTO AD_Form_Access "
+ "(AD_Form_ID, AD_Role_ID,"
+ " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsReadWrite) "
+ "SELECT f.AD_Form_ID, " + roleClientOrgUser
+ "FROM AD_Form f LEFT JOIN AD_Form_Access fa ON "
+ "(fa.AD_Role_ID=" + getAD_Role_ID()
+ " AND f.AD_Form_ID = fa.AD_Form_ID) "
+ "WHERE f.IsActive = 'Y' AND fa.AD_Form_ID IS NULL AND AccessLevel IN ";
String sqlWorkflow = "INSERT INTO AD_WorkFlow_Access "
+ "(AD_WorkFlow_ID, AD_Role_ID,"
+ " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,IsReadWrite) "
+ "SELECT w.AD_WorkFlow_ID, " + roleClientOrgUser
+ "FROM AD_WorkFlow w LEFT JOIN AD_WorkFlow_Access wa ON "
+ "(wa.AD_Role_ID=" + getAD_Role_ID()
+ " AND w.AD_WorkFlow_ID = wa.AD_WorkFlow_ID) "
+ "WHERE w.IsActive = 'Y' AND w.AD_Client_ID IN (0," + getAD_Client_ID() + ") AND wa.AD_WorkFlow_ID IS NULL AND AccessLevel IN ";
String sqlDocAction = "INSERT INTO AD_Document_Action_Access "
+ "(AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy,"
+ "C_DocType_ID , AD_Ref_List_ID, AD_Role_ID) "
+ "(SELECT "
+ getAD_Client_ID() + ",0,'Y', getDate(),"
+ getUpdatedBy() + ", getDate()," + getUpdatedBy()
+ ", doctype.C_DocType_ID, action.AD_Ref_List_ID, rol.AD_Role_ID "
+ "FROM AD_Client client "
+ "INNER JOIN C_DocType doctype ON (doctype.AD_Client_ID=client.AD_Client_ID) "
+ "INNER JOIN AD_Ref_List action ON (action.AD_Reference_ID=135) "
+ "INNER JOIN AD_Role rol ON (rol.AD_Client_ID=client.AD_Client_ID "
+ "AND rol.AD_Role_ID=" + getAD_Role_ID()
+ ") LEFT JOIN AD_Document_Action_Access da ON "
+ "(da.AD_Role_ID=" + getAD_Role_ID()
+ " AND da.C_DocType_ID=doctype.C_DocType_ID AND da.AD_Ref_List_ID=action.AD_Ref_List_ID) "
+ "WHERE doctype.IsActive = 'Y' AND (da.C_DocType_ID IS NULL AND da.AD_Ref_List_ID IS NULL)) ";
String sqlInfo = "INSERT INTO AD_InfoWindow_Access "
+ "(AD_InfoWindow_ID, AD_Role_ID,"
+ " AD_Client_ID,AD_Org_ID,IsActive,Created,CreatedBy,Updated,UpdatedBy) "
+ "SELECT i.AD_InfoWindow_ID," + getAD_Role_ID() + ","
+ getAD_Client_ID() + "," + getAD_Org_ID() + ",'Y',getDate(),"
+ getUpdatedBy() + ", getDate()," + getUpdatedBy()
+ " FROM AD_InfoWindow i LEFT JOIN AD_InfoWindow_Access ia ON "
+ "(ia.AD_Role_ID=" + getAD_Role_ID()
+ " AND i.AD_InfoWindow_ID = ia.AD_InfoWindow_ID) "
+ " INNER JOIN AD_Table tt ON (i.AD_Table_ID=tt.AD_Table_ID) "
+ "WHERE i.IsActive = 'Y' AND i.AD_Client_ID IN (0," + getAD_Client_ID() + ") AND ia.AD_InfoWindow_ID IS NULL"
+ " AND tt.AccessLevel IN ";
/**
* Fill AD_xx_Access
* ---------------------------------------------------------------------------
* SCO# Levels S__ 100 4 System info
* SCO 111 7 System shared info
* SC_ 110 6 System/Client info
* _CO 011 3 Client shared info
* _C_ 011 2 Client
* __O 001 1 Organization info
* Roles:
* S 4,7,6
* _CO 7,6,3,2,1
* __O 3,1,7
*/
String roleAccessLevel = null;
String roleAccessLevelWin = null;
if (USERLEVEL_System.equals(getUserLevel()))
roleAccessLevel = "('4','7','6')";
else if (USERLEVEL_Client.equals(getUserLevel()))
roleAccessLevel = "('7','6','3','2')";
else if (USERLEVEL_ClientPlusOrganization.equals(getUserLevel()))
roleAccessLevel = "('7','6','3','2','1')";
else // if (USERLEVEL_Organization.equals(getUserLevel()))
{
roleAccessLevel = "('3','1','7')";
roleAccessLevelWin = roleAccessLevel
+ " AND w.Name NOT LIKE '%(all)%'";
}
if (roleAccessLevelWin == null)
roleAccessLevelWin = roleAccessLevel;
if (reset)
deleteAccessRecords();
int win = DB.executeUpdateEx(sqlWindow + roleAccessLevelWin, get_TrxName());
int proc = DB.executeUpdateEx(sqlProcess + roleAccessLevel, get_TrxName());
int form = DB.executeUpdateEx(sqlForm + roleAccessLevel, get_TrxName());
int wf = DB.executeUpdateEx(sqlWorkflow + roleAccessLevel, get_TrxName());
int docact = DB.executeUpdateEx(sqlDocAction, get_TrxName());
int info = DB.executeUpdateEx(sqlInfo + roleAccessLevel, get_TrxName());
if (DB.isGenerateUUIDSupported()) {
DB.executeUpdateEx("UPDATE AD_Window_Access SET AD_Window_Access_UU=generate_uuid() WHERE AD_Window_Access_UU IS NULL", get_TrxName());
DB.executeUpdateEx("UPDATE AD_Process_Access SET AD_Process_Access_UU=generate_uuid() WHERE AD_Process_Access_UU IS NULL", get_TrxName());
DB.executeUpdateEx("UPDATE AD_Form_Access SET AD_Form_Access_UU=generate_uuid() WHERE AD_Form_Access_UU IS NULL", get_TrxName());
DB.executeUpdateEx("UPDATE AD_Workflow_Access SET AD_Workflow_Access_UU=generate_uuid() WHERE AD_Workflow_Access_UU IS NULL", get_TrxName());
DB.executeUpdateEx("UPDATE AD_Document_Action_Access SET AD_Document_Action_Access_UU=generate_uuid() WHERE AD_Document_Action_Access_UU IS NULL", get_TrxName());
DB.executeUpdateEx("UPDATE AD_InfoWindow_Access SET AD_InfoWindow_Access_UU=generate_uuid() WHERE AD_InfoWindow_Access_UU IS NULL", get_TrxName());
} else {
UUIDGenerator.updateUUID(MColumn.get(getCtx(), MWindowAccess.Table_Name, PO.getUUIDColumnName(MWindowAccess.Table_Name)), get_TrxName());
UUIDGenerator.updateUUID(MColumn.get(getCtx(), MProcessAccess.Table_Name, PO.getUUIDColumnName(MProcessAccess.Table_Name)), get_TrxName());
UUIDGenerator.updateUUID(MColumn.get(getCtx(), MFormAccess.Table_Name, PO.getUUIDColumnName(MFormAccess.Table_Name)), get_TrxName());
UUIDGenerator.updateUUID(MColumn.get(getCtx(), MWorkflowAccess.Table_Name, PO.getUUIDColumnName(MWorkflowAccess.Table_Name)), get_TrxName());
UUIDGenerator.updateUUID(MColumn.get(getCtx(), MDocumentActionAccess.Table_Name, PO.getUUIDColumnName(MDocumentActionAccess.Table_Name)), get_TrxName());
UUIDGenerator.updateUUID(MColumn.get(getCtx(), MInfoWindowAccess.Table_Name, PO.getUUIDColumnName(MInfoWindowAccess.Table_Name)), get_TrxName());
}
loadAccess(true);
return Msg.parseTranslation(getCtx(), "@AD_Window_ID@ #" + win
+ " - @AD_Process_ID@ #" + proc
+ " - @AD_Form_ID@ #" + form
+ " - @AD_Workflow_ID@ #" + wf
+ " - @DocAction@ #" + docact
+ " - @AD_InfoWindow_ID@ #" + info);
} // createAccessRecords
/**
* Delete Access Records of the role after the role was (successfully) deleted.
*/
private void deleteAccessRecords() {
String whereDel = " WHERE AD_Role_ID=" + getAD_Role_ID();
//
int winDel = DB.executeUpdateEx("DELETE FROM AD_Window_Access" + whereDel, get_TrxName());
int procDel = DB.executeUpdateEx("DELETE FROM AD_Process_Access" + whereDel, get_TrxName());
int formDel = DB.executeUpdateEx("DELETE FROM AD_Form_Access" + whereDel, get_TrxName());
int wfDel = DB.executeUpdateEx("DELETE FROM AD_WorkFlow_Access" + whereDel, get_TrxName());
int docactDel = DB.executeUpdateEx("DELETE FROM AD_Document_Action_Access" + whereDel, get_TrxName());
int infoDel = DB.executeUpdateEx("DELETE FROM AD_InfoWindow_Access" + whereDel, get_TrxName());
if (log.isLoggable(Level.FINE)) log.fine("AD_Window_Access=" + winDel
+ ", AD_Process_Access=" + procDel
+ ", AD_Form_Access=" + formDel
+ ", AD_Workflow_Access=" + wfDel
+ ", AD_Document_Action_Access=" + docactDel
+ ", AD_InfoWindow_Access=" + infoDel);
}
/**
* String Representation
* @return info
*/
public String toString()
{
StringBuilder sb = new StringBuilder("MRole[");
sb.append(getAD_Role_ID()).append(",").append(getName())
.append(",UserLevel=").append(getUserLevel())
.append(",").append(getClientWhere(false))
.append(",").append(getOrgWhere(false))
.append("]");
return sb.toString();
} // toString
/**
* Extended String Representation
* @param ctx Properties
* @return extended info
*/
public String toStringX (Properties ctx)
{
StringBuilder sb = new StringBuilder();
sb.append(Msg.translate(ctx, "AD_Role_ID")).append("=").append(getName())
.append(" - ").append(Msg.translate(ctx, "IsCanExport")).append("=").append(Msg.translate(ctx,String.valueOf(isCanExport())))
.append(" - ").append(Msg.translate(ctx, "IsCanReport")).append("=").append(Msg.translate(ctx,String.valueOf(isCanReport())))
.append(Env.NL).append(Env.NL);
//
for (int i = 0; i < m_orgAccess.length; i++)
sb.append(m_orgAccess[i].toString()).append(Env.NL);
sb.append(Env.NL);
//
loadTableAccess(false);
for (int i = 0; i < m_tableAccess.length; i++)
sb.append(m_tableAccess[i].toStringX(ctx)).append(Env.NL);
if (m_tableAccess.length > 0)
sb.append(Env.NL);
//
loadColumnAccess(false);
for (int i = 0; i < m_columnAccess.length; i++)
sb.append(m_columnAccess[i].toStringX(ctx)).append(Env.NL);
if (m_columnAccess.length > 0)
sb.append(Env.NL);
//
loadRecordAccess(false);
for (int i = 0; i < m_recordAccess.length; i++)
sb.append(m_recordAccess[i].toStringX(ctx)).append(Env.NL);
return sb.toString();
} // toStringX
/*************************************************************************
* Access Management
************************************************************************/
/** User */
private int m_AD_User_ID = -1;
/** Positive List of Organizational Access */
private OrgAccess[] m_orgAccess = null;
/** List of Table Access */
private MTableAccess[] m_tableAccess = null;
/** List of Column Access */
private MColumnAccess[] m_columnAccess = null;
/** List of Record Access */
private MRecordAccess[] m_recordAccess = null;
/** List of Dependent Record Access */
private MRecordAccess[] m_recordDependentAccess = null;
/** Window Access */
private HashMap<Integer,Boolean> m_windowAccess = null;
/** Process Access */
private HashMap<Integer,Boolean> m_processAccess = null;
/** Task Access */
private HashMap<Integer,Boolean> m_taskAccess = null;
/** Workflow Access */
private HashMap<Integer,Boolean> m_workflowAccess = null;
/** Form Access */
private HashMap<Integer,Boolean> m_formAccess = null;
/** Info Windows */
private HashMap<Integer, Boolean> m_infoAccess;
/**
* Set Logged in user
* @param AD_User_ID user requesting info
*/
public void setAD_User_ID(int AD_User_ID)
{
m_AD_User_ID = AD_User_ID;
} // setAD_User_ID
/**
* Get Logged in user
* @return AD_User_ID user requesting info
*/
public int getAD_User_ID()
{
return m_AD_User_ID;
} // getAD_User_ID
/**************************************************************************
* Load Access Info
* @param reload re-load from disk
*/
public void loadAccess (boolean reload)
{
loadOrgAccess(reload);
loadTableAccess(reload);
loadColumnAccess(reload);
loadRecordAccess(reload);
if (reload)
{
m_windowAccess = null;
m_processAccess = null;
m_taskAccess = null;
m_workflowAccess = null;
m_formAccess = null;
}
loadIncludedRoles(reload); // Load/Reload included roles - metas-2009_0021_AP1_G94
} // loadAccess
/**
* Load Org Access
* @param reload reload
*/
private void loadOrgAccess (boolean reload)
{
if (!(reload || m_orgAccess == null))
return;
//
ArrayList<OrgAccess> list = new ArrayList<OrgAccess>();
if (isUseUserOrgAccess())
loadOrgAccessUser(list);
else
loadOrgAccessRole(list);
m_orgAccess = new OrgAccess[list.size()];
list.toArray(m_orgAccess);
if (log.isLoggable(Level.FINE)) log.fine("#" + m_orgAccess.length + (reload ? " - reload" : ""));
if (Ini.isClient())
{
StringBuilder sb = new StringBuilder();
for (int i = 0; i < m_orgAccess.length; i++)
{
if (i > 0)
sb.append(",");
sb.append(m_orgAccess[i].AD_Org_ID);
}
Env.setContext(Env.getCtx(), "#User_Org", sb.toString());
}
} // loadOrgAccess
/**
* Load Org Access User
* @param list list
*/
private void loadOrgAccessUser(ArrayList<OrgAccess> list)
{
if (getAD_User_ID() == -1) {
log.info("Trying to load Org Access from User but user has not been set");
}
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM AD_User_OrgAccess "
+ "WHERE AD_User_ID=? AND IsActive='Y' AND AD_Client_ID = ?";
try
{
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, getAD_User_ID());
pstmt.setInt(2, Env.getAD_Client_ID(getCtx()));
rs = pstmt.executeQuery();
while (rs.next())
{
MUserOrgAccess oa = new MUserOrgAccess(getCtx(), rs, get_TrxName());
loadOrgAccessAdd (list, new OrgAccess(oa.getAD_Client_ID(), oa.getAD_Org_ID(), oa.isReadOnly()));
}
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
}
} // loadOrgAccessRole
/**
* Load Org Access Role
* @param list list
*/
private void loadOrgAccessRole(ArrayList<OrgAccess> list)
{
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM AD_Role_OrgAccess "
+ "WHERE AD_Role_ID=? AND IsActive='Y' AND AD_Client_ID = ?";
try
{
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, getAD_Role_ID());
pstmt.setInt(2, Env.getAD_Client_ID(getCtx()));
rs = pstmt.executeQuery();
while (rs.next())
{
MRoleOrgAccess oa = new MRoleOrgAccess(getCtx(), rs, get_TrxName());
loadOrgAccessAdd (list, new OrgAccess(oa.getAD_Client_ID(), oa.getAD_Org_ID(), oa.isReadOnly()));
}
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
}
} // loadOrgAccessRole
/**
* Load Org Access Add Tree to List
* @param list list
* @param oa org access
* @see org.compiere.util.Login
*/
private void loadOrgAccessAdd (ArrayList<OrgAccess> list, OrgAccess oa)
{
if (list.contains(oa))
return;
list.add(oa);
// Do we look for trees?
if (getAD_Tree_Org_ID() == 0)
return;
MOrg org = MOrg.get(oa.AD_Org_ID);
if (!org.isSummary())
return;
// Summary Org - Get Dependents
MTree_Base tree = MTree_Base.get(getCtx(), getAD_Tree_Org_ID(), get_TrxName());
String sql = "SELECT AD_Client_ID, AD_Org_ID FROM AD_Org "
+ "WHERE IsActive='Y' AND AD_Org_ID IN (SELECT Node_ID FROM "
+ tree.getNodeTableName()
+ " WHERE AD_Tree_ID=? AND Parent_ID=? AND IsActive='Y')";
PreparedStatement pstmt = null;
ResultSet rs = null;
try
{
pstmt = DB.prepareStatement (sql, get_TrxName());
pstmt.setInt (1, tree.getAD_Tree_ID());
pstmt.setInt(2, org.getAD_Org_ID());
rs = pstmt.executeQuery ();
while (rs.next ())
{
int AD_Client_ID = rs.getInt(1);
int AD_Org_ID = rs.getInt(2);
loadOrgAccessAdd (list, new OrgAccess(AD_Client_ID, AD_Org_ID, oa.readOnly));
}
}
catch (Exception e)
{
log.log (Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
rs = null; pstmt = null;
}
} // loadOrgAccessAdd
/**
* Load Table Access
* @param reload reload
*/
private void loadTableAccess(boolean reload)
{
if (m_tableAccess != null && !reload)
return;
ArrayList<MTableAccess> list = new ArrayList<MTableAccess>();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM AD_Table_Access "
+ "WHERE AD_Role_ID=? AND IsActive='Y'";
try
{
pstmt = DB.prepareStatement(sql, get_TrxName());
pstmt.setInt(1, getAD_Role_ID());
rs = pstmt.executeQuery();
while (rs.next())
list.add(new MTableAccess(getCtx(), rs, get_TrxName()));
}
catch (Exception e)
{
log.log(Level.SEVERE, sql, e);
}
finally
{
DB.close(rs, pstmt);
}
m_tableAccess = new MTableAccess[list.size()];
list.toArray(m_tableAccess);
if (log.isLoggable(Level.FINE)) log.fine("#" + m_tableAccess.length);
} // loadTableAccess
/**
* Check if tableName is a view
* @param tableName
* @return boolean
*/
private boolean isView(String tableName)
{
MTable table = MTable.get(getCtx(), tableName);
if (table == null)
return false;
return MTable.get(getCtx(), tableName).isView();
}
private String getIdColumnName(String tableName)
{
StringBuilder colkey = new StringBuilder(tableName).append("_ID");
MTable table = MTable.get(getCtx(), tableName);
if (table == null)
return null;
if (MTable.get(getCtx(), tableName).columnExists(colkey.toString()))
return colkey.toString();
return null;
}
/**
* Load Column Access
* @param reload reload
*/
private void loadColumnAccess(boolean reload)
{
if (m_columnAccess != null && !reload)
return;
ArrayList<MColumnAccess> list = new ArrayList<MColumnAccess>();
PreparedStatement pstmt = null;
ResultSet rs = null;
String sql = "SELECT * FROM AD_Column_Access "
+ "WHERE AD_Role_ID=? AND IsActive='Y'";
try
{
pstmt = DB.prepareStatement(sql, get_TrxName());