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 tools.gsf.facade.runtag.user;
18
19 import COM.FutureTense.Interfaces.ICS;
20
21 import java.util.Enumeration;
22 import java.util.HashMap;
23 import java.util.Map;
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 }
46 this.ics = ics;
47 }
48
49 /**
50 * Switch from the current user to a new user, (if username and password are
51 * correct). If Username and password aren't correct, nothing happens. The
52 * new user sees no variables or session variables from the previous user,
53 * but lists and objects in the object pool are not affected.
54 *
55 * @param username new userid. Null is not allowed
56 * @param password new password. Null is not allowed.
57 * @return true on success, false on login failure
58 */
59 public boolean switchTo(String username, String password) {
60
61 saveState();
62 clearState();
63 if (!Login.login(ics, username, password)) {
64 restoreState();
65 vars = null;
66 ssvars = null;
67 return false;
68 }
69
70 return true;
71 }
72
73 /**
74 * Switch back to the previous user.
75 */
76 public void switchBack() {
77
78 if (vars == null || ssvars == null) {
79 throw new IllegalStateException("Can't \"switch back\" without \"switching to\" first.");
80 }
81
82 Logout.logout(ics);
83 clearState();
84 restoreState();
85
86 vars = null;
87 ssvars = null;
88 }
89
90 private void restoreState() {
91 for (String key : vars.keySet()) {
92 ics.SetVar(key, vars.get(key));
93 }
94 for (String key : ssvars.keySet()) {
95 ics.SetSSVar(key, ssvars.get(key));
96 }
97 }
98
99 private void clearState() {
100 Enumeration<?> e = ics.GetVars();
101 while (e.hasMoreElements()) {
102 ics.RemoveVar((String) e.nextElement());
103 }
104 e = ics.GetSSVars();
105 while (e.hasMoreElements()) {
106 ics.RemoveSSVar((String) e.nextElement());
107 }
108 }
109
110 private void saveState() {
111 vars = new HashMap<String, String>();
112 Enumeration<?> e = ics.GetVars();
113 while (e.hasMoreElements()) {
114 String key = (String) e.nextElement();
115 vars.put(key, ics.GetVar(key));
116 }
117 ssvars = new HashMap<String, String>();
118 e = ics.GetSSVars();
119 while (e.hasMoreElements()) {
120 String key = (String) e.nextElement();
121 ssvars.put(key, ics.GetSSVar(key));
122 }
123 }
124 }