001: /*******************************************************************************
002: * Licensed to the Apache Software Foundation (ASF) under one
003: * or more contributor license agreements. See the NOTICE file
004: * distributed with this work for additional information
005: * regarding copyright ownership. The ASF licenses this file
006: * to you under the Apache License, Version 2.0 (the
007: * "License"); you may not use this file except in compliance
008: * with the License. You may obtain a copy of the License at
009: *
010: * http://www.apache.org/licenses/LICENSE-2.0
011: *
012: * Unless required by applicable law or agreed to in writing,
013: * software distributed under the License is distributed on an
014: * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
015: * KIND, either express or implied. See the License for the
016: * specific language governing permissions and limitations
017: * under the License.
018: *******************************************************************************/package org.ofbiz.pos.event;
019:
020: import java.util.Locale;
021:
022: import org.ofbiz.pos.screen.PosScreen;
023: import org.ofbiz.pos.component.InputWithPassword;
024: import org.ofbiz.pos.component.Output;
025: import org.ofbiz.pos.PosTransaction;
026: import org.ofbiz.base.util.UtilProperties;
027: import org.ofbiz.base.util.UtilValidate;
028: import org.ofbiz.base.util.Debug;
029: import org.ofbiz.guiapp.xui.XuiSession;
030: import org.ofbiz.entity.GenericValue;
031:
032: public class SecurityEvents {
033:
034: public static final String module = SecurityEvents.class.getName();
035:
036: public static void login(PosScreen pos) {
037: pos.setWaitCursor();
038: String[] func = pos.getInput().getFunction("LOGIN");
039: if (func == null) {
040: pos.getInput().setFunction("LOGIN", "");
041: }
042: baseLogin(pos, false);
043: pos.setNormalCursor();
044: }
045:
046: public static void logout(PosScreen pos) {
047: pos.setWaitCursor();
048: PosTransaction trans = PosTransaction.getCurrentTx(pos
049: .getSession());
050: XuiSession session = pos.getSession();
051: trans.closeTx();
052: session.logout();
053: pos.showPage("pospanel");
054: PosScreen.currentScreen.setLock(true);
055: pos.setNormalCursor();
056: }
057:
058: public static void mgrLogin(PosScreen pos) {
059: pos.setWaitCursor();
060: XuiSession session = pos.getSession();
061: if (session.hasRole(session.getUserLogin(), "MANAGER")) {
062: ManagerEvents.mgrLoggedIn = true;
063: pos.showPage("mgrpanel");
064: PosScreen.currentScreen.getInput().clear();
065: } else {
066: String[] func = pos.getInput().getFunction("MGRLOGIN");
067: if (func == null) {
068: pos.getInput().setFunction("MGRLOGIN", "");
069: }
070: baseLogin(pos, true);
071: }
072: pos.setNormalCursor();
073: }
074:
075: public static void lock(PosScreen pos) {
076: pos.setLock(true);
077: }
078:
079: private static void baseLogin(PosScreen pos, boolean mgr) {
080: XuiSession session = pos.getSession();
081: Output output = pos.getOutput();
082: InputWithPassword input = pos.getInput();
083:
084: String loginFunc = mgr ? "MGRLOGIN" : "LOGIN";
085: String[] func = input.getLastFunction();
086: String text = input.value();
087: if (func != null && func[0].equals(loginFunc)) {
088: if (UtilValidate.isEmpty(func[1])
089: && UtilValidate.isEmpty(text)) {
090: output.print(UtilProperties.getMessage("pos", "ULOGIN",
091: Locale.getDefault()));
092: input.setFunction(loginFunc);
093: input.setPasswordInput(false);
094: } else if (UtilValidate.isEmpty(func[1])) {
095: output.print(UtilProperties.getMessage("pos", "UPASSW",
096: Locale.getDefault()));
097: input.setFunction(loginFunc);
098: input.setPasswordInput(true);
099: } else {
100: input.setPasswordInput(false);
101: String username = func[1];
102: String 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: input.clear();
110: input.setFunction(loginFunc);
111: output.print(e.getMessage()
112: + " "
113: + UtilProperties.getMessage("pos",
114: "ULOGIN", Locale.getDefault()));
115: }
116: if (passed) {
117: input.clear();
118: pos.setLock(false);
119: pos.refresh();
120: return;
121: }
122: } else {
123: GenericValue mgrUl = null;
124: try {
125: mgrUl = session.checkLogin(username, password);
126: } catch (XuiSession.UserLoginFailure e) {
127: output.print(e.getMessage());
128: input.clear();
129: }
130: if (mgrUl != null) {
131: boolean isMgr = session.hasRole(mgrUl,
132: "MANAGER");
133: if (!isMgr) {
134: output.print(UtilProperties.getMessage(
135: "pos", "UserNotmanager", Locale
136: .getDefault()));
137: input.clear();
138: } else {
139: ManagerEvents.mgrLoggedIn = true;
140: pos.showPage("mgrpanel");
141: }
142: }
143: }
144: }
145: } else {
146: Debug
147: .log(
148: "Login function called but not prepared as a function!",
149: module);
150: }
151: }
152: }
|