View Javadoc
1   /**
2    * This file Copyright (c) 2008-2017 Magnolia International
3    * Ltd.  (http://www.magnolia-cms.com). All rights reserved.
4    *
5    *
6    * This file is dual-licensed under both the Magnolia
7    * Network Agreement and the GNU General Public License.
8    * You may elect to use one or the other of these licenses.
9    *
10   * This file is distributed in the hope that it will be
11   * useful, but AS-IS and WITHOUT ANY WARRANTY; without even the
12   * implied warranty of MERCHANTABILITY or FITNESS FOR A
13   * PARTICULAR PURPOSE, TITLE, or NONINFRINGEMENT.
14   * Redistribution, except as permitted by whichever of the GPL
15   * or MNA you select, is prohibited.
16   *
17   * 1. For the GPL license (GPL), you can redistribute and/or
18   * modify this file under the terms of the GNU General
19   * Public License, Version 3, as published by the Free Software
20   * Foundation.  You should have received a copy of the GNU
21   * General Public License, Version 3 along with this program;
22   * if not, write to the Free Software Foundation, Inc., 51
23   * Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
24   *
25   * 2. For the Magnolia Network Agreement (MNA), this file
26   * and the accompanying materials are made available under the
27   * terms of the MNA which accompanies this distribution, and
28   * is available at http://www.magnolia-cms.com/mna.html
29   *
30   * Any modifications to this file must keep this entire header
31   * intact.
32   *
33   */
34  package info.magnolia.module.demoproject.setup;
35  
36  import info.magnolia.module.DefaultModuleVersionHandler;
37  import info.magnolia.module.InstallContext;
38  import info.magnolia.module.delta.AbstractRepositoryTask;
39  import info.magnolia.module.delta.AddGroupToGroupTask;
40  import info.magnolia.module.delta.AddPermissionTask;
41  import info.magnolia.module.delta.AddRoleToGroupTask;
42  import info.magnolia.module.delta.AddRoleToUserTask;
43  import info.magnolia.module.delta.ArrayDelegateTask;
44  import info.magnolia.module.delta.BootstrapConditionally;
45  import info.magnolia.module.delta.BootstrapSingleResource;
46  import info.magnolia.module.delta.DeltaBuilder;
47  import info.magnolia.module.delta.IsAuthorInstanceDelegateTask;
48  import info.magnolia.module.delta.IsModuleInstalledOrRegistered;
49  import info.magnolia.module.delta.ModuleDependencyBootstrapTask;
50  import info.magnolia.module.delta.NodeExistsDelegateTask;
51  import info.magnolia.module.delta.OrderNodeBeforeTask;
52  import info.magnolia.module.delta.PartialBootstrapTask;
53  import info.magnolia.module.delta.SetDefaultPublicURITask;
54  import info.magnolia.module.delta.Task;
55  import info.magnolia.module.delta.TaskExecutionException;
56  import info.magnolia.module.forum.DefaultForumManager;
57  import info.magnolia.module.model.Version;
58  import info.magnolia.repository.RepositoryConstants;
59  
60  import java.util.ArrayList;
61  import java.util.List;
62  
63  import javax.jcr.ImportUUIDBehavior;
64  import javax.jcr.RepositoryException;
65  
66  /**
67   * Module version handler class of the demo project.
68   */
69  public class DemoProjectVersionHandler extends DefaultModuleVersionHandler {
70  
71      protected static final String GROUP_DEMO_PROJECT_EDITORS = "demo-project-editors";
72      protected static final String GROUP_DEMO_PROJECT_PUBLISHERS = "demo-project-publishers";
73      protected static final String USER_PETER = "peter";
74      protected static final String USER_ERIC = "eric";
75  
76      public DemoProjectVersionHandler() {
77  
78          register(DeltaBuilder.checkPrecondition("3.0", "3.1"));
79  
80          register(DeltaBuilder.update("3.0.1", "")
81                  .addTask(new NodeExistsDelegateTask("Reinstall PUR related contents on registration page", "", RepositoryConstants.WEBSITE, "/demo-project/members-area/registration",
82                          new PartialBootstrapTask("", "", "/mgnl-bootstrap/demo-project/website.demo-project.xml", "/demo-project/members-area/registration/content/0", ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING)))
83                  .addTask(new NodeExistsDelegateTask("Reinstall PUR related double-opt-in page", "", RepositoryConstants.WEBSITE, "/demo-project/members-area/registration/double-opt-in-confirmation",
84                          new PartialBootstrapTask("", "", "/mgnl-bootstrap/demo-project/website.demo-project.xml", "/demo-project/members-area/registration/double-opt-in-confirmation", ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING)))
85                  .addTask(new IsModuleInstalledOrRegistered("Reinstall PUR configuration for demo-project", "public-user-registration",
86                          new BootstrapSingleResource("Reinstall PUR configuration for demo-project", "Update passwordRetrievalStrategy configuration of PUR", "/mgnl-bootstrap/public-user-registration/config.modules.public-user-registration.config.configurations.demo-project.xml", ImportUUIDBehavior.IMPORT_UUID_COLLISION_REPLACE_EXISTING)))
87          );
88      }
89  
90      @Override
91      protected List<Task> getDefaultUpdateTasks(Version forVersion) {
92          final List<Task> tasks = new ArrayList<Task>();
93          tasks.addAll(super.getDefaultUpdateTasks(forVersion));
94          tasks.add(new CreateAllDummyEventsTask("Creating dummy events for demo-project", "Creating different dummy Event pages for the Events-Overview page as sample events.", "/demo-project/service/dummy-events"));
95          tasks.add(new CreateAllDummyEventsTask("Creating dummy events for demo-features", "Creating different dummy Event pages for the Events-Overview page as sample events.", "/demo-features/aggregation-paragraphs/dummy-events"));
96          return tasks;
97      }
98  
99      @Override
100     protected List<Task> getExtraInstallTasks(InstallContext ctx) {
101         final List<Task> tasks = new ArrayList<Task>();
102         tasks.add(installDemoUsers);
103         tasks.addAll(super.getExtraInstallTasks(ctx));
104         tasks.add(new RemoveETKContentInCE());
105         tasks.add(new BootstrapETKDataTask());
106         tasks.add(new BootstrapMultiSiteDataTask());
107         tasks.add(getPurConfigurationChanges());
108         tasks.add(addSiteAwarePermissionToAnonymousRoleToDenyAccessToMembersArea());
109         tasks.add(orderClientCallback());
110         tasks.add(new PersistSecurityChanges());
111         tasks.add(addImagingBaseRoleToDemoGroups());
112         tasks.add(addEditorsGroupToDemoProjectEditorsGroup());
113         tasks.add(new CreateAllDummyEventsTask("Creating dummy events for demo-project", "Creating different dummy Event pages for the Events-Overview page as sample events.", "/demo-project/service/dummy-events"));
114         tasks.add(new CreateAllDummyEventsTask("Creating dummy events for demo-features", "Creating different dummy Event pages for the Events-Overview page as sample events.", "/demo-features/aggregation-paragraphs/dummy-events"));
115         tasks.add(new BootstrapConditionally("Blogs", "Bootstrap rss data - Magnolia blogs", "/mgnl-bootstrap/demo-project-rssaggregator/rss.MagnoliaBlogs.xml"));
116         tasks.add(new BootstrapConditionally("News", "Bootstrap rss data - news", "/mgnl-bootstrap/demo-project-rssaggregator/rss.news.xml"));
117         tasks.add(new ModuleDependencyBootstrapTask("contacts"));
118         tasks.add(new SetDefaultPublicURITask("defaultPublicURI"));
119 
120         return tasks;
121     }
122 
123     private Task installDemoUsers = new ArrayDelegateTask("Reinstall demo users",
124             //users with same name might be already installed with travel-demo
125             new BootstrapConditionally("Install user eric", "Bootstrap sample demo editor if doesn't exist or add him to the group: " + GROUP_DEMO_PROJECT_EDITORS,
126                     "/info/magnolia/module/demo-project/setup/users/users.admin.eric.xml", new AddUserToGroupIfNotInTask(USER_ERIC, GROUP_DEMO_PROJECT_EDITORS)),
127             new BootstrapConditionally("Install user peter", "Bootstrap sample demo publisher if doesn't exist or add him to the group: " + GROUP_DEMO_PROJECT_PUBLISHERS,
128                     "/info/magnolia/module/demo-project/setup/users/users.admin.peter.xml",  new AddUserToGroupIfNotInTask(USER_PETER, GROUP_DEMO_PROJECT_PUBLISHERS)),
129             makeEricForumAdmin(),
130             makePeterForumModerator()
131     );
132 
133     private IsModuleInstalledOrRegistered addEditorsGroupToDemoProjectEditorsGroup() {
134         return new IsModuleInstalledOrRegistered("Demo editors group", "Adds the editors group to the subgroups of the demo-project-editors group if the workflow module is installed",
135                 "workflow", new AddGroupToGroupTask("", "editors", "demo-project-editors"));
136     }
137 
138     private Task addImagingBaseRoleToDemoGroups() {
139         return new IsModuleInstalledOrRegistered("Add imaging-base role", "Adds imaging base-role to demo-* groups", "imaging", new ArrayDelegateTask("", "",
140                 new AddRoleToGroupTask("", "imaging-base", "demo-project-editors"),
141                 new AddRoleToGroupTask("", "imaging-base", "demo-project-publishers"),
142                 new AddRoleToGroupTask("", "imaging-base", "demo-project-members")));
143     }
144 
145     private Task getPurConfigurationChanges() {
146         return new IsAuthorInstanceDelegateTask("Update anonymous user", "Denies access to the protected member area.",
147                 null, // nothing to do on an author instance
148                 new AddPermissionTask(null, null, "anonymous", "uri", "/demo-project/members-area/protected*", 0, false));
149     }
150 
151     private Task addSiteAwarePermissionToAnonymousRoleToDenyAccessToMembersArea() {
152         return new IsAuthorInstanceDelegateTask("Update anonymous user", "Denies access to the protected member area for site-aware check.",
153                 null, // nothing to do on an author instance
154                 new AddPermissionTask(null, null, "anonymous", "uri", "<demo-project>/members-area/protected*", 0, false));
155     }
156 
157     private Task orderClientCallback() {
158         return new OrderNodeBeforeTask("Order callbacks.", "Order public callback before form to not be ignored when callback condition is met.", RepositoryConstants.CONFIG, "/server/filters/securityCallback/clientCallbacks/public", "form");
159     }
160 
161     /**
162      * TODO - this is a temp fix for MGNLSTK-608, until MAGNOLIA-3147 is fixed.
163      */
164     private static final class PersistSecurityChanges extends AbstractRepositoryTask {
165 
166         private PersistSecurityChanges() {
167             super("Save security data", "Security classes normally save the changes immediately which fails in cased the modified node (group, role or user) is a new node.");
168         }
169 
170         @Override
171         protected void doExecute(InstallContext installContext) throws RepositoryException, TaskExecutionException {
172             installContext.getJCRSession(RepositoryConstants.USERS).save();
173             installContext.getJCRSession(RepositoryConstants.USER_GROUPS).save();
174             installContext.getJCRSession(RepositoryConstants.USER_ROLES).save();
175         }
176     }
177 
178     private Task makePeterForumModerator() {
179         Task grantRolesForPeter = new AddRoleToUserTask("Grant forum_ALL-moderator to Peter.", "peter", DefaultForumManager.ROLE_FORUM_ALL_MODERATOR);
180         return new NodeExistsDelegateTask("Grant Peter some roles to moderate if he exists.", "Granting peter some roles to moderate if he exists.", RepositoryConstants.USERS, "/admin/peter", grantRolesForPeter);
181     }
182 
183     private Task makeEricForumAdmin() {
184         Task grantRolesForEric = new AddRoleToUserTask("Grant  forum_ALL-admin to Eric.", "eric", DefaultForumManager.ROLE_FORUM_ALL_ADMIN);
185         return new NodeExistsDelegateTask("Grant Eric some roles to moderate if he exists.", "Granting Eric some roles to moderate if he exists.", RepositoryConstants.USERS, "/admin/eric", grantRolesForEric);
186     }
187 }