📄 dbconfig.java
字号:
try { for (int i = 0; i < groups.size(); i++) { String group = (String)groups.get(i); if (group == null) throw new DBConfigException("Invalid addUser() parameters!"); } } catch (ClassCastException e) { throw new DBConfigException("Invalid addUser() parameters!"); } // throws a DBConfigException if the user does not exist ID uID = getUid(user); // initiator of the operation int uid1; ID gID; // check if uid belongs to admin check_belong_admin(uID); // check if the user is already installed on the system try { getUid(name); } catch (DBConfigException e) { // if user belongs to admin then he must have full permissions for (int i = 0; i < groups.size(); i++) if (groups.get(i).equals("admin")) { permissions = ADD_NEW_GROUPS | ADD_NEW_ALGORITHMS | ADD_NEW_DATABASES; break; } // the name is not already in use // add the new user to the users list users.add(new User(uid1 = getNextUID(), name, password, permissions)); // add the group "all" to the rest of groups groups.add("all"); eliminateDuplicates(groups); boolean all = true; // add all user - group associations for (int i = 0; i < groups.size(); i++) { try { // throws a DBConfigException if the group does not exist gID = getGid((String)groups.get(i)); // add the pair uid1 - gid to the usersGroups list usersGroups.add(new UserGroup(uid1, gID.id)); } catch (DBConfigException e1) { // one of the groups is not valid all = false; } } // save DBConfig object to the disk saveDBConfig(); if (all) return; else throw new DBConfigWarning("Not all specified groups are valid groups."); } // if this point is reached, no exception was thrown in the try above // newName is already in use, can not add a user with this name throw new DBConfigException("The user name " + name + " is already in use."); } /** * Deletes a user. All the resources owned by the user will be inherited * by the admin user. * * @exception DBConfigException The operation can not be completed. * @param user The initiator of the operation. The <code>user</code> * should belong to the admin group. * @param name The name of the user to be deleted. */ public synchronized void deleteUser(String user, String name) throws DBConfigException { // check arguments for validity if (user == null || name == null) throw new DBConfigException("Invalid deleteUser() parameters!"); // throws a DBConfigException if the user does not exist ID uID = getUid(user); // initiator of the operation ID uID1 = getUid(name); // user to be deleted if (uID1.id < FIRST_FREE_UID) throw new DBConfigException("Cannot remove " + name); // check if uid belong to the group admin // throws a DBConfigException if not check_belong_admin(uID); // if the user uID1 is logged on if (((User)users.get(uID1.index)).countLogged > 0) // schedule the user to be deleted ((User)users.get(uID1.index)).toBeDeleted = true; else // else, remove user deleteUser(uID1); // save DBConfig object to the disk saveDBConfig(); } /** * Changes the name of a user. * * @exception DBConfigException The operation can not be completed. * @param user The initiator of the operation. The <code>user</code> * should belong to the admin group. * @param name The name of the user. * @param newName The new name of the user. */ public synchronized void changeUserName(String user, String name, String newName) throws DBConfigException { // check arguments for validity if (user == null || name == null || newName == null) throw new DBConfigException("Invalid changeUserName() parameters!"); // throws a DBConfigException if the user does not exist ID uID = getUid(user); // initiator of the operation ID uID1 = getUid(name); // user whose name will be changed if (uID1.id == ADMIN_UID) throw new DBConfigException("Cannot change name for admin."); if (uID1.id == ANONYMOUS_UID) throw new DBConfigException("Cannot change name for anonymous."); // if the user uID1 is logged on if (((User)users.get(uID1.index)).countLogged > 0) throw new DBConfigException("Cannot change name for a logged user."); // check if uid belong to the group admin // throws a DBConfigException if not check_belong_admin(uID); try { getUid(newName); } catch (DBConfigException e) { // the new name is not already in use // change the old name to the new name ((User)users.get(uID1.index)).name = newName; // save DBConfig object to the disk saveDBConfig(); return; } // if this point is reached, no exception was thrown in the try above // newName is already in use, can not change the old name // to the newName throw new DBConfigException("User name " + newName + " is already in use."); } /** * Changes the password of a user. * * @exception DBConfigException The operation can not be completed. * @param user The initiator of the operation. The <code>user</code> * should belong to the admin group or should be the same as * <code>name</code>. * @param name The name of the user whose password will be changed. * @param newPassword The new password. */ public synchronized void changePassword(String user, String name, String newPassword) throws DBConfigException { // check arguments for validity if (user == null || name == null || newPassword == null) throw new DBConfigException("Invalid changePassword() parameters!"); // throws a DBConfigException if the user does not exist ID uID = getUid(user); // initiator of the operation ID uID1 = getUid(name); // user whose password will be changed // if we are not in the case that a user wants to change its // own password, check if the user belongs to the admin group // throws a DBConfigException if the user does not belong if (!(user.equals(name))) check_belong_admin(uID); // change password ((User)users.get(uID1.index)).password = newPassword; // save DBConfig object to the disk saveDBConfig(); } /** * Changes the permissions of a user. * * @exception DBConfigException The operation can not be completed. * @param user The initiator of the operation. The <code>user</code> * should belong to the admin group. * @param name The name of the user whose permissions will be changed. * @param newPermissions The new permissions. */ public synchronized void changePermissions(String user, String name, long newPermissions) throws DBConfigException { // check arguments for validity if (user == null || name == null) throw new DBConfigException("Invalid changePermissions() parameters!"); // throws a DBConfigException if the user does not exist ID uID = getUid(user); // initiator of the operation ID uID1 = getUid(name); // user whose permissions will be changed if (uID1.id == ADMIN_UID) throw new DBConfigException("Cannot change permissions for admin."); if (uID1.id == ANONYMOUS_UID) throw new DBConfigException("Cannot change permissions for anonymous."); // check if uid belong to the group admin // throws a DBConfigException if not check_belong_admin(uID); try { check_belong_admin(uID1); } catch (DBConfigException e) { // change the permissions ((User)users.get(uID1.index)).permissions = newPermissions; // save DBConfig object to the disk saveDBConfig(); return; } // we get here if the name was an administrator throw new DBConfigException("Cannot change permissions for administrators."); } /** * Increment a user's connection count. * * @exception DBConfigException The operation can not be completed. * @param user The name of the user. */ public synchronized void markLogged(String user) throws DBConfigException { // check arguments for validity if (user == null) throw new DBConfigException("Invalid markLogged() parameters!"); // throws a DBConfigException if the user does not exist ID uID = getUid(user); ((User)users.get(uID.index)).countLogged++; // save DBConfig object to the disk saveDBConfig(); } /** * Decrement a user's connection count. * * @exception DBConfigException the operation can not be completed. * @param user The name of the user. */ public synchronized void unmarkLogged(String user) throws DBConfigException { // check arguments for validity if (user == null) throw new DBConfigException("Invalid unmarkLogged() parameters!"); // throws a DBConfigException if the user does not exist ID uID = getUid(user); // if the user is scheduled to be deleted, delete the user if (((User)users.get(uID.index)).toBeDeleted) deleteUser(uID); else ((User)users.get(uID.index)).countLogged--; // save DBConfig object to the disk saveDBConfig(); } // in the following algorithm related methods, variable name refers to // algorithm name. /** * Adds a new algorithm. The <code>user</code> will be the owner * of the algorithm. * * @exception DBConfigException The operation can not be completed, * a user with the name <code>user</code> does not exist or * a group with the name <code>group</code> does not exist. * @param user The owner of the new algorithm. This user should have * permissions to add new algorithms in the system. * @param name The name of the algorithm. * @param group The group to which the algorithm belongs. */ public synchronized void addAlgorithm(String user, String name, String group) throws DBConfigException { // check arguments for validity if (user == null || name == null || group == null) throw new DBConfigException("Invalid addAlgorithm() parameters!"); // throws a DBConfigException if the user or group do not exist ID uID = getUid(user); // initiator of the operation ID gID = getGid(group); // the group to which the algorithm belongs // checks if the user has permissions to add a new algorithm // throws a DBConfigException if not check_perm_algorithms(uID); // check if the an algorithm with name already exists try { getAid(name); } catch (DBConfigException e) { // the name for this algorithm is not already in use // add the new algorithm and return algorithms.add(new Algorithm(getNextAID(), name, uID.id, gID.id)); // save DBConfig object to the disk saveDBConfig(); return; } // if this point is reached, no exception was thrown in the try above // this name of the algorithm is already in use // can not complete the operation throw new DBConfigException("The algorithm name " + name + " is already in use."); } /** * Deletes an algorithm. * * @exception DBConfigException The operation can not be completed, * a user with the name <code>user</code> does not exist or * an algorithm with the name <code>name</code> does not exist. * @param user The initiator of the operation. The <code>user</code> * should be the owner of the algorithm or he should belong to the * admin group. * @param name The name of the algorithm to be deleted. */ public synchronized void deleteAlgorithm(String user, String name) throws DBConfigException { // check arguments for validity if (user == null || name == null) throw new DBConfigException("Invalid deleteAlgorithm() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID aID = getAid(name); if (aID.id < FIRST_FREE_AID) throw new DBConfigException("Cannot remove " + name); // check if uid is the admin try { check_belong_admin(uID); } catch (DBConfigException e) { // uid is not in admin group // check if it is the owner of the algorithm // look for aid in the list of algorithms // if uid is not the owner of aid a DBConfigException // is thrown check_owner_algorithm(uID, aID); } // if this point is reached uid is in admin or // uid is the owner of the algorithm aid // if algorithm is used just mark it for deletion if (((Algorithm)algorithms.get(aID.index)).countUsage > 0) { ((Algorithm)algorithms.get(aID.index)).toBeDeleted = true; // save DBConfig object to the disk saveDBConfig(); } else { // otherwise remove the algorithm from the list algorithms.remove(aID.index); // save DBConfig object to the disk saveDBConfig(); try { File file_to_delete = new File(name + ".jar"); if(file_to_delete.delete() == false) System.err.println("Could not delete " + name + ".jar"); } catch (Throwable e) { throw new DBConfigException("Some errors occured: " + e); } } } /** * Changes the group of the algorithm. *
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -