|
11 | 11 | import com.marklogic.mgmt.resource.security.RoleManager;
|
12 | 12 |
|
13 | 13 | import java.io.File;
|
| 14 | +import java.util.HashSet; |
| 15 | +import java.util.Set; |
14 | 16 |
|
15 | 17 | public class DeployRolesCommand extends AbstractResourceCommand {
|
16 | 18 |
|
17 | 19 | // Used internally
|
18 | 20 | private boolean removeRolesAndPermissionsDuringDeployment = false;
|
19 | 21 | private ResourceMapper resourceMapper;
|
| 22 | + private Set<String> roleNamesThatDontNeedToBeRedeployed; |
20 | 23 |
|
21 | 24 | public DeployRolesCommand() {
|
22 | 25 | setExecuteSortOrder(SortOrderConstants.DEPLOY_ROLES);
|
23 | 26 | setUndoSortOrder(SortOrderConstants.DELETE_ROLES);
|
24 | 27 | }
|
25 | 28 |
|
26 | 29 | /**
|
27 |
| - * The set of roles is processed twice. The first time, the roles are saved without any permissions or dependent roles. |
28 |
| - * This is to avoid issues where the roles depend on each other or on themselves. The second time, the roles are |
29 |
| - * saved with permissions and dependent roles, which is guaranteed to work now that the roles have all been created. |
| 30 | + * The set of roles is processed twice. The first time, the roles are saved without any default permissions or references to other roles. |
| 31 | + * This is to avoid issues where the roles refer to each other or to themselves (via default permissions). The second time, the roles are |
| 32 | + * saved with permissions and references to other roles, which is guaranteed to work now that the roles have all been created. |
30 | 33 | *
|
31 | 34 | * @param context
|
32 | 35 | */
|
33 | 36 | @Override
|
34 | 37 | public void execute(CommandContext context) {
|
35 | 38 | removeRolesAndPermissionsDuringDeployment = true;
|
36 | 39 | if (logger.isInfoEnabled()) {
|
37 |
| - logger.info("Deploying roles without any permissions or dependent roles"); |
| 40 | + logger.info("Deploying roles minus their default permissions and references to roles"); |
38 | 41 | }
|
| 42 | + roleNamesThatDontNeedToBeRedeployed = new HashSet<>(); |
39 | 43 | super.execute(context);
|
40 | 44 | if (logger.isInfoEnabled()) {
|
41 |
| - logger.info("Deploying roles with permissions and dependent roles"); |
| 45 | + logger.info("Redeploying roles that have default permissions and/or references to roles"); |
42 | 46 | }
|
43 | 47 | removeRolesAndPermissionsDuringDeployment = false;
|
44 | 48 | super.execute(context);
|
45 | 49 | }
|
46 | 50 |
|
| 51 | + /** |
| 52 | + * If this is the first time roles are being deployed by this command - indicated by the removeRolesAndPermissionsDuringDeployment |
| 53 | + * class variable - then each payload is modified so that default permissions and role references are not included, |
| 54 | + * thus ensuring that the role can be created successfully. |
| 55 | + * |
| 56 | + * If this is the second time that roles are being deployed by this command, then the entire payload is sent. However, |
| 57 | + * if the role doesn't have any default permissions or role references, it will not be deployed a second time, as |
| 58 | + * there was nothing missing from the first deployment of the role. |
| 59 | + * |
| 60 | + * @param mgr |
| 61 | + * @param context |
| 62 | + * @param f |
| 63 | + * @param payload |
| 64 | + * @return |
| 65 | + */ |
47 | 66 | @Override
|
48 | 67 | protected String adjustPayloadBeforeSavingResource(ResourceManager mgr, CommandContext context, File f, String payload) {
|
49 | 68 | payload = super.adjustPayloadBeforeSavingResource(mgr, context, f, payload);
|
| 69 | + |
| 70 | + if (resourceMapper == null) { |
| 71 | + API api = new API(context.getManageClient(), context.getAdminManager()); |
| 72 | + resourceMapper = new DefaultResourceMapper(api); |
| 73 | + } |
| 74 | + |
| 75 | + Role role = resourceMapper.readResource(payload, Role.class); |
| 76 | + |
| 77 | + // Is this the first time the roles are being deployed? |
50 | 78 | if (removeRolesAndPermissionsDuringDeployment) {
|
51 |
| - if (resourceMapper == null) { |
52 |
| - API api = new API(context.getManageClient(), context.getAdminManager()); |
53 |
| - resourceMapper = new DefaultResourceMapper(api); |
| 79 | + if (role.hasPermissionsOrRoles()) { |
| 80 | + role.clearPermissionsAndRoles(); |
| 81 | + return role.getJson(); |
| 82 | + } else { |
| 83 | + roleNamesThatDontNeedToBeRedeployed.add(role.getRoleName()); |
| 84 | + return payload; |
| 85 | + } |
| 86 | + } |
| 87 | + // Else it's the second time roles are being deployed, but no need to deploy a role if it doesn't have any default permissions or role references |
| 88 | + else if (roleNamesThatDontNeedToBeRedeployed.contains(role.getRoleName())) { |
| 89 | + if (logger.isInfoEnabled()) { |
| 90 | + logger.info("Not redeploying role " + role.getRoleName() + ", as it does not have any default permissions or references to other roles"); |
54 | 91 | }
|
55 |
| - Role role = resourceMapper.readResource(payload, Role.class); |
56 |
| - role.clearPermissionsAndRoles(); |
57 |
| - return role.getJson(); |
| 92 | + return null; |
| 93 | + } |
| 94 | + // Else log a message to indicate that the role is being redeployed |
| 95 | + else if (logger.isInfoEnabled()) { |
| 96 | + logger.info("Redeploying role " + role.getRoleName() + " with default permissions and references to other roles included"); |
58 | 97 | }
|
| 98 | + |
59 | 99 | return payload;
|
60 | 100 | }
|
61 | 101 |
|
|
0 commit comments