📄 testchacc.java
字号:
}
protected void tearDown() {
removeOpenCms();
}
};
return wrapper;
}
/**
* Test the creation and deletion of access control entries and checks permissions of a test user.<p>
*
* @throws Throwable if something goes wrong
*/
public void testChaccAddRemove() throws Throwable {
echo("Testing adding and removing ACEs on files and folders");
CmsObject cms = getCmsObject();
cms.createGroup("Testgroup", "A test group", 0, null);
CmsGroup testGroup = cms.readGroup("Testgroup");
cms.createUser("testuser", "test", "A test user", null);
cms.addUserToGroup("testuser", "Testgroup");
CmsUser testUser = cms.readUser("testuser");
CmsProject offline = cms.readProject("Offline");
String resName = "/folder2/";
cms.lockResource(resName);
cms.chacc(resName, I_CmsPrincipal.PRINCIPAL_USER, testUser.getName(), "+r+w+v+i");
cms.chacc(resName, I_CmsPrincipal.PRINCIPAL_GROUP, testGroup.getName(), "+r+v+i");
cms.unlockResource(resName);
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
CmsPermissionSet permissions = new CmsPermissionSet(CmsPermissionSet.PERMISSION_READ
| CmsPermissionSet.PERMISSION_VIEW
| CmsPermissionSet.PERMISSION_WRITE, 0);
// check set permissions for the test user
cms.loginUser("testuser", "test");
cms.getRequestContext().setCurrentProject(offline);
cms.lockResource(resName);
assertTrue(cms.hasPermissions(cms.readResource(resName), permissions));
assertTrue(cms.hasPermissions(cms.readResource("/folder2/index.html"), permissions));
assertFalse(cms.hasPermissions(cms.readResource("/folder1/"), permissions));
cms.unlockResource(resName);
// switch back to Admin user and remove ACE
cms.loginUser("Admin", "admin");
cms.getRequestContext().setCurrentProject(offline);
cms.lockResource(resName);
cms.rmacc(resName, I_CmsPrincipal.PRINCIPAL_USER, testUser.getName());
cms.unlockResource(resName);
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
cms.loginUser("testuser", "test");
cms.getRequestContext().setCurrentProject(offline);
assertFalse(cms.hasPermissions(cms.readResource(resName), CmsPermissionSet.ACCESS_WRITE));
cms.loginUser("Admin", "admin");
cms.getRequestContext().setCurrentProject(offline);
cms.lockResource(resName);
cms.rmacc(resName, I_CmsPrincipal.PRINCIPAL_GROUP, testGroup.getName());
cms.unlockResource(resName);
OpenCms.getPublishManager().publishProject(cms);
OpenCms.getPublishManager().waitWhileRunning();
// re-check permissions of test user after removing ACE
cms.loginUser("testuser", "test");
assertFalse(cms.hasPermissions(cms.readResource(resName), permissions));
}
/**
* Test the chacc method for the special 'all others' principal.<p>
*
* @throws Throwable if something goes wrong
*/
public void testChaccFileAllOthers() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing the chacc method for the special 'all others' principal");
CmsPermissionSet permissions = CmsPermissionSet.ACCESS_READ;
String resource = "/folder1/subfolder11/";
int flags = CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE
+ CmsAccessControlEntry.ACCESS_FLAGS_ALLOTHERS
+ CmsAccessControlEntry.ACCESS_FLAGS_INHERIT;
storeResources(cms, resource, true);
cms.lockResource(resource);
cms.chacc(
resource,
I_CmsPrincipal.PRINCIPAL_GROUP,
CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_NAME,
permissions.getAllowedPermissions(),
permissions.getDeniedPermissions(),
flags);
cms.unlockResource(resource);
// now evaluate the result
assertFilter(cms, resource, OpenCmsTestResourceFilter.FILTER_CHACC);
// test the ace of the new permission
// add the all others flag to the acl
CmsResource res = cms.readResource(resource, CmsResourceFilter.ALL);
CmsAccessControlEntry ace = new CmsAccessControlEntry(
res.getResourceId(),
CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_ID,
permissions.getAllowedPermissions(),
permissions.getDeniedPermissions(),
flags);
assertAce(cms, resource, ace);
// test the acl with the permission set
int denied = permissions.getDeniedPermissions();
if ((flags & CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE) > 0) {
denied = 0;
}
CmsPermissionSet permission = new CmsPermissionSet(permissions.getAllowedPermissions(), denied);
assertAcl(cms, resource, CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_ID, permission);
// now check all the subresources in the folder, access must be modified as well
List subresources = cms.readResources(resource, CmsResourceFilter.ALL);
Iterator j = subresources.iterator();
while (j.hasNext()) {
CmsResource subRes = (CmsResource)j.next();
String subResName = cms.getSitePath(subRes);
// now evaluate the result
assertFilter(cms, subResName, OpenCmsTestResourceFilter.FILTER_CHACC);
// test the ace of the new permission
// add the group and the inherited flag to the acl
ace = new CmsAccessControlEntry(
res.getResourceId(),
CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_ID,
permissions.getAllowedPermissions(),
permissions.getDeniedPermissions(),
flags + CmsAccessControlEntry.ACCESS_FLAGS_INHERITED);
assertAce(cms, subResName, ace);
// test the acl with the permission set
permission = new CmsPermissionSet(permissions.getAllowedPermissions(), denied);
assertAcl(cms, resource, subResName, CmsAccessControlEntry.PRINCIPAL_ALL_OTHERS_ID, permission);
}
}
/**
* Test the chacc method on a file and a group.<p>
*
* @throws Throwable if something goes wrong
*/
public void testChaccFileGroup() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing chacc on a file and a group");
chaccFileGroup(
this,
cms,
"/index.html",
cms.readGroup("Users"),
CmsPermissionSet.ACCESS_READ,
CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE);
}
/**
* Test the chacc method for the special 'overwrite all' principal.<p>
*
* @throws Throwable if something goes wrong
*/
public void testChaccFileOverwriteAll() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing the chacc method for the special 'overwrite all' principal");
CmsPermissionSet permissions = CmsPermissionSet.ACCESS_READ;
String resource = "/folder1/subfolder11/";
int flags = CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE
+ CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE_ALL
+ CmsAccessControlEntry.ACCESS_FLAGS_INHERIT;
storeResources(cms, resource, true);
cms.lockResource(resource);
cms.chacc(
resource,
I_CmsPrincipal.PRINCIPAL_GROUP,
CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_NAME,
permissions.getAllowedPermissions(),
permissions.getDeniedPermissions(),
flags);
cms.unlockResource(resource);
// now evaluate the result
assertFilter(cms, resource, OpenCmsTestResourceFilter.FILTER_CHACC);
// test the ace of the new permission
// add the all others flag to the acl
CmsResource res = cms.readResource(resource, CmsResourceFilter.ALL);
CmsAccessControlEntry ace = new CmsAccessControlEntry(
res.getResourceId(),
CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_ID,
permissions.getAllowedPermissions(),
permissions.getDeniedPermissions(),
flags);
assertAce(cms, resource, ace);
// test the acl with the permission set
int denied = permissions.getDeniedPermissions();
if ((flags & CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE) > 0) {
denied = 0;
}
CmsPermissionSet permission = new CmsPermissionSet(permissions.getAllowedPermissions(), denied);
assertAcl(cms, resource, CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_ID, permission);
// now check all the subresources in the folder, access must be modified as well
List subresources = cms.readResources(resource, CmsResourceFilter.ALL);
Iterator j = subresources.iterator();
while (j.hasNext()) {
CmsResource subRes = (CmsResource)j.next();
String subResName = cms.getSitePath(subRes);
// now evaluate the result
assertFilter(cms, subResName, OpenCmsTestResourceFilter.FILTER_CHACC);
// test the ace of the new permission
// add the group and the inherited flag to the acl
ace = new CmsAccessControlEntry(
res.getResourceId(),
CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_ID,
permissions.getAllowedPermissions(),
permissions.getDeniedPermissions(),
flags + CmsAccessControlEntry.ACCESS_FLAGS_INHERITED);
assertAce(cms, subResName, ace);
// test the acl with the permission set
permission = new CmsPermissionSet(permissions.getAllowedPermissions(), denied);
assertAcl(cms, resource, subResName, CmsAccessControlEntry.PRINCIPAL_OVERWRITE_ALL_ID, permission);
}
}
/**
* Test the chacc method on a file and a user.<p>
*
* @throws Throwable if something goes wrong
*/
public void testChaccFileUser() throws Throwable {
CmsObject cms = getCmsObject();
echo("Testing chacc on a file and a user");
chaccFileUser(this, cms, "/folder1/index.html", cms.readUser("Guest"), CmsPermissionSet.ACCESS_WRITE, 0);
}
/**
* Test the chacc method on a folder and a group.<p>
*
* @throws Throwable if something goes wrong
*/
public void testChaccFolderGroup() throws Throwable {
//TODO: This test is not working correctly so far!
CmsObject cms = getCmsObject();
echo("Testing chacc on a folder and a group");
chaccFolderGroup(
this,
cms,
"/folder2/",
cms.readGroup("Guests"),
CmsPermissionSet.ACCESS_READ,
CmsAccessControlEntry.ACCESS_FLAGS_OVERWRITE + CmsAccessControlEntry.ACCESS_FLAGS_INHERIT);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -