1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.impl.role;
17
18 import java.sql.Timestamp;
19 import java.util.ArrayList;
20 import java.util.Collection;
21 import java.util.HashSet;
22 import java.util.List;
23 import java.util.Set;
24 import java.util.concurrent.TimeUnit;
25
26 import org.apache.commons.lang.StringUtils;
27 import org.joda.time.DateTime;
28 import org.kuali.rice.core.api.membership.MemberType;
29 import org.kuali.rice.kim.api.group.GroupMember;
30 import org.kuali.rice.kim.impl.common.delegate.DelegateMemberBo;
31 import org.kuali.rice.kim.impl.common.delegate.DelegateTypeBo;
32 import org.kuali.rice.kim.impl.group.GroupMemberBo;
33
34 public class RoleInternalServiceImpl extends RoleServiceBase implements RoleInternalService{
35 @Override
36 public void principalInactivated(String principalId) {
37 if (StringUtils.isBlank(principalId)) {
38 throw new IllegalArgumentException("principalId is null or blank");
39 }
40
41 long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
42 Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
43
44 inactivatePrincipalRoleMemberships(principalId, yesterday);
45 inactivatePrincipalGroupMemberships(principalId, yesterday);
46 inactivatePrincipalDelegations(principalId, yesterday);
47 inactivateApplicationRoleMemberships(principalId, yesterday);
48 }
49
50 @Override
51 public void roleInactivated(String roleId) {
52 if (StringUtils.isBlank(roleId)) {
53 throw new IllegalArgumentException("roleId is null or blank");
54 }
55
56 long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
57 Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
58
59 List<String> roleIds = new ArrayList<String>();
60 roleIds.add(roleId);
61 inactivateRoleMemberships(roleIds, yesterday);
62 inactivateRoleDelegations(roleIds, yesterday);
63 inactivateMembershipsForRoleAsMember(roleIds, yesterday);
64 }
65
66 private void inactivateRoleMemberships(List<String> roleIds, Timestamp yesterday) {
67 List<RoleMemberBo> roleMemberBoList = getStoredRoleMembersForRoleIds(roleIds, null, null);
68 for (RoleMemberBo roleMemberBo : roleMemberBoList) {
69 roleMemberBo.setActiveToDateValue(yesterday);
70
71 notifyOnMemberRemoval(RoleMemberBo.to(roleMemberBo));
72 getDataObjectService().save(roleMemberBo);
73 }
74
75 }
76
77 private void inactivateRoleDelegations(List<String> roleIds, Timestamp yesterday) {
78 List<DelegateTypeBo> delegations = getStoredDelegationImplsForRoleIds(roleIds);
79 for (DelegateTypeBo delegation : delegations) {
80 delegation.setActive(false);
81 for (DelegateMemberBo delegationMember : delegation.getMembers()) {
82 delegationMember.setActiveToDateValue(yesterday);
83 getDataObjectService().save(delegationMember);
84 }
85 }
86 }
87
88 private void inactivateMembershipsForRoleAsMember(List<String> roleIds, Timestamp yesterday) {
89 List<RoleMemberBo> roleMemberBoList = getStoredRoleMembershipsForRoleIdsAsMembers(roleIds, null);
90 for (RoleMemberBo roleMemberBo : roleMemberBoList) {
91 roleMemberBo.setActiveToDateValue(yesterday);
92
93 notifyOnMemberRemoval(RoleMemberBo.to(roleMemberBo));
94 getDataObjectService().save(roleMemberBo);
95 }
96 }
97
98 @Override
99 public void groupInactivated(String groupId) {
100 if (StringUtils.isBlank(groupId)) {
101 throw new IllegalArgumentException("groupId is null or blank");
102 }
103
104 long oneDayInMillis = TimeUnit.DAYS.toMillis(1);
105 Timestamp yesterday = new Timestamp(System.currentTimeMillis() - oneDayInMillis);
106
107 List<String> groupIds = new ArrayList<String>();
108 groupIds.add(groupId);
109 inactivatePrincipalGroupMemberships(groupIds, yesterday);
110 inactivateGroupRoleMemberships(groupIds, yesterday);
111 }
112
113 protected void inactivateApplicationRoleMemberships(String principalId, Timestamp yesterday) {
114
115 }
116
117 protected void inactivatePrincipalRoleMemberships(String principalId, Timestamp yesterday) {
118
119 List<RoleMemberBo> roleMembers = getStoredRolePrincipalsForPrincipalIdAndRoleIds(null, principalId, null);
120 Set<String> roleIds = new HashSet<String>(roleMembers.size());
121 for (RoleMemberBo roleMemberBo : roleMembers) {
122 roleMemberBo.setActiveToDateValue(yesterday);
123 roleIds.add(roleMemberBo.getRoleId());
124
125 notifyOnMemberRemoval(RoleMemberBo.to(roleMemberBo));
126 getDataObjectService().save(roleMemberBo);
127 }
128 }
129
130 protected void inactivateGroupRoleMemberships(List<String> groupIds, Timestamp yesterday) {
131 List<RoleMemberBo> roleMemberBosOfGroupType = getStoredRoleGroupsForGroupIdsAndRoleIds(null, groupIds, null);
132 for (RoleMemberBo roleMemberBo : roleMemberBosOfGroupType) {
133 roleMemberBo.setActiveToDateValue(yesterday);
134
135 notifyOnMemberRemoval(RoleMemberBo.to(roleMemberBo));
136 getDataObjectService().save(roleMemberBo);
137 }
138 }
139
140 protected void inactivatePrincipalGroupMemberships(String principalId, Timestamp yesterday) {
141 if ( StringUtils.isBlank(principalId) ) {
142 return;
143 }
144
145 List<String> groupIds = getGroupService().getGroupIdsByPrincipalId(principalId);
146 if (groupIds.isEmpty() ) {
147 return;
148 }
149
150 Collection<GroupMember> groupMembers = getGroupService().getMembers(groupIds);
151 List<GroupMember> groupPrincipals = new ArrayList<GroupMember>(groupMembers.size());
152 for (GroupMember groupMembershipInfo : groupMembers) {
153 if (MemberType.PRINCIPAL.equals(groupMembershipInfo.getType())
154 && StringUtils.equals(principalId, groupMembershipInfo.getMemberId())
155 && groupMembershipInfo.isActive(new DateTime())) {
156 groupPrincipals.add(groupMembershipInfo);
157
158
159 }
160 }
161
162 for (GroupMember gm : groupPrincipals) {
163 GroupMember.Builder builder = GroupMember.Builder.create(gm);
164 builder.setActiveToDate(new DateTime(yesterday.getTime()));
165 getDataObjectService().save(GroupMemberBo.from(builder.build()));
166 }
167 }
168
169 protected void inactivatePrincipalGroupMemberships(List<String> groupIds, Timestamp yesterday) {
170 if (groupIds == null || groupIds.isEmpty() ) {
171 return;
172 }
173 Collection<GroupMember> groupMemberships = getGroupService().getMembers(groupIds);
174 if ( groupMemberships.isEmpty() ) {
175 return;
176 }
177 List<GroupMember> groupMembers = new ArrayList<GroupMember>();
178 for (GroupMember groupMembershipInfo : groupMemberships) {
179 if (MemberType.GROUP.equals(groupMembershipInfo.getType())
180 && groupMembershipInfo.isActive(new DateTime())) {
181 groupMembers.add(groupMembershipInfo);
182 }
183 }
184
185 for (GroupMember groupMember : groupMembers) {
186 GroupMember.Builder builder = GroupMember.Builder.create(groupMember);
187 builder.setActiveToDate(new DateTime(yesterday.getTime()));
188 getDataObjectService().save(GroupMemberBo.from(builder.build()));
189 }
190 }
191
192 protected void inactivatePrincipalDelegations(String principalId, Timestamp yesterday) {
193 List<DelegateMemberBo> delegationMembers = getStoredDelegationPrincipalsForPrincipalIdAndDelegationIds(null,
194 principalId);
195 for (DelegateMemberBo delegateMemberBo : delegationMembers) {
196 delegateMemberBo.setActiveToDateValue(yesterday);
197 getDataObjectService().save(delegateMemberBo);
198 }
199 }
200 }