View Javadoc

1   /*
2    * Copyright 2008 FatWire Corporation. All Rights Reserved.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *    http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package com.fatwire.gst.foundation.facade.runtag.user;
18  
19  import java.util.Enumeration;
20  import java.util.HashMap;
21  import java.util.Map;
22  
23  import COM.FutureTense.Interfaces.ICS;
24  
25  /**
26   * This class provides the user.su body tag functionality. Unfortunately, RunTag
27   * does not support body tags with actions in both the starttag and endtag, and
28   * this functionality requires this. Instead, this class is stateful and
29   * contains a switchTo() and switchBack() method.
30   * <p/>
31   * Note that this class is NOT THREAD SAFE.
32   * 
33   * @author Tony Field
34   * @since Feb 1, 2011
35   */
36  public final class SwitchUser {
37      private final ICS ics;
38  
39      private Map<String, String> vars;
40      private Map<String, String> ssvars;
41  
42      public SwitchUser(ICS ics) {
43          if (ics == null)
44              throw new IllegalArgumentException("ICS cannot be null");
45          this.ics = ics;
46      }
47  
48      /**
49       * Switch from the current user to a new user, (if username and password are
50       * correct). If Username and password aren't correct, nothing happens. The
51       * new user sees no variables or session variables from the previous user,
52       * but lists and objects in the object pool are not affected.
53       * 
54       * @param username new userid. Null is not allowed
55       * @param password new password. Null is not allowed.
56       * @return true on success, false on login failure
57       */
58      public boolean switchTo(String username, String password) {
59  
60          saveState();
61          clearState();
62          if (!Login.login(ics, username, password)) {
63              restoreState();
64              vars = null;
65              ssvars = null;
66              return false;
67          }
68  
69          return true;
70      }
71  
72      /**
73       * Switch back to the previous user.
74       */
75      public void switchBack() {
76  
77          if (vars == null || ssvars == null)
78              throw new IllegalStateException("Can't \"switch back\" without \"switching to\" first.");
79  
80          Logout.logout(ics);
81          clearState();
82          restoreState();
83  
84          vars = null;
85          ssvars = null;
86      }
87  
88      private void restoreState() {
89          for (String key : vars.keySet()) {
90              ics.SetVar(key, vars.get(key));
91          }
92          for (String key : ssvars.keySet()) {
93              ics.SetSSVar(key, ssvars.get(key));
94          }
95      }
96  
97      private void clearState() {
98          Enumeration<?> e = ics.GetVars();
99          while (e.hasMoreElements()) {
100             ics.RemoveVar((String) e.nextElement());
101         }
102         e = ics.GetSSVars();
103         while (e.hasMoreElements()) {
104             ics.RemoveSSVar((String) e.nextElement());
105         }
106     }
107 
108     private void saveState() {
109         vars = new HashMap<String, String>();
110         Enumeration<?> e = ics.GetVars();
111         while (e.hasMoreElements()) {
112             String key = (String) e.nextElement();
113             vars.put(key, ics.GetVar(key));
114         }
115         ssvars = new HashMap<String, String>();
116         e = ics.GetSSVars();
117         while (e.hasMoreElements()) {
118             String key = (String) e.nextElement();
119             ssvars.put(key, ics.GetSSVar(key));
120         }
121     }
122 }