Skip to main content
summaryrefslogtreecommitdiffstats
blob: e5d0cbd5bc25b8d596ed7394b14acade10cca9bf (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
/*******************************************************************************
 * Copyright (c) 2004, 2007 Boeing.
 * 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:
 *     Boeing - initial API and implementation
 *******************************************************************************/
package org.eclipse.osee.framework.skynet.core;

import java.util.logging.Level;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.osee.framework.core.client.ClientSessionManager;
import org.eclipse.osee.framework.core.data.SystemUser;
import org.eclipse.osee.framework.core.exception.OseeCoreException;
import org.eclipse.osee.framework.core.exception.OseeStateException;
import org.eclipse.osee.framework.core.exception.UserNotInDatabase;
import org.eclipse.osee.framework.core.operation.Operations;
import org.eclipse.osee.framework.logging.OseeLog;
import org.eclipse.osee.framework.plugin.core.util.IExceptionableRunnable;
import org.eclipse.osee.framework.plugin.core.util.Jobs;
import org.eclipse.osee.framework.skynet.core.artifact.BranchManager;
import org.eclipse.osee.framework.skynet.core.event.OseeEventManager;
import org.eclipse.osee.framework.skynet.core.event.model.AccessControlEvent;
import org.eclipse.osee.framework.skynet.core.event.model.AccessControlEventType;
import org.eclipse.osee.framework.skynet.core.internal.Activator;
import org.eclipse.osee.framework.skynet.core.transaction.SkynetTransaction;

/**
 * @author Roberto E. Escobar
 */
final class ClientUser {
   private static final ClientUser instance = new ClientUser();

   private User currentUser;
   private boolean isGuestNotificationAllowed;
   private boolean isGuestAuthenticationAllowed;

   private ClientUser() {
      this.currentUser = null;
      this.isGuestAuthenticationAllowed = true;
      this.isGuestNotificationAllowed = true;
   }

   static synchronized User getMainUser() throws OseeCoreException {
      if (!ClientSessionManager.isSessionValid() || instance.currentUser == null) {
         instance.populateCurrentUser();
         if (!instance.currentUser.isActive()) {
            instance.currentUser.setActive(true);
            instance.currentUser.persist();
         }
      }
      return instance.currentUser;
   }

   private void populateCurrentUser() throws OseeCoreException {
      ClientSessionManager.ensureSessionCreated();
      if (ClientSessionManager.isSessionValid()) {
         String userId = ClientSessionManager.getSession().getUserId();
         try {
            if (userId.equals(SystemUser.BootStrap.getUserID())) {
               setCurrentUser(BootStrapUser.getInstance());
            } else {
               if (ClientSessionManager.isUserCreationRequired()) {
                  SkynetTransaction transaction =
                     new SkynetTransaction(BranchManager.getCommonBranch(), "Populate current user");
                  UserManager.createMainUser(ClientSessionManager.getCurrentUserInfo(), transaction);
                  setCurrentUser(UserManager.getUserByUserId(ClientSessionManager.getCurrentUserInfo().getUserID()));
                  Operations.executeWorkAndCheckStatus(transaction);

                  ClientSessionManager.clearUserCreationRequired();
               } else {
                  setCurrentUser(UserManager.getUserByUserId(ClientSessionManager.getCurrentUserInfo().getUserID()));
               }
            }
         } catch (UserNotInDatabase ex) {
            executeGuestLogin();
         }
      } else {
         executeGuestLogin();
      }
   }

   private void setCurrentUser(User newUser) throws OseeStateException {
      this.currentUser = newUser;

      if (newUser == null) {
         throw new OseeStateException("Setting current user to null.");
      } else {
         if (isGuestNotificationAllowed && newUser.getName().equals(SystemUser.Guest.getName())) {
            isGuestNotificationAllowed = false;
            OseeLog.log(Activator.class, Level.INFO,
               "You are logged into OSEE as \"Guest\".  If this is unexpected notify your OSEE admin");
         }
      }
      if (ClientSessionManager.isSessionValid()) {
         notifyListeners();
      }
   }

   private void executeGuestLogin() throws OseeCoreException {
      if (isGuestAuthenticationAllowed) {
         ClientSessionManager.authenticateAsGuest();
         setCurrentUser(UserManager.getUser(SystemUser.Guest));
         isGuestAuthenticationAllowed = false;
      }
   }

   private void notifyListeners() {
      Jobs.runInJob("Osee User Authenticated", new IExceptionableRunnable() {

         @Override
         public IStatus run(IProgressMonitor monitor) throws Exception {
            AccessControlEvent event = new AccessControlEvent();
            event.setEventType(AccessControlEventType.UserAuthenticated);
            OseeEventManager.kickAccessControlArtifactsEvent(this, event);
            return Status.OK_STATUS;
         }

      }, Activator.class, Activator.PLUGIN_ID);

   }
}

Back to the top