001/*
002 * (C) Copyright 2006-2011 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 *
019 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
020 */
021
022package org.nuxeo.ecm.core.api.impl;
023
024import java.io.Serializable;
025import java.util.ArrayList;
026import java.util.Collections;
027import java.util.List;
028
029import org.nuxeo.ecm.core.api.DocumentModel;
030import org.nuxeo.ecm.core.api.NuxeoPrincipal;
031import org.nuxeo.ecm.core.api.security.SecurityConstants;
032
033/**
034 * NuxeoPrincipal stub implementation.
035 * <p>
036 * TODO this should replace the DetachedNuxeoPrincipal from user manager to minimize principal implementations.
037 *
038 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
039 */
040public class UserPrincipal implements NuxeoPrincipal, Serializable {
041
042    private static final long serialVersionUID = 2013321088068583749L;
043
044    protected boolean anonymous;
045
046    protected boolean administrator;
047
048    protected String userName;
049
050    protected List<String> groups;
051
052    protected List<String> roles;
053
054    protected String firstName;
055
056    protected String lastName;
057
058    protected String email;
059
060    protected String company;
061
062    protected transient String password;
063
064    protected DocumentModel model;
065
066    protected String originatingUser;
067
068    /**
069     * @deprecated use {@link #UserPrincipal(String, List, boolean, boolean)} instead: this constructor assumes that
070     *             members of the "administrators" group is an administrator.
071     */
072    @Deprecated
073    public UserPrincipal(String username) {
074        this(username, new ArrayList<String>(), false, false);
075    }
076
077    /**
078     * @deprecated use {@link #UserPrincipal(String, List, boolean, boolean)} instead: this constructor assumes that
079     *             members of the "administrators" group is an administrator.
080     */
081    @Deprecated
082    public UserPrincipal(String username, List<String> groups) {
083        // BBB: members of group 'administrators' are considered administrators
084        this(username, groups, false, groups != null && groups.contains(SecurityConstants.ADMINISTRATORS));
085    }
086
087    public UserPrincipal(String username, List<String> groups, boolean anonymous, boolean administrator) {
088        userName = username;
089        List<String> emptyGroups = Collections.emptyList();
090        this.groups = groups == null ? emptyGroups : groups;
091        this.anonymous = anonymous;
092        this.administrator = administrator;
093    }
094
095    @Override
096    public String getEmail() {
097        return email;
098    }
099
100    @Override
101    public void setEmail(String email) {
102        this.email = email;
103    }
104
105    @Override
106    public String getCompany() {
107        return company;
108    }
109
110    @Override
111    public String getFirstName() {
112        return firstName;
113    }
114
115    @Override
116    public String getLastName() {
117        return lastName;
118    }
119
120    @Override
121    public void setCompany(String company) {
122        this.company = company;
123    }
124
125    @Override
126    public void setFirstName(String firstName) {
127        this.firstName = firstName;
128    }
129
130    @Override
131    public void setLastName(String lastName) {
132        this.lastName = lastName;
133    }
134
135    @Override
136    public void setName(String name) {
137        userName = name;
138    }
139
140    @Override
141    public String getName() {
142        return userName;
143    }
144
145    @Override
146    public List<String> getGroups() {
147        return groups;
148    }
149
150    // TODO OG: this is not the true semantics but is it really a problem here?
151    @Override
152    public List<String> getAllGroups() {
153        return groups;
154    }
155
156    @Override
157    public List<String> getRoles() {
158        return roles;
159    }
160
161    @Override
162    public void setGroups(List<String> groups) {
163        this.groups = groups;
164    }
165
166    @Override
167    public void setRoles(List<String> roles) {
168        this.roles = roles;
169    }
170
171    @Override
172    public String getPassword() {
173        return password;
174    }
175
176    @Override
177    public void setPassword(String password) {
178        this.password = password;
179    }
180
181    @Override
182    public String getPrincipalId() {
183        return null;
184    }
185
186    @Override
187    public void setPrincipalId(String principalId) {
188    }
189
190    @Override
191    public DocumentModel getModel() {
192        return model;
193    }
194
195    @Override
196    public void setModel(DocumentModel model) {
197        this.model = model;
198    }
199
200    @Override
201    public boolean isMemberOf(String group) {
202        return false;
203    }
204
205    @Override
206    public boolean equals(Object o) {
207        if (this == o) {
208            return true;
209        }
210        if (!(o instanceof UserPrincipal)) {
211            return false;
212        }
213
214        UserPrincipal that = (UserPrincipal) o;
215
216        // XXX: autogenerated junk, yuck!
217        if (company == null ? that.company != null : !company.equals(that.company)) {
218            return false;
219        }
220        if (firstName == null ? that.firstName != null : !firstName.equals(that.firstName)) {
221            return false;
222        }
223        if (groups == null ? that.groups != null : !groups.equals(that.groups)) {
224            return false;
225        }
226        if (lastName == null ? that.lastName != null : !lastName.equals(that.lastName)) {
227            return false;
228        }
229        if (password == null ? that.password != null : !password.equals(that.password)) {
230            return false;
231        }
232        if (roles == null ? that.roles != null : !roles.equals(that.roles)) {
233            return false;
234        }
235        if (userName == null ? that.userName != null : !userName.equals(that.userName)) {
236            return false;
237        }
238
239        return true;
240    }
241
242    @Override
243    public int hashCode() {
244        int result = userName == null ? 0 : userName.hashCode();
245        result = 31 * result + (groups == null ? 0 : groups.hashCode());
246        result = 31 * result + (roles == null ? 0 : roles.hashCode());
247        result = 31 * result + (firstName == null ? 0 : firstName.hashCode());
248        result = 31 * result + (lastName == null ? 0 : lastName.hashCode());
249        result = 31 * result + (company == null ? 0 : company.hashCode());
250        result = 31 * result + (password == null ? 0 : password.hashCode());
251        return result;
252    }
253
254    @Override
255    public boolean isAdministrator() {
256        return administrator;
257    }
258
259    @Override
260    public String getTenantId() {
261        return null;
262    }
263
264    @Override
265    public boolean isAnonymous() {
266        return anonymous;
267    }
268
269    @Override
270    public String getOriginatingUser() {
271        return originatingUser;
272    }
273
274    @Override
275    public void setOriginatingUser(String originatingUser) {
276        this.originatingUser = originatingUser;
277    }
278
279    @Override
280    public String getActingUser() {
281        return getOriginatingUser() == null ? getName() : getOriginatingUser();
282    }
283
284    @Override
285    public boolean isTransient() {
286        String name = getName();
287        return name != null && name.startsWith(TRANSIENT_USER_PREFIX);
288    }
289
290}