📄 administrator.java
字号:
/**************************************************************** * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. * * * * Copyright 2008 Jun Li(SiChuan University, the School of * * Software Engineering). All rights reserved. * * * * Licensed to the JMS under one or more contributor license * * agreements. See the LICENCE file distributed with this * * work for additional information regarding copyright * * ownership. The JMS licenses this file you may not use this * * file except in compliance with the License. * * * * Unless required by applicable law or agreed to in writing, * * software distributed under the License is distributed on an * * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY * * KIND, either express or implied. See the License for the * * specific language governing permissions and limitations * * under the License. * ****************************************************************/package org.jpxx.mail.plugin.console.admin;import java.util.ArrayList;import java.util.List;import org.jpxx.mail.Util.Security.Base64;import org.jpxx.mail.plugin.console.ConsoleConfiguration;/** * JMS Console server manager. * * @author Jun Li * @version $Revision: 0.0.3 $, $Date: 2008/10/26 $ * @since JMS 0.0.3 */public class Administrator { private final String SEP = ";"; private String user; private String pass; private ConsoleConfiguration cc = null; /** * */ public Administrator() { cc = new ConsoleConfiguration(); user = cc.getProperty("USER"); pass = cc.getProperty("PASS"); if (user == null || pass == null || user.equals("") || pass.equals("")) { String dPass = Base64.encodeStr("123456"); // Default manager cc.appendProperty("USER", "root"); cc.appendProperty("PASS", dPass); user = "root"; pass = dPass; } } /** * Verify admin. * * @param userName admin login name * @param password admin password * @return If userName matchs password return true, otherwise return false. * */ public boolean checkAdmin(String userName, String password) { try { String u[] = user.split(SEP); String p[] = Base64.decodeStr(pass).split(SEP); // Check admin exists or not for (int i = 0; i < u.length; i++) { if (u[i].equals(userName)) { return p[i].equals(password); } } return false; } catch (Exception e) { return false; } } /** * Add admin * @param userName admin login name * @param password admin password * @return */ public boolean addAdmin(String userName, String password) { if (isExist(userName)) { return false; } try { String p[] = Base64.decodeStr(pass).split(SEP); String temp = ""; for (int i = 0; i < p.length; i++) { temp += p[i] + SEP; } temp += password; // Encrypt password temp = Base64.encodeStr(temp); if (user.endsWith(SEP)) { user = user.substring(0, user.lastIndexOf(SEP)); } cc.setProperty("USER", user + SEP + userName); cc.setProperty("PASS", temp); return true; } catch (Exception e) { return false; } } /** * * @param userName admin login name * @param password admin password * @return */ public boolean modifyAdmin(String userName, String password) { if (!isExist(userName)) { return false; } try { String u[] = user.split(SEP); String p[] = Base64.decodeStr(pass).split(SEP); String temp = ""; for (int i = 0; i < u.length; i++) { if (u[i].equals(userName)) { p[i] = password; } temp += p[i] + SEP; } if (temp.endsWith(SEP)) { temp = temp.substring(0, temp.lastIndexOf(SEP)); } // Encrypt password temp = Base64.encodeStr(temp); cc.setProperty("PASS", temp); return true; } catch (Exception e) { return false; } } /** * * @param userName admin login name * @return */ public boolean deleteAdmin(String userName) { if (userName.equals("root") || !isExist(userName)) { return false; } try { String u[] = user.split(SEP); String p[] = Base64.decodeStr(pass).split(SEP); String temp1 = ""; String temp2 = ""; int len = u.length; for (int i = 0; i < len; i++) { if (!u[i].equals(userName)) { if (i == len - 1) { temp1 += u[i]; temp2 += p[i]; } else { temp1 += u[i] + SEP; temp2 += p[i] + SEP; } } } if (temp1.endsWith(SEP)) { temp1 = temp1.substring(0, temp1.lastIndexOf(SEP)); } if (temp2.endsWith(SEP)) { temp2 = temp2.substring(0, temp2.lastIndexOf(SEP)); } temp2 = Base64.encodeStr(temp2); cc.setProperty("USER", temp1); cc.setProperty("PASS", temp2); return true; } catch (Exception e) { return false; } } /** * List all administrators. * * @return all administrators by a list */ public List<String> list() { updateData(); List<String> admin = new ArrayList<String>(); try { String u[] = user.split(SEP); // Check admin exists or not for (int i = 0; i < u.length; i++) { if (!u[i].trim().equals("")) { admin.add(u[i].trim()); } } } catch (Exception e) { } return admin; } /** * Update data in memory. */ public void updateData() { cc = new ConsoleConfiguration(); user = cc.getProperty("USER"); pass = cc.getProperty("PASS"); } /** * Check root mamanger. * * @param password root password. * @return If root matchs password return true, otherwise return false. */ public boolean checkRoot(String password) { return checkAdmin("root", password); } /** * Get admin's password. * * @param userName * @return */ public String getPassword(String userName) { updateData(); try { String u[] = user.split(SEP); String p[] = Base64.decodeStr(pass).split(SEP); // Check admin exists or not for (int i = 0; i < u.length; i++) { if (u[i].equals(userName)) { return p[i]; } } return null; } catch (Exception e) { return null; } } /** * Check admin userName is exist or not * @param userName admin userName * @return If there is an exception then renturn false. */ private boolean isExist(String userName) { try { String u[] = user.split(SEP); String p[] = Base64.decodeStr(pass).split(SEP); // Check admin exists or not for (int i = 0; i < u.length; i++) { if (u[i].equals(userName)) { return true; } } return false; } catch (Exception e) { return false; } }}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -