1 /*
2 * MIT License
3 *
4 * Copyright (c) 2010-2022 The Waffle Project Contributors: https://github.com/Waffle/waffle/graphs/contributors
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
12 *
13 * The above copyright notice and this permission notice shall be included in all
14 * copies or substantial portions of the Software.
15 *
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22 * SOFTWARE.
23 */
24 package waffle.jaas;
25
26 import java.security.Principal;
27 import java.util.Collection;
28 import java.util.Collections;
29 import java.util.Enumeration;
30 import java.util.HashMap;
31 import java.util.Map;
32
33 /**
34 * Group principal.
35 *
36 * @author rockchip[dot]tv[at]gmail[dot]com
37 *
38 * @deprecated This class is deprecated as hiding a principal inside another principal is not JAAS compliant. Use the
39 * Principals in the Subject to directly enroll groups or roles by name.
40 */
41 @Deprecated
42 public class GroupPrincipal extends UserPrincipal {
43
44 /** The Constant serialVersionUID. */
45 private static final long serialVersionUID = 1L;
46
47 /** The fqn. */
48 private final String fqn;
49
50 /** A list of fqn members for this group. */
51 private final Map<Principal, Principal> members;
52
53 /**
54 * Instantiates a new group principal.
55 *
56 * @param fqn
57 * the fqn
58 */
59 public GroupPrincipal(final String fqn) {
60 super(fqn);
61
62 this.fqn = fqn;
63 this.members = new HashMap<>();
64 }
65
66 @Override
67 public String getName() {
68 return this.fqn;
69 }
70
71 /**
72 * Add user principal to member.
73 *
74 * @param user
75 * principal
76 *
77 * @return True if user principal is a member
78 */
79 public boolean addMember(final Principal user) {
80 final boolean isMember = this.members.containsKey(user);
81 if (!isMember) {
82 this.members.put(user, user);
83 }
84 return isMember;
85 }
86
87 /**
88 * Is user principal a member of the group.
89 *
90 * @param user
91 * principal
92 *
93 * @return True if user principal is a member
94 */
95 public boolean isMember(final Principal user) {
96 boolean isMember = this.members.containsKey(user);
97 if (!isMember) {
98 final Collection<Principal> values = this.members.values();
99 for (final Principal principal : values) {
100 if (principal instanceof GroupPrincipal) {
101 final GroupPrincipal group = (GroupPrincipal) principal;
102 isMember = group.isMember(user);
103 if (isMember) {
104 break;
105 }
106 }
107 }
108 }
109 return isMember;
110 }
111
112 /**
113 * Member enumeration.
114 *
115 * @return enumerated members
116 */
117 public Enumeration<Principal> members() {
118 return Collections.enumeration(this.members.values());
119 }
120
121 /**
122 * Remove user from member.
123 *
124 * @param user
125 * principal
126 *
127 * @return True if user principal is removed
128 */
129 public boolean removeMember(final Principal user) {
130 final Object prev = this.members.remove(user);
131 return prev != null;
132 }
133
134 @Override
135 public String toString() {
136 final StringBuilder tmp = new StringBuilder(this.getName());
137 tmp.append("(members:");
138 for (final Principal principal : this.members.keySet()) {
139 tmp.append(principal);
140 tmp.append(',');
141 }
142 tmp.setCharAt(tmp.length() - 1, ')');
143 return tmp.toString();
144 }
145
146 }