KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > ofbiz > pos > event > SecurityEvents


1 /*
2  * $Id: SecurityEvents.java 5462 2005-08-05 18:35:48Z jonesde $
3  *
4  * Copyright (c) 2004 The Open For Business Project - www.ofbiz.org
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining a
7  * copy of this software and associated documentation files (the "Software"),
8  * to deal in the Software without restriction, including without limitation
9  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
10  * and/or sell copies of the Software, and to permit persons to whom the
11  * Software is furnished to do so, subject to the following conditions:
12  *
13  * The above copyright notice and this permission notice shall be included
14  * in all copies or substantial portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
19  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
20  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT
21  * OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.
23  *
24  */

25 package org.ofbiz.pos.event;
26
27 import java.util.Locale JavaDoc;
28
29 import org.ofbiz.pos.screen.PosScreen;
30 import org.ofbiz.pos.component.Input;
31 import org.ofbiz.pos.component.Output;
32 import org.ofbiz.pos.PosTransaction;
33 import org.ofbiz.base.util.UtilProperties;
34 import org.ofbiz.base.util.UtilValidate;
35 import org.ofbiz.base.util.Debug;
36 import org.ofbiz.guiapp.xui.XuiSession;
37 import org.ofbiz.entity.GenericValue;
38
39 /**
40  *
41  * @author <a HREF="mailto:jaz@ofbiz.org">Andy Zeneski</a>
42  * @version $Rev: 5462 $
43  * @since 3.2
44  */

45 public class SecurityEvents {
46
47     public static final String JavaDoc module = SecurityEvents.class.getName();
48
49     public static void login(PosScreen pos) {
50         String JavaDoc[] func = pos.getInput().getFunction("LOGIN");
51         if (func == null) {
52             pos.getInput().setFunction("LOGIN", "");
53         }
54         baseLogin(pos, false);
55     }
56
57     public static void logout(PosScreen pos) {
58         PosTransaction trans = PosTransaction.getCurrentTx(pos.getSession());
59         XuiSession session = pos.getSession();
60         trans.closeTx();
61         session.logout();
62         pos.showPage("pospanel");
63         PosScreen.currentScreen.setLock(true);
64     }
65
66     public static void mgrLogin(PosScreen pos) {
67         XuiSession session = pos.getSession();
68         if (session.hasRole(session.getUserLogin(), "MANAGER")) {
69             ManagerEvents.mgrLoggedIn = true;
70             pos.showPage("mgrpanel");
71             PosScreen.currentScreen.getInput().clear();
72         } else {
73             String JavaDoc[] func = pos.getInput().getFunction("MGRLOGIN");
74             if (func == null) {
75                 pos.getInput().setFunction("MGRLOGIN", "");
76             }
77             baseLogin(pos, true);
78         }
79     }
80
81     public static void lock(PosScreen pos) {
82         pos.setLock(true);
83     }
84
85     private static void baseLogin(PosScreen pos, boolean mgr) {
86         XuiSession session = pos.getSession();
87         Output output = pos.getOutput();
88         Input input = pos.getInput();
89
90         String JavaDoc loginFunc = mgr ? "MGRLOGIN" : "LOGIN";
91         String JavaDoc[] func = input.getLastFunction();
92         String JavaDoc text = input.value();
93         if (func != null && func[0].equals(loginFunc)) {
94             if (UtilValidate.isEmpty(func[1]) && UtilValidate.isEmpty(text)) {
95                 output.print(UtilProperties.getMessage("pos","ULOGIN",Locale.getDefault()));
96                 input.setFunction(loginFunc);
97             } else if (UtilValidate.isEmpty(func[1])) {
98                 output.print(UtilProperties.getMessage("pos","UPASSW",Locale.getDefault()));
99                 input.setFunction(loginFunc);
100             } else {
101                 String JavaDoc username = func[1];
102                 String JavaDoc password = text;
103                 if (!mgr) {
104                     boolean passed = false;
105                     try {
106                         session.login(username, password);
107                         passed = true;
108                     } catch (XuiSession.UserLoginFailure e) {
109                         output.print(e.getMessage());
110                         input.clear();
111                     }
112                     if (passed) {
113                         input.clear();
114                         pos.setLock(false);
115                         pos.refresh();
116                         return;
117                     }
118                 } else {
119                     GenericValue mgrUl = null;
120                     try {
121                         mgrUl = session.checkLogin(username, password);
122                     } catch (XuiSession.UserLoginFailure e) {
123                         output.print(e.getMessage());
124                         input.clear();
125                     }
126                     if (mgrUl != null) {
127                         boolean isMgr = session.hasRole(mgrUl, "MANAGER");
128                         if (!isMgr) {
129                             output.print(UtilProperties.getMessage("pos","UserNotmanager",Locale.getDefault()));
130                             input.clear();
131                         } else {
132                             ManagerEvents.mgrLoggedIn = true;
133                             pos.showPage("mgrpanel");
134                         }
135                     }
136                 }
137             }
138         } else {
139             Debug.log("Login function called but not prepared as a function!", module);
140         }
141     }
142 }
Popular Tags