001package de.deepamehta.accesscontrol.migrations;
002
003import de.deepamehta.accesscontrol.AccessControlService;
004import de.deepamehta.config.ConfigService;
005import de.deepamehta.workspaces.WorkspacesService;
006
007import de.deepamehta.core.Topic;
008import de.deepamehta.core.service.Inject;
009import de.deepamehta.core.service.Migration;
010
011import java.util.List;
012import java.util.logging.Logger;
013
014
015
016/**
017 * Creates the "Administration" workspace.
018 * Reassigns existing "Login enabled" and "Disk Quota" config topics to the "Administration" workspace.
019 * Creates "Enabled Sharing Modes" config topics for existing usernames.
020 * <p>
021 * Runs only in UPDATE mode.
022 * <p>
023 * Note: when CLEAN_INSTALLing DM 4.8 the "Administration" workspace is already created in migration 4, and the
024 * config topics are already assigned to the "Administration" workspace (as the Config service is already in charge
025 * when the "admin" user account is created, in migration 10).
026 * <p>
027 * The "Administration" workspace must exist before the "admin" user account is created (migration 10).
028 * <p>
029 * Part of DM 4.8
030 */
031public class Migration12 extends Migration {
032
033    // ---------------------------------------------------------------------------------------------- Instance Variables
034
035    @Inject private AccessControlService acService;
036    @Inject private WorkspacesService wsService;
037    @Inject private ConfigService configService;
038
039    private long administrationWorkspaceId;
040
041    private Logger logger = Logger.getLogger(getClass().getName());
042
043    // -------------------------------------------------------------------------------------------------- Public Methods
044
045    @Override
046    public void run() {
047        // 1) create "Administration" workspace (Note: there is a copy in migration 4)
048        Topic systemWorkspace = wsService.createWorkspace(
049            AccessControlService.ADMINISTRATION_WORKSPACE_NAME,
050            AccessControlService.ADMINISTRATION_WORKSPACE_URI,
051            AccessControlService.ADMINISTRATION_WORKSPACE_SHARING_MODE
052        );
053        // Note: at migration running time our plugin listeners are not yet registered
054        // (furthermore there is no user logged in). So we set the owner manually here.
055        acService.setWorkspaceOwner(systemWorkspace, AccessControlService.ADMIN_USERNAME);
056        // Note: we don't set a particular creator/modifier here as we don't want suggest that the Administration
057        // workspace has been created by the "admin" user. Instead the creator/modifier of the Administration
058        // workspace remain undefined as the Administration workspace is actually created by the system itself.
059        //
060        // 2) reassign existing "Login enabled" and "Disk Quota" config topics
061        administrationWorkspaceId = dm4.getAccessControl().getAdministrationWorkspaceId();
062        assignConfigTopics("dm4.accesscontrol.login_enabled");
063        assignConfigTopics("dm4.files.disk_quota");
064        // Note: the "Disk Quota" config topics are reassigned to the Administration workspace which is created
065        // only here. The "Disk Quota" config type belongs to the Files plugin and the reassignment is supposed
066        // to be performed by a Files migration. But the Files migrations run *before* the Access Control migrations
067        // (as the Access Control plugin depends indirectly on the Files plugin).
068        //
069        // 3) create "Enabled Sharing Modes" config topics
070        List<Topic> usernames = dm4.getTopicsByType("dm4.accesscontrol.username");
071        logger.info("########## Creating config topics of type \"dm4.workspaces.enabled_sharing_modes\" for " +
072            usernames.size() + " usernames");
073        for (Topic username : usernames) {
074            configService.createConfigTopic("dm4.workspaces.enabled_sharing_modes", username);
075        }
076        // Note: the "Enabled Sharing Modes" config topics are assigned to the Administration workspace which is
077        // created only here. The "Enabled Sharing Modes" config type belongs to the Workspaces plugin and the
078        // config topics are supposed to be created by a Workspaces migration. But the Workspaces migrations run
079        // *before* the Access Control migrations (as the Access Control plugin depends on the Workspaces plugin).
080    }
081
082    // ------------------------------------------------------------------------------------------------- Private Methods
083
084    private void assignConfigTopics(String configTypeUri) {
085        List<Topic> configTopics = dm4.getTopicsByType(configTypeUri);
086        logger.info("########## Reassigning " + configTopics.size() + " config topics of type \"" + configTypeUri +
087            "\" to workspace \"Administration\"");
088        for (Topic configTopic : configTopics) {
089            wsService.assignToWorkspace(configTopic, administrationWorkspaceId);
090        }
091    }
092}