Skip to content

Commit

Permalink
Fix requested changes
Browse files Browse the repository at this point in the history
  • Loading branch information
rmiccoli committed Jul 12, 2024
1 parent 04a6db4 commit c89bbee
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 42 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -149,17 +149,15 @@ public void updateClientStatus(String clientId, boolean status, String userId) {
client = clientService.updateClientStatus(client, status, userId);
String message = "Client " + (status ? "enabled" : "disabled");
eventPublisher.publishEvent(new ClientStatusChangedEvent(this, client, message));
if (!client.getContacts().isEmpty()) {
notificationFactory.createClientStatusChangedMessage(client);
}
List<ScimUser> owners =
getClientOwners(client.getClientId(), PagingUtils.buildUnpagedPageRequest()).getResources();
if (!owners.isEmpty()) {
for (ScimUser owner : owners) {
notificationFactory.createClientStatusChangedMessageForClientOwners(client,
owner.getEmails().get(0).getValue());
}
}
notificationFactory.createClientStatusChangedMessageFor(client, getClientOwners(clientId));
}

private List<IamAccount> getClientOwners(String clientId) {
return clientService.findClientOwners(clientId, PagingUtils.buildUnpagedPageRequest())
.getContent()
.stream()
.map(IamAccountClient::getAccount)
.collect(Collectors.toList());
}

@Validated(OnClientUpdate.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
*/
package it.infn.mw.iam.notification;

import java.util.List;
import java.util.Optional;

import org.mitre.oauth2.model.ClientDetailsEntity;
Expand Down Expand Up @@ -42,8 +43,7 @@ public interface NotificationFactory {

IamEmailNotification createGroupMembershipRejectedMessage(IamGroupRequest groupRequest);

IamEmailNotification createClientStatusChangedMessage(ClientDetailsEntity client);
IamEmailNotification createClientStatusChangedMessageFor(ClientDetailsEntity client,
List<IamAccount> accounts);

IamEmailNotification createClientStatusChangedMessageForClientOwners(ClientDetailsEntity client,
String email);
}
Original file line number Diff line number Diff line change
Expand Up @@ -28,15 +28,16 @@
import java.util.UUID;
import java.util.stream.Collectors;

import freemarker.template.Configuration;
import freemarker.template.TemplateException;

import org.mitre.oauth2.model.ClientDetailsEntity;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;

import freemarker.template.Configuration;
import freemarker.template.Template;
import freemarker.template.TemplateException;
import it.infn.mw.iam.api.account.password_reset.PasswordResetController;
import it.infn.mw.iam.core.IamDeliveryStatus;
import it.infn.mw.iam.core.IamNotificationType;
Expand All @@ -47,8 +48,6 @@
import it.infn.mw.iam.persistence.model.IamGroupRequest;
import it.infn.mw.iam.persistence.model.IamNotificationReceiver;
import it.infn.mw.iam.persistence.model.IamRegistrationRequest;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import freemarker.template.Template;

public class TransientNotificationFactory implements NotificationFactory {

Expand Down Expand Up @@ -256,41 +255,31 @@ public IamEmailNotification createGroupMembershipRejectedMessage(IamGroupRequest
}

@Override
public IamEmailNotification createClientStatusChangedMessage(ClientDetailsEntity client) {
public IamEmailNotification createClientStatusChangedMessageFor(ClientDetailsEntity client,
List<IamAccount> accounts) {
Set<String> recipients = client.getContacts();

Map<String, Object> model = new HashMap<>();
model.put("clientId", client.getClientId());
model.put("clientName", client.getClientName());
model.put("clientStatus", client.isActive());
model.put("isClientActive", client.isActive());
model.put(ORGANISATION_NAME, organisationName);

String subject = "Changed client status";

List<String> contacts = new ArrayList<>(recipients);

IamEmailNotification notification = createMessage("clientStatusChanged.ftl", model,
IamNotificationType.CLIENT_STATUS, subject, contacts);

LOG.debug("Updated client status. Client id {}, active {}", client.getClientId(),
client.isActive());
return notification;
}

@Override
public IamEmailNotification createClientStatusChangedMessageForClientOwners(
ClientDetailsEntity client, String email) {
for (IamAccount a : accounts) {
recipients.add(a.getUserInfo().getEmail());
}

Map<String, Object> model = new HashMap<>();
model.put("clientId", client.getClientId());
model.put("clientName", client.getClientName());
model.put("clientStatus", client.isActive());
model.put(ORGANISATION_NAME, organisationName);
List<String> emails = new ArrayList<>(recipients);

String subject = "Changed client status";
if (emails.isEmpty()) {
LOG.warn("No emails to send notification to for client id {}", client.getClientId());
return null;
}

IamEmailNotification notification = createMessage("clientStatusChanged.ftl", model,
IamNotificationType.CLIENT_STATUS, subject, asList(email));
IamNotificationType.CLIENT_STATUS, subject, emails);

LOG.debug("Updated client status. Client id {}, active {}", client.getClientId(),
client.isActive());
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
Dear user,

this mail is to inform you that client ${clientName} with id ${clientId} has been <#if clientStatus>ACTIVATED<#else>SUSPENDED</#if>.
this mail is to inform you that client ${clientName} with id ${clientId} has been <#if isClientActive>ACTIVATED<#else>SUSPENDED</#if>.

The ${organisationName} registration service

0 comments on commit c89bbee

Please sign in to comment.