001/*
002 * Copyright (c) 2006-2011 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the Eclipse Public License v1.0
006 * which accompanies this distribution, and is available at
007 * http://www.eclipse.org/legal/epl-v10.html
008 *
009 * Contributors:
010 *     Nuxeo - initial API and implementation
011 *
012 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
013 */
014
015package org.nuxeo.ecm.core.api.impl;
016
017import java.io.Serializable;
018import java.util.ArrayList;
019import java.util.Collections;
020import java.util.List;
021
022import org.nuxeo.ecm.core.api.DocumentModel;
023import org.nuxeo.ecm.core.api.NuxeoPrincipal;
024import org.nuxeo.ecm.core.api.security.SecurityConstants;
025
026/**
027 * NuxeoPrincipal stub implementation.
028 * <p>
029 * TODO this should replace the DetachedNuxeoPrincipal from user manager to minimize principal implementations.
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    /**
062     * @deprecated use {@link #UserPrincipal(String, List, boolean, boolean)} instead: this constructor assumes that
063     *             members of the "administrators" group is an administrator.
064     */
065    @Deprecated
066    public UserPrincipal(String username) {
067        this(username, new ArrayList<String>(), false, false);
068    }
069
070    /**
071     * @deprecated use {@link #UserPrincipal(String, List, boolean, boolean)} instead: this constructor assumes that
072     *             members of the "administrators" group is an administrator.
073     */
074    @Deprecated
075    public UserPrincipal(String username, List<String> groups) {
076        // BBB: members of group 'administrators' are considered administrators
077        this(username, groups, false, groups != null && groups.contains(SecurityConstants.ADMINISTRATORS));
078    }
079
080    public UserPrincipal(String username, List<String> groups, boolean anonymous, boolean administrator) {
081        userName = username;
082        List<String> emptyGroups = Collections.emptyList();
083        this.groups = groups == null ? emptyGroups : groups;
084        this.anonymous = anonymous;
085        this.administrator = administrator;
086    }
087
088    @Override
089    public String getEmail() {
090        return email;
091    }
092
093    @Override
094    public void setEmail(String email) {
095        this.email = email;
096    }
097
098    @Override
099    public String getCompany() {
100        return company;
101    }
102
103    @Override
104    public String getFirstName() {
105        return firstName;
106    }
107
108    @Override
109    public String getLastName() {
110        return lastName;
111    }
112
113    @Override
114    public void setCompany(String company) {
115        this.company = company;
116    }
117
118    @Override
119    public void setFirstName(String firstName) {
120        this.firstName = firstName;
121    }
122
123    @Override
124    public void setLastName(String lastName) {
125        this.lastName = lastName;
126    }
127
128    @Override
129    public void setName(String name) {
130        userName = name;
131    }
132
133    @Override
134    public String getName() {
135        return userName;
136    }
137
138    @Override
139    public List<String> getGroups() {
140        return groups;
141    }
142
143    // TODO OG: this is not the true semantics but is it really a problem here?
144    @Override
145    public List<String> getAllGroups() {
146        return groups;
147    }
148
149    @Override
150    public List<String> getRoles() {
151        return roles;
152    }
153
154    @Override
155    public void setGroups(List<String> groups) {
156        this.groups = groups;
157    }
158
159    @Override
160    public void setRoles(List<String> roles) {
161        this.roles = roles;
162    }
163
164    @Override
165    public String getPassword() {
166        return password;
167    }
168
169    @Override
170    public void setPassword(String password) {
171        this.password = password;
172    }
173
174    @Override
175    public String getPrincipalId() {
176        return null;
177    }
178
179    @Override
180    public void setPrincipalId(String principalId) {
181    }
182
183    @Override
184    public DocumentModel getModel() {
185        return model;
186    }
187
188    @Override
189    public void setModel(DocumentModel model) {
190        this.model = model;
191    }
192
193    @Override
194    public boolean isMemberOf(String group) {
195        return false;
196    }
197
198    @Override
199    public boolean equals(Object o) {
200        if (this == o) {
201            return true;
202        }
203        if (!(o instanceof UserPrincipal)) {
204            return false;
205        }
206
207        UserPrincipal that = (UserPrincipal) o;
208
209        // XXX: autogenerated junk, yuck!
210        if (company == null ? that.company != null : !company.equals(that.company)) {
211            return false;
212        }
213        if (firstName == null ? that.firstName != null : !firstName.equals(that.firstName)) {
214            return false;
215        }
216        if (groups == null ? that.groups != null : !groups.equals(that.groups)) {
217            return false;
218        }
219        if (lastName == null ? that.lastName != null : !lastName.equals(that.lastName)) {
220            return false;
221        }
222        if (password == null ? that.password != null : !password.equals(that.password)) {
223            return false;
224        }
225        if (roles == null ? that.roles != null : !roles.equals(that.roles)) {
226            return false;
227        }
228        if (userName == null ? that.userName != null : !userName.equals(that.userName)) {
229            return false;
230        }
231
232        return true;
233    }
234
235    @Override
236    public int hashCode() {
237        int result = userName == null ? 0 : userName.hashCode();
238        result = 31 * result + (groups == null ? 0 : groups.hashCode());
239        result = 31 * result + (roles == null ? 0 : roles.hashCode());
240        result = 31 * result + (firstName == null ? 0 : firstName.hashCode());
241        result = 31 * result + (lastName == null ? 0 : lastName.hashCode());
242        result = 31 * result + (company == null ? 0 : company.hashCode());
243        result = 31 * result + (password == null ? 0 : password.hashCode());
244        return result;
245    }
246
247    @Override
248    public boolean isAdministrator() {
249        return administrator;
250    }
251
252    @Override
253    public String getTenantId() {
254        return null;
255    }
256
257    @Override
258    public boolean isAnonymous() {
259        return anonymous;
260    }
261
262    @Override
263    public String getOriginatingUser() {
264        return originatingUser;
265    }
266
267    @Override
268    public void setOriginatingUser(String originatingUser) {
269        this.originatingUser = originatingUser;
270    }
271
272    @Override
273    public String getActingUser() {
274        return getOriginatingUser() == null ? getName() : getOriginatingUser();
275    }
276
277}