Cq62MediaContentUpgrade.java
4.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
/*
* Decompiled with CFR 0_118.
*
* Could not load the following classes:
* javax.jcr.Node
* javax.jcr.RepositoryException
* javax.jcr.Session
* org.apache.jackrabbit.api.JackrabbitSession
* org.apache.jackrabbit.api.security.user.Authorizable
* org.apache.jackrabbit.api.security.user.Group
* org.apache.jackrabbit.api.security.user.UserManager
* org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils
* org.slf4j.Logger
* org.slf4j.LoggerFactory
*/
package com.day.cq.compat.codeupgrade.impl.cq62;
import com.day.cq.compat.codeupgrade.internal.api.ProgressInfoProvider;
import java.security.Principal;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.apache.jackrabbit.api.JackrabbitSession;
import org.apache.jackrabbit.api.security.user.Authorizable;
import org.apache.jackrabbit.api.security.user.Group;
import org.apache.jackrabbit.api.security.user.UserManager;
import org.apache.jackrabbit.commons.jackrabbit.authorization.AccessControlUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
public class Cq62MediaContentUpgrade
implements ProgressInfoProvider {
private static final String MEDIA_EDITORS = "mp-editors";
private static final String MEDIA_CONTRIBUTORS = "mp-contributors";
private static final String AUTHORS = "content-authors";
private static final String DPS_CLOUD_PATH = "/etc/cloudservices/dps";
private static final String PUBLICATIONS_PATH = "/content/publications";
private static final Logger LOGGER = LoggerFactory.getLogger((String)Cq62MediaContentUpgrade.class.getName());
private static final String[] DPS_CLOUD_PRIVILEGES = new String[]{"jcr:read", "rep:write"};
private static final String[] EDITOR_PUBLICATIONS_PRIVILEGES = new String[]{"jcr:read", "rep:write", "jcr:lockManagement", "jcr:versionManagement"};
private String progressInfo;
@Override
public String getProgressInfo() {
return this.progressInfo;
}
void setProgressInfo(String info) {
this.progressInfo = info;
LOGGER.info(this.progressInfo);
}
void doUpgrade(Session session) throws RepositoryException {
Group contentAuthorsGroup;
UserManager userManager = ((JackrabbitSession)session).getUserManager();
Authorizable mpEditors = userManager.getAuthorizable("mp-editors");
Authorizable mpContributors = userManager.getAuthorizable("mp-contributors");
if (mpEditors == null) {
LOGGER.debug("Unable to find the mp-editors group, therefore no upgrade required");
} else if (mpEditors.isGroup()) {
this.setPrivilegesForAuthorizable(session, mpEditors, "/etc/cloudservices/dps", DPS_CLOUD_PRIVILEGES);
this.setPrivilegesForAuthorizable(session, mpEditors, "/content/publications", EDITOR_PUBLICATIONS_PRIVILEGES);
this.setPrivilegesForAuthorizable(session, mpContributors, "/content/publications", null);
}
Authorizable contentAuthors = userManager.getAuthorizable("content-authors");
if (contentAuthors != null && contentAuthors.isGroup() && !(contentAuthorsGroup = (Group)contentAuthors).isMember(mpEditors)) {
this.setProgressInfo(String.format("adding mp-editors to the authors group", new Object[0]));
contentAuthorsGroup.addMember(mpEditors);
}
session.save();
this.setProgressInfo("All done.");
}
private void setPrivilegesForAuthorizable(Session session, Authorizable authorizable, String path, String[] privileges) {
try {
this.setProgressInfo(String.format("starting to migrating %s ACLs", path));
Node node = session.getNode(path);
if (node != null) {
AccessControlUtils.clear((Node)node, (String)authorizable.getPrincipal().getName());
if (privileges != null) {
AccessControlUtils.addAccessControlEntry((Session)session, (String)path, (Principal)authorizable.getPrincipal(), (String[])privileges, (boolean)true);
}
}
this.setProgressInfo(String.format("finsihed migrating %s ACLs", path));
}
catch (RepositoryException e) {
LOGGER.warn(String.format("Unable to located %s, skipping migration", path));
}
}
}