Skip to content

Commit 8b5bfb1

Browse files
GaOrtigaGabriel Ortiga FernandesDaanHoogland
authored
create parameter to determine whether roles are public or private (#6960)
Co-authored-by: Gabriel Ortiga Fernandes <gabriel.fernandes@scclouds.com.br> Co-authored-by: dahn <daan.hoogland@gmail.com>
1 parent 957c0a5 commit 8b5bfb1

File tree

19 files changed

+435
-57
lines changed

19 files changed

+435
-57
lines changed

api/src/main/java/org/apache/cloudstack/acl/Role.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,5 @@
2323
public interface Role extends RoleEntity, InternalIdentity, Identity {
2424
RoleType getRoleType();
2525
boolean isDefault();
26+
boolean isPublicRole();
2627
}

api/src/main/java/org/apache/cloudstack/acl/RoleService.java

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,17 @@ public interface RoleService {
3838
* Moreover, we will check if the requested role is of 'Admin' type; roles with 'Admin' type should only be visible to 'root admins'.
3939
* Therefore, if a non-'root admin' user tries to search for an 'Admin' role, this method will return null.
4040
*/
41+
Role findRole(Long id, boolean removePrivateRoles);
42+
4143
Role findRole(Long id);
4244

43-
Role createRole(String name, RoleType roleType, String description);
45+
Role createRole(String name, RoleType roleType, String description, boolean publicRole);
4446

45-
Role createRole(String name, Role role, String description);
47+
Role createRole(String name, Role role, String description, boolean publicRole);
4648

47-
Role importRole(String name, RoleType roleType, String description, List<Map<String, Object>> rules, boolean forced);
49+
Role importRole(String name, RoleType roleType, String description, List<Map<String, Object>> rules, boolean forced, boolean isPublicRole);
4850

49-
Role updateRole(Role role, String name, RoleType roleType, String description);
51+
Role updateRole(Role role, String name, RoleType roleType, String description, Boolean publicRole);
5052

5153
boolean deleteRole(Role role);
5254

api/src/main/java/org/apache/cloudstack/api/command/admin/acl/CreateRoleCmd.java

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,10 @@ public class CreateRoleCmd extends RoleCmd {
4848
description = "ID of the role to be cloned from. Either roleid or type must be passed in")
4949
private Long roleId;
5050

51+
@Parameter(name = ApiConstants.IS_PUBLIC, type = CommandType.BOOLEAN, description = "Indicates whether the role will be visible to all users (public) or only to root admins (private)." +
52+
" If this parameter is not specified during the creation of the role its value will be defaulted to true (public).")
53+
private boolean publicRole = true;
54+
5155
/////////////////////////////////////////////////////
5256
/////////////////// Accessors ///////////////////////
5357
/////////////////////////////////////////////////////
@@ -60,6 +64,9 @@ public Long getRoleId() {
6064
return roleId;
6165
}
6266

67+
public boolean isPublicRole() {
68+
return publicRole;
69+
}
6370
/////////////////////////////////////////////////////
6471
/////////////// API Implementation///////////////////
6572
/////////////////////////////////////////////////////
@@ -81,10 +88,10 @@ public void execute() {
8188
}
8289

8390
CallContext.current().setEventDetails("Role: " + getRoleName() + ", from role: " + getRoleId() + ", description: " + getRoleDescription());
84-
role = roleService.createRole(getRoleName(), existingRole, getRoleDescription());
91+
role = roleService.createRole(getRoleName(), existingRole, getRoleDescription(), isPublicRole());
8592
} else {
8693
CallContext.current().setEventDetails("Role: " + getRoleName() + ", type: " + getRoleType() + ", description: " + getRoleDescription());
87-
role = roleService.createRole(getRoleName(), getRoleType(), getRoleDescription());
94+
role = roleService.createRole(getRoleName(), getRoleType(), getRoleDescription(), isPublicRole());
8895
}
8996

9097
if (role == null) {

api/src/main/java/org/apache/cloudstack/api/command/admin/acl/ImportRoleCmd.java

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,10 @@ public class ImportRoleCmd extends RoleCmd {
6464
description = "Force create a role with the same name. This overrides the role type, description and rule permissions for the existing role. Default is false.")
6565
private Boolean forced;
6666

67+
@Parameter(name = ApiConstants.IS_PUBLIC, type = CommandType.BOOLEAN, description = "Indicates whether the role will be visible to all users (public) or only to root admins (private)." +
68+
" If this parameter is not specified during the creation of the role its value will be defaulted to true (public).")
69+
private boolean publicRole = true;
70+
6771
@Inject
6872
ApiServerService _apiServer;
6973

@@ -114,6 +118,10 @@ public boolean isForced() {
114118
return (forced != null) ? forced : false;
115119
}
116120

121+
public boolean isPublicRole() {
122+
return publicRole;
123+
}
124+
117125
/////////////////////////////////////////////////////
118126
/////////////// API Implementation///////////////////
119127
/////////////////////////////////////////////////////
@@ -130,7 +138,7 @@ public void execute() {
130138
}
131139

132140
CallContext.current().setEventDetails("Role: " + getRoleName() + ", type: " + getRoleType() + ", description: " + getRoleDescription());
133-
Role role = roleService.importRole(getRoleName(), getRoleType(), getRoleDescription(), getRules(), isForced());
141+
Role role = roleService.importRole(getRoleName(), getRoleType(), getRoleDescription(), getRules(), isForced(), isPublicRole());
134142
if (role == null) {
135143
throw new ServerApiException(ApiErrorCode.INTERNAL_ERROR, "Failed to import role");
136144
}

api/src/main/java/org/apache/cloudstack/api/command/admin/acl/ListRolesCmd.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,7 @@ private void setupResponse(final Pair<List<Role>, Integer> roles) {
9292
roleResponse.setRoleType(role.getRoleType());
9393
roleResponse.setDescription(role.getDescription());
9494
roleResponse.setIsDefault(role.isDefault());
95+
roleResponse.setPublicRole(role.isPublicRole());
9596
roleResponse.setObjectName("role");
9697
roleResponses.add(roleResponse);
9798
}
@@ -104,7 +105,7 @@ private void setupResponse(final Pair<List<Role>, Integer> roles) {
104105
public void execute() {
105106
Pair<List<Role>, Integer> roles;
106107
if (getId() != null && getId() > 0L) {
107-
roles = new Pair<List<Role>, Integer>(Collections.singletonList(roleService.findRole(getId())), 1);
108+
roles = new Pair<>(Collections.singletonList(roleService.findRole(getId(), true)), 1);
108109
} else if (StringUtils.isNotBlank(getName()) || StringUtils.isNotBlank(getKeyword())) {
109110
roles = roleService.findRolesByName(getName(), getKeyword(), getStartIndex(), getPageSizeVal());
110111
} else if (getRoleType() != null) {

api/src/main/java/org/apache/cloudstack/api/command/admin/acl/RoleCmd.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ protected void setupResponse(final Role role) {
5858
response.setRoleName(role.getName());
5959
response.setRoleType(role.getRoleType());
6060
response.setDescription(role.getDescription());
61+
response.setPublicRole(role.isPublicRole());
6162
response.setResponseName(getCommandName());
6263
response.setObjectName("role");
6364
setResponseObject(response);

api/src/main/java/org/apache/cloudstack/api/command/admin/acl/UpdateRoleCmd.java

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,9 @@ public class UpdateRoleCmd extends RoleCmd {
5252
@Parameter(name = ApiConstants.DESCRIPTION, type = BaseCmd.CommandType.STRING, description = "The description of the role")
5353
private String roleDescription;
5454

55+
@Parameter(name = ApiConstants.IS_PUBLIC, type = CommandType.BOOLEAN, description = "Indicates whether the role will be visible to all users (public) or only to root admins (private).")
56+
private Boolean publicRole;
57+
5558
/////////////////////////////////////////////////////
5659
/////////////////// Accessors ///////////////////////
5760
/////////////////////////////////////////////////////
@@ -64,6 +67,10 @@ public String getRoleName() {
6467
return roleName;
6568
}
6669

70+
public Boolean isPublicRole() {
71+
return publicRole;
72+
}
73+
6774
/////////////////////////////////////////////////////
6875
/////////////// API Implementation///////////////////
6976
/////////////////////////////////////////////////////
@@ -80,7 +87,7 @@ public void execute() {
8087
throw new ServerApiException(ApiErrorCode.PARAM_ERROR, "Invalid role id provided");
8188
}
8289
CallContext.current().setEventDetails("Role: " + getRoleName() + ", type:" + getRoleType() + ", description: " + getRoleDescription());
83-
role = roleService.updateRole(role, getRoleName(), getRoleType(), getRoleDescription());
90+
role = roleService.updateRole(role, getRoleName(), getRoleType(), getRoleDescription(), isPublicRole());
8491
setupResponse(role);
8592
}
8693

api/src/main/java/org/apache/cloudstack/api/response/BaseRoleResponse.java

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,11 @@ public class BaseRoleResponse extends BaseResponse {
3636
@Param(description = "the description of the role")
3737
private String roleDescription;
3838

39+
@SerializedName(ApiConstants.IS_PUBLIC)
40+
@Param(description = "Indicates whether the role will be visible to all users (public) or only to root admins (private)." +
41+
" If this parameter is not specified during the creation of the role its value will be defaulted to true (public).")
42+
private boolean publicRole = true;
43+
3944
public void setId(String id) {
4045
this.id = id;
4146
}
@@ -47,4 +52,8 @@ public void setRoleName(String roleName) {
4752
public void setDescription(String description) {
4853
this.roleDescription = description;
4954
}
55+
56+
public void setPublicRole(boolean publicRole) {
57+
this.publicRole = publicRole;
58+
}
5059
}

api/src/test/java/org/apache/cloudstack/api/command/test/CreateRoleCmdTest.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ public void testCreateRoleWithRoleType() {
5454
when(role.getDescription()).thenReturn("User test");
5555
when(role.getName()).thenReturn("testuser");
5656
when(role.getRoleType()).thenReturn(RoleType.User);
57-
when(roleService.createRole(createRoleCmd.getRoleName(), createRoleCmd.getRoleType(), createRoleCmd.getRoleDescription())).thenReturn(role);
57+
when(roleService.createRole(createRoleCmd.getRoleName(), createRoleCmd.getRoleType(), createRoleCmd.getRoleDescription(), true)).thenReturn(role);
5858
createRoleCmd.execute();
5959
RoleResponse response = (RoleResponse) createRoleCmd.getResponseObject();
6060
Assert.assertEquals((String) ReflectionTestUtils.getField(response, "roleName"), role.getName());
@@ -71,7 +71,7 @@ public void testCreateRoleWithExistingRole() {
7171
when(newRole.getDescription()).thenReturn("User test");
7272
when(newRole.getName()).thenReturn("testuser");
7373
when(newRole.getRoleType()).thenReturn(RoleType.User);
74-
when(roleService.createRole(createRoleCmd.getRoleName(), role, createRoleCmd.getRoleDescription())).thenReturn(newRole);
74+
when(roleService.createRole(createRoleCmd.getRoleName(), role, createRoleCmd.getRoleDescription(), true)).thenReturn(newRole);
7575
createRoleCmd.execute();
7676
RoleResponse response = (RoleResponse) createRoleCmd.getResponseObject();
7777
Assert.assertEquals((String) ReflectionTestUtils.getField(response, "roleName"), newRole.getName());

api/src/test/java/org/apache/cloudstack/api/command/test/ImportRoleCmdTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ public void testImportRoleSuccess() {
9393
when(role.getDescription()).thenReturn("test user imported");
9494
when(role.getName()).thenReturn("Test User");
9595
when(role.getRoleType()).thenReturn(RoleType.User);
96-
when(roleService.importRole(anyString(),any(), anyString(), any(), anyBoolean())).thenReturn(role);
96+
when(roleService.importRole(anyString(), any(), anyString(), any(), anyBoolean(), anyBoolean())).thenReturn(role);
9797

9898
importRoleCmd.execute();
9999
RoleResponse response = (RoleResponse) importRoleCmd.getResponseObject();

0 commit comments

Comments
 (0)