Merge "Install tools/libs from doc hub image"
[aaf/authz.git] / auth / auth-deforg / src / main / java / org / onap / aaf / org / DefaultOrgIdentity.java
1 /*******************************************************************************
2  * ============LICENSE_START====================================================
3  * * org.onap.aaf
4  * * ===========================================================================
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
6  * * ===========================================================================
7  * * Licensed under the Apache License, Version 2.0 (the "License");
8  * * you may not use this file except in compliance with the License.
9  * * You may obtain a copy of the License at
10  * * 
11  *  *      http://www.apache.org/licenses/LICENSE-2.0
12  * * 
13  *  * Unless required by applicable law or agreed to in writing, software
14  * * distributed under the License is distributed on an "AS IS" BASIS,
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * * See the License for the specific language governing permissions and
17  * * limitations under the License.
18  * * ============LICENSE_END====================================================
19  * *
20  * *
21  ******************************************************************************/
22 package org.onap.aaf.org;
23
24 import java.io.IOException;
25 import java.util.List;
26
27 import org.onap.aaf.auth.env.AuthzTrans;
28 import org.onap.aaf.auth.local.AbsData.Reuse;
29 import org.onap.aaf.auth.org.Organization;
30 import org.onap.aaf.auth.org.OrganizationException;
31 import org.onap.aaf.auth.org.Organization.Identity;
32 import org.onap.aaf.cadi.config.Config;
33 import org.onap.aaf.org.Identities.Data;
34
35 /**
36  * Org Users are essential representations of Identities within the Org.  Since this is a highly individual 
37  * thing for most Orgs, i.e. some use LDAP, some need feed, some use something else, this object will allow
38  * the Organization to connect to their own Identity systems...
39  * 
40  *
41  */
42 public class DefaultOrgIdentity implements Identity {
43         private static final String CONTRACTOR = "c";
44         private static final String EMPLOYEE = "e";
45         private static final String APPLICATION = "a";
46         private static final String NON_ACTIVE = "n";
47
48         private final static int TIMEOUT = Integer.parseInt(Config.AAF_CONN_TIMEOUT_DEF);
49
50         private DefaultOrg org;
51         //package on purpose
52         Data identity;
53         private AuthzTrans trans;
54
55         public DefaultOrgIdentity(AuthzTrans trans, String key, DefaultOrg dorg) throws OrganizationException {
56                 this.trans = trans;
57                 org = dorg;
58                 identity=null;
59                 try {
60                         org.identities.open(trans, TIMEOUT);
61                         try {
62                                 Reuse r = org.identities.reuse();
63                                 int at = key.indexOf(dorg.getDomain());
64                                 String search;
65                                 if(at>=0) {
66                                         search = key.substring(0,at);
67                                 } else {
68                                         search = key;
69                                 }
70                                 identity = org.identities.find(search, r);
71
72
73
74                                 if(identity==null) {
75                                         identity = Identities.NO_DATA;
76                                 }
77                         } finally {
78                                 org.identities.close(trans);
79                         }
80                 } catch (IOException e) {
81                         throw new OrganizationException(e);
82                 }
83         }
84
85         @Override
86         public boolean equals(Object b) {
87                 if(b instanceof DefaultOrgIdentity) {
88                         return identity.id.equals(((DefaultOrgIdentity)b).identity.id);
89                 }
90                 return false;
91         }
92
93
94         @Override
95         public int hashCode() {
96                 return identity.hashCode();
97         }
98
99         @Override
100         public String id() {
101                 return identity.id;
102         }
103
104         @Override
105         public String fullID() {
106                 return identity.id+'@'+org.getDomain();
107         }
108
109         @Override
110         public String type() {
111                 switch(identity.status) {
112                         case EMPLOYEE: return DefaultOrg.Types.Employee.name();
113                         case CONTRACTOR: return DefaultOrg.Types.Contractor.name();
114                         case APPLICATION: return DefaultOrg.Types.Application.name();
115                         case NON_ACTIVE: return DefaultOrg.Types.NotActive.name();
116                         default:
117                                 return "Unknown";
118                 }
119         }
120
121         @Override
122         public Identity responsibleTo() throws OrganizationException {
123                 if("".equals(identity.responsibleTo) && isFound()) { // cover the situation of Top Dog... reports to no-one.
124                         return this;
125                 } else {
126                         return org.getIdentity(trans, identity.responsibleTo);
127                 }
128         }
129
130         @Override
131         public List<String> delegate() {
132                 //NOTE:  implement Delegate system, if desired
133                 return DefaultOrg.NULL_DELEGATES;
134         }
135
136         @Override
137         public String email() {
138                 return identity.email;
139         }
140
141         @Override
142         public String fullName() {
143                 return identity.name;
144         }
145
146         @Override
147         public String firstName() {
148                 return identity.fname;
149         }
150
151         @Override
152         public String mayOwn() {
153                 // Assume only Employees are responsible for Resources.
154                 if(identity.status==null|| identity.status.length()==0) {
155                         return "Identity must have valid status";
156                 } else if(EMPLOYEE.equals(identity.status)) {
157                         return null; // This is "Yes, is Responsible"
158                 } else {
159                         return "Reponsible Party must be an Employee";
160                 }
161         }
162
163         @Override
164         public boolean isFound() {
165                 return identity!=Identities.NO_DATA; // yes, object comparison intended
166         }
167
168         @Override
169         public boolean isPerson() {
170                 return !identity.status.equals(APPLICATION);
171         }
172
173         @Override
174         public Organization org() {
175                 return org;
176         }
177
178
179 }