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