001/*
002 * (C) Copyright 2006-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Nuxeo - initial API and implementation
018 */
019package org.nuxeo.ecm.core.api.impl;
020
021import java.io.Serializable;
022import java.util.Collections;
023import java.util.List;
024
025import org.nuxeo.ecm.core.api.DocumentModel;
026import org.nuxeo.ecm.core.api.NuxeoPrincipal;
027
028/**
029 * NuxeoPrincipal stub implementation.
030 *
031 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
032 */
033public class UserPrincipal implements NuxeoPrincipal, Serializable {
034
035    private static final long serialVersionUID = 2013321088068583749L;
036
037    protected boolean anonymous;
038
039    protected boolean administrator;
040
041    protected String userName;
042
043    protected List<String> groups;
044
045    protected List<String> roles;
046
047    protected String firstName;
048
049    protected String lastName;
050
051    protected String email;
052
053    protected String company;
054
055    protected transient String password;
056
057    protected DocumentModel model;
058
059    protected String originatingUser;
060
061    public UserPrincipal(String username, List<String> groups, boolean anonymous, boolean administrator) {
062        userName = username;
063        List<String> emptyGroups = Collections.emptyList();
064        this.groups = groups == null ? emptyGroups : groups;
065        this.anonymous = anonymous;
066        this.administrator = administrator;
067    }
068
069    @Override
070    public String getEmail() {
071        return email;
072    }
073
074    @Override
075    public void setEmail(String email) {
076        this.email = email;
077    }
078
079    @Override
080    public String getCompany() {
081        return company;
082    }
083
084    @Override
085    public String getFirstName() {
086        return firstName;
087    }
088
089    @Override
090    public String getLastName() {
091        return lastName;
092    }
093
094    @Override
095    public void setCompany(String company) {
096        this.company = company;
097    }
098
099    @Override
100    public void setFirstName(String firstName) {
101        this.firstName = firstName;
102    }
103
104    @Override
105    public void setLastName(String lastName) {
106        this.lastName = lastName;
107    }
108
109    @Override
110    public void setName(String name) {
111        userName = name;
112    }
113
114    @Override
115    public String getName() {
116        return userName;
117    }
118
119    @Override
120    public List<String> getGroups() {
121        return groups;
122    }
123
124    // TODO OG: this is not the true semantics but is it really a problem here?
125    @Override
126    public List<String> getAllGroups() {
127        return groups;
128    }
129
130    @Override
131    public List<String> getRoles() {
132        return roles;
133    }
134
135    @Override
136    public void setGroups(List<String> groups) {
137        this.groups = groups;
138    }
139
140    @Override
141    public void setRoles(List<String> roles) {
142        this.roles = roles;
143    }
144
145    @Override
146    public String getPassword() {
147        return password;
148    }
149
150    @Override
151    public void setPassword(String password) {
152        this.password = password;
153    }
154
155    @Override
156    public String getPrincipalId() {
157        return null;
158    }
159
160    @Override
161    public void setPrincipalId(String principalId) {
162    }
163
164    @Override
165    public DocumentModel getModel() {
166        return model;
167    }
168
169    @Override
170    public void setModel(DocumentModel model) {
171        this.model = model;
172    }
173
174    @Override
175    public boolean isMemberOf(String group) {
176        return false;
177    }
178
179    @Override
180    public boolean equals(Object o) {
181        if (this == o) {
182            return true;
183        }
184        if (!(o instanceof UserPrincipal)) {
185            return false;
186        }
187
188        UserPrincipal that = (UserPrincipal) o;
189
190        // XXX: autogenerated junk, yuck!
191        if (company == null ? that.company != null : !company.equals(that.company)) {
192            return false;
193        }
194        if (firstName == null ? that.firstName != null : !firstName.equals(that.firstName)) {
195            return false;
196        }
197        if (groups == null ? that.groups != null : !groups.equals(that.groups)) {
198            return false;
199        }
200        if (lastName == null ? that.lastName != null : !lastName.equals(that.lastName)) {
201            return false;
202        }
203        if (password == null ? that.password != null : !password.equals(that.password)) {
204            return false;
205        }
206        if (roles == null ? that.roles != null : !roles.equals(that.roles)) {
207            return false;
208        }
209        if (userName == null ? that.userName != null : !userName.equals(that.userName)) {
210            return false;
211        }
212
213        return true;
214    }
215
216    @Override
217    public int hashCode() {
218        int result = userName == null ? 0 : userName.hashCode();
219        result = 31 * result + (groups == null ? 0 : groups.hashCode());
220        result = 31 * result + (roles == null ? 0 : roles.hashCode());
221        result = 31 * result + (firstName == null ? 0 : firstName.hashCode());
222        result = 31 * result + (lastName == null ? 0 : lastName.hashCode());
223        result = 31 * result + (company == null ? 0 : company.hashCode());
224        result = 31 * result + (password == null ? 0 : password.hashCode());
225        return result;
226    }
227
228    @Override
229    public boolean isAdministrator() {
230        return administrator;
231    }
232
233    @Override
234    public String getTenantId() {
235        return null;
236    }
237
238    @Override
239    public boolean isAnonymous() {
240        return anonymous;
241    }
242
243    @Override
244    public String getOriginatingUser() {
245        return originatingUser;
246    }
247
248    @Override
249    public void setOriginatingUser(String originatingUser) {
250        this.originatingUser = originatingUser;
251    }
252
253    @Override
254    public String getActingUser() {
255        return getOriginatingUser() == null ? getName() : getOriginatingUser();
256    }
257
258    @Override
259    public boolean isTransient() {
260        String name = getName();
261        return name != null && name.startsWith(TRANSIENT_USER_PREFIX);
262    }
263
264}