|
| 1 | +package com.docusign.controller.eSignature.examples; |
| 2 | + |
| 3 | +import com.docusign.DSConfiguration; |
| 4 | +import com.docusign.common.WorkArguments; |
| 5 | +import com.docusign.controller.eSignature.services.SharedAccessService; |
| 6 | +import com.docusign.core.model.DoneExample; |
| 7 | +import com.docusign.core.model.Session; |
| 8 | +import com.docusign.core.model.User; |
| 9 | +import com.docusign.esign.api.AccountsApi; |
| 10 | +import com.docusign.esign.api.EnvelopesApi; |
| 11 | +import com.docusign.esign.api.UsersApi; |
| 12 | +import com.docusign.esign.client.ApiClient; |
| 13 | +import com.docusign.esign.client.ApiException; |
| 14 | +import com.docusign.esign.client.auth.OAuth; |
| 15 | +import com.docusign.esign.model.EnvelopesInformation; |
| 16 | +import com.docusign.esign.model.NewUsersSummary; |
| 17 | +import com.docusign.esign.model.UserInformation; |
| 18 | +import io.swagger.annotations.Api; |
| 19 | +import org.springframework.beans.factory.annotation.Autowired; |
| 20 | +import org.springframework.http.MediaType; |
| 21 | +import org.springframework.stereotype.Controller; |
| 22 | +import org.springframework.ui.ModelMap; |
| 23 | +import org.springframework.web.bind.annotation.RequestMapping; |
| 24 | +import org.springframework.web.bind.annotation.RequestMethod; |
| 25 | + |
| 26 | +import javax.servlet.http.HttpServletResponse; |
| 27 | + |
| 28 | +import static com.docusign.controller.eSignature.examples.EG043ControllerSharedAccess.EG_043; |
| 29 | + |
| 30 | +@Controller |
| 31 | +@RequestMapping(EG_043) |
| 32 | +public class EG043ControllerSharedAccess extends AbstractEsignatureController { |
| 33 | + public static final String EG_043 = "/eg043"; |
| 34 | + public static final String ACTIVATE_USER = "/activateUser"; |
| 35 | + public static final String LOGOUT = "/logout"; |
| 36 | + public static final String LIST_ENVELOPES = "/listEnvelopes"; |
| 37 | + public static final String ACT_ON_BEHALF = "X-DocuSign-Act-On-Behalf"; |
| 38 | + public static final String HOME_PAGE = "/"; |
| 39 | + private final Session session; |
| 40 | + private final User user; |
| 41 | + |
| 42 | + @Autowired |
| 43 | + public EG043ControllerSharedAccess(DSConfiguration config, Session session, User user) { |
| 44 | + super(config, EG_043); |
| 45 | + this.session = session; |
| 46 | + this.user = user; |
| 47 | + } |
| 48 | + |
| 49 | + @Override |
| 50 | + protected Object doWork(WorkArguments args, ModelMap model, |
| 51 | + HttpServletResponse response) throws Exception { |
| 52 | + String basePath = session.getBasePath(); |
| 53 | + String accessToken = user.getAccessToken(); |
| 54 | + |
| 55 | + UsersApi usersApi = createUsersApi(basePath, accessToken); |
| 56 | + |
| 57 | + UserInformation userInformation = (new SharedAccessService()).checkIfAgentExists( |
| 58 | + usersApi, |
| 59 | + session.getAccountId(), |
| 60 | + args.getAgentEmail() |
| 61 | + ); |
| 62 | + |
| 63 | + if (userInformation == null) { |
| 64 | + NewUsersSummary usersSummary = (new SharedAccessService()).createAgent( |
| 65 | + usersApi, |
| 66 | + session.getAccountId(), |
| 67 | + args.getAgentEmail(), |
| 68 | + args.getAgentName(), |
| 69 | + args.getActivationCode()); |
| 70 | + |
| 71 | + this.session.setCreatedUserId(usersSummary.getNewUsers().get(0).getUserId()); |
| 72 | + |
| 73 | + DoneExample.createDefault("Agent user created") |
| 74 | + .withMessage(getTextForCodeExampleByApiType().ResultsPageText) |
| 75 | + .withJsonObject(usersSummary) |
| 76 | + .withRedirect(EG_043 + ACTIVATE_USER) |
| 77 | + .addToModel(model, config); |
| 78 | + } else { |
| 79 | + this.session.setCreatedUserId(userInformation.getUserId()); |
| 80 | + |
| 81 | + DoneExample.createDefault("Agent user created") |
| 82 | + .withMessage(getTextForCodeExampleByApiType().ResultsPageText) |
| 83 | + .withJsonObject(userInformation) |
| 84 | + .withRedirect(EG_043 + ACTIVATE_USER) |
| 85 | + .addToModel(model, config); |
| 86 | + } |
| 87 | + |
| 88 | + return DONE_EXAMPLE_PAGE; |
| 89 | + } |
| 90 | + |
| 91 | + @RequestMapping(value = ACTIVATE_USER, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) |
| 92 | + public Object activateUser(ModelMap model) throws ApiException { |
| 93 | + String basePath = session.getBasePath(); |
| 94 | + String accessToken = user.getAccessToken(); |
| 95 | + |
| 96 | + AccountsApi accountsApi = createAccountsApi(basePath, accessToken); |
| 97 | + OAuth.UserInfo user = getCurrentUserId(basePath, accessToken); |
| 98 | + String userId = user.getSub(); |
| 99 | + |
| 100 | + try { |
| 101 | + (new SharedAccessService()).activateAgent( |
| 102 | + accountsApi, |
| 103 | + session.getAccountId(), |
| 104 | + userId, |
| 105 | + session.getCreatedUserId()); |
| 106 | + |
| 107 | + this.config.setPrincipalUserId(userId); |
| 108 | + this.config.setShareAccessExampleScenario(true); |
| 109 | + this.config.setAdditionalRedirect(true); |
| 110 | + |
| 111 | + DoneExample.createDefault("Authenticate as the agent") |
| 112 | + .withMessage(getTextForCodeExampleByApiType().getAdditionalPage().get(0).ResultsPageText) |
| 113 | + .withRedirect(LOGOUT) |
| 114 | + .addToModel(model, config); |
| 115 | + } catch(ApiException exception) { |
| 116 | + DoneExample.createDefault("Authenticate as the agent") |
| 117 | + .withMessage(getTextForCodeExampleByApiType().getAdditionalPage().get(3).ResultsPageText) |
| 118 | + .withRedirect(EG_043 + ACTIVATE_USER) |
| 119 | + .addToModel(model, config); |
| 120 | + } |
| 121 | + |
| 122 | + return DONE_EXAMPLE_PAGE; |
| 123 | + } |
| 124 | + |
| 125 | + @RequestMapping(value = LIST_ENVELOPES, method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE) |
| 126 | + public Object listEnvelopes(ModelMap model) throws ApiException { |
| 127 | + String basePath = session.getBasePath(); |
| 128 | + String accessToken = user.getAccessToken(); |
| 129 | + |
| 130 | + ApiClient apiClient = createApiClient(basePath, accessToken); |
| 131 | + apiClient.addDefaultHeader(ACT_ON_BEHALF, this.config.getPrincipalUserId()); |
| 132 | + EnvelopesApi envelopesApi = new EnvelopesApi(apiClient); |
| 133 | + |
| 134 | + EnvelopesInformation envelopesInformation = (new SharedAccessService()).getEnvelopes( |
| 135 | + envelopesApi, |
| 136 | + session.getAccountId()); |
| 137 | + |
| 138 | + if(envelopesInformation.getEnvelopes() == null || envelopesInformation.getEnvelopes().isEmpty()){ |
| 139 | + DoneExample.createDefault("No envelopes in the principal user's account") |
| 140 | + .withMessage(getTextForCodeExampleByApiType().getAdditionalPage().get(2).ResultsPageText) |
| 141 | + .withRedirect(HOME_PAGE) |
| 142 | + .addToModel(model, config); |
| 143 | + } else { |
| 144 | + DoneExample.createDefault("Principal's envelopes visible in the agent's Shared Access UI") |
| 145 | + .withMessage(getTextForCodeExampleByApiType().getAdditionalPage().get(1).ResultsPageText) |
| 146 | + .withJson(envelopesInformation.toString()) |
| 147 | + .withRedirect(HOME_PAGE) |
| 148 | + .addToModel(model, config); |
| 149 | + } |
| 150 | + |
| 151 | + return DONE_EXAMPLE_PAGE; |
| 152 | + } |
| 153 | +} |
0 commit comments