View Javadoc
1   /*
2    * Copyright 2017 the original author or authors.
3    *
4    *  Licensed under the Apache License, Version 2.0 (the "License");
5    *  you may not use this file except in compliance with the License.
6    *  You may obtain a copy of the License at
7    *
8    *         http://www.apache.org/licenses/LICENSE-2.0
9    *
10   *  Unless required by applicable law or agreed to in writing, software
11   *  distributed under the License is distributed on an "AS IS" BASIS,
12   *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   *  See the License for the specific language governing permissions and
14   *  limitations under the License.
15   */
16  package org.openehealth.ipf.commons.audit.event;
17  
18  
19  import org.openehealth.ipf.commons.audit.AuditException;
20  import org.openehealth.ipf.commons.audit.codes.*;
21  import org.openehealth.ipf.commons.audit.types.PurposeOfUse;
22  
23  import java.util.Collections;
24  
25  /**
26   * Builds an User Authentication representing a Network Entry event as specified in
27   * http://dicom.nema.org/medical/dicom/current/output/html/part15.html#sect_A.5.3.12
28   * <p>
29   * This message describes the event that a user has attempted to log on or log off.
30   * This report can be made regardless of whether the attempt was successful or not.
31   * No Participant Objects are needed for this message.
32   * </p>
33   * <p>
34   * The user usually has UserIsRequestor TRUE, but in the case of a logout timer,
35   * the Node might be the UserIsRequestor.
36   * </p>
37   *
38   * @author Christian Ohr
39   * @since 3.5
40   */
41  public class UserAuthenticationBuilder extends BaseAuditMessageBuilder<UserAuthenticationBuilder> {
42  
43      public UserAuthenticationBuilder(EventOutcomeIndicator outcome,
44                                       String eventOutcomeDescription,
45                                       EventTypeCode eventTypeCode,
46                                       PurposeOfUse... purposesOfUse) {
47          super();
48          setEventIdentification(outcome,
49                  eventOutcomeDescription,
50                  EventActionCode.Execute,
51                  EventIdCode.UserAuthentication,
52                  eventTypeCode,
53                  purposesOfUse
54          );
55      }
56  
57      public UserAuthenticationBuilder setAuthenticatedParticipant(String userId, String networkId) {
58          return setAuthenticatedParticipant(userId, null, null, true, null, networkId);
59      }
60  
61      /**
62       * Sets the Active Participant of the Node or System entering or leaving the network
63       *
64       * @param userId    The person or process accessing the audit trail. If both are known,
65       *                  then two active participants shall be included (both the person and the process).
66       * @param altUserId The Active Participant's Alternate UserID
67       * @param userName  The Active Participant's UserName
68       * @param networkId The Active Participant's Network Access Point ID
69       */
70      public UserAuthenticationBuilder setAuthenticatedParticipant(String userId,
71                                                                   String altUserId,
72                                                                   String userName,
73                                                                   boolean userIsRequestor,
74                                                                   ActiveParticipantRoleIdCode roleId,
75                                                                   String networkId) {
76          return addActiveParticipant(
77                  userId,
78                  altUserId,
79                  userName,
80                  userIsRequestor,
81                  roleId != null ? Collections.singletonList(roleId) : Collections.emptyList(),
82                  networkId);
83      }
84  
85      public UserAuthenticationBuilder setAuthenticatingSystemParticipant(String userId, String networkId) {
86          return setAuthenticatingSystemParticipant(userId, null, null, true, null, networkId);
87      }
88  
89      /**
90       * Node or System performing authentication
91       *
92       * @param userId    The Active Participant's UserID
93       * @param altUserId The Active Participant's Alternate UserID
94       * @param userName  The Active Participant's UserName
95       * @param networkId The Active Participant's Network Access Point ID
96       */
97      public UserAuthenticationBuilder setAuthenticatingSystemParticipant(String userId,
98                                                                          String altUserId,
99                                                                          String userName,
100                                                                         boolean userIsRequestor,
101                                                                         ActiveParticipantRoleIdCode roleId,
102                                                                         String networkId) {
103         return addActiveParticipant(
104                 userId,
105                 altUserId,
106                 userName,
107                 userIsRequestor,
108                 Collections.singletonList(roleId),
109                 networkId);
110     }
111 
112     public static class Login extends UserAuthenticationBuilder {
113 
114         public Login(EventOutcomeIndicator outcome, PurposeOfUse... purposeOfUse) {
115             this(outcome, null, purposeOfUse);
116         }
117 
118         public Login(EventOutcomeIndicator outcome, String eventOutcomeDescription, PurposeOfUse... purposeOfUse) {
119             super(outcome, eventOutcomeDescription, EventTypeCode.Login, purposeOfUse);
120         }
121     }
122 
123     public static class Logout extends UserAuthenticationBuilder {
124 
125         public Logout(EventOutcomeIndicator outcome, PurposeOfUse... purposeOfUse) {
126             this(outcome, null, purposeOfUse);
127         }
128 
129         public Logout(EventOutcomeIndicator outcome, String eventOutcomeDescription, PurposeOfUse... purposeOfUse) {
130             super(outcome, eventOutcomeDescription, EventTypeCode.Logout, purposeOfUse);
131         }
132     }
133 
134     @Override
135     public void validate() {
136         super.validate();
137         int participants = getMessage().getActiveParticipants().size();
138         if (participants < 1 || participants > 2) {
139             throw new AuditException("Must have one or two ActiveParticipants");
140         }
141     }
142 }