我正在尝试为 Windows NAS 服务器中的特定用户提供文件夹/子文件夹的只读权限,具体场景如下:
如果用户已经拥有某些权限,则删除所有权限。
为启用/禁用继承提供读取权限。
我尝试如下:
rem it's happing only for disable inheritance, how to do it for enable inheritance.
rem Remove access:
icacls NAS-path /remove:g UserNmae:(OI)(CI) /T
rem Provide read acces:
rem icacls NAS-path /grant UserNmae:(OI)(CI)R /T
我将如何获取用户名(谁在访问该路径)以便我可以为该用户申请?
答案1
/**
* Method responsible to get the permissions from provider folder
* @param path
* @return List of users
*/
public Map<String,String> getPermissionsFromProviderFolder(String path){
try {
Path files = Paths.get(path);
AclFileAttributeView aclFileAttributes = Files.getFileAttributeView(
files, AclFileAttributeView.class);
for (AclEntry aclEntry : aclFileAttributes.getAcl()) {
String user = aclEntry.principal().toString();
users.put(user,path); // users is a map defined
}
logger.logLoadOperation("folder security users list for :" + path, Level.INFO, null);
} catch (IOException e) {
e.printStackTrace();
}
return users;
}
/**
* Method responsible to set the permissions to result log folder
* @param path
* @param permission
*/
public void resetPermissionsToResultLogs(String resultLogFolder, String permission, Map<String,String> users) {
String command=null;
for (Map.Entry<String, String> userlist : users.entrySet()) {
String[] provider = userlist.getKey().split("\\(");
String user = provider[0].trim();
System.out.print("------location:-" + resultLogFolder);
System.out.print("------user:-----" + user);
System.out.println("");
if("remove".equalsIgnoreCase(permission)){
command = ICACLS +" "+ '"'+resultLogFolder+'"' +" /remove:g " + '"'+user+'"'+ ":(OI)(CI) /T";
System.out.println("remve= "+command);
}
if("read".equalsIgnoreCase(permission)){
command = ICACLS +" "+ '"'+resultLogFolder+'"' +" /grant " +'"'+user+'"'+ ":(OI)(CI)(R) /T";
System.out.println("grant read ="+command);
}
}
}
**我无法删除所有权限,但除此之外,一切正常。谢谢