Skip to content

Commit

Permalink
Merge pull request #18 from rollbar/wj-enable-uninstall
Browse files Browse the repository at this point in the history
Enable package uninstall
  • Loading branch information
waltjones authored Jun 5, 2020
2 parents 7c48124 + 1521d4e commit 8da5e23
Show file tree
Hide file tree
Showing 5 changed files with 246 additions and 57 deletions.
62 changes: 62 additions & 0 deletions force-app/main/default/classes/EmailServiceInstaller.cls
Original file line number Diff line number Diff line change
Expand Up @@ -88,4 +88,66 @@ public with sharing class EmailServiceInstaller {
Http h = new Http();
HttpResponse res = h.send(req);
}

public static void deleteEmailServiceFunction(EmailServicesFunction emailServiceFunction)
{
String baseUrl = URL.getOrgDomainUrl().toExternalForm() + '/services/Soap/u/48.0';

HTTPRequest req = new HTTPRequest();
req.setEndpoint(baseUrl);
req.setMethod('POST');
req.setHeader('SOAPAction', '""');
req.setHeader('Content-Type', 'text/xml');
req.setBody(
'<?xml version="1.0" encoding="utf-8"?>' +
'<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">' +
'<env:Header>' +
'<urn:SessionHeader>' +
'<urn:sessionId>'+UserInfo.getSessionId()+'</urn:sessionId>' +
'</urn:SessionHeader>' +
'</env:Header>' +
'<env:Body>' +
'<urn:delete>' +
'<urn:ids>' +
emailServiceFunction.Id +
'</urn:ids>' +
'</urn:delete>' +
'</env:Body>' +
'</env:Envelope>'
);

Http h = new Http();
HttpResponse res = h.send(req);
}

public static void deleteEmailServiceAddress(EmailServicesAddress emailServiceAddress)
{
String baseUrl = URL.getOrgDomainUrl().toExternalForm() + '/services/Soap/u/48.0';

HTTPRequest req = new HTTPRequest();
req.setEndpoint(baseUrl);
req.setMethod('POST');
req.setHeader('SOAPAction', '""');
req.setHeader('Content-Type', 'text/xml');
req.setBody(
'<?xml version="1.0" encoding="utf-8"?>' +
'<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:urn="urn:partner.soap.sforce.com">' +
'<env:Header>' +
'<urn:SessionHeader>' +
'<urn:sessionId>'+UserInfo.getSessionId()+'</urn:sessionId>' +
'</urn:SessionHeader>' +
'</env:Header>' +
'<env:Body>' +
'<urn:delete>' +
'<urn:ids>' +
emailServiceAddress.Id +
'</urn:ids>' +
'</urn:delete>' +
'</env:Body>' +
'</env:Envelope>'
);

Http h = new Http();
HttpResponse res = h.send(req);
}
}
21 changes: 20 additions & 1 deletion force-app/main/default/classes/RollbarConfigureController.cls
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,27 @@ public with sharing class RollbarConfigureController {
return null;
}

public Pagereference uninstall() {
RollbarInstaller.prepareUninstall();

this.settings = RollbarSettings__c.getOrgDefaults();
this.settings.PreparedForUninstall__c = true;
upsert this.settings;
updateConfigState();
return null;
}

public Pagereference reinstall() {
this.settings.PreparedForUninstall__c = false;
upsert this.settings;
updateConfigState();
return null;
}

public void updateConfigState() {
if (!this.settings.Initialized__c) {
if (this.settings.PreparedForUninstall__c) {
this.configState = 'ConfigState.UNINSTALL';
} else if (!this.settings.Initialized__c) {
this.configState = 'ConfigState.INIT';
} else if (!this.settings.SalesforceApiEnabled__c) {
this.configState = 'ConfigState.SF_API';
Expand Down
126 changes: 111 additions & 15 deletions force-app/main/default/classes/RollbarInstaller.cls
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,22 @@ public with sharing class RollbarInstaller {
return RollbarInstaller.enableServices(config.endpoint());
}

@future(callout=true)
public static void prepareUninstall()
{
RollbarSettings__c settings = RollbarSettings__c.getOrgDefaults();

deleteApexEmailNotification();
deleteEmailServicesFunction();
deleteRemoteSiteSetting();

settings.RollbarApiEnabled__c = remoteSiteSettingSetUp();
settings.EmailServiceEnabled__c = rollbarEmailServiceSetUp();
settings.NotificationForwardingEnabled__c = apexNotificationsForwardingSetUp();

upsert settings;
}

public static String enableServices(String apiDomain)
{
String message = null;
Expand Down Expand Up @@ -103,26 +119,55 @@ public with sharing class RollbarInstaller {

public static Boolean rollbarApiEndpointAllowed(String endpoint)
{
MetadataService.RemoteSiteSetting setting = remoteSiteSetting();

if (setting == null) {
return false;
}

if (setting.fullName != 'RollbarAPI') {
return false;
}

if (!endpoint.contains(setting.url)) {
return false;
}

return true;
}

public static MetadataService.RemoteSiteSetting remoteSiteSetting() {
MetadataService.MetadataPort service = MetadataService.createServiceWithSession();
MetadataService.ReadRemoteSiteSettingResult result = (MetadataService.ReadRemoteSiteSettingResult)service.readMetadata('RemoteSiteSetting', new String[] {'RollbarAPI'});

MetadataService.Metadata[] records = result.getRecords();

if (records.size() == 0) {
return false;
return null;
}

MetadataService.RemoteSiteSetting setting = (MetadataService.RemoteSiteSetting)result.getRecords()[0];

if (setting.fullName != 'RollbarAPI') {
return false;
if (setting.fullName == 'RollbarAPI') {
return setting;
} else {
return null;
}
}

if (!endpoint.contains(setting.url)) {
public static Boolean remoteSiteSettingSetUp() {
if (remoteSiteSetting() != null) {
return true;
} else {
return false;
}
}

return true;
public static void deleteRemoteSiteSetting() {
if (RollbarInstaller.remoteSiteSetting() != null) {
MetadataService.MetadataPort service = MetadataService.createServiceWithSession();
service.deleteMetadata('RemoteSiteSetting', new String[] {'RollbarAPI'});
}
}

public static Boolean rollbarPingSuccessful()
Expand All @@ -140,37 +185,88 @@ public with sharing class RollbarInstaller {
public static Boolean rollbarEmailServiceSetUp()
{
try {
EmailServicesFunction emailFunction = [SELECT Id FROM EmailServicesFunction WHERE FunctionName='RollbarEmailService'];
EmailServicesAddress emailServicesAddress = [SELECT Id, LocalPart, EmailDomainName FROM EmailServicesAddress WHERE FunctionId = :emailFunction.Id];
EmailServicesFunction emailFunction = emailServicesFunction();
EmailServicesAddress emailServicesAddress = emailServicesAddress(emailFunction);
} catch (Exception ex) {
return false;
}

return true;
}

public static Boolean apexNotificationsForwardingSetUp()
{
public static EmailServicesFunction emailServicesFunction() {
return [SELECT Id FROM EmailServicesFunction WHERE FunctionName='RollbarEmailService'];
}

public static EmailServicesAddress emailServicesAddress(EmailServicesFunction emailFunction) {
return [SELECT Id, LocalPart, EmailDomainName FROM EmailServicesAddress WHERE FunctionId = :emailFunction.Id];
}

public static ApexEmailNotification apexEmailNotification() {
EmailServicesFunction emailFunction;
EmailServicesAddress emailServicesAddress;
try {
emailFunction = [SELECT Id FROM EmailServicesFunction WHERE FunctionName='RollbarEmailService'];
emailServicesAddress = [SELECT Id, LocalPart, EmailDomainName FROM EmailServicesAddress WHERE FunctionId = :emailFunction.Id];
emailFunction = emailServicesFunction();
emailServicesAddress = emailServicesAddress(emailFunction);
} catch (Exception ex) {
return false;
return null;
}

String emailServiceAddress = emailServicesAddress.LocalPart + '@' + emailServicesAddress.EmailDomainName;

Boolean forwarding = false;
ApexEmailNotification[] apexNotifications = [SELECT Email, UserId FROM ApexEmailNotification];
for (ApexEmailNotification notification : apexNotifications) {
if (notification.Email == emailServiceAddress) {
forwarding = true;
return notification;
}
}

return forwarding;
return null; // Not found
}

public static Boolean apexNotificationsForwardingSetUp()
{
ApexEmailNotification notification = apexEmailNotification();

if (notification != null) {
return true;
}
return false;
}

public static void deleteApexEmailNotification() {
ApexEmailNotification notification = ApexEmailNotification();

if (notification != null) {
String baseUrl = URL.getSalesforceBaseUrl().toExternalForm() + '/services/data/v35.0/tooling/';

HTTPRequest req = new HTTPRequest();
req.setEndpoint(baseUrl + 'sobjects/ApexEmailNotification/' + notification.Id + '/');
req.setMethod('DELETE');
req.setHeader('Authorization', 'Bearer ' + UserInfo.getSessionId());
req.setHeader('Content-Type', 'application/json');

Http h = new Http();
HttpResponse res = h.send(req);
}
}

public static void deleteEmailServicesFunction() {
EmailServicesFunction emailServicesFunction = EmailServicesFunction();

if (emailServicesFunction != null) {
deleteEmailServicesAddress(emailServicesFunction);

EmailServiceInstaller.deleteEmailServiceFunction(emailServicesFunction);
}
}

public static void deleteEmailServicesAddress(EmailServicesFunction emailServicesFunction) {
EmailServicesAddress emailServicesAddress = EmailServicesAddress(emailServicesFunction);

if (emailServicesAddress != null) {
EmailServiceInstaller.deleteEmailServiceAddress(emailServicesAddress);
}
}

public static Boolean accessTokenCorrect(Config config)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
<?xml version="1.0" encoding="UTF-8"?>
<CustomField xmlns="http://soap.sforce.com/2006/04/metadata">
<fullName>PreparedForUninstall__c</fullName>
<defaultValue>false</defaultValue>
<externalId>false</externalId>
<label>Prepared For Uninstall</label>
<trackTrending>false</trackTrending>
<type>Checkbox</type>
</CustomField>
Loading

0 comments on commit 8da5e23

Please sign in to comment.