Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

S2U-36 5.2.1.1 Announcements - Ability to assign announcements to a role #12124

Merged
merged 1 commit into from
Nov 27, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -348,3 +348,18 @@ view=View
sort_by_title_tooltip=Sort by title
sort_by_site_tooltip=Sort by title
widget_title=Announcements

### S2U-36 Announcements - Ability to assign announcements to a role
java.alert.youchooserole = You need to choose at least one role.
gen.therearerole=There are currently no role announcements at this location.
displayto.roles = Display this announcement <strong>to selected roles</strong> only

view.byrole = By role
roles = Roles
announcement.label.selectedroles = A list of roles to select

announcement.edit.selectallroles = Select All Roles
announcement.edit.selectonerole = You must select at least one role
announcement.edit.allselectedroles = All roles selected
announcement.edit.rolesselected = roles selected
announcement.edit.searchrole = Search role
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,18 @@ view=Mostra
sort_by_title_tooltip=Ordena per t\u00edtol
sort_by_site_tooltip=Ordena per t\u00edtol
widget_title=Anuncis

### S2U-36 Announcements - Ability to assign announcements to a role
java.alert.youchooserole=Ha de triar almenys un rol.
gen.therearerole=Actualment, no hi ha avisos per a rols en aquesta ubicaci\u00f3.
displayto.roles=Mostra aquest av\u00eds nom\u00e9s als <strong> rols seleccionats </strong>

view.byrole=Per rol
roles=Rols
announcement.label.selectedroles=Una llista de rols per seleccionar

announcement.edit.selectallroles = Selecciona tots els rols
announcement.edit.selectonerole = Cal que seleccioneu almenys un rol
announcement.edit.allselectedroles = Tots els rols est\u00e1n seleccionats
announcement.edit.rolesselected = rols seleccionats
announcement.edit.searchrole = Cerca rol
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,18 @@ view=Mostrar
sort_by_title_tooltip=Ordenar por t\u00edtulo
sort_by_site_tooltip=Ordenar por sitio
widget_title=Anuncios

### S2U-36 Announcements - Ability to assign announcements to a role
java.alert.youchooserole=Debe escoger al menos un rol.
gen.therearerole=Actualmente, no existen anuncios para roles en esta ubicaci\u00f3n.
displayto.roles=Mostrar a los roles seleccionados

view.byrole=Por rol
roles=Roles
announcement.label.selectedroles=Una lista de roles por seleccionar

announcement.edit.selectallroles = Selecciona todos los Roles
announcement.edit.selectonerole = Debe seleccionar al menos un rol
announcement.edit.allselectedroles = Todos los roles seleccionados
announcement.edit.rolesselected = roles seleccionados
announcement.edit.searchrole = Buscar rol
Original file line number Diff line number Diff line change
Expand Up @@ -347,3 +347,18 @@ view=Bistaratu
sort_by_title_tooltip=Ordenatu izenburuaren arabera
sort_by_site_tooltip=Ordenatu izenburuaren arabera
widget_title=Oharrak

### S2U-36 Announcements - Ability to assign announcements to a role
java.alert.youchooserole=Rola bat aukeratu behar duzu, gutxienez.
gen.therearerole=Orain ez dago ohar-talderik rolakleku honetan.
displayto.roles=Bistaratu aukeratutako rolak

view.byrole=Rolaren arabera
roles=Rolak
announcement.label.selectedroles=Hautatzeko rolen zerrenda

announcement.edit.selectallroles=Hautatu Rol guztiak
announcement.edit.selectonerole=Gutxienez rol bat hautatu behar duzu
announcement.edit.allselectedroles=Hautatutako rol guztiak
announcement.edit.rolesselected=hautatutako rolak
announcement.edit.searchrole=Bilaketa rola
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ public class AnnouncementsUserNotificationHandler extends AbstractUserNotificati
@Resource(name = "org.sakaiproject.springframework.orm.hibernate.GlobalTransactionManager")
private PlatformTransactionManager transactionManager;

private static final String SELECTED_ROLES_PROPERTY = "selectedRoles";

@Override
public List<String> getHandledEvents() {

Expand Down Expand Up @@ -172,6 +174,19 @@ protected void doInTransactionWithoutResult(TransactionStatus status) {
usersList.addAll(site.getGroup(group).getUsersIsAllowed(AnnouncementService.SECURE_ANNC_READ));
}
}

if (message.getProperties().getPropertyList(SELECTED_ROLES_PROPERTY) != null) {
Set<String> usersListAux = new HashSet<>();
ArrayList<String> selectedRolesList = new ArrayList<String>(message.getProperties().getPropertyList(SELECTED_ROLES_PROPERTY));
for (String selectedRole : selectedRolesList) {
for (String userId: usersList) {
if (site.getMember(userId).getRole().getId().equalsIgnoreCase(selectedRole)) {
usersListAux.add(userId);
}
}
}
usersList = new HashSet<>(usersListAux);
}

for (String to : usersList) {
if (!from.equals(to) && !securityService.isSuperUser(to)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
package org.sakaiproject.announcement.impl;

import java.time.Instant;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

Expand Down Expand Up @@ -408,12 +409,54 @@ private String getAddress(Event event, AddressField field)
}

/**
* Add to the user list any other users who should be notified about this ref's change.
* @inheritDoc
*/
protected List<User> getRecipients(Event event)
{
Reference ref = entityManager.newReference(event.getResource());
String siteId = (getSite() != null) ? getSite() : ref.getContext();
try {
Site site = siteService.getSite(siteId);
List<User> users = super.getRecipients(event);

// remove the not allowed users
removeNotAllowedRecipients(users, ref, site);
return users;
} catch (Exception any) {
return new ArrayList<User>();
}
}

/**
* Remove in the user list any other users who shouldn't be notified
*
* @param users
* The user list, already populated based on site visit and resource ability.
* a list of users for message
* @param ref
* The entity reference.
* @param site
* The site where the announcement resides
*/
protected void removeNotAllowedRecipients(List<User> users, Reference ref, Site site)
{
final AnnouncementMessage msg = (AnnouncementMessage) ref.getEntity();
if (msg.getProperties().getPropertyList("selectedRoles") == null) {
return;
}
users.removeIf( u -> {
if (site.getMember(u.getId()) != null) {
for (String role : msg.getProperties().getPropertyList("selectedRoles")) {
if (role.equals(site.getUserRole(u.getId()).getId())) {
return false;
}
}
}
return true;
});
}

/**
* @inheritDoc
*/
protected void addSpecialRecipients(List users, Reference ref)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Calendar;
import java.util.Collection;
import java.util.Collections;
import java.util.Comparator;
import java.util.Date;
Expand All @@ -47,6 +48,7 @@
import org.sakaiproject.announcement.tool.AnnouncementAction;
import org.sakaiproject.announcement.tool.AnnouncementWrapper;
import org.sakaiproject.announcement.tool.AnnouncementWrapperComparator;
import org.sakaiproject.authz.api.Member;
import org.sakaiproject.authz.api.SecurityService;
import org.sakaiproject.component.api.ServerConfigurationService;
import org.sakaiproject.component.cover.ComponentManager;
Expand Down Expand Up @@ -277,9 +279,28 @@ private List<?> getAnnouncements(String siteId, Map<String,Object> params, boole

for (Message m : announcements) {
AnnouncementMessage a = (AnnouncementMessage)m;
boolean showAnnouncement = false;
List selectedRolesList = a.getProperties().getPropertyList("selectedRoles");
String ownerId = a.getAnnouncementHeader().getFrom().getId();
boolean isOwner = ownerId.equals(currentUserId);
if (isOwner || securityService.isSuperUser() || selectedRolesList == null) {
showAnnouncement = true;
} else {
Member currentMember = site.getMember(currentUserId);
String memberRole = currentMember.getRole().getId();
ArrayList<String> selectedRolesArray = new ArrayList<String>(selectedRolesList);
String[] selectedRoles = selectedRolesArray.toArray(new String[selectedRolesArray.size()]);
for (String selectedRole : selectedRoles) {
if (memberRole.equals(selectedRole)) {
showAnnouncement = true;
}
}
}
try {
DecoratedAnnouncement da = createDecoratedAnnouncement(a, siteTitle);
decoratedAnnouncements.add(da);
if (showAnnouncement) {
DecoratedAnnouncement da = createDecoratedAnnouncement(a, siteTitle);
decoratedAnnouncements.add(da);
}
} catch (Exception e) {
//this can throw an exception if we are not logged in, ie public, this is fine so just deal with it and continue
log.info("Exception caught processing announcement: {} for user: {}. Skipping...", m.getId(), currentUserId);
Expand Down Expand Up @@ -335,6 +356,13 @@ private DecoratedAnnouncement createDecoratedAnnouncement(AnnouncementMessage a,
da.setSiteId(siteId);
da.setSiteTitle(siteTitle);
da.setHighlight(a.getProperties().getProperty("highlight"));
List selectedRolesList = a.getProperties().getPropertyList("selectedRoles");
ArrayList<String> selectedRolesArray = null;
if (selectedRolesList != null) {
selectedRolesArray = new ArrayList<String>(selectedRolesList);
String[] selectedRoles = selectedRolesArray.toArray(new String[selectedRolesArray.size()]);
da.setSelectedRoles(selectedRoles);
}

//get attachments
List<DecoratedAttachment> attachments = new ArrayList<DecoratedAttachment>();
Expand Down Expand Up @@ -738,6 +766,7 @@ public class DecoratedAnnouncement implements Comparable<Object> {
@Getter @Setter private String siteId;
@Getter @Setter private String announcementId;
@Getter @Setter private String highlight;
@Getter @Setter private String[] selectedRoles;
@Getter @Setter private String siteTitle;
@Getter @Setter private String channel;

Expand Down
Loading