📄 dbconfig.java
字号:
else throw new DBConfigWarning("Not all specified users are valid users."); } // if this point is reached, no exception was thrown in the try above // this name of the group is already in use // can not complete The operation throw new DBConfigException("The group name " + name + " is already in use."); } /** * Deletes a group. * * @exception DBConfigException The operation can not be completed. * @param user The initiator of the operation. The <code>user</code> * should be the owner of the group or he should belong to the * admin group. * @param name The name of the group to be deleted. */ public synchronized void deleteGroup(String user, String name) throws DBConfigException { // check arguments for validity if (user == null || name == null) throw new DBConfigException("Invalid deleteGroup() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID gID = getGid(name); if (gID.id < FIRST_FREE_GID) throw new DBConfigException("Cannot remove " + name); for (int i = 0; i < algorithms.size(); i++) { Algorithm alg = (Algorithm)algorithms.get(i); if (alg.gid == gID.id) throw new DBConfigException("Cannot delete this group since it is used by algorithm " + alg.name); } for (int i = 0; i < databases.size(); i++) { Database db = (Database)databases.get(i); if (db.gid == gID.id) throw new DBConfigException("Cannot delete this group since it is used by database " + db.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 // look for gid in the list of groups // if uid is not the owner of gid a DBConfigException // is thrown check_owner_group(uID, gID); } // if this point is reached uid is in admin or // uid is the owner of the group gid // remove the group from the list groups.remove(gID.index); // remove all user - group associations for (int j = 0; j < usersGroups.size(); j++) if (((UserGroup)usersGroups.get(j)).gid == gID.id) { // remove this user - group association usersGroups.remove(j); // stay on the same position j--; } // save DBConfig object to the disk saveDBConfig(); } /** * Changes the name of a group. * * @exception DBConfigException The operation can not be completed. * @param user The initiator of the operation. The <code>user</code> * should be the owner of the group or he should belong to the * admin group. * @param name The old name of the group. * @param newName The new name of the group. */ public synchronized void changeGroupName(String user, String name, String newName) throws DBConfigException { // check arguments for validity if (user == null || name == null || newName == null) throw new DBConfigException("Invalid changeGroupName() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID gID = getGid(name); if (gID.id < FIRST_FREE_GID) throw new DBConfigException("Cannot change the name of this group."); // 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 // look for gid in the list of groups // if uid is not the owner of gid a DBConfigException // is thrown check_owner_group(uID, gID); } // if this point is reached uid is in admin or // uid is the owner of the group gid // check if the new name is not in use try { getGid(newName); } catch (DBConfigException e) { // the new name is not already in use // change the old name to the new name ((Group)groups.get(gID.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(newName + " is already in use."); } /** * Add a user to a group. * * @exception DBConfigException The operation can not be completed. * @param user The initiator of the operation. The <code>user</code> * should be the owner of the group or he should belong to the * admin group. * @param name The name of the group. * @param userNameh The name of the user to be added to the group. */ public synchronized void addUserToGroup(String user, String name, String userName) throws DBConfigException { // check arguments for validity if (user == null || name == null || userName == null) throw new DBConfigException("Invalid addUserToGroup() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID gID = getGid(name); ID uID1 = getUid(userName); // user to be added to the group // 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 // look for gid in the list of groups // if uid is not the owner of gid a DBConfigException // is thrown check_owner_group(uID, gID); } // if this point is reached uid is in admin or // uid is the owner of the group gid // check if the pair uid1 - gid is already in the usersGroups list for (int i = 0; i < usersGroups.size(); i++) if (((UserGroup)usersGroups.get(i)).gid == gID.id && ((UserGroup)usersGroups.get(i)).uid == uID1.id ) // the pair uid1 - gid is already in the usersGroups list // no need to add it again return; // add user - group association usersGroups.add(new UserGroup(uID1.id, gID.id)); // save DBConfig object to the disk saveDBConfig(); } /** * Removes a user from a group. * * @exception DBConfigException The operation can not be completed. * @param user The initiator of the operation. The <code>user</code> * should be the owner of the group or he should belong to the * admin group. The owner of the group can not be deleted from the group. * @param name The name of the group. * @param userName The name of the user to be removed from the group. */ public synchronized void removeUserFromGroup(String user, String name, String userName) throws DBConfigException { // check arguments for validity if (user == null || name == null || userName == null) throw new DBConfigException("Invalid removeUserFromGroup() parameters!"); // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID gID = getGid(name); ID uID1 = getUid(userName); // user to be removed from the group if (uID1.id == ADMIN_UID && gID.id == ADMIN_GID) throw new DBConfigException("Cannot remove user admin from group admin."); if (gID.id == ALL_GID) throw new DBConfigException("Cannot remove a user from group all."); // 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 // look for gid in the list of groups // if uid is not the owner of gid a DBConfigException // is thrown check_owner_group(uID, gID); // if the owner of the group is also the user // to be deleted, just return if (user.equals(userName)) throw new DBConfigException("Cannot remove group owner from group."); } // if this point is reached uid is in admin or // uid is the owner of the group gid // and uid is not equal to uid1 for (int i = 0; i < usersGroups.size(); i++) if (((UserGroup)usersGroups.get(i)).gid == gID.id && ((UserGroup)usersGroups.get(i)).uid == uID1.id ) { // remove the uid1 - gid pair from the list // of user - group associations usersGroups.remove(i); // uid1 - gid should be unique // save DBConfig object to the disk saveDBConfig(); return; } } /** * Sets for an user the new list of groups to which the user belongs. * * @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 userName The name of the user whose groups will be changed. * @param newGroups The list of the new groups. */ public synchronized void setGroupsForUser(String user, String userName, Vector newGroups) throws DBConfigException { // check arguments for validity if (user == null || userName == null || newGroups == null) throw new DBConfigException("Invalid setGroupsForUser() parameters!"); try { for (int i = 0; i < newGroups.size(); i++) { String group = (String)newGroups.get(i); if (group == null) throw new DBConfigException("Invalid setGroupsForUser() parameters!"); } } catch (ClassCastException e) { throw new DBConfigException("Invalid setGroupsForUser() parameters!"); } // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID uID1 = getUid(userName); // user whose groups will be modified // check if uid is the admin check_belong_admin(uID); // get a list of old groups to which user belongs Vector oldGroups = listGroupsForUser(userName); // delete all the groups from the old_groups // except the ones owned by userName or group 'all' for (int i = 0; i < oldGroups.size(); i++) if (!userName.equals(getGroupOwner((String)oldGroups.get(i))) && !oldGroups.get(i).equals("all")) removeUserFromGroup(user, (String)oldGroups.get(i), userName); // make sure 'all' is among the groups we set newGroups.add("all"); eliminateDuplicates(newGroups); // add the new groups for (int i = 0; i < newGroups.size(); i++) addUserToGroup(user, (String)newGroups.get(i), userName); } /** * Sets for a group the new list of users that belong to this group. * * @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 owner of the group. * @param groupName The name of the group whose users will be changed. * @param newUsers The list of the new users. */ public synchronized void setUsersForGroup(String user, String groupName, Vector newUsers) throws DBConfigException { // check arguments for validity if (user == null || groupName == null || newUsers == null) throw new DBConfigException("Invalid setUsersForGroup() parameters!"); try { for (int i = 0; i < newUsers.size(); i++) { String u = (String)newUsers.get(i); if (u == null) throw new DBConfigException("Invalid setUsersForGroup() parameters!"); } } catch (ClassCastException e) { throw new DBConfigException("Invalid setUsersForGroup() parameters!"); } // if one of the following does not exist // a DBConfigException is thrown ID uID = getUid(user); ID gID = getGid(groupName); // 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 // look for gid in the list of groups // if uid is not the owner of gid a DBConfigException // is thrown check_owner_group(uID, gID); } // if this point is reached uid is in admin or // uid is the owner of the group gid // get a list of old users belonging to the group Vector oldUsers = listUsersForGroup(groupName); String groupOwner = getGroupOwner(groupName); // delete all the users from the old_users for (int i = 0; i < oldUsers.size(); i++) if (!groupOwner.equals((String)oldUsers.get(i))) removeUserFromGroup(user, groupName, (String)oldUsers.get(i)); eliminateDuplicates(newUsers); // add the new users for (int i = 0; i < newUsers.size(); i++) addUserToGroup(user, groupName, (String)newUsers.get(i)); } // in the following user related methods, variable name refers to // user name. /** * Adds a new user and specify to what groups it belongs. * * @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 added. * @param password The password of the user to be added. * @param permissions The permissions of the user to be added. * @param groups The list of groups to which the user to be added belongs. */ public synchronized void addUser(String user, String name, String password, long permissions, Vector groups) throws DBConfigException { // check arguments for validity if (user == null || name == null || password == null || groups == null) throw new DBConfigException("Invalid addUser() parameters!");
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -