3131import cz .metacentrum .perun .core .api .exceptions .WrongReferenceAttributeValueException ;
3232import cz .metacentrum .perun .core .bl .PerunBl ;
3333import cz .metacentrum .perun .core .blImpl .AuthzResolverBlImpl ;
34+ import cz .metacentrum .perun .registrar .exceptions .FormNotExistsException ;
3435import cz .metacentrum .perun .registrar .exceptions .RegistrarException ;
3536import cz .metacentrum .perun .registrar .model .Application ;
3637import cz .metacentrum .perun .registrar .model .ApplicationForm ;
5556 *
5657 * @author Pavel Zlámal <[email protected] > 5758 */
58- public class EscPithiaOrgnizations extends DefaultRegistrarModule {
59+ public class EscPithiaOrganizations extends DefaultRegistrarModule {
5960
60- final static Logger log = LoggerFactory .getLogger (EscPithiaOrgnizations .class );
61+ final static Logger log = LoggerFactory .getLogger (EscPithiaOrganizations .class );
6162
6263 @ Override
6364 public Application approveApplication (PerunSession session , Application app ) throws UserNotExistsException , PrivilegeException , AlreadyAdminException , GroupNotExistsException , VoNotExistsException , MemberNotExistsException , AlreadyMemberException , ExternallyManagedException , WrongAttributeValueException , WrongAttributeAssignmentException , AttributeNotExistsException , WrongReferenceAttributeValueException , RegistrarException , ExtendMembershipException , ExtSourceNotExistsException , NotGroupMemberException {
@@ -82,63 +83,67 @@ public Application approveApplication(PerunSession session, Application app) thr
8283 throw new InternalErrorException ("Unable to create required group - organization!" , e );
8384 }
8485
85- // make requestor an admin of new organization
86+ Group organizationAdminsGroup = new Group (newOrganization .getId (), "admins" , "Admins of " +organizationName );
87+ Group organizationMembersGroup = new Group (newOrganization .getId (), "members" , "Members of " +organizationName );
88+
8689 try {
87- AuthzResolverBlImpl . setRole ( session , user , newOrganization , Role . GROUPADMIN );
88- } catch (RoleCannotBeManagedException e ) {
89- throw new InternalErrorException ("Unable to set group manager for group - organization!" , e );
90+ organizationAdminsGroup = perun . getGroupsManagerBl (). createGroup ( session , newOrganization , organizationAdminsGroup );
91+ } catch (GroupExistsException | GroupRelationNotAllowed | GroupRelationAlreadyExists e ) {
92+ throw new InternalErrorException ("Unable to create required group - organization:admins !" , e );
9093 }
9194
92- // make requestor a member of new organization
93- perun .getGroupsManagerBl ().addMember (session , newOrganization , member );
95+ try {
96+ organizationMembersGroup = perun .getGroupsManagerBl ().createGroup (session , newOrganization , organizationMembersGroup );
97+ } catch (GroupExistsException | GroupRelationNotAllowed | GroupRelationAlreadyExists e ) {
98+ throw new InternalErrorException ("Unable to create required group - organization:members!" , e );
99+ }
100+
101+ // make "admins" group an admin of new organization (both subgroups)
94102
95- // copy registration form and notifications from template
96103 try {
97- Group templateGroup = getTemplateGroup (session , vo );
98- registrar .createApplicationFormInGroup (session , newOrganization );
99- registrar .copyFormFromGroupToGroup (session , templateGroup , newOrganization );
100- registrar .getMailManager ().copyMailsFromGroupToGroup (session , templateGroup , newOrganization );
101-
102- ApplicationForm newForm = registrar .getFormForGroup (newOrganization );
103-
104- List <ApplicationFormItem > items = registrar .getFormItems (session , newForm );
105- for (ApplicationFormItem item : items ) {
106- if ("header" .equals (item .getShortname ())) {
107- Map <Locale , ApplicationFormItem .ItemTexts > i18n = item .getI18n ();
108- for (Locale locale : i18n .keySet ()) {
109- ApplicationFormItem .ItemTexts text = i18n .get (locale );
110- text .setLabel (text .getLabel ().replace ("{groupName}" , newOrganization .getShortName ()));
111- }
112- registrar .updateFormItem (session , item );
113- }
114- }
104+ AuthzResolverBlImpl .setRole (session , organizationAdminsGroup , organizationAdminsGroup , Role .GROUPADMIN );
105+ } catch (RoleCannotBeManagedException e ) {
106+ throw new InternalErrorException ("Unable to set group manager for group - organization:admins!" , e );
107+ }
108+ try {
109+ AuthzResolverBlImpl .setRole (session , organizationAdminsGroup , organizationMembersGroup , Role .GROUPADMIN );
110+ } catch (RoleCannotBeManagedException e ) {
111+ throw new InternalErrorException ("Unable to set group manager for group - organization:members!" , e );
112+ }
115113
116- List <ApplicationMail > mails = registrar .getMailManager ().getApplicationMails (session ,newForm );
117- for (ApplicationMail mail : mails ) {
118- Map <Locale , ApplicationMail .MailText > texts = mail .getMessage ();
119- for (Locale locale : texts .keySet ()) {
120- ApplicationMail .MailText text = texts .get (locale );
121- text .setSubject (text .getSubject ().replace ("{groupName}" , newOrganization .getShortName ()));
122- text .setText (text .getText ().replace ("{groupName}" , newOrganization .getShortName ()));
123- }
124- registrar .getMailManager ().updateMailById (session , mail );
125- }
114+ // make requestor a member and admin of new organization
115+ perun .getGroupsManagerBl ().addMember (session , organizationMembersGroup , member );
116+ perun .getGroupsManagerBl ().addMember (session , organizationAdminsGroup , member );
117+
118+ // copy registration form and notifications from template
119+ copyFormAndNotifications (session , newOrganization .getShortName (), getTemplateGroup (session , vo ),organizationMembersGroup );
120+ copyFormAndNotifications (session , newOrganization .getShortName (), getAdminTemplateGroup (session , vo ), organizationAdminsGroup );
126121
122+ // set specific module for the admins group
123+ ApplicationForm newForm = null ;
124+ try {
125+ newForm = registrar .getFormForGroup (organizationAdminsGroup );
126+ newForm .setModuleClassName ("EscPithiaOrganizationAdmins" );
127+ registrar .updateForm (session , newForm );
127128 } catch (PerunException e ) {
128- throw new InternalErrorException ("Unable to set registration form and notifications to group - organization !" , e );
129+ throw new InternalErrorException ("Can't set registration module to the admins groups !" , e );
129130 }
130131
131- // TODO - copy group settings
132- Group templateGroup = getTemplateGroup (session , vo );
132+ // copy group settings from template
133133
134- copySettings (perun , session , templateGroup ,newOrganization ,AttributesManager .NS_GROUP_ATTR_DEF +":blockManualMemberAdding" );
135- // not necessary since default from VO is used
136- //copySettings(perun, session, templateGroup,newOrganization,AttributesManager.NS_GROUP_ATTR_DEF+":registrarURL");
134+ Group templateGroup = getTemplateGroup (session , vo );
135+ copySettings (perun , session , templateGroup , organizationMembersGroup ,AttributesManager .NS_GROUP_ATTR_DEF +":blockManualMemberAdding" );
137136 // handle replace in attribute value
138137 Attribute attribute = perun .getAttributesManagerBl ().getAttribute (session , templateGroup , AttributesManager .NS_GROUP_ATTR_DEF +":applicationURL" );
139- attribute .setValue (attribute .valueAsString ().replace ("{groupName}" , URLEncoder .encode (newOrganization . getShortName (), StandardCharsets .UTF_8 )));
140- perun .getAttributesManagerBl ().setAttribute (session , newOrganization , attribute );
138+ attribute .setValue (attribute .valueAsString ().replace ("{groupName}" , URLEncoder .encode (organizationMembersGroup . getName (), StandardCharsets .UTF_8 )));
139+ perun .getAttributesManagerBl ().setAttribute (session , organizationMembersGroup , attribute );
141140
141+ Group adminTemplateGroup = getTemplateGroup (session , vo );
142+ copySettings (perun , session , adminTemplateGroup , organizationAdminsGroup ,AttributesManager .NS_GROUP_ATTR_DEF +":blockManualMemberAdding" );
143+ // handle replace in attribute value
144+ Attribute attribute2 = perun .getAttributesManagerBl ().getAttribute (session , adminTemplateGroup , AttributesManager .NS_GROUP_ATTR_DEF +":applicationURL" );
145+ attribute2 .setValue (attribute2 .valueAsString ().replace ("{groupName}" , URLEncoder .encode (organizationAdminsGroup .getName (), StandardCharsets .UTF_8 )));
146+ perun .getAttributesManagerBl ().setAttribute (session , organizationAdminsGroup , attribute2 );
142147
143148 // remove group member in the end, so they can ask for more organizations later
144149 perun .getGroupsManagerBl ().removeMember (session , group , member );
@@ -180,9 +185,55 @@ private Group getTemplateGroup(PerunSession session, Vo vo) throws PrivilegeExce
180185
181186 }
182187
188+ private Group getAdminTemplateGroup (PerunSession session , Vo vo ) throws PrivilegeException , RegistrarException , GroupNotExistsException {
189+
190+ PerunBl perun = (PerunBl )session .getPerun ();
191+ return perun .getGroupsManagerBl ().getGroupByName (session , vo , "organizationTemplateAdmins" );
192+
193+ }
194+
183195 private void copySettings (PerunBl perun , PerunSession session , Group templateGroup , Group newOrganization , String attributeName ) throws WrongAttributeAssignmentException , AttributeNotExistsException , WrongReferenceAttributeValueException , WrongAttributeValueException {
184196 Attribute attribute = perun .getAttributesManagerBl ().getAttribute (session , templateGroup , attributeName );
185197 perun .getAttributesManagerBl ().setAttribute (session , newOrganization , attribute );
186198 }
187199
200+ private void copyFormAndNotifications (PerunSession session , String organizationName , Group templateGroup , Group destinationGroup ) {
201+
202+ // copy registration form and notifications from template
203+ try {
204+ registrar .createApplicationFormInGroup (session , destinationGroup );
205+ registrar .copyFormFromGroupToGroup (session , templateGroup , destinationGroup );
206+ registrar .getMailManager ().copyMailsFromGroupToGroup (session , templateGroup , destinationGroup );
207+
208+ ApplicationForm newForm = registrar .getFormForGroup (destinationGroup );
209+
210+ List <ApplicationFormItem > items = registrar .getFormItems (session , newForm );
211+ for (ApplicationFormItem item : items ) {
212+ if ("header" .equals (item .getShortname ())) {
213+ Map <Locale , ApplicationFormItem .ItemTexts > i18n = item .getI18n ();
214+ for (Locale locale : i18n .keySet ()) {
215+ ApplicationFormItem .ItemTexts text = i18n .get (locale );
216+ text .setLabel (text .getLabel ().replace ("{groupName}" , organizationName ));
217+ }
218+ registrar .updateFormItem (session , item );
219+ }
220+ }
221+
222+ List <ApplicationMail > mails = registrar .getMailManager ().getApplicationMails (session ,newForm );
223+ for (ApplicationMail mail : mails ) {
224+ Map <Locale , ApplicationMail .MailText > texts = mail .getMessage ();
225+ for (Locale locale : texts .keySet ()) {
226+ ApplicationMail .MailText text = texts .get (locale );
227+ text .setSubject (text .getSubject ().replace ("{groupName}" , organizationName ));
228+ text .setText (text .getText ().replace ("{groupName}" , organizationName ));
229+ }
230+ registrar .getMailManager ().updateMailById (session , mail );
231+ }
232+
233+ } catch (PerunException e ) {
234+ throw new InternalErrorException ("Unable to set registration form and notifications to group - organization!" , e );
235+ }
236+
237+ }
238+
188239}
0 commit comments