Skip to content

Commit

Permalink
MGR-80 show groups and users for a role (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
madness-inc committed Sep 16, 2019
1 parent b93bd81 commit be5c578
Show file tree
Hide file tree
Showing 6 changed files with 87 additions and 45 deletions.
6 changes: 6 additions & 0 deletions application-home/conf/datasources/ds-roles.xml
Expand Up @@ -76,6 +76,12 @@
<field name="permissions" type="list:select" binding="permissionIds">
<label>permissions</label>
</field>
<field name="groupIds" type="list:checkbox" readonly="true">
<label>groups</label>
</field>
<field name="subjectIds" type="list:checkbox" readonly="true">
<label>subjects</label>
</field>
</meta-data>
</config>
<bean id="roles">
Expand Down
18 changes: 6 additions & 12 deletions src/main/java/org/appng/application/manager/form/GroupForm.java
Expand Up @@ -22,13 +22,19 @@

import org.appng.core.domain.GroupImpl;

import lombok.Getter;
import lombok.Setter;

/**
* Bindclass used for creating/updating a {@link GroupImpl}.
*
* @author Matthias Müller
*
*/
@Getter
@Setter
public class GroupForm {

private GroupImpl group;

private List<Integer> roleIds = new ArrayList<Integer>();
Expand All @@ -46,16 +52,4 @@ public GroupImpl getGroup() {
return group;
}

public void setGroup(GroupImpl group) {
this.group = group;
}

public List<Integer> getRoleIds() {
return roleIds;
}

public void setRoleIds(List<Integer> roleIds) {
this.roleIds = roleIds;
}

}
20 changes: 7 additions & 13 deletions src/main/java/org/appng/application/manager/form/RoleForm.java
Expand Up @@ -22,16 +22,23 @@

import org.appng.core.domain.RoleImpl;

import lombok.Getter;
import lombok.Setter;

/**
* Bindclass used for creating/updating a {@link RoleImpl}.
*
* @author Matthias Müller
*
*/
@Getter
@Setter
public class RoleForm {

private RoleImpl role;
private List<Integer> permissionIds = new ArrayList<Integer>();
private List<Integer> groupIds = new ArrayList<Integer>();
private List<Integer> userIds = new ArrayList<Integer>();

public RoleForm() {
this(new RoleImpl());
Expand All @@ -45,17 +52,4 @@ public RoleForm(RoleImpl role) {
public RoleImpl getRole() {
return role;
}

public void setRole(RoleImpl role) {
this.role = role;
}

public List<Integer> getPermissionIds() {
return permissionIds;
}

public void setPermissionIds(List<Integer> permissionIds) {
this.permissionIds = permissionIds;
}

}
Expand Up @@ -313,19 +313,15 @@ public DataContainer searchGroups(FieldProcessor fp, Site site, Integer siteId,
if (null == group) {
throw new BusinessException("no such group : " + groupId, MessageConstants.GROUP_NOT_EXISTS);
}
Selection selection = getRoleSelection(group, site.getId());

List<String> users = group.getSubjects().stream()
.map(s -> String.format("%s (%s)", s.getAuthName(), s.getRealname())).sorted()
.collect(Collectors.toList());
if (!users.isEmpty()) {
Selection subjects = new SelectionBuilder<String>("subjects").title(MessageConstants.SUBJECTS)
.type(SelectionType.CHECKBOX).options(users).select(users).build();
Selection roleSelection = getRoleSelection(group, site.getId());
data.getSelections().add(roleSelection);

if (!group.getSubjects().isEmpty()) {
Selection subjects = getSubjectSelection(group.getSubjects(), "subjects");
data.getSelections().add(subjects);
} else {
fp.getMetaData().getFields().remove(fp.getField("group.subjects"));
}
data.getSelections().add(selection);
data.setItem(new GroupForm(group));
} else {
Selection nameFilter = new SelectionBuilder<String>(FILTER_GROUP_NAME)
Expand All @@ -341,6 +337,13 @@ public DataContainer searchGroups(FieldProcessor fp, Site site, Integer siteId,
return data;
}

private Selection getSubjectSelection(Collection<? extends Subject> subjects, String id) {
List<String> users = subjects.stream().map(s -> String.format("%s (%s)", s.getAuthName(), s.getRealname()))
.sorted().collect(Collectors.toList());
return new SelectionBuilder<String>(id).title(MessageConstants.SUBJECTS).type(SelectionType.CHECKBOX)
.options(users).select(users).disable(users).build();
}

private Selection getRoleSelection(Group group, Integer siteId) {
SiteImpl site = siteRepository.findOne(siteId);
Selection selection = selectionFactory.fromObjects("roles", "roles", new Object[0], (Selector) null);
Expand Down Expand Up @@ -453,9 +456,8 @@ public DataContainer searchPackageVersions(Request request, FieldProcessor fp, I
}

/**
* Returns a {@link Packages}-object from a certain repository. {@link Packages}
* are made available to other appNG instances via the
* {@link RepositoryService}.
* Returns a {@link Packages}-object from a certain repository. {@link Packages} are made available to other appNG
* instances via the {@link RepositoryService}.
*/
public Packages searchPackages(Environment environment, FieldProcessor fp, String repositoryName, String digest,
String packageName) throws BusinessException {
Expand All @@ -469,9 +471,8 @@ public Packages searchPackages(Environment environment, FieldProcessor fp, Strin
}

/**
* Returns a {@link PackageVersions}-object from a certain repository.
* {@link PackageVersions} are made available to other appNG instances via the
* {@link RepositoryService}.
* Returns a {@link PackageVersions}-object from a certain repository. {@link PackageVersions} are made available to
* other appNG instances via the {@link RepositoryService}.
*/
public PackageVersions searchPackageVersions(Environment environment, FieldProcessor fp, String repositoryName,
String packageName, String digest) throws BusinessException {
Expand Down Expand Up @@ -702,6 +703,30 @@ public DataContainer searchRole(FieldProcessor fp, Integer roleId, Integer appId
if (null == role) {
throw new BusinessException("no such Role " + roleId, MessageConstants.ROLE_NOT_EXISTS);
}
List<GroupImpl> groupList = groupRepository.findGroupsForApplicationRole(roleId);
List<String> groups = groupList.stream().map(GroupImpl::getName).sorted().collect(Collectors.toList());
if (!groups.isEmpty()) {
Selection groupSelection = new SelectionBuilder<String>("groupIds").title(MessageConstants.GROUPS)
.type(SelectionType.CHECKBOX).options(groups).disable(groups).select(groups).build();
data.getSelections().add(groupSelection);
} else {
fp.getMetaData().getFields().remove(fp.getField("groupIds"));
}

SearchQuery<SubjectImpl> subjectQuery = subjectRepository.createSearchQuery();
subjectQuery.distinct();
subjectQuery.setAppendEntityAlias(false);
subjectQuery.join("join e.groups g");
subjectQuery.in("g", groupList);
List<SubjectImpl> subjectList = subjectRepository.search(subjectQuery, null).getContent();

if (!subjectList.isEmpty()) {
Selection subjectSelection = getSubjectSelection(subjectList, "subjectIds");
data.getSelections().add(subjectSelection);
} else {
fp.getMetaData().getFields().remove(fp.getField("subjectIds"));
}

Selection selection = getPermissionSelection(role.getApplication().getId(), role);
data.getSelections().add(selection);
RoleForm form = new RoleForm(role);
Expand Down Expand Up @@ -934,8 +959,8 @@ private void checkUniqueRepositoryName(Request request, Repository repository, F
}
}

public DataContainer searchSites(Environment environment, FieldProcessor fp, Integer siteId, String name, String domain)
throws BusinessException {
public DataContainer searchSites(Environment environment, FieldProcessor fp, Integer siteId, String name,
String domain) throws BusinessException {
DataContainer data = new DataContainer(fp);
if (siteId != null) {
SiteImpl site = siteRepository.findOne(siteId);
Expand All @@ -962,9 +987,8 @@ public DataContainer searchSites(Environment environment, FieldProcessor fp, Int
siteImpl.setStartupTime(site.getStartupTime());
}
}
Selection nameFilter = new SelectionBuilder<String>(FILTER_SITE_NAME)
.defaultOption(FILTER_SITE_NAME, name).title(MessageConstants.NAME).type(SelectionType.TEXT)
.select(name).build();
Selection nameFilter = new SelectionBuilder<String>(FILTER_SITE_NAME).defaultOption(FILTER_SITE_NAME, name)
.title(MessageConstants.NAME).type(SelectionType.TEXT).select(name).build();
Selection domainFilter = new SelectionBuilder<String>(FILTER_SITE_DOMAIN)
.defaultOption(FILTER_SITE_DOMAIN, domain).title(MessageConstants.DOMAIN).type(SelectionType.TEXT)
.select(domain).build();
Expand Down
12 changes: 12 additions & 0 deletions src/test/resources/xml/RoleTest-testShowOne.xml
Expand Up @@ -15,6 +15,12 @@
<field name="permissions" type="list:select" binding="permissionIds">
<label id="permissions">Permissions</label>
</field>
<field name="groupIds" type="list:checkbox" readonly="true" binding="role.groupIds">
<label id="groups">Groups</label>
</field>
<field name="subjectIds" type="list:checkbox" readonly="true" binding="role.subjectIds">
<label id="subjects">Users</label>
</field>
</meta-data>
</config>
<data>
Expand All @@ -36,6 +42,12 @@
<field name="permissions" type="list:select">
<value></value>
</field>
<field name="groupIds" type="list:checkbox">
<value></value>
</field>
<field name="subjectIds" type="list:checkbox">
<value></value>
</field>
</result>
</data>
</datasource>
12 changes: 12 additions & 0 deletions src/test/resources/xml/RoleTest-testUpdate-action.xml
Expand Up @@ -37,6 +37,12 @@
<field name="permissions" type="list:select" binding="permissionIds">
<label id="permissions">Permissions</label>
</field>
<field name="groupIds" type="list:checkbox" readonly="true" binding="role.groupIds">
<label id="groups">Groups</label>
</field>
<field name="subjectIds" type="list:checkbox" readonly="true" binding="role.subjectIds">
<label id="subjects">Users</label>
</field>
</meta-data>
</config>
<condition expression="${form_action eq 'update-role' and not empty appid and not empty roleid}" />
Expand Down Expand Up @@ -67,6 +73,12 @@
<field name="permissions" type="list:select">
<value></value>
</field>
<field name="groupIds" type="list:checkbox">
<value></value>
</field>
<field name="subjectIds" type="list:checkbox">
<value></value>
</field>
</result>
</data>
<bean id="roles">
Expand Down

0 comments on commit be5c578

Please sign in to comment.