]> git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/auth/CurrentUser.java
Improve social networking
[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.cms.CmsView;
29 import org.argeo.cms.util.CmsUtils;
30 import org.osgi.service.useradmin.Authorization;
31
32 /** Static utilities */
33 public final class CurrentUser {
34 /**
35 * @return the authenticated username or null if not authenticated /
36 * anonymous
37 */
38 public static String getUsername() {
39 return getUsername(currentSubject());
40 }
41
42 public static String getDisplayName() {
43 return getDisplayName(currentSubject());
44 }
45
46 public static boolean isAnonymous() {
47 return isAnonymous(currentSubject());
48 }
49
50 public static boolean isAnonymous(Subject subject) {
51 String username = getUsername(subject);
52 return username == null
53 || username.equalsIgnoreCase(AuthConstants.ROLE_ANONYMOUS);
54 }
55
56 private static Subject currentSubject() {
57 CmsView cmsView = CmsUtils.getCmsView();
58 if (cmsView != null)
59 return cmsView.getSubject();
60 Subject subject = Subject.getSubject(AccessController.getContext());
61 if (subject != null)
62 return subject;
63 throw new CmsException("Cannot find related subject");
64 }
65
66 public final static String getUsername(Subject subject) {
67 // Subject subject = Subject.getSubject(AccessController.getContext());
68 // if (subject == null)
69 // return null;
70 if (subject.getPrincipals(X500Principal.class).size() != 1)
71 return null;
72 Principal principal = subject.getPrincipals(X500Principal.class)
73 .iterator().next();
74 return principal.getName();
75
76 }
77
78 public final static String getDisplayName(Subject subject) {
79 return getAuthorization(subject).toString();
80 }
81
82 private static Authorization getAuthorization(Subject subject) {
83 return subject.getPrivateCredentials(Authorization.class).iterator()
84 .next();
85 }
86
87 public final static Set<String> roles() {
88 return roles(currentSubject());
89 }
90
91 public final static Set<String> roles(Subject subject) {
92 Set<String> roles = new HashSet<String>();
93 X500Principal userPrincipal = subject
94 .getPrincipals(X500Principal.class).iterator().next();
95 roles.add(userPrincipal.getName());
96 for (Principal group : subject.getPrincipals(Group.class)) {
97 roles.add(group.getName());
98 }
99 return roles;
100 }
101
102 private CurrentUser() {
103 }
104 }