📄 usermanager.java
字号:
while (contacts != null && contacts.hasMoreElements()) { Contact contact = (Contact) contacts.nextElement(); if (contact != null) { if (contact.getType().equals("xmppAddress")) { value = contact.getInfo(); break; } } } return value; } /** * Get a numeric service provider * * @param userid * the userid of the user to return * @return String the service provider */ public String getNumericPage(String userid) throws IOException, MarshalException, ValidationException { update(); User user = (User) m_users.get(userid); if (user == null) return ""; String value = ""; Enumeration contacts = user.enumerateContact(); while (contacts != null && contacts.hasMoreElements()) { Contact contact = (Contact) contacts.nextElement(); if (contact != null) { if (contact.getType().equals("numericPage")) { value = contact.getServiceProvider(); break; } } } return value; } /** * Get a text pin * * @param userid * the userid of the user to return * @return String the text pin */ public String getTextPin(String userid) throws IOException, MarshalException, ValidationException { update(); User user = (User) m_users.get(userid); if (user == null) return ""; String value = ""; Enumeration contacts = user.enumerateContact(); while (contacts != null && contacts.hasMoreElements()) { Contact contact = (Contact) contacts.nextElement(); if (contact != null) { if (contact.getType().equals("textPage")) { value = contact.getInfo(); break; } } } return value; } /** * Get a Text Page Service Provider * * @param userid * the userid of the user to return * @return String the text page service provider. */ public String getTextPage(String userid) throws IOException, MarshalException, ValidationException { update(); User user = (User) m_users.get(userid); if (user == null) return ""; String value = ""; Enumeration contacts = user.enumerateContact(); while (contacts != null && contacts.hasMoreElements()) { Contact contact = (Contact) contacts.nextElement(); if (contact != null) { if (contact.getType().equals("textPage")) { value = contact.getServiceProvider(); break; } } } return value; } /** * Get a numeric pin * * @param userid * the userid of the user to return * @return String the numeric pin */ public String getNumericPin(String userid) throws IOException, MarshalException, ValidationException { update(); User user = (User) m_users.get(userid); if (user == null) return ""; String value = ""; Enumeration contacts = user.enumerateContact(); while (contacts != null && contacts.hasMoreElements()) { Contact contact = (Contact) contacts.nextElement(); if (contact != null) { if (contact.getType().equals("numericPage")) { value = contact.getInfo(); break; } } } return value; } /** */ public synchronized void saveUsers(Collection usersList) throws Exception { // clear out the interanal structure and reload it m_users.clear(); Iterator i = usersList.iterator(); while (i.hasNext()) { User curUser = (User) i.next(); m_users.put(curUser.getUserId(), curUser); } } /** * Removes the user from the list of users. Then overwrites to the * "users.xml" */ public synchronized void deleteUser(String name) throws Exception { // Check if the user exists if (m_users.containsKey(name)) { // Delete the user in the user map. m_users.remove(name); // Delete the user in the group. m_groupManager.deleteUser(name); // Delete the user in the view. // viewFactory.deleteUser(name); } else { throw new Exception("UserFactory:delete The old user name " + name + " is not found"); } saveCurrent(); } /** * Saves into "users.xml" file */ private synchronized void saveCurrent() throws Exception { Header header = oldHeader; header.setCreated(EventConstants.formatToString(new Date())); Users users = new Users(); Collection collUsers = (Collection) m_users.values(); Iterator iter = collUsers.iterator(); while (iter != null && iter.hasNext()) { User tmpUser = (User) iter.next(); users.addUser(tmpUser); } Userinfo userinfo = new Userinfo(); userinfo.setUsers(users); userinfo.setHeader(header); oldHeader = header; // marshall to a string first, then write the string to the file. This // way the original config // isn't lost if the xml from the marshall is hosed. StringWriter stringWriter = new StringWriter(); Marshaller.marshal(userinfo, stringWriter); String writerString = stringWriter.toString(); saveXML(writerString); } /** * @param writerString * @throws IOException */ protected abstract void saveXML(String writerString) throws IOException ; /** * When this method is called users name is changed, so also is the username * belonging to the group and the view. Also overwrites the "users.xml" file */ public synchronized void renameUser(String oldName, String newName) throws Exception { // Get the old data if (m_users.containsKey(oldName)) { User data = (User) m_users.get(oldName); if (data == null) { m_users.remove(oldName); throw new Exception("UserFactory:rename the data contained for old user " + oldName + " is null"); } else { // Rename the user in the user map. m_users.remove(oldName); data.setUserId(newName); m_users.put(newName, data); // Rename the user in the group. m_groupManager.renameUser(oldName, newName); // Rename the user in the view. // viewFactory.renameUser(oldName, newName); } } else { throw new Exception("UserFactory:rename the old user name " + oldName + " is not found"); } saveCurrent(); } /** * Sets the password for this user, assuming that the value passed in is * already encrypted properly * * @param userID * the user ID to change the pasword for * @param aPassword * the encrypted password */ public void setEncryptedPassword(String userID, String aPassword) throws Exception { User user = (User) m_users.get(userID); if (user != null) { user.setPassword(aPassword); } saveCurrent(); } /** * Sets the password for this user, first encrypting it * * @param userID * the user ID to change the pasword for * @param aPassword * the password */ public void setUnencryptedPassword(String userID, String aPassword) throws Exception { User user = (User) m_users.get(userID); if (user != null) { user.setPassword(encryptedPassword(aPassword)); } saveCurrent(); } /** * @param aPassword * @return */ public String encryptedPassword(String aPassword) { String encryptedPassword = null; try { MessageDigest digest = MessageDigest.getInstance("MD5"); // build the digest, get the bytes, convert to hexadecimal string // and return encryptedPassword = hexToString(digest.digest(aPassword.getBytes())); } catch (NoSuchAlgorithmException e) { throw new IllegalStateException(e.toString()); } return encryptedPassword; } /** * @param data * @return */ private String hexToString(byte[] data) { char[] hexadecimals = { '0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F' }; // check to see if the byte array has an even number of elements if ((data.length % 2) != 0) return null; // there will be two hexadecimal characters for each byte element char[] buffer = new char[data.length * 2]; for (int i = 0; i < data.length; i++) { int low = (int) (data[i] & 0x0f); int high = (int) ((data[i] & 0xf0) >> 4); buffer[i * 2] = hexadecimals[high]; buffer[i * 2 + 1] = hexadecimals[low]; } return new String(buffer); } /** * This method compares two encrypted strings for equality. * * @param userID * the user ID to check against. * @param aPassword * the password to check for equality * @return true if the two passwords are equal (after encryption), false * otherwise */ public boolean comparePasswords(String userID, String aPassword) { User user = (User) m_users.get(userID); if (user == null) return false; return user.getPassword().equals(encryptedPassword(aPassword)); } /** * @throws IOException * @throws FileNotFoundException * @throws MarshalException * @throws ValidationException */ protected abstract void update() throws IOException, FileNotFoundException, MarshalException, ValidationException;}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -