-
Notifications
You must be signed in to change notification settings - Fork 7
/
Container.java
1728 lines (1465 loc) · 55.1 KB
/
Container.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
/*
* Copyright (c) 2004-2018 Fred Hutchinson Cancer Research Center
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.labkey.api.data;
import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonProperty;
import org.apache.commons.lang3.StringUtils;
import org.apache.logging.log4j.Logger;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import org.json.JSONArray;
import org.json.JSONObject;
import org.labkey.api.Constants;
import org.labkey.api.action.SpringActionController;
import org.labkey.api.admin.FolderExportContext;
import org.labkey.api.cache.BlockingCache;
import org.labkey.api.cache.CacheManager;
import org.labkey.api.collections.Sets;
import org.labkey.api.data.PropertyManager.PropertyMap;
import org.labkey.api.module.FolderType;
import org.labkey.api.module.FolderTypeManager;
import org.labkey.api.module.Module;
import org.labkey.api.module.ModuleLoader;
import org.labkey.api.pipeline.PipeRoot;
import org.labkey.api.pipeline.PipelineService;
import org.labkey.api.portal.ProjectUrls;
import org.labkey.api.reports.Report;
import org.labkey.api.reports.ReportService;
import org.labkey.api.security.HasPermission;
import org.labkey.api.security.SecurableResource;
import org.labkey.api.security.SecurityManager;
import org.labkey.api.security.SecurityPolicy;
import org.labkey.api.security.SecurityPolicyManager;
import org.labkey.api.security.User;
import org.labkey.api.security.UserPrincipal;
import org.labkey.api.security.permissions.AdminPermission;
import org.labkey.api.security.permissions.EnableRestrictedModules;
import org.labkey.api.security.permissions.Permission;
import org.labkey.api.security.permissions.ReadPermission;
import org.labkey.api.security.roles.Role;
import org.labkey.api.settings.AppProps;
import org.labkey.api.settings.LookAndFeelProperties;
import org.labkey.api.study.StudyService;
import org.labkey.api.util.ContainerContext;
import org.labkey.api.util.DateUtil;
import org.labkey.api.util.FileUtil;
import org.labkey.api.util.GUID;
import org.labkey.api.util.NetworkDrive;
import org.labkey.api.util.PageFlowUtil;
import org.labkey.api.util.Path;
import org.labkey.api.util.logging.LogHelper;
import org.labkey.api.view.ActionURL;
import org.labkey.api.view.FolderTab;
import org.labkey.api.view.ForbiddenProjectException;
import org.labkey.api.view.Portal;
import org.labkey.api.view.WebPartFactory;
import org.labkey.api.webdav.WebdavResource;
import org.labkey.api.webdav.WebdavService;
import org.springframework.validation.BindException;
import java.beans.PropertyChangeEvent;
import java.io.File;
import java.io.Serializable;
import java.lang.ref.WeakReference;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.Collections;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.function.BooleanSupplier;
/**
* Basic hierarchical structure for holding data within LabKey Server. Security is configured at the container level.
* Projects, folders, and workbooks are all different types of containers.
*
* {@link ContainerManager} for more info
*
* CONSIDER: extend {@link org.labkey.api.data.Entity}
*/
public class Container implements Serializable, Comparable<Container>, SecurableResource, ContainerContext, HasPermission, Parameter.JdbcParameterValue
{
private static final Logger LOG = LogHelper.getLogger(Container.class, "Information about projects and folders");
private GUID _id;
private Path _path;
private Date _created;
private int _createdBy;
private int _rowId; //Unique for this installation
/**
* Used to arbitrarily reorder siblings within a container.
*/
private int _sortOrder;
private String _description;
private transient Module _defaultModule;
private transient WeakReference<Container> _parent;
private ContainerType _containerType;
// include in results from searches outside this container?
private final boolean _searchable;
//optional non-unique title for the container
private String _title;
private LockState _lockState = null;
private LocalDate _expirationDate = null;
private final static BlockingCache<GUID, Set<Module>> REQUIRED_MODULES_CACHE = new BlockingCache<>(
CacheManager.getCache(
Constants.getMaxContainers(),
CacheManager.DAY,
"Required modules per containers"),
(key, argument) -> {
if (!(argument instanceof Container c))
{
throw new IllegalStateException("Expected usage pattern is to include the container instance as the argument. Key: " + key);
}
Set<Module> requiredModules = new HashSet<>(c.getRequiredModulesForFolderType(c.getFolderType()));
requiredModules.add(ModuleLoader.getInstance().getModule("API"));
requiredModules.add(ModuleLoader.getInstance().getModule("Internal"));
for (Container child: c.getChildren())
{
if (child.isWorkbook())
{
requiredModules.addAll(c.getRequiredModulesForFolderType(child.getFolderType()));
}
}
return Collections.unmodifiableSet(requiredModules);
});
static
{
// Clear the required modules cache on any change to the container tree or container properties
ContainerManager.addContainerListener(new ContainerManager.ContainerListener()
{
@Override
public void containerCreated(Container c, User user)
{
REQUIRED_MODULES_CACHE.clear();
}
@Override
public void containerDeleted(Container c, User user)
{
REQUIRED_MODULES_CACHE.clear();
}
@Override
public void containerMoved(Container c, Container oldParent, User user)
{
REQUIRED_MODULES_CACHE.clear();
}
@Override
public @NotNull Collection<String> canMove(Container c, Container newParent, User user)
{
return Collections.emptyList();
}
@Override
public void propertyChange(PropertyChangeEvent evt)
{
REQUIRED_MODULES_CACHE.clear();
}
});
}
// Might add others in the future (e.g., ReadOnly)
public enum LockState
{
Unlocked("unlocked", false, false),
Inaccessible("locked, making it inaccessible to everyone except administrators", true, false),
Excluded("excluded from project locking and review", false, true);
private final String _description;
private final boolean _locked;
private final boolean _excluded;
LockState(String description, boolean locked, boolean excluded)
{
_description = description;
_locked = locked;
_excluded = excluded;
}
public String getDescription()
{
return _description;
}
public boolean isLocked()
{
return _locked;
}
public boolean isExcluded()
{
return _excluded;
}
}
// UNDONE: BeanFactory for Container
@JsonCreator
private Container(@JsonProperty("_searchable") boolean searchable)
{
_searchable = searchable;
}
/* public only for unit testing */
public Container(Container dirParent, String name, String id, int rowId, int sortOrder, Date created, int createdBy, boolean searchable)
{
_path = null == dirParent && StringUtils.isEmpty(name) ? Path.rootPath : ContainerManager.makePath(dirParent, name);
_id = new GUID(id);
_parent = new WeakReference<>(dirParent);
_rowId = rowId;
_sortOrder = sortOrder;
_created = created;
_createdBy = createdBy;
_searchable = searchable;
// default to normal container type until told otherwise
_containerType = ContainerTypeRegistry.get().getType(NormalContainerType.NAME);
}
@Override
public Container getContainer(Map context)
{
return this;
}
// BEGIN Container Type forwarding methods
public boolean canHaveChildren()
{
return _containerType.canHaveChildren();
}
public boolean includeForImportExport(FolderExportContext context)
{
// only include subfolders if requested by user (otherwise just container tabs)
// but don't include the current folder in the case of creating a folder from template
// tab - always
// normal - if context says to
// workbook - never
return _containerType.includeForImportExport(context);
}
public boolean shouldRemoveFromPortal()
{
return _containerType.shouldRemoveFromPortal();
}
public boolean includePropertiesAsChild(boolean includeTabs)
{
// workbook - never
// normal - always
// tab - if flagged
return _containerType.includePropertiesAsChild(includeTabs);
}
public boolean isInFolderNav()
{
return _containerType.isInFolderNav();
}
public boolean isConvertibleToTab()
{
return _containerType.isConvertibleToTab();
}
/**
* This behavior is related to Issue 15301. In general, we expect API actions to target the same container as that request; however,
* on insert/update/delete a row can sometimes specify a different container ID. In this situation, allow the containerType of the container
* the row is attempting to use to determine whether this CRUD action is allowed.
* @param targetContainer The targetContainer, typically supplied by a row
*/
public boolean allowRowMutationForContainer(Container targetContainer)
{
return targetContainer != null && targetContainer.getContainerType().allowRowMutationFromContainer(this, targetContainer);
}
public Class<? extends Permission> getPermissionNeededToDelete()
{
return _containerType.getPermissionNeededToDelete();
}
public boolean isDuplicatedInContainerFilter()
{
// To reduce the number of ids that need to be passed around, filter out workbooks. They'll get included
// automatically because we always add them via the SQL that we generate
return _containerType.isDuplicatedInContainerFilter();
}
public String getTitleFor(ContainerType.TitleContext context)
{
return _containerType.getTitleFor(context, this);
}
public boolean isContainerFor(ContainerType.DataType dataType)
{
if (dataType == ContainerType.DataType.assayData)
{
// the folder type may indicate that assays should be uploaded to workbooks
return !getFolderType().getForceAssayUploadIntoWorkbooks() || isWorkbook();
}
return this.getContainerFor(dataType) == this;
}
public Container getContainerFor(ContainerType.DataType dataType)
{
return _containerType.getContainerFor(dataType, this);
}
public Set<Container> getContainersFor(ContainerType.DataType dataType)
{
return _containerType.getContainersFor(dataType, this);
}
// END Container Type forwarding methods
@NotNull
public String getName()
{
return _path.getName();
}
@Override
@NotNull
public String getResourceName()
{
return isRoot() ? "/" : _path.getName();
}
public Date getCreated()
{
return _created;
}
public int getCreatedBy()
{
return _createdBy;
}
public boolean isInheritedAcl()
{
return !(getPolicy().getResourceId().equals(getId()));
}
/**
* @return the parent container, or the root container (with path "/") if called on the root
*/
public Container getParent()
{
Container parent = _parent == null ? null : _parent.get();
if (null == parent && _path.size() > 0)
{
parent = ContainerManager.getForPath(_path.getParent());
_parent = new WeakReference<>(parent);
}
return parent;
}
/**
* @return the unencoded container path ignoring "/"
*/
public String getPath()
{
return _path.toString("/", "");
}
public Path getParsedPath()
{
return _path;
}
/**
* returned string begins and ends with "/" so you can slap it between
* getContextPath() and action.view
*/
public String getEncodedPath()
{
return _path.encode("/", "/");
}
public String getId()
{
return _id.toString();
}
public GUID getEntityId()
{
return _id;
}
public int getRowId()
{
return _rowId;
}
public void setRowId(int rowId)
{
_rowId = rowId;
}
public int getSortOrder()
{
return _sortOrder;
}
public void setSortOrder(int sortOrder)
{
_sortOrder = sortOrder;
}
public String getDescription()
{
return _description;
}
public void setDescription(String description)
{
_description = description;
}
/**
* Get the project Container or null if isRoot().
*
* @return The project Container or null if isRoot().
*/
public @Nullable Container getProject()
{
// Root has no project
if (isRoot())
return null;
Container project = this;
while (!project.isProject())
{
project = project.getParent();
if (null == project) // deleted container?
return null;
}
return project;
}
// Note: don't use the security policy directly unless you really have to... call hasPermission() or hasOneOf()
// instead, to ensure proper behavior during impersonation.
public @NotNull SecurityPolicy getPolicy()
{
return SecurityPolicyManager.getPolicy(this);
}
public boolean hasPermission(String logMsg, @NotNull UserPrincipal user, @NotNull Class<? extends Permission> perm)
{
return SecurityManager.hasAllPermissions(logMsg, getPolicy(), user, Set.of(perm), Set.of());
}
@Override
public boolean hasPermission(@NotNull UserPrincipal user, @NotNull Class<? extends Permission> perm)
{
return SecurityManager.hasAllPermissions(null, getPolicy(), user, Set.of(perm), Set.of());
}
public boolean hasPermission(String logMsg, @NotNull UserPrincipal user, @NotNull Class<? extends Permission> perm, @Nullable Set<Role> contextualRoles)
{
return SecurityManager.hasAllPermissions(logMsg, getPolicy(), user, Set.of(perm), contextualRoles);
}
public boolean hasPermission(@NotNull UserPrincipal user, @NotNull Class<? extends Permission> perm, @Nullable Set<Role> contextualRoles)
{
return SecurityManager.hasAllPermissions(null, getPolicy(), user, Set.of(perm), contextualRoles);
}
public boolean hasPermissions(@NotNull UserPrincipal user, @NotNull Set<Class<? extends Permission>> permissions)
{
return SecurityManager.hasAllPermissions(null, getPolicy(), user, permissions, Set.of());
}
public boolean hasPermissions(@NotNull UserPrincipal user, @NotNull Set<Class<? extends Permission>> permissions, @Nullable Set<Role> contextualRoles)
{
return SecurityManager.hasAllPermissions(null, getPolicy(), user, permissions, contextualRoles);
}
public boolean hasOneOf(@NotNull UserPrincipal user, @NotNull Set<Class<? extends Permission>> perms)
{
return SecurityManager.hasAnyPermissions(null, getPolicy(), user, perms, Set.of());
}
@SafeVarargs
public final boolean hasOneOf(@NotNull User user, @NotNull Class<? extends Permission>... perms)
{
return SecurityManager.hasAnyPermissions(null, getPolicy(), user, new HashSet<>(Arrays.asList(perms)), Set.of());
}
public boolean isForbiddenProject(User user)
{
return handleForbiddenProject(user, false);
}
public void throwIfForbiddenProject(User user)
{
handleForbiddenProject(user, true);
}
private boolean handleForbiddenProject(User user, boolean shouldThrow)
{
if (null != user && !user.isSearchUser())
{
@Nullable Container impersonationProject = user.getImpersonationProject();
@Nullable Container currentProject = getProject();
// Root is never forbidden (site admin case), otherwise, impersonation project must match current project
if (null != impersonationProject && !impersonationProject.equals(currentProject))
{
if (shouldThrow)
throw new ForbiddenProjectException("You are not allowed to access this folder while impersonating within a different project.");
return true;
}
// Handle locked projects
if (null != currentProject)
{
LockState lockState = currentProject.getLockState();
if (lockState.isLocked() && ContainerManager.LOCKED_PROJECT_HANDLER.isForbidden(currentProject, user, lockState))
{
if (shouldThrow)
throw new ForbiddenProjectException("You are not allowed to access this folder; it is " + lockState.getDescription() + ".");
return true;
}
}
}
return false;
}
public boolean isProject()
{
return _path.size() == 1;
}
public boolean isRoot()
{
return _path.size() == 0;
}
public boolean shouldDisplay(User user)
{
if (!isInFolderNav())
return false;
String name = _path.getName();
if (name.length() == 0)
return true; // Um, I guess we should display it?
char c = name.charAt(0);
if (c == '_' || c == '.')
{
return user != null && (user.hasRootAdminPermission() || hasPermission(user, AdminPermission.class));
}
else
{
return true;
}
}
/**
* Should use property check in the ContainerType interface instead of this explicit check
* @return indication of whether this is a container tab or not.
*/
public boolean isContainerTab()
{
return _containerType instanceof TabContainerType;
}
/* Use property checks in the ContainerType interface instead of this explicit check */
public boolean isWorkbook()
{
return _containerType instanceof WorkbookContainerType;
}
/* Use property checks in the ContainerType interface instead of this explicit check */
public boolean isWorkbookOrTab()
{
return _containerType instanceof WorkbookContainerType || _containerType instanceof TabContainerType;
}
Map<String, Boolean> typesOfChildren = new HashMap<>();
public synchronized boolean hasChildrenOfType(String type)
{
if (null == typesOfChildren.get(type))
{
typesOfChildren.put(type, false);
for (Container ch : getChildren())
{
if (ch.getContainerType().getName().equalsIgnoreCase(type))
{
typesOfChildren.put(type, true);
break;
}
}
}
return typesOfChildren.get(type);
}
public synchronized boolean hasChildrenOfAnyType(Set<String> types)
{
return types.stream().anyMatch(this::hasChildrenOfType);
}
public boolean isSearchable()
{
return _searchable;
}
/**
* Returns true if possibleAncestor is a parent of this container,
* or a parent-of-a-parent, etc.
*/
public boolean hasAncestor(Container possibleAncestor)
{
if (isRoot() || getParent() == null)
return false;
if (getParent().equals(possibleAncestor))
return true;
return getParent().hasAncestor(possibleAncestor);
}
public Container getChild(String folderName)
{
return ContainerManager.getChild(this,folderName);
}
public boolean hasChild(String folderName)
{
return getChild(folderName) != null;
}
public boolean hasChildren()
{
return getChildren().size() > 0;
}
public List<Container> getChildren()
{
return ContainerManager.getChildren(this);
}
@Override
@NotNull
public List<SecurableResource> getChildResources(User user)
{
//add all sub-containers the user is allowed to read
List<SecurableResource> ret = new ArrayList<>(ContainerManager.getChildren(this, user, ReadPermission.class));
// TODO: Shouldn't each module register a provider to add their securable resources? This knowledge about study,
// reports, and pipeline roots shouldn't be hard-coded in Container.
//add resources from study
StudyService sts = StudyService.get();
if (null != sts)
ret.addAll(sts.getSecurableResources(this, user));
//add report descriptors
//this seems much more cumbersome than it should be
for (Report report : ReportService.get().getReports(user, this))
{
SecurityPolicy policy = SecurityPolicyManager.getPolicy(report.getDescriptor());
if (policy.hasPermission(user, AdminPermission.class))
ret.add(report.getDescriptor());
}
//add pipeline root
PipeRoot root = PipelineService.get().findPipelineRoot(this);
if (null != root)
{
SecurityPolicy policy = SecurityPolicyManager.getPolicy(root);
if (policy.hasPermission(user, AdminPermission.class))
ret.add(root);
}
return ret;
}
/**
* Finds a securable resource within this container or child containers with the same id
* as the given resource id.
* @param resourceId The resource id to find
* @param user The current user (searches only resources that user can see)
* @return The resource or null if not found
*/
@Nullable
public SecurableResource findSecurableResource(String resourceId, User user)
{
if (null == resourceId)
return null;
if (getResourceId().equals(resourceId))
return this;
//recurse down all non-container resources
SecurableResource resource = findSecurableResourceInContainer(resourceId, user, this);
if (null != resource)
return resource;
//recurse down child containers
for(Container child : getChildren())
{
//only look in child containers where the user has read perm
if (child.hasPermission(user, ReadPermission.class))
{
resource = child.findSecurableResource(resourceId, user);
if (null != resource)
return resource;
}
}
return null;
}
protected SecurableResource findSecurableResourceInContainer(String resourceId, User user, SecurableResource parent)
{
for (SecurableResource child : parent.getChildResources(user))
{
if (child instanceof Container)
continue;
if (child.getResourceId().equals(resourceId))
return child;
SecurableResource resource = findSecurableResourceInContainer(resourceId, user, child);
if (null != resource)
return resource;
}
return null;
}
public String toString()
{
return getClass().getName() + "@" + System.identityHashCode(this) + " " + _path + " " + _id;
}
public boolean equals(Object o)
{
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
final Container container = (Container) o;
return _id == null ? container._id == null : _id.equals(container._id);
}
public int hashCode()
{
return _id.hashCode();
}
public static boolean isLegalName(String name, boolean isProject, StringBuilder error)
{
if (StringUtils.isBlank(name))
{
error.append("Blank names are not allowed.");
return false;
}
if (name.length() > 255)
{
error.append("Folder name must be shorter than 255 characters");
return false;
}
if (!FileUtil.isLegalName(name))
{
error.append("Folder name must be a legal filename and not contain one of '/', '\\', ':', '?', '<', '>', '*', '|', '\"', '^'");
return false;
}
if (name.contains(";"))
{
error.append("Semicolons are not allowed in folder names.");
return false;
}
if (name.startsWith("@"))
{
error.append("Folder name may not begin with '@'.");
return false;
}
if (StringUtils.endsWithIgnoreCase(name, ".view") || StringUtils.endsWithIgnoreCase(name, ".api") || StringUtils.endsWithIgnoreCase(name, ".post"))
{
error.append("Folder name should not end with '.view', '.api', or '.post'.");
return false;
}
//Don't allow ISOControl characters as they are not handled well by the databases
for( int i = 0; i < name.length(); ++i)
{
if (Character.isISOControl(name.charAt(i)))
{
error.append("Non-printable characters are not allowed in folder names.");
return false;
}
}
if (isProject && !isLegalProjectName(name))
{
error.append("Project name can't be \"").append(name).append("\".");
return false;
}
return true;
}
// Any names that we register as special servlets won't work as project names
private static final Set<String> ILLEGAL_PROJECT_NAMES = Sets.newCaseInsensitiveHashSet("cas", "_rstudio", "_webdav", "filecontent");
// Check for illegal project names
public static boolean isLegalProjectName(String name)
{
return !ILLEGAL_PROJECT_NAMES.contains(name) &&
!name.matches("__r\\d+"); // Used by permalinks to alias containers by rowId
}
public static boolean isLegalTitle(String name, StringBuilder error)
{
if (StringUtils.isBlank(name))
{
return true; //titles can be blank
}
if (name.length() > 1000)
{
error.append("Title must be shorter than 1000 characters");
return false;
}
//Don't allow ISOControl characters as they are not handled well by the databases
for( int i = 0; i < name.length(); ++i)
{
if (Character.isISOControl(name.charAt(i)))
{
error.append("Non-printable characters are not allowed in titles.");
return false;
}
}
return true;
}
public @NotNull ActionURL getStartURL(User user)
{
FolderType ft = getFolderType();
if (!FolderType.NONE.equals(ft))
return ft.getStartURL(this, user);
Module module = getDefaultModule(user);
if (module != null)
{
ActionURL helper = module.getTabURL(this, user);
if (helper != null)
return helper;
}
return PageFlowUtil.urlProvider(ProjectUrls.class).getBeginURL(this);
}
public Module getDefaultModule()
{
return getDefaultModule(null);
}
public Module getDefaultModule(@Nullable User user)
{
if (isRoot())
return null;
if (_defaultModule == null)
{
try (var ignore = SpringActionController.ignoreSqlUpdates())
{
Map<String, String> props = PropertyManager.getProperties(this, "defaultModules");
String defaultModuleName = props.get("name");
boolean initRequired = false;
if (null == defaultModuleName || null == ModuleLoader.getInstance().getModule(defaultModuleName))
{
defaultModuleName = "Core";
initRequired = true;
}
Module defaultModule = ModuleLoader.getInstance().getModule(defaultModuleName);
//set default module
if (initRequired)
setDefaultModule(defaultModule);
//ensure that default module is included in active module set
//should be there already if it's not portal, but if it is core, we have to add it for upgrade
if (defaultModuleName.compareToIgnoreCase("Core") == 0)
{
Set<Module> modules = new HashSet<>(getActiveModules(user));
if (!modules.contains(defaultModule))
{
modules.add(defaultModule);
setActiveModules(modules, user);
}
}
_defaultModule = defaultModule;
}
}
return _defaultModule;
}
public void setFolderType(FolderType folderType, User user, BindException errors, Set<Module> ensureModules)
{
setFolderType(folderType, user, errors);
if (!errors.hasErrors())
{
// Check modules explicitly requested (folderType's modules already checked by setFolderType
if (!hasEnableRestrictedModules(user))
{
for (Module module : ensureModules)
{
if (module.getRequireSitePermission())
{
errors.reject(SpringActionController.ERROR_MSG, "Modules not enabled because module '" + module.getName() + "' is restricted and you do not have the necessary permission to enable it.");
}
}
}
if (!errors.hasErrors())
{
Set<Module> modules = new HashSet<>(folderType.getActiveModules());
modules.addAll(ensureModules);
setActiveModules(modules, user);
}
}
}
public void setFolderType(FolderType folderType, User user)
{
BindException errors = new BindException(new Object(), "dummy");
setFolderType(folderType, user, errors);
}
public void setFolderType(FolderType folderType, User user, BindException errors)