From 7414d835253107cc82e28fe4c757d59d4910329c Mon Sep 17 00:00:00 2001 From: jensp Date: Tue, 7 Mar 2017 18:17:37 +0000 Subject: [PATCH] CCM NG/ccm-cms: Several bug fixes for RoleAdminPane git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@4623 8810af33-2d31-482b-a856-94f89814c4df Former-commit-id: 56ba52be5ec5d27180dacc6103a44fde4140e904 --- .../cms/ui/role/BaseRoleItemPane.java | 15 ++- .../cms/ui/role/MemberTableModelBuilder.java | 10 +- .../arsdigita/cms/ui/role/RoleAdminPane.java | 10 +- .../cms/ui/role/RoleAdminPaneController.java | 95 +++++++++++++++++++ .../arsdigita/cms/ui/role/RoleListModel.java | 29 +++--- .../cms/ui/role/RoleRequestLocal.java | 17 ++-- 6 files changed, 141 insertions(+), 35 deletions(-) create mode 100644 ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleAdminPaneController.java diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/BaseRoleItemPane.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/BaseRoleItemPane.java index 67ab524d8..7d5d1b45f 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/BaseRoleItemPane.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/BaseRoleItemPane.java @@ -18,7 +18,6 @@ */ package com.arsdigita.cms.ui.role; - import com.arsdigita.bebop.ActionLink; import com.arsdigita.bebop.FormProcessException; import com.arsdigita.bebop.Label; @@ -151,15 +150,15 @@ class BaseRoleItemPane extends BaseItemPane { properties.add(new Property(lz("cms.ui.name"), role.getName())); // Right now just loads the default locale description. - properties.add(new Property(lz("cms.ui.description"), - role.getDescription().getValue( - config - .getDefaultLocale()))); + properties.add(new Property( + lz("cms.ui.description"), + role.getDescription().getValue(config.getDefaultLocale()))); // Since Permissions don't seem to have a "pretty" form, the granted privilege is used. - final String permissions = role.getPermissions().stream() - .map(Permission::getGrantedPrivilege) - .collect(Collectors.joining(", ")); + final RoleAdminPaneController controller = cdiUtil.findBean( + RoleAdminPaneController.class); + final String permissions = controller + .generateGrantedPermissionsString(role); if (permissions.length() > 0) { properties.add(new Property(lz("cms.ui.role.privileges"), diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/MemberTableModelBuilder.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/MemberTableModelBuilder.java index 36f574831..7c912317c 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/MemberTableModelBuilder.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/MemberTableModelBuilder.java @@ -58,12 +58,10 @@ class MemberTableModelBuilder extends AbstractTableModelBuilder { final PageState state) { final Role role = roleRequestLocal.getRole(state); - final List members = role.getMemberships() - .stream() - .map(membership -> membership.getMember()) - .collect(Collectors.toList()); - members.sort((member1, member2) -> member1.getName().compareTo( - member2.getName())); + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final RoleAdminPaneController controller = cdiUtil.findBean( + RoleAdminPaneController.class); + final List members = controller.createRoleMemberList(role); return new Model(members); } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleAdminPane.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleAdminPane.java index 1d8a1cdb7..33863c56f 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleAdminPane.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleAdminPane.java @@ -59,7 +59,7 @@ import org.librecms.contentsection.privileges.AdminPrivileges; * * @author Justin Ross <jross@redhat.com> * @author Yannick Bülter - * @authr Jens Pelzetter + * @author Jens Pelzetter * */ public class RoleAdminPane extends BaseAdminPane { @@ -172,7 +172,13 @@ public class RoleAdminPane extends BaseAdminPane { public final ListModel makeModel(final List list, final PageState state) { final ContentSection section = CMS.getContext().getContentSection(); - return new RoleListModel(section.getRoles()); + final CdiUtil cdiUtil = CdiUtil.createCdiUtil(); + final RoleAdminPaneController controller = cdiUtil + .findBean(RoleAdminPaneController.class); + final java.util.List roles = controller + .findRolesForContentSection(section); + + return new RoleListModel(roles); } } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleAdminPaneController.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleAdminPaneController.java new file mode 100644 index 000000000..020b62010 --- /dev/null +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleAdminPaneController.java @@ -0,0 +1,95 @@ +/* + * Copyright (C) 2017 LibreCCM Foundation. + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2.1 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, + * MA 02110-1301 USA + */ +package com.arsdigita.cms.ui.role; + +import org.libreccm.security.Party; +import org.libreccm.security.Permission; +import org.libreccm.security.Role; +import org.libreccm.security.RoleRepository; +import org.librecms.contentsection.ContentSection; +import org.librecms.contentsection.ContentSectionManager; +import org.librecms.contentsection.ContentSectionRepository; + +import java.util.ArrayList; +import java.util.List; +import java.util.stream.Collectors; + +import javax.enterprise.context.RequestScoped; +import javax.inject.Inject; +import javax.transaction.Transactional; + +/** + * + * @author Jens Pelzetter + */ +@RequestScoped +public class RoleAdminPaneController { + + @Inject + private ContentSectionRepository sectionRepo; + + @Inject + private ContentSectionManager sectionManager; + + @Inject + private RoleRepository roleRepo; + + @Transactional(Transactional.TxType.REQUIRED) + public List findRolesForContentSection(final ContentSection section) { + final ContentSection contentSection = sectionRepo + .findById(section.getObjectId()) + .orElseThrow(() -> new IllegalArgumentException(String.format( + "No ContentSection with id %d in the database. " + + "Where did that ID come from?", + section.getObjectId()))); + + return new ArrayList<>(contentSection.getRoles()); + } + + @Transactional(Transactional.TxType.REQUIRED) + public String generateGrantedPermissionsString(final Role role) { + final Role theRole = roleRepo + .findById(role.getRoleId()) + .orElseThrow(() -> new IllegalArgumentException(String.format( + "No role with ID %d in the database. Where did that Id come from?", + role.getRoleId()))); + + return theRole.getPermissions().stream() + .map(Permission::getGrantedPrivilege) + .collect(Collectors.joining(", ")); + } + + @Transactional(Transactional.TxType.REQUIRED) + public List createRoleMemberList(final Role role) { + final Role theRole = roleRepo + .findById(role.getRoleId()) + .orElseThrow(() -> new IllegalArgumentException(String.format( + "No role with ID %d in the database. Where did that Id come from?", + role.getRoleId()))); + + return theRole.getMemberships() + .stream() + .map(membership -> membership.getMember()) + .sorted((member1, member2) -> { + return member1.getName().compareTo(member2.getName()); + }) + .collect(Collectors.toList()); + } + +} diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleListModel.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleListModel.java index 0df105732..8d99874d8 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleListModel.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleListModel.java @@ -19,38 +19,44 @@ package com.arsdigita.cms.ui.role; import com.arsdigita.bebop.list.ListModel; + import org.libreccm.security.Role; import java.util.Collection; import java.util.Iterator; +import java.util.List; /** - * Provides a {@link ListModel} implementation for Collections of Roles. - * This class is usable like an iterator, with an exception. - * The {@link #next()} method only moves the iterator forward. To get elements you need to first - * use {@link #next()} and afterwards {@link #getRole()}, {@link #getElement()} or {@link #getKey()}. + * Provides a {@link ListModel} implementation for Collections of Roles. This + * class is usable like an iterator, with an exception. The {@link #next()} + * method only moves the iterator forward. To get elements you need to first use + * {@link #next()} and afterwards {@link #getRole()}, {@link #getElement()} or + * {@link #getKey()}. * - * Also remember that the iterator does not move unless {@link #next()} is called. + * Also remember that the iterator does not move unless {@link #next()} is + * called. * * @author Yannick Bülter */ class RoleListModel implements ListModel { - private final Collection m_roles; - + private final List m_roles; private Iterator iterator; - private Role currentRole; - RoleListModel(final Collection roles) { + RoleListModel(final List roles) { m_roles = roles; iterator = roles.iterator(); } @Override public final boolean next() { - currentRole = iterator.next(); - return currentRole != null; + if (iterator.hasNext()) { + currentRole = iterator.next(); + return true; + } else { + return false; + } } @Override @@ -74,4 +80,5 @@ class RoleListModel implements ListModel { public final void reset() { iterator = m_roles.iterator(); } + } diff --git a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleRequestLocal.java b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleRequestLocal.java index 573960294..6b1f06e0c 100755 --- a/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleRequestLocal.java +++ b/ccm-cms/src/main/java/com/arsdigita/cms/ui/role/RoleRequestLocal.java @@ -20,22 +20,23 @@ package com.arsdigita.cms.ui.role; import com.arsdigita.bebop.PageState; import com.arsdigita.bebop.RequestLocal; -import com.arsdigita.util.Assert; + import org.libreccm.security.Role; -/** +import java.util.Optional; + +/** * See {@link RequestLocal} for more information. * * @author Yannick Bülter - * @version $Id: RoleRequestLocal.java 287 2005-02-22 00:29:02Z sskracic $ */ abstract class RoleRequestLocal extends RequestLocal { final Role getRole(final PageState state) { - final Role role = (Role) get(state); - - Assert.exists(role, "Role role"); - - return role; + @SuppressWarnings("unchecked") + final Optional role = (Optional) get(state); + + return role.get(); } + }