001/*
002 * (C) Copyright 2006-2007 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$
020 */
021
022package org.nuxeo.ecm.platform.usermanager;
023
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.DocumentModel;
027import org.nuxeo.ecm.core.api.NuxeoPrincipal;
028
029/**
030 * Describes a detached NuxeoPrincipal.
031 *
032 * @author Mariana Cedica
033 */
034public class DetachedNuxeoPrincipal implements NuxeoPrincipal {
035
036    public static DetachedNuxeoPrincipal detach(NuxeoPrincipal principal) {
037        DetachedNuxeoPrincipal detachedPrincipal = new DetachedNuxeoPrincipal(principal.getPrincipalId());
038        detachedPrincipal.name = principal.getName();
039        detachedPrincipal.firstName = principal.getFirstName();
040        detachedPrincipal.lastName = principal.getLastName();
041        detachedPrincipal.password = principal.getPassword();
042        detachedPrincipal.company = principal.getCompany();
043        detachedPrincipal.groups = principal.getGroups();
044        detachedPrincipal.allGroups = principal.getAllGroups();
045        detachedPrincipal.roles = principal.getRoles();
046        detachedPrincipal.isAdministrator = principal.isAdministrator();
047        detachedPrincipal.isAnonymous = principal.isAnonymous();
048        detachedPrincipal.email = principal.getEmail();
049        return detachedPrincipal;
050    }
051
052    protected DetachedNuxeoPrincipal(String principalId) {
053        this.principalId = principalId;
054    }
055
056    public DetachedNuxeoPrincipal(String principalId, String name, String firstName, String lastName, String password,
057            String email, String company, List<String> groups, List<String> allGroups, List<String> roles,
058            boolean isAdministrator, boolean isAnonymous) {
059        this.principalId = principalId;
060        this.name = name;
061        this.firstName = firstName;
062        this.lastName = lastName;
063        this.password = password;
064        this.email = email;
065        this.company = company;
066        this.groups = groups;
067        this.allGroups = allGroups;
068        this.roles = roles;
069        this.isAdministrator = isAdministrator;
070        this.isAnonymous = isAnonymous;
071    }
072
073    private static final long serialVersionUID = 1L;
074
075    protected String principalId;
076
077    protected String name;
078
079    protected String firstName;
080
081    protected String lastName;
082
083    protected String password;
084
085    protected String email;
086
087    protected String company;
088
089    protected List<String> groups;
090
091    protected List<String> allGroups;
092
093    protected List<String> roles;
094
095    protected boolean isAdministrator;
096
097    protected boolean isAnonymous;
098
099    @Override
100    public String getName() {
101        return name;
102    }
103
104    @Override
105    public String getFirstName() {
106        return firstName;
107    }
108
109    @Override
110    public String getLastName() {
111        return lastName;
112    }
113
114    @Override
115    public String getPassword() {
116        return password;
117    }
118
119    @Override
120    public String getCompany() {
121        return company;
122    }
123
124    @Override
125    public String getEmail() {
126        return email;
127    }
128
129    @Override
130    public List<String> getGroups() {
131        return groups;
132    }
133
134    @Override
135    public List<String> getAllGroups() {
136        return allGroups;
137    }
138
139    @Override
140    public boolean isMemberOf(String group) {
141        return allGroups.contains(group);
142    }
143
144    @Override
145    public List<String> getRoles() {
146        return roles;
147    }
148
149    @Override
150    public void setName(String name) {
151        throw new UnsupportedOperationException();
152
153    }
154
155    @Override
156    public void setFirstName(String firstName) {
157        throw new UnsupportedOperationException();
158
159    }
160
161    @Override
162    public void setLastName(String lastName) {
163        throw new UnsupportedOperationException();
164    }
165
166    @Override
167    public void setGroups(List<String> groups) {
168        throw new UnsupportedOperationException();
169
170    }
171
172    @Override
173    public void setRoles(List<String> roles) {
174        throw new UnsupportedOperationException();
175
176    }
177
178    @Override
179    public void setEmail(String email) {
180        this.email = email;
181    }
182
183    @Override
184    public void setCompany(String company) {
185        throw new UnsupportedOperationException();
186
187    }
188
189    @Override
190    public void setPassword(String password) {
191        throw new UnsupportedOperationException();
192
193    }
194
195    @Override
196    public String getPrincipalId() {
197        return principalId;
198    }
199
200    @Override
201    public void setPrincipalId(String principalId) {
202        throw new UnsupportedOperationException();
203    }
204
205    @Override
206    public DocumentModel getModel() {
207        throw new UnsupportedOperationException();
208    }
209
210    @Override
211    public void setModel(DocumentModel model) {
212        throw new UnsupportedOperationException();
213
214    }
215
216    @Override
217    public boolean isAdministrator() {
218        return isAdministrator;
219    }
220
221    @Override
222    public String getTenantId() {
223        return null;
224    }
225
226    @Override
227    public boolean isAnonymous() {
228        return isAnonymous;
229    }
230
231    @Override
232    public String getOriginatingUser() {
233        throw new UnsupportedOperationException();
234    }
235
236    @Override
237    public void setOriginatingUser(String originatingUser) {
238        throw new UnsupportedOperationException();
239    }
240
241    @Override
242    public String getActingUser() {
243        return getName();
244    }
245
246    @Override
247    public boolean isTransient() {
248        String name = getName();
249        return name != null && name.startsWith(TRANSIENT_USER_PREFIX);
250    }
251}