1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16 package org.kuali.rice.kim.web.struts.action;
17
18 import org.apache.commons.lang.StringUtils;
19 import org.apache.struts.action.ActionForm;
20 import org.apache.struts.action.ActionForward;
21 import org.apache.struts.action.ActionMapping;
22 import org.kuali.rice.core.api.membership.MemberType;
23 import org.kuali.rice.core.api.util.RiceConstants;
24 import org.kuali.rice.core.api.util.RiceKeyConstants;
25 import org.kuali.rice.kew.api.exception.WorkflowException;
26 import org.kuali.rice.kim.api.KimConstants;
27 import org.kuali.rice.kim.api.group.Group;
28 import org.kuali.rice.kim.api.identity.principal.Principal;
29 import org.kuali.rice.kim.api.role.Role;
30 import org.kuali.rice.kim.api.services.KimApiServiceLocator;
31 import org.kuali.rice.kim.api.type.KimType;
32 import org.kuali.rice.kim.bo.ui.GroupDocumentMember;
33 import org.kuali.rice.kim.document.IdentityManagementGroupDocument;
34 import org.kuali.rice.kim.rule.event.ui.AddGroupMemberEvent;
35 import org.kuali.rice.kim.web.struts.form.IdentityManagementGroupDocumentForm;
36 import org.kuali.rice.kns.web.struts.action.KualiTableRenderAction;
37 import org.kuali.rice.kns.web.struts.form.KualiDocumentFormBase;
38 import org.kuali.rice.kns.web.struts.form.KualiTableRenderFormMetadata;
39 import org.kuali.rice.krad.service.KRADServiceLocatorWeb;
40 import org.kuali.rice.krad.util.GlobalVariables;
41
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpServletResponse;
44 import java.sql.Timestamp;
45 import java.util.Calendar;
46 import java.util.HashMap;
47 import java.util.Map;
48
49
50
51
52
53
54 public class IdentityManagementGroupDocumentAction extends IdentityManagementDocumentActionBase implements KualiTableRenderAction {
55
56
57
58
59
60 public IdentityManagementGroupDocumentAction() {
61 super();
62 addMethodToCallToUncheckedList( CHANGE_MEMBER_TYPE_CODE_METHOD_TO_CALL );
63 addMethodToCallToUncheckedList( CHANGE_NAMESPACE_METHOD_TO_CALL );
64 }
65
66
67
68
69
70
71
72
73
74
75
76
77
78 public ActionForward sort(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
79
80 return mapping.findForward(RiceConstants.MAPPING_BASIC);
81 }
82
83 @Override
84 public ActionForward execute(ActionMapping mapping, ActionForm form,
85 HttpServletRequest request, HttpServletResponse response) throws Exception {
86 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
87 if ( StringUtils.isBlank( groupDocumentForm.getGroupId() ) ) {
88 String groupId = request.getParameter(KimConstants.PrimaryKeyConstants.GROUP_ID);
89 groupDocumentForm.setGroupId(groupId);
90 }
91 String kimTypeId = request.getParameter(KimConstants.PrimaryKeyConstants.KIM_TYPE_ID);
92 setKimType(kimTypeId, groupDocumentForm);
93
94
95 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
96 if (groupDocumentForm.getMemberRows() != null) {
97 memberTableMetadata.jumpToPage(memberTableMetadata.getViewedPageNumber(), groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
98
99 memberTableMetadata.sort(groupDocumentForm.getMemberRows(), groupDocumentForm.getRecordsPerPage());
100 }
101
102 ActionForward forward = super.execute(mapping, groupDocumentForm, request, response);
103
104 groupDocumentForm.setCanAssignGroup(validAssignGroup(groupDocumentForm.getGroupDocument()));
105 return forward;
106 }
107
108 protected void setKimType(String kimTypeId, IdentityManagementGroupDocumentForm groupDocumentForm){
109 if ( StringUtils.isNotBlank(kimTypeId) ) {
110 KimType kType = KimApiServiceLocator.getKimTypeInfoService().getKimType(kimTypeId);
111 groupDocumentForm.setKimType(kType);
112 if (groupDocumentForm.getGroupDocument() != null) {
113 groupDocumentForm.getGroupDocument().setKimType(kType);
114 }
115 } else if ( groupDocumentForm.getGroupDocument() != null && StringUtils.isNotBlank(groupDocumentForm.getGroupDocument().getGroupTypeId() ) ) {
116 groupDocumentForm.setKimType(KimApiServiceLocator.getKimTypeInfoService().getKimType(
117 groupDocumentForm.getGroupDocument().getGroupTypeId()));
118 groupDocumentForm.getGroupDocument().setKimType(groupDocumentForm.getKimType());
119 }
120 }
121
122
123
124
125
126
127 @Override
128 protected void loadDocument(KualiDocumentFormBase form)
129 throws WorkflowException {
130 super.loadDocument(form);
131
132 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
133 setKimType(groupDocumentForm.getGroupId(), groupDocumentForm);
134 groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
135
136 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
137 if (groupDocumentForm.getMemberRows() != null) {
138 memberTableMetadata.jumpToFirstPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
139 }
140 }
141
142
143
144
145
146
147 @Override
148 protected void createDocument(KualiDocumentFormBase form)
149 throws WorkflowException {
150 super.createDocument(form);
151 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
152 if ( groupDocumentForm.getGroupId() == null ) {
153 groupDocumentForm.getGroupDocument().setKimType(groupDocumentForm.getKimType());
154 groupDocumentForm.getGroupDocument().initializeDocumentForNewGroup();
155 groupDocumentForm.setGroupId( groupDocumentForm.getGroupDocument().getGroupId() );
156 setKimType(groupDocumentForm.getGroupDocument().getGroupTypeId(), groupDocumentForm);
157 } else {
158 loadGroupIntoDocument( groupDocumentForm.getGroupId(), groupDocumentForm );
159 }
160 KualiTableRenderFormMetadata memberTableMetadata = groupDocumentForm.getMemberTableMetadata();
161 if (groupDocumentForm.getMemberRows() != null) {
162 memberTableMetadata.jumpToFirstPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
163 }
164 }
165
166
167 protected void loadGroupIntoDocument( String groupId, IdentityManagementGroupDocumentForm groupDocumentForm){
168 Group group = KimApiServiceLocator.getGroupService().getGroup(groupId);
169 getUiDocumentService().loadGroupDoc(groupDocumentForm.getGroupDocument(), group);
170 }
171
172
173
174
175 public String getActionName(){
176 return KimConstants.KimUIConstants.KIM_GROUP_DOCUMENT_ACTION;
177 }
178
179 protected boolean validAssignGroup(IdentityManagementGroupDocument document){
180 boolean rulePassed = true;
181 Map<String,String> additionalPermissionDetails = new HashMap<String,String>();
182 if (!StringUtils.isEmpty(document.getGroupNamespace())) {
183 additionalPermissionDetails.put(KimConstants.AttributeConstants.NAMESPACE_CODE, document.getGroupNamespace());
184 additionalPermissionDetails.put(KimConstants.AttributeConstants.GROUP_NAME, document.getGroupName());
185 if (!getDocumentHelperService().getDocumentAuthorizer(document).isAuthorizedByTemplate(
186 document,
187 KimConstants.NAMESPACE_CODE,
188 KimConstants.PermissionTemplateNames.POPULATE_GROUP,
189 GlobalVariables.getUserSession().getPrincipalId(),
190 additionalPermissionDetails, null)){
191 rulePassed = false;
192 }
193 }
194 return rulePassed;
195 }
196
197 public ActionForward changeMemberTypeCode(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
198 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
199 groupDocumentForm.getMember().setMemberName("");
200 return refresh(mapping, groupDocumentForm, request, response);
201 }
202
203 public ActionForward addMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
204 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
205 GroupDocumentMember newMember = groupDocumentForm.getMember();
206
207
208 if (StringUtils.isEmpty(newMember.getMemberId())
209 && StringUtils.isNotEmpty(newMember.getMemberName())
210 && StringUtils.isNotEmpty(newMember.getMemberNamespaceCode())
211 && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.GROUP_MEMBER_TYPE.getCode())) {
212 Group tempGroup = KimApiServiceLocator.getGroupService().getGroupByNamespaceCodeAndName(
213 newMember.getMemberNamespaceCode(), newMember.getMemberName());
214 if (tempGroup != null) {
215 newMember.setMemberId(tempGroup.getId());
216 }
217 }
218
219
220 if (StringUtils.isEmpty(newMember.getMemberId())
221 && StringUtils.isNotEmpty(newMember.getMemberName())
222 && StringUtils.equals(newMember.getMemberTypeCode(), KimConstants.KimGroupMemberTypes.PRINCIPAL_MEMBER_TYPE.getCode())) {
223 Principal principal = KimApiServiceLocator.getIdentityService().getPrincipalByPrincipalName(newMember.getMemberName());
224 if (principal != null) {
225 newMember.setMemberId(principal.getPrincipalId());
226 }
227 }
228 if(checkKimDocumentGroupMember(newMember) &&
229 KRADServiceLocatorWeb.getKualiRuleService().applyRules(new AddGroupMemberEvent("", groupDocumentForm.getGroupDocument(), newMember))){
230 newMember.setDocumentNumber(groupDocumentForm.getDocument().getDocumentNumber());
231 groupDocumentForm.getGroupDocument().addMember(newMember);
232 groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
233 groupDocumentForm.getMemberTableMetadata().jumpToLastPage(groupDocumentForm.getMemberRows().size(), groupDocumentForm.getRecordsPerPage());
234 }
235 return mapping.findForward(RiceConstants.MAPPING_BASIC);
236 }
237
238 protected boolean checkKimDocumentGroupMember(GroupDocumentMember newMember){
239 if(StringUtils.isBlank(newMember.getMemberTypeCode()) || StringUtils.isBlank(newMember.getMemberId())){
240 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_EMPTY_ENTRY,
241 new String[] {"Member Type Code and Member ID"});
242 return false;
243 }
244 if(MemberType.PRINCIPAL.getCode().equals(newMember.getMemberTypeCode())){
245 Principal principalInfo = getIdentityService().getPrincipal(newMember.getMemberId());
246 if (principalInfo == null) {
247 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
248 new String[] {newMember.getMemberId()});
249 return false;
250 }
251 else {
252 newMember.setMemberName(principalInfo.getPrincipalName());
253 }
254 } else if(MemberType.GROUP.getCode().equals(newMember.getMemberTypeCode())){
255 Group groupInfo = null;
256 groupInfo = KimApiServiceLocator.getGroupService().getGroup(newMember.getMemberId());
257 if (groupInfo == null) {
258 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
259 new String[] {newMember.getMemberId()});
260 return false;
261 }
262 else {
263 newMember.setMemberName(groupInfo.getName());
264 newMember.setMemberNamespaceCode(groupInfo.getNamespaceCode());
265 }
266 } else if(MemberType.ROLE.getCode().equals(newMember.getMemberTypeCode())){
267 Role role = KimApiServiceLocator.getRoleService().getRole(newMember.getMemberId());
268 if (role == null) {
269 GlobalVariables.getMessageMap().putError("document.member.memberId", RiceKeyConstants.ERROR_MEMBERID_MEMBERTYPE_MISMATCH,
270 new String[] {newMember.getMemberId()});
271 return false;
272 }
273 else if(StringUtils.equals(newMember.getMemberTypeCode(), MemberType.ROLE.getCode())
274 && !validateRole(newMember.getMemberId(), role, "document.member.memberId", "Role")){
275 return false;
276 }
277 else {
278 newMember.setMemberName(role.getName());
279 newMember.setMemberNamespaceCode(role.getNamespaceCode());
280 }
281 }
282 return true;
283 }
284
285 public ActionForward deleteMember(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws Exception {
286 IdentityManagementGroupDocumentForm groupDocumentForm = (IdentityManagementGroupDocumentForm) form;
287 GroupDocumentMember memberToBeInactivated = groupDocumentForm.getGroupDocument().getMembers().get(getLineToDelete(request));
288 Calendar cal = Calendar.getInstance();
289 memberToBeInactivated.setActiveToDate(new Timestamp(cal.getTimeInMillis()));
290 groupDocumentForm.getGroupDocument().getMembers().set(getLineToDelete(request), memberToBeInactivated);
291 groupDocumentForm.setMember(groupDocumentForm.getGroupDocument().getBlankMember());
292 return mapping.findForward(RiceConstants.MAPPING_BASIC);
293 }
294
295 }