Skip to content

Commit a5ac1b9

Browse files
authored
ImpersonationContext -> PermissionsContext rename/repackage (#7462)
1 parent 15032b2 commit a5ac1b9

22 files changed

Lines changed: 281 additions & 297 deletions

api/src/org/labkey/api/security/ClonedUser.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
package org.labkey.api.security;
22

3-
import org.labkey.api.security.impersonation.ImpersonationContext;
43
import org.labkey.api.security.roles.Role;
54
import org.labkey.api.security.roles.RoleManager;
65

@@ -14,13 +13,13 @@
1413

1514
public abstract class ClonedUser extends User
1615
{
17-
protected ClonedUser(User user, ImpersonationContext ctx)
16+
protected ClonedUser(User user, PermissionsContext ctx)
1817
{
1918
this(user.getEmail(), user.getUserId(), user.getFriendlyName(), user.getFirstName(), user.getLastName(), user.isActive(), user.getLastLogin(), user.getPhone(), user.getLastActivity(), ctx);
2019
}
2120

2221
protected ClonedUser(String email, int userId, String displayName, String firstName, String lastName, boolean active,
23-
Date lastLogin, String phone, Date lastActivity, ImpersonationContext ctx)
22+
Date lastLogin, String phone, Date lastActivity, PermissionsContext ctx)
2423
{
2524
super(email, userId);
2625
setDisplayName(displayName);

api/src/org/labkey/api/security/impersonation/DisallowPrivilegedRolesContext.java renamed to api/src/org/labkey/api/security/DisallowPrivilegedRolesContext.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,19 +13,17 @@
1313
* See the License for the specific language governing permissions and
1414
* limitations under the License.
1515
*/
16-
package org.labkey.api.security.impersonation;
16+
package org.labkey.api.security;
1717

1818
/*
19-
A "not impersonating" context that filters out privileged site roles (i.e., Site Admin, Platform Developer)
19+
A context that filters out privileged site roles (i.e., Site Admin, Platform Developer)
2020
*/
2121

22-
import org.labkey.api.security.SecurableResource;
23-
import org.labkey.api.security.User;
2422
import org.labkey.api.security.roles.Role;
2523

2624
import java.util.stream.Stream;
2725

28-
public class DisallowPrivilegedRolesContext extends NotImpersonatingContext
26+
public class DisallowPrivilegedRolesContext extends NormalPermissionsContext
2927
{
3028
private static final DisallowPrivilegedRolesContext INSTANCE = new DisallowPrivilegedRolesContext();
3129

api/src/org/labkey/api/security/ElevatedUser.java

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22

33
import org.labkey.api.audit.permissions.CanSeeAuditLogPermission;
44
import org.labkey.api.data.Container;
5-
import org.labkey.api.security.impersonation.ImpersonationContext;
6-
import org.labkey.api.security.impersonation.WrappedImpersonationContext;
75
import org.labkey.api.security.permissions.Permission;
86
import org.labkey.api.security.roles.CanSeeAuditLogRole;
97
import org.labkey.api.security.roles.Role;
@@ -24,10 +22,10 @@ public class ElevatedUser extends ClonedUser
2422
{
2523
private ElevatedUser(User user, Set<Role> rolesToAdd)
2624
{
27-
super(user, new WrappedImpersonationContext(user.getImpersonationContext(), rolesToAdd));
25+
super(user, new WrappedPermissionsContext(user.getPermissionsContext(), rolesToAdd));
2826
}
2927

30-
private ElevatedUser(User user, ImpersonationContext ctx)
28+
private ElevatedUser(User user, PermissionsContext ctx)
3129
{
3230
super(user, ctx);
3331
}
@@ -52,7 +50,7 @@ public static ElevatedUser getElevatedUser(User user, Collection<Class<? extends
5250
/**
5351
* Used to reconstitute an ElevatedUser from its component parts
5452
*/
55-
public static ElevatedUser getElevatedUser(User user, ImpersonationContext ctx)
53+
public static ElevatedUser getElevatedUser(User user, PermissionsContext ctx)
5654
{
5755
return new ElevatedUser(user, ctx);
5856
}

api/src/org/labkey/api/security/LimitedUser.java

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,6 @@
2626
import org.labkey.api.data.Container;
2727
import org.labkey.api.data.ContainerManager;
2828
import org.labkey.api.pipeline.PipelineJob;
29-
import org.labkey.api.security.impersonation.ImpersonationContext;
30-
import org.labkey.api.security.impersonation.NotImpersonatingContext;
3129
import org.labkey.api.security.permissions.AdminPermission;
3230
import org.labkey.api.security.permissions.InsertPermission;
3331
import org.labkey.api.security.permissions.ReadPermission;
@@ -54,7 +52,7 @@
5452
public class LimitedUser extends ClonedUser
5553
{
5654
// Must be a named class to allow Jackson deserialization (e.g., Evaluation Content loads folder archives via the pipeline using AdminUser)
57-
private static class LimitedUserImpersonatingContext extends NotImpersonatingContext
55+
private static class LimitedUserImpersonatingContext extends NormalPermissionsContext
5856
{
5957
private final Set<Role> _roles;
6058

@@ -99,7 +97,7 @@ private LimitedUser(
9997
@JsonProperty("_lastLogin") Date lastLogin,
10098
@JsonProperty("_phone") String phone,
10199
@JsonProperty("_lastActivity") Date lastActivity,
102-
@JsonProperty("_impersonationContext") ImpersonationContext ctx
100+
@JsonProperty("_impersonationContext") PermissionsContext ctx
103101
)
104102
{
105103
super(name, userId, displayName, firstName, lastName, active, lastLogin, phone, lastActivity, ctx);

api/src/org/labkey/api/security/impersonation/NotImpersonatingContext.java renamed to api/src/org/labkey/api/security/NormalPermissionsContext.java

Lines changed: 118 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -1,119 +1,118 @@
1-
/*
2-
* Copyright (c) 2011-2019 LabKey Corporation
3-
*
4-
* Licensed under the Apache License, Version 2.0 (the "License");
5-
* you may not use this file except in compliance with the License.
6-
* You may obtain a copy of the License at
7-
*
8-
* http://www.apache.org/licenses/LICENSE-2.0
9-
*
10-
* Unless required by applicable law or agreed to in writing, software
11-
* distributed under the License is distributed on an "AS IS" BASIS,
12-
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13-
* See the License for the specific language governing permissions and
14-
* limitations under the License.
15-
*/
16-
package org.labkey.api.security.impersonation;
17-
18-
import org.jetbrains.annotations.Nullable;
19-
import org.labkey.api.data.Container;
20-
import org.labkey.api.module.ModuleLoader;
21-
import org.labkey.api.security.GroupManager;
22-
import org.labkey.api.security.LoginUrls;
23-
import org.labkey.api.security.PrincipalArray;
24-
import org.labkey.api.security.User;
25-
import org.labkey.api.security.permissions.AdminPermission;
26-
import org.labkey.api.security.permissions.CanImpersonatePrivilegedSiteRolesPermission;
27-
import org.labkey.api.util.PageFlowUtil;
28-
import org.labkey.api.view.ActionURL;
29-
import org.labkey.api.view.NavTree;
30-
31-
/**
32-
* Used when a user is not impersonating another user, group, or role. That is, they are logged in normally, and
33-
* operating as themselves.
34-
*/
35-
public class NotImpersonatingContext implements ImpersonationContext
36-
{
37-
private static final NotImpersonatingContext INSTANCE = new NotImpersonatingContext();
38-
39-
public static NotImpersonatingContext get()
40-
{
41-
return INSTANCE;
42-
}
43-
44-
protected NotImpersonatingContext()
45-
{
46-
}
47-
48-
@Override
49-
public boolean isImpersonating()
50-
{
51-
return false;
52-
}
53-
54-
@Override
55-
public @Nullable Container getImpersonationProject()
56-
{
57-
return null;
58-
}
59-
60-
@Override
61-
public User getAdminUser()
62-
{
63-
return null;
64-
}
65-
66-
@Override
67-
public String getCacheKey()
68-
{
69-
return "";
70-
}
71-
72-
@Override
73-
public ActionURL getReturnUrl()
74-
{
75-
return null;
76-
}
77-
78-
@Override
79-
public ImpersonationContextFactory getFactory()
80-
{
81-
return null;
82-
}
83-
84-
@Override
85-
public PrincipalArray getGroups(User user)
86-
{
87-
return GroupManager.getAllGroupsForPrincipal(user);
88-
}
89-
90-
@Override
91-
public void addMenu(NavTree menu, Container c, User user, ActionURL currentURL)
92-
{
93-
if (ModuleLoader.getInstance().isStartupComplete())
94-
{
95-
@Nullable Container project = c.getProject();
96-
97-
// Must be site or project admin (folder admins can't impersonate)
98-
if (user.hasRootAdminPermission() || (null != project && project.hasPermission(user, AdminPermission.class)))
99-
{
100-
NavTree impersonateMenu = new NavTree("Impersonate");
101-
UserImpersonationContextFactory.addMenu(impersonateMenu);
102-
GroupImpersonationContextFactory.addMenu(impersonateMenu);
103-
RoleImpersonationContextFactory.addMenu(impersonateMenu);
104-
menu.addChild(impersonateMenu);
105-
}
106-
// Or Impersonating Troubleshooter to impersonate site roles only
107-
else if (null == project && user.hasRootPermission(CanImpersonatePrivilegedSiteRolesPermission.class))
108-
{
109-
NavTree impersonateMenu = new NavTree("Impersonate");
110-
RoleImpersonationContextFactory.addMenu(impersonateMenu);
111-
menu.addChild(impersonateMenu);
112-
}
113-
}
114-
115-
NavTree signOut = new NavTree("Sign Out", PageFlowUtil.urlProvider(LoginUrls.class).getLogoutURL(c));
116-
signOut.usePost();
117-
menu.addChild(signOut);
118-
}
119-
}
1+
/*
2+
* Copyright (c) 2011-2019 LabKey Corporation
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
package org.labkey.api.security;
17+
18+
import org.jetbrains.annotations.Nullable;
19+
import org.labkey.api.data.Container;
20+
import org.labkey.api.module.ModuleLoader;
21+
import org.labkey.api.security.impersonation.GroupImpersonationContextFactory;
22+
import org.labkey.api.security.impersonation.ImpersonationContextFactory;
23+
import org.labkey.api.security.impersonation.RoleImpersonationContextFactory;
24+
import org.labkey.api.security.impersonation.UserImpersonationContextFactory;
25+
import org.labkey.api.security.permissions.AdminPermission;
26+
import org.labkey.api.security.permissions.CanImpersonatePrivilegedSiteRolesPermission;
27+
import org.labkey.api.util.PageFlowUtil;
28+
import org.labkey.api.view.ActionURL;
29+
import org.labkey.api.view.NavTree;
30+
31+
/**
32+
* Used when a user is logged in normally and operating as themselves, not impersonating another user, group, or role.
33+
*/
34+
public class NormalPermissionsContext implements PermissionsContext
35+
{
36+
private static final NormalPermissionsContext INSTANCE = new NormalPermissionsContext();
37+
38+
public static NormalPermissionsContext get()
39+
{
40+
return INSTANCE;
41+
}
42+
43+
protected NormalPermissionsContext()
44+
{
45+
}
46+
47+
@Override
48+
public boolean isImpersonating()
49+
{
50+
return false;
51+
}
52+
53+
@Override
54+
public @Nullable Container getImpersonationProject()
55+
{
56+
return null;
57+
}
58+
59+
@Override
60+
public User getAdminUser()
61+
{
62+
return null;
63+
}
64+
65+
@Override
66+
public String getCacheKey()
67+
{
68+
return "";
69+
}
70+
71+
@Override
72+
public ActionURL getReturnUrl()
73+
{
74+
return null;
75+
}
76+
77+
@Override
78+
public ImpersonationContextFactory getFactory()
79+
{
80+
return null;
81+
}
82+
83+
@Override
84+
public PrincipalArray getGroups(User user)
85+
{
86+
return GroupManager.getAllGroupsForPrincipal(user);
87+
}
88+
89+
@Override
90+
public void addMenu(NavTree menu, Container c, User user, ActionURL currentURL)
91+
{
92+
if (ModuleLoader.getInstance().isStartupComplete())
93+
{
94+
@Nullable Container project = c.getProject();
95+
96+
// Must be site or project admin (folder admins can't impersonate)
97+
if (user.hasRootAdminPermission() || (null != project && project.hasPermission(user, AdminPermission.class)))
98+
{
99+
NavTree impersonateMenu = new NavTree("Impersonate");
100+
UserImpersonationContextFactory.addMenu(impersonateMenu);
101+
GroupImpersonationContextFactory.addMenu(impersonateMenu);
102+
RoleImpersonationContextFactory.addMenu(impersonateMenu);
103+
menu.addChild(impersonateMenu);
104+
}
105+
// Or Impersonating Troubleshooter to impersonate site roles only
106+
else if (null == project && user.hasRootPermission(CanImpersonatePrivilegedSiteRolesPermission.class))
107+
{
108+
NavTree impersonateMenu = new NavTree("Impersonate");
109+
RoleImpersonationContextFactory.addMenu(impersonateMenu);
110+
menu.addChild(impersonateMenu);
111+
}
112+
}
113+
114+
NavTree signOut = new NavTree("Sign Out", PageFlowUtil.urlProvider(LoginUrls.class).getLogoutURL(c));
115+
signOut.usePost();
116+
menu.addChild(signOut);
117+
}
118+
}

0 commit comments

Comments
 (0)