]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java
Remove deprecated APIs
[lgpl/argeo-commons.git] / org.argeo.cms / src / org / argeo / cms / auth / CurrentUser.java
1 /*
2 * Copyright (C) 2007-2012 Argeo GmbH
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.argeo.cms.auth;
17
18 import java.security.AccessController;
19 import java.security.Principal;
20 import java.security.acl.Group;
21 import java.util.HashSet;
22 import java.util.Set;
23
24 import javax.security.auth.Subject;
25 import javax.security.auth.x500.X500Principal;
26
27 import org.argeo.cms.CmsException;
28 import org.argeo.eclipse.ui.specific.UiContext;
29 import org.argeo.node.NodeAuthenticated;
30 import org.argeo.node.NodeConstants;
31 import org.osgi.service.useradmin.Authorization;
32
33 /** Static utilities */
34 public final class CurrentUser {
35 /**
36 * @return the authenticated username or null if not authenticated /
37 * anonymous
38 */
39 public static String getUsername() {
40 return getUsername(currentSubject());
41 }
42
43 public static String getDisplayName() {
44 return getDisplayName(currentSubject());
45 }
46
47 public static boolean isAnonymous() {
48 return isAnonymous(currentSubject());
49 }
50
51 public static boolean isAnonymous(Subject subject) {
52 String username = getUsername(subject);
53 return username == null
54 || username.equalsIgnoreCase(NodeConstants.ROLE_ANONYMOUS);
55 }
56
57 private static Subject currentSubject() {
58 NodeAuthenticated cmsView = getNodeAuthenticated();
59 if (cmsView != null)
60 return cmsView.getSubject();
61 Subject subject = Subject.getSubject(AccessController.getContext());
62 if (subject != null)
63 return subject;
64 throw new CmsException("Cannot find related subject");
65 }
66
67 /**
68 * The node authenticated component (typically a CMS view) related to this
69 * display, or null if none is available from this call.
70 */
71 public static NodeAuthenticated getNodeAuthenticated() {
72 return UiContext.getData(NodeAuthenticated.KEY);
73 }
74
75 public final static String getUsername(Subject subject) {
76 if (subject.getPrincipals(X500Principal.class).size() != 1)
77 return null;
78 Principal principal = subject.getPrincipals(X500Principal.class)
79 .iterator().next();
80 return principal.getName();
81 }
82
83 public final static String getDisplayName(Subject subject) {
84 return getAuthorization(subject).toString();
85 }
86
87 private static Authorization getAuthorization(Subject subject) {
88 return subject.getPrivateCredentials(Authorization.class).iterator()
89 .next();
90 }
91
92 public final static Set<String> roles() {
93 return roles(currentSubject());
94 }
95
96 public final static Set<String> roles(Subject subject) {
97 Set<String> roles = new HashSet<String>();
98 X500Principal userPrincipal = subject
99 .getPrincipals(X500Principal.class).iterator().next();
100 roles.add(userPrincipal.getName());
101 for (Principal group : subject.getPrincipals(Group.class)) {
102 roles.add(group.getName());
103 }
104 return roles;
105 }
106
107 private CurrentUser() {
108 }
109 }