|
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