Skip to content

Commit

Permalink
Updated display rules and agent logic to be based on configured roles…
Browse files Browse the repository at this point in the history
… (allow_roles_*). #65
  • Loading branch information
JoelProminic committed Nov 12, 2024
1 parent 30665cc commit 2141edb
Show file tree
Hide file tree
Showing 25 changed files with 292 additions and 33 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public class CategoryCreate extends CategoryCreateBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_DOCUMENTATION_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public class CategoryDelete extends CategoryDeleteBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_DOCUMENTATION_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,10 @@
*/
public class CategoryRead extends CategoryReadBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_DOCUMENTATION_VIEW;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public class CategoryUpdate extends CategoryUpdateBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_DOCUMENTATION_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@
*/
public class CustomBookmarkCreate extends CustomBookmarkCreateBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_BOOKMARKS_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SecurityBuilder.ROLE_ADMINISTRATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
public class CustomBookmarkDelete extends CustomBookmarkDeleteBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_BOOKMARKS_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SecurityBuilder.ROLE_ADMINISTRATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
*/
public class CustomBookmarkRead extends CustomBookmarkReadBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_BOOKMARKS_VIEW;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
public class CustomBookmarkUpdate extends CustomBookmarkUpdateBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_BOOKMARKS_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SecurityBuilder.ROLE_ADMINISTRATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public class DatabaseRead extends CRUDAgentBase implements RoleRestrictedAgent

protected Map<String, Collection<String> > bookmarkCache = null;

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_BROWSE_MY_SERVER;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public class DocumentationFormCreate extends DocumentationFormCreateBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_DOCUMENTATION_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public class DocumentationFormDelete extends DocumentationFormDeleteBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_DOCUMENTATION_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public class DocumentationFormRead extends DocumentationFormReadBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_DOCUMENTATION_VIEW;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,10 @@
*/
public class DocumentationFormUpdate extends DocumentationFormUpdateBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_DOCUMENTATION_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
*/
public class GenesisDirectoryCreate extends GenesisDirectoryCreateBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_GENESIS_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SecurityBuilder.ROLE_ADMINISTRATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
public class GenesisDirectoryDelete extends GenesisDirectoryDeleteBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_GENESIS_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SecurityBuilder.ROLE_ADMINISTRATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@
*/
public class GenesisDirectoryRead extends GenesisDirectoryReadBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_GENESIS_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SecurityBuilder.ROLE_ADMINISTRATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
*/
public class GenesisDirectoryUpdate extends GenesisDirectoryUpdateBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return SecurityBuilder.RESTRICT_GENESIS_MANAGE;
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SecurityBuilder.ROLE_ADMINISTRATOR);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,14 @@
public interface RoleRestrictedAgent
{
/**
* Get the allowed roles for the agent. Should be used when building the securityi
* Get an ID that will be used to lookup the role restrictions for this agent.
* If not <code>null</code>, this will have priority over {@link #getAllowedRoles()}.
*/
public String getRoleRestrictionID();

/**
* Get the allowed roles for the agent. Should be used when building the security.
* Lower priority than getRoleRestrictionID
*/
public Collection<String> getAllowedRoles();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,17 @@ public class SecurityBuilder
{
public static final String ROLE_ADMINISTRATOR = "Administrator";

public static final String RESTRICT_DOCUMENTATION_VIEW = "viewDocumentation";
public static final String RESTRICT_DOCUMENTATION_MANAGE = "manageDocumentation";
public static final String RESTRICT_APPS_VIEW = "viewInstalledApps";
public static final String RESTRICT_APPS_INSTALL = "installApps";
public static final String RESTRICT_BOOKMARKS_VIEW = "viewBookmarks";
public static final String RESTRICT_BOOKMARKS_MANAGE = "manageBookmarks";
public static final String RESTRICT_BROWSE_MY_SERVER = "browseMyServer";
public static final String RESTRICT_GENESIS_MANAGE = "additionalGenesis";

public static SecurityInterface buildInstance(Database roleDatabase, RoleRestrictedAgent agent, Session session, LogInterface log) {
return new SimpleRoleSecurity(roleDatabase, agent.getAllowedRoles(), session, log);
return new SimpleRoleSecurity(roleDatabase, agent.getRoleRestrictionID(), agent.getAllowedRoles(), session, log);
}

public static Collection<String> buildList(String role1) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@
public class SimpleRoleSecurity extends SecurityInterface
{
protected LogInterface log = null;
protected Set<String> allowedRoles = new TreeSet<String>();
protected Set<String> allowedRoles = null; // Initialize when loaded. new TreeSet<String>();
protected Set<String> userRoles = new TreeSet<String>();
protected String roleRestrictionID = null;
protected Database roleDatabase = null;

protected Collection<String> userLookupKeys = new ArrayList<String>();
Expand Down Expand Up @@ -125,6 +126,33 @@ public SimpleRoleSecurity(Database roleDatabase, Collection<String> roles, Sessi
}
}

/**
* Initialize the security with a list of roles.
* Add additional roles with {@link #addAllowedRole(String)}.
* @param roleDatabase the role configuration database
* @param session the session
* @param log the log
*/
public SimpleRoleSecurity(Database roleDatabase, String roleRestrictionID, Collection<String> roles, Session session, LogInterface log)
{
this(roleDatabase, session, log);
if (!DominoUtils.isValueEmpty(roleRestrictionID)){
log.dbg("Using role restriction ID for agent security.");
this.roleRestrictionID = roleRestrictionID;
// initialize role list dynamically to avoid Domino calls in test cases
}
else {
if (null != roles && roles.size() > 0) {
for (String role : roles) {
addAllowedRole(role);
}
}
else {
log.err("No roles defined.");
}
}
}


/**
* See {@link #isAuthorizedForRoles()}.
Expand Down Expand Up @@ -375,7 +403,11 @@ public void refreshUserRoles() {
* With the default logic, this will return <code>true</code> if the user has any of the specified roles.
*/
public boolean isAuthorizedForRoles() {
for (String allowedRole : this.allowedRoles) {
return isAuthorizedForRoles(getAllowedRoles());
}

public boolean isAuthorizedForRoles(Collection<String> allowedRoles) {
for (String allowedRole : allowedRoles) {
if (userRoles.contains(allowedRole)) {
return true;
}
Expand All @@ -384,20 +416,70 @@ public boolean isAuthorizedForRoles() {
return false;
}

/**
* Utility for XMLAuthenticationTest to lookup restrictions for multiple IDs
*/
public boolean isAuthorizedForRoles(String roleRestrictionID) {
return isAuthorizedForRoles(getRolesByRestrictionID(roleRestrictionID));
}

/**
* Get the allowed roles for a user
*/
public Collection<String> getAllowedRoles() {
// initialize dynamically when using roleRestrictionID
if (null == allowedRoles) {
if (!DominoUtils.isValueEmpty(roleRestrictionID)) {
// make sure the roles are initialized regardless
allowedRoles = new TreeSet<String>();
allowedRoles.addAll(getRolesByRestrictionID(roleRestrictionID));
}
else {
log.dbg("No roles defined. Initializing empty list");
allowedRoles = new TreeSet<String>();
}
}
// return an independent copy to avoid edits
return new TreeSet<String>(allowedRoles);
}

/**
* Lookup the allowed roles for the given roleRestrictionID
*/
public Collection<String> getRolesByRestrictionID(String roleRestrictionID) {
TreeSet<String> roles = new TreeSet<String>();
try {
String key = "allow_roles_" + roleRestrictionID;
log.dbg("Looking up roles for key '" + key + "'");
Vector raw = ConfigurationUtils.getConfigAsVector(roleDatabase, key);
if (!DominoUtils.isListEmpty(raw)) {
for (Object curRole : raw) {
roles.add(curRole.toString());
}
}
else {
log.err("No roles defined for key '" + key + "'");
}
}
catch(Exception ex) {
log.err("Exception while loading roles for ID '" + roleRestrictionID + "': ", ex);
}
return roles;
}

public String getRoleRestrictionID() {
return roleRestrictionID;
}

/**
* Add an allowed role for this agent.
* @param role the role to add
* @return <code>true</code> if the role was not already added
*/
public boolean addAllowedRole(String role) {
if (null == allowedRoles) {
allowedRoles = new TreeSet<String>();
}
return allowedRoles.add(role);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
*/
public class XMLAuthenticationTest extends CRUDAgentBase implements RoleRestrictedAgent {

public String getRoleRestrictionID() {
return null; // allow all
}

public Collection<String> getAllowedRoles() {
return SecurityBuilder.buildList(SimpleRoleSecurity.ROLE_ALL);
}
Expand Down Expand Up @@ -211,13 +215,15 @@ else if (DominoUtils.isValueEmpty(vector.get(0).toString())) {

public JSONObject getDisplayRules(Database configDatabase) {
JSONObject display = new JSONObject();
display.put("documentation", shouldDisplay(configDatabase, "documentation"));
display.put("installApps", shouldDisplay(configDatabase, "installApps"));
display.put("genesisDirectory", shouldDisplay(configDatabase, "genesisDirectory"));
display.put("viewInstalledApps", shouldDisplay(configDatabase, "viewInstalledApps"));
display.put("viewBookmarks", shouldDisplay(configDatabase, "viewBookmarks"));
display.put("manageBookmarks", shouldDisplay(configDatabase, "manageBookmarks"));
display.put("browseMyServer", shouldDisplay(configDatabase, "browseMyServer"));
display.put("documentation", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_DOCUMENTATION_VIEW));
display.put("viewDocumentation", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_DOCUMENTATION_VIEW));
display.put("manageDocumentation", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_DOCUMENTATION_VIEW));
display.put("installApps", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_APPS_INSTALL));
display.put("additionalGenesis", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_GENESIS_MANAGE));
display.put("viewInstalledApps", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_APPS_VIEW));
display.put("viewBookmarks", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_BOOKMARKS_VIEW));
display.put("manageBookmarks", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_BOOKMARKS_MANAGE));
display.put("browseMyServer", shouldDisplay(configDatabase, SecurityBuilder.RESTRICT_BROWSE_MY_SERVER));
return display;
}

Expand All @@ -230,9 +236,11 @@ public JSONObject getDisplayRules(Database configDatabase) {
*/
public boolean shouldDisplay(Database configDatabase, String sectionID) {
try {
String value = ConfigurationUtils.getConfigAsString(configDatabase, "allow_" + sectionID);
return "true".equalsIgnoreCase(value);
// treat any other value as false
// // UI Testing
// String value = ConfigurationUtils.getConfigAsString(configDatabase, "allow_" + sectionID);
// return "true".equalsIgnoreCase(value);
// // treat any other value as false
return ((SimpleRoleSecurity)getSecurity()).isAuthorizedForRoles(sectionID);
}
catch (Exception ex) {
getLog().err("Exception when checking display rights for '" + sectionID + "'. Default to hidden");
Expand Down
Loading

0 comments on commit 2141edb

Please sign in to comment.