This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Index: ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/context/SimpleContext.java | |
| IDEA additional info: | |
| Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
| <+>UTF-8 | |
| =================================================================== | |
| --- ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/context/SimpleContext.java (revision 47e28b95d4660e1d8e7916466f07ead711a24869) | |
| +++ ../../git/magnolia-5.4-repo/main/magnolia-core/src/main/java/info/magnolia/context/SimpleContext.java (revision ) | |
| @@ -56,6 +56,7 @@ | |
| * The context used to get hierarchy managers or similar. | |
| */ | |
| private Context ctx; | |
| + private User user; | |
| /** | |
| * Uses current instance of <code>MgnlContext</code> at the time of creation as it's internal reference context. | |
| @@ -76,6 +77,11 @@ | |
| } | |
| } | |
| + public SimpleContext(Map<String, Object> map, User user) { | |
| + this(map); | |
| + this.user = user; | |
| + } | |
| + | |
| /** | |
| * Delegate to the inner context. | |
| */ | |
| @@ -102,7 +108,11 @@ | |
| */ | |
| @Override | |
| public User getUser() { | |
| + if (this.user == null) { | |
| - return this.ctx.getUser(); | |
| + return this.ctx.getUser(); | |
| + } else { | |
| + return this.user; | |
| + } | |
| } | |
| /** |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Index: ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/main/java/info/magnolia/module/scheduler/SchedulerConsts.java | |
| IDEA additional info: | |
| Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
| <+>UTF-8 | |
| =================================================================== | |
| --- ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/main/java/info/magnolia/module/scheduler/SchedulerConsts.java (revision 2fc765187707e88dc199b18cc20a1e375ed27ed1) | |
| +++ ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/main/java/info/magnolia/module/scheduler/SchedulerConsts.java (revision ) | |
| @@ -33,6 +33,7 @@ | |
| */ | |
| package info.magnolia.module.scheduler; | |
| + | |
| /** | |
| * Some useful constants used throughout the module. | |
| */ | |
| @@ -68,6 +69,11 @@ | |
| * The command name. | |
| */ | |
| static final String CONFIG_JOB_COMMAND = "command"; | |
| + | |
| + /** | |
| + * Name of the user that has launched the command. | |
| + */ | |
| + static final String ATTRIBUTE_REQUESTOR = info.magnolia.context.Context.ATTRIBUTE_REQUESTOR; | |
| /** | |
| * The group name we use to work with the scheduler. | |
| Index: ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/test/java/info/magnolia/module/scheduler/CommandJobTest.java | |
| IDEA additional info: | |
| Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
| <+>UTF-8 | |
| =================================================================== | |
| --- ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/test/java/info/magnolia/module/scheduler/CommandJobTest.java (revision 2fc765187707e88dc199b18cc20a1e375ed27ed1) | |
| +++ ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/test/java/info/magnolia/module/scheduler/CommandJobTest.java (revision ) | |
| @@ -37,7 +37,9 @@ | |
| import static org.junit.Assert.*; | |
| import static org.mockito.Mockito.*; | |
| +import info.magnolia.cms.security.SecuritySupport; | |
| import info.magnolia.cms.security.User; | |
| +import info.magnolia.cms.security.UserManager; | |
| import info.magnolia.commands.CommandsManager; | |
| import info.magnolia.commands.chain.Command; | |
| import info.magnolia.commands.chain.Context; | |
| @@ -51,6 +53,8 @@ | |
| import info.magnolia.test.mock.MockWebContext; | |
| import java.io.IOException; | |
| +import java.util.HashMap; | |
| +import java.util.Map; | |
| import org.junit.After; | |
| import org.junit.Before; | |
| @@ -68,6 +72,7 @@ | |
| private MagnoliaConfigurationProperties configurationProperties; | |
| private CommandsManager commandsManager; | |
| private User user; | |
| + private UserManager userManager; | |
| @Before | |
| public void setUp() throws IOException { | |
| @@ -82,9 +87,14 @@ | |
| when(user.getName()).thenReturn("foo"); | |
| ctx.setUser(user); | |
| + SecuritySupport securitySupport = mock(SecuritySupport.class); | |
| + userManager = mock(UserManager.class); | |
| + when(securitySupport.getUserManager()).thenReturn(userManager); | |
| + | |
| ComponentsTestUtil.setInstance(MagnoliaConfigurationProperties.class, configurationProperties); | |
| ComponentsTestUtil.setInstance(SystemContext.class, ctx); | |
| ComponentsTestUtil.setInstance(CommandsManager.class, commandsManager); | |
| + ComponentsTestUtil.setInstance(SecuritySupport.class, securitySupport); | |
| } | |
| @After | |
| @@ -212,6 +222,60 @@ | |
| verify(jobCtx).setResult(argumentCaptor.capture()); | |
| assertFalse(argumentCaptor.getValue().isSuccess()); | |
| assertThat(argumentCaptor.getValue().getException(), is(exception)); | |
| + } | |
| + } | |
| + | |
| + @Test | |
| + public void testUserInCommandJobIsCorrectUser() throws JobExecutionException { | |
| + // GIVEN | |
| + CommandJob cj = new CommandJob(); | |
| + final Exception exception = new Exception(); | |
| + JobDataMap jobDataMap = new JobDataMap(); | |
| + jobDataMap.put(SchedulerConsts.CONFIG_JOB_COMMAND, "testCommand"); | |
| + jobDataMap.put(SchedulerConsts.CONFIG_JOB_COMMAND_CATALOG, "testCatalog"); | |
| + Map params = new HashMap<String, Object>(); | |
| + params.put(SchedulerConsts.ATTRIBUTE_REQUESTOR, "eric"); | |
| + jobDataMap.put(SchedulerConsts.CONFIG_JOB_PARAMS, params); | |
| + | |
| + MockContext context = new MockContext(); | |
| + User user = mock(User.class); | |
| + when(user.getName()).thenReturn("eric"); | |
| + context.setUser(user); | |
| + MgnlContext.setInstance(context); | |
| + when(userManager.getUser("eric")).thenReturn(user); | |
| + | |
| + final String username = MgnlContext.getUser().getName(); | |
| + | |
| + when(jobCtx.getJobDetail()).thenReturn(jobDetail); | |
| + when(jobDetail.getName()).thenReturn("Test Job"); | |
| + when(jobCtx.getJobDetail().getJobDataMap()).thenReturn(jobDataMap); | |
| + when(commandsManager.getCommand("testCatalog", "testCommand")).thenReturn(new Command() { | |
| + | |
| + | |
| + @Override | |
| + public boolean execute(Context context) throws Exception { | |
| + if (MgnlContext.getUser().getName().equalsIgnoreCase(username)) { | |
| + return true; | |
| + } | |
| + throw exception; | |
| + } | |
| + | |
| + @Override | |
| + public Command clone() throws CloneNotSupportedException { | |
| + return null; | |
| + } | |
| + }); | |
| + | |
| + // WHEN | |
| + final ArgumentCaptor<CommandJob.JobResult> argumentCaptor = | |
| + ArgumentCaptor.forClass(CommandJob.JobResult.class); | |
| + | |
| + // THEN | |
| + try { | |
| + cj.execute(jobCtx); | |
| + } catch (JobExecutionException e) { | |
| + // THEN | |
| + fail("user context is not correct " + username); | |
| } | |
| } | |
| } | |
| Index: ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/main/java/info/magnolia/module/scheduler/CommandJob.java | |
| IDEA additional info: | |
| Subsystem: com.intellij.openapi.diff.impl.patch.CharsetEP | |
| <+>UTF-8 | |
| =================================================================== | |
| --- ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/main/java/info/magnolia/module/scheduler/CommandJob.java (revision 2fc765187707e88dc199b18cc20a1e375ed27ed1) | |
| +++ ../../git/magnolia-5.4-repo/scheduler/magnolia-module-scheduler/src/main/java/info/magnolia/module/scheduler/CommandJob.java (revision ) | |
| @@ -33,7 +33,9 @@ | |
| */ | |
| package info.magnolia.module.scheduler; | |
| +import info.magnolia.cms.security.SecuritySupport; | |
| import info.magnolia.cms.security.SilentSessionOp; | |
| +import info.magnolia.cms.security.User; | |
| import info.magnolia.commands.CommandsManager; | |
| import info.magnolia.commands.chain.Command; | |
| import info.magnolia.context.Context; | |
| @@ -45,6 +47,7 @@ | |
| import java.util.Map; | |
| +import javax.inject.Inject; | |
| import javax.jcr.Node; | |
| import javax.jcr.RepositoryException; | |
| import javax.jcr.Session; | |
| @@ -64,6 +67,23 @@ | |
| private static Logger log = LoggerFactory.getLogger(CommandJob.class); | |
| + private final SystemContext systemContext; | |
| + private final SecuritySupport securitySupport; | |
| + private final MagnoliaConfigurationProperties magnoliaProperties; | |
| + private final CommandsManager commandsManager; | |
| + | |
| + public CommandJob() { | |
| + this(Components.getComponent(SystemContext.class), SecuritySupport.Factory.getInstance(), Components.getComponent(MagnoliaConfigurationProperties.class), Components.getComponent(CommandsManager.class)); | |
| + } | |
| + | |
| + @Inject | |
| + public CommandJob(SystemContext systemContext, SecuritySupport securitySupport, MagnoliaConfigurationProperties magnoliaProperties, CommandsManager commandsManager) { | |
| + this.systemContext = systemContext; | |
| + this.securitySupport = securitySupport; | |
| + this.magnoliaProperties = magnoliaProperties; | |
| + this.commandsManager = commandsManager; | |
| + } | |
| + | |
| /** | |
| * Called by the scheduler. Get the command. Create a magnolia context. | |
| */ | |
| @@ -72,12 +92,18 @@ | |
| log.info("Starting job [{}]...", jobCtx.getJobDetail().getName()); | |
| Map jobData = jobCtx.getJobDetail().getJobDataMap(); | |
| - String instanceClusterId = Components.getComponent(MagnoliaConfigurationProperties.class).getProperty("magnolia.clusterid"); | |
| + String instanceClusterId = magnoliaProperties.getProperty("magnolia.clusterid"); | |
| String jobClusterId = null; | |
| + User user = null; | |
| if (jobData.containsKey(SchedulerConsts.CONFIG_JOB_PARAMS) && jobData.get(SchedulerConsts.CONFIG_JOB_PARAMS) != null) { | |
| - jobClusterId = (String) ((Map) jobData.get(SchedulerConsts.CONFIG_JOB_PARAMS)).get("clusterId"); | |
| + Map params = (Map) jobData.get(SchedulerConsts.CONFIG_JOB_PARAMS); | |
| + jobClusterId = (String) params.get("clusterId"); | |
| + if (params.containsKey(SchedulerConsts.ATTRIBUTE_REQUESTOR)) { | |
| + String requester = (String) params.get(SchedulerConsts.ATTRIBUTE_REQUESTOR); | |
| + user = securitySupport.getUserManager().getUser(requester); | |
| - } | |
| + } | |
| + } | |
| String catalogName = (String) jobData.get(SchedulerConsts.CONFIG_JOB_COMMAND_CATALOG); | |
| String cmdName = (String) jobData.get(SchedulerConsts.CONFIG_JOB_COMMAND); | |
| @@ -85,8 +111,7 @@ | |
| if (StringUtils.isBlank(jobClusterId) || jobClusterId.equals(instanceClusterId)) { | |
| try { | |
| // init context | |
| - MgnlContext.setInstance(new SimpleContext(Components.getComponent(SystemContext.class))); | |
| - | |
| + MgnlContext.setInstance(new SimpleContext(systemContext, user)); | |
| Command cmd = Components.getComponent(CommandsManager.class).getCommand(catalogName, cmdName); | |
| if (cmd == null) { | |
| String errorMessage = "Can't find command [" + cmdName + "] for job in catalog [{" + catalogName + "}]"; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment