Skip to main content
aboutsummaryrefslogtreecommitdiffstats
blob: 25054007a7b83834f4781025c13770295c590f6f (plain) (blame)
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
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
/*
 * Copyright (c) 2013, 2015 Eike Stepper (Berlin, Germany) and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *    Christian W. Damus (CEA LIST) - initial API and implementation
 */
package org.eclipse.emf.cdo.security.internal.ui.handlers;

import org.eclipse.emf.cdo.admin.CDOAdminClientRepository;
import org.eclipse.emf.cdo.common.model.CDOPackageRegistryPopulator;
import org.eclipse.emf.cdo.eresource.CDOResource;
import org.eclipse.emf.cdo.net4j.CDONet4jSession;
import org.eclipse.emf.cdo.net4j.CDONet4jSessionConfiguration;
import org.eclipse.emf.cdo.security.User;
import org.eclipse.emf.cdo.security.internal.ui.editor.CDOSecurityFormEditor;
import org.eclipse.emf.cdo.security.internal.ui.messages.Messages;
import org.eclipse.emf.cdo.security.ui.ISecurityManagementContext;
import org.eclipse.emf.cdo.session.CDOSession;
import org.eclipse.emf.cdo.ui.CDOEditorInput;
import org.eclipse.emf.cdo.ui.CDOEditorUtil;
import org.eclipse.emf.cdo.view.CDOView;

import org.eclipse.emf.internal.cdo.session.CDOSessionFactory;

import org.eclipse.net4j.util.ObjectUtil;
import org.eclipse.net4j.util.StringUtil;
import org.eclipse.net4j.util.container.IManagedContainer;
import org.eclipse.net4j.util.container.IPluginContainer;
import org.eclipse.net4j.util.security.CredentialsProviderFactory;
import org.eclipse.net4j.util.security.IPasswordCredentialsProvider;
import org.eclipse.net4j.util.security.NotAuthenticatedException;
import org.eclipse.net4j.util.ui.UIUtil;
import org.eclipse.net4j.util.ui.handlers.LongRunningHandler;

import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.ui.IEditorInput;
import org.eclipse.ui.IEditorPart;
import org.eclipse.ui.IEditorReference;
import org.eclipse.ui.IPartListener;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.IWorkbenchPart;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.handlers.HandlerUtil;
import org.eclipse.ui.statushandlers.StatusManager;

import java.util.Set;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * "Manage Security" command handler, which opens the Security Manager editor
 * in the context of the currently selected {@link CDOSession}, with the help
 * of an optional {@link ISecurityManagementContext} adapter.
 *
 * @author Christian W. Damus (CEA LIST)
 */
public class ManageSecurityHandler extends LongRunningHandler
{
  private IWorkbenchPart part;

  private ISecurityManagementContext context;

  private CDOSession session;

  public ManageSecurityHandler()
  {
  }

  @Override
  protected void extractEventDetails(ExecutionEvent event)
  {
    super.extractEventDetails(event);

    part = HandlerUtil.getActivePart(event);
    context = getContext(event);
  }

  @Override
  protected void preRun() throws Exception
  {
    if (part == null)
    {
      // No workbench page available in which to open the editor
      cancel();
      return;
    }

    setSession(getSession());
    if (session != null && !session.isClosed())
    {
      final IWorkbenchPage page = part.getSite().getPage();

      IEditorPart existing = findEditor(page, session);
      if (existing != null)
      {
        // Activate this editor and we're done
        cancel();
        page.activate(existing);
      }
    }
  }

  protected CDOSession getSession()
  {
    return UIUtil.adaptElement(getSelection(), CDOSession.class);
  }

  protected void setSession(CDOSession session)
  {
    this.session = session;
  }

  @Override
  protected void doExecute(IProgressMonitor progressMonitor) throws Exception
  {
    // Open a new security editor
    final CDOView[] view = new CDOView[] { context.connect(session) };
    if (view[0] == null || view[0].isClosed())
    {
      showWarning(Messages.ManageSecurityHandler_0, Messages.ManageSecurityHandler_1);
      return;
    }

    try
    {
      final CDOResource resource = context.getSecurityResource(view[0]);
      if (resource == null)
      {
        showWarning(Messages.ManageSecurityHandler_0, Messages.ManageSecurityHandler_2);
      }
      else
      {
        UIUtil.getDisplay().syncExec(new Runnable()
        {

          public void run()
          {
            IEditorInput input = CDOEditorUtil.createCDOEditorInput(view[0], resource.getPath(), false);

            try
            {
              IEditorPart editor = part.getSite().getPage().openEditor(input, CDOSecurityFormEditor.ID);
              if (editor != null)
              {
                hookCloseListener(editor, context, view[0]);
                view[0] = null; // Don't disconnect it until later
              }
            }
            catch (PartInitException e)
            {
              StatusManager.getManager().handle(e.getStatus(), StatusManager.SHOW);
            }
          }
        });
      }
    }
    finally
    {
      if (view[0] != null)
      {
        context.disconnect(view[0]);
      }
    }
  }

  protected void showWarning(final String title, final String message)
  {
    UIUtil.getDisplay().syncExec(new Runnable()
    {

      public void run()
      {
        MessageDialog.openWarning(part.getSite().getShell(), title, message);
      }
    });
  }

  IEditorPart findEditor(IWorkbenchPage page, CDOSession session)
  {
    for (IEditorReference next : page.getEditorReferences())
    {
      if (CDOSecurityFormEditor.ID.equals(next.getId()))
      {
        IEditorPart candidate = next.getEditor(false);
        if (candidate != null)
        {
          IEditorInput input = candidate.getEditorInput();
          if (input instanceof CDOEditorInput)
          {
            CDOView view = ((CDOEditorInput)input).getView();
            if (view != null && !view.isClosed() && session.equals(view.getSession()))
            {
              return candidate;
            }
          }
        }
      }
    }

    return null;
  }

  ISecurityManagementContext getContext(ExecutionEvent event)
  {
    ISecurityManagementContext result = null;

    IWorkbenchPart part = HandlerUtil.getActivePart(event);
    if (part != null)
    {
      result = part.getAdapter(ISecurityManagementContext.class);
    }

    if (result == null)
    {
      result = ISecurityManagementContext.DEFAULT;
    }

    return result;
  }

  private void hookCloseListener(final IEditorPart editor, final ISecurityManagementContext context, final CDOView view)
  {
    final IWorkbenchPage page = editor.getSite().getPage();
    page.addPartListener(new IPartListener()
    {
      private final IEditorInput input = editor.getEditorInput();

      private final Set<IEditorPart> openEditors = new java.util.HashSet<IEditorPart>();

      {
        openEditors.add(editor);
      }

      public void partClosed(IWorkbenchPart part)
      {
        openEditors.remove(part);
        if (openEditors.isEmpty())
        {
          // No more editors using this view
          context.disconnect(view);
          page.removePartListener(this);
        }
      }

      public void partOpened(IWorkbenchPart part)
      {
        if (part instanceof IEditorPart)
        {
          IEditorPart editor = (IEditorPart)part;
          if (input.equals(editor.getEditorInput()))
          {
            // The user opened the advanced-mode editor from the form editor
            openEditors.add(editor);
          }
        }
      }

      public void partDeactivated(IWorkbenchPart part)
      {
        // Pass
      }

      public void partBroughtToTop(IWorkbenchPart part)
      {
        // Pass
      }

      public void partActivated(IWorkbenchPart part)
      {
        // Pass
      }
    });
  }

  /**
   * A specialized handler that gets or creates a session in the context of a repository in the
   * CDO Administration view.
   *
   * @author Christian W. Damus (CEA LIST)
   */
  public static class Sessionless extends ManageSecurityHandler implements CDOAdminClientRepository.SessionConfigurator
  {
    private static final AtomicInteger NEXT_SESSION_NUMBER = new AtomicInteger();

    public void prepare(CDONet4jSessionConfiguration configuration)
    {
      IPasswordCredentialsProvider credentialsProvider = getCredentialsProvider();
      configuration.setCredentialsProvider(credentialsProvider);
    }

    @Override
    protected CDOSession getSession()
    {
      return getExistingAdminSession(getRepository());
    }

    @Override
    protected void doExecute(IProgressMonitor progressMonitor) throws Exception
    {
      CDOAdminClientRepository repository = getRepository();
      CDOSession session = getExistingAdminSession(repository);
      if (session == null)
      {
        session = openSession(getRepository());
        setSession(session);
      }

      if (session != null)
      {
        super.doExecute(progressMonitor);
      }
    }

    protected CDOAdminClientRepository getRepository()
    {
      return UIUtil.adaptElement(getSelection(), CDOAdminClientRepository.class);
    }

    protected CDOSession getExistingAdminSession(CDOAdminClientRepository repository)
    {
      for (Object element : IPluginContainer.INSTANCE.getElements(CDOSessionFactory.PRODUCT_GROUP))
      {
        CDONet4jSession session = ObjectUtil.tryCast(element, CDONet4jSession.class);

        // If there's no user ID, then the repository doesn't require authentication,
        // so we can use that session
        if (session != null && !session.isClosed()
            && (User.ADMINISTRATOR.equals(session.getUserID()) || StringUtil.isEmpty(session.getUserID()))
            && ObjectUtil.equals(session.getRepositoryInfo().getUUID(), repository.getUUID()))
        {
          return session;
        }
      }

      return null;
    }

    protected CDOSession openSession(CDOAdminClientRepository repository)
    {
      try
      {
        CDONet4jSession result = repository.openSession(this);
        if (result != null)
        {
          CDOPackageRegistryPopulator.populate(result.getPackageRegistry());

          // Add this session to the shared container so that it may appear in the CDO Sessions view
          String description = "session" + NEXT_SESSION_NUMBER.incrementAndGet(); //$NON-NLS-1$
          IPluginContainer.INSTANCE.putElement(CDOSessionFactory.PRODUCT_GROUP, "security", description, result); //$NON-NLS-1$
        }

        return result;
      }
      catch (NotAuthenticatedException e)
      {
        // User cancelled authentication
        return null;
      }
    }

    protected IPasswordCredentialsProvider getCredentialsProvider()
    {
      IManagedContainer container = IPluginContainer.INSTANCE;
      String productGroup = CredentialsProviderFactory.PRODUCT_GROUP;
      String factoryType = "interactive"; //$NON-NLS-1$
      IPasswordCredentialsProvider credentialsProvider = (IPasswordCredentialsProvider)container
          .getElement(productGroup, factoryType, null);

      if (credentialsProvider == null)
      {
        credentialsProvider = UIUtil.createInteractiveCredentialsProvider();
      }

      return credentialsProvider;
    }
  }
}

Back to the top