001/*
002 * (C) Copyright 2006-2012 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 GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Thomas Roger <troger@nuxeo.com>
016 */
017
018package org.nuxeo.ecm.core.api;
019
020import java.security.Principal;
021import java.util.ArrayList;
022import java.util.Arrays;
023import java.util.Collections;
024import java.util.List;
025
026import org.nuxeo.ecm.core.api.security.SecurityConstants;
027import org.nuxeo.runtime.api.login.LoginComponent;
028
029/**
030 * @author <a href="mailto:bs@nuxeo.com">Bogdan Stefanescu</a>
031 */
032public class SystemPrincipal implements NuxeoPrincipal {
033
034    private static final long serialVersionUID = -3381784063138281706L;
035
036    private static final char[] SYS_PASSWORD = null;
037
038    private static final List<String> SYS_GROUPS = Collections.unmodifiableList(Arrays.asList(SecurityConstants.ADMINISTRATORS));
039
040    private static final List<String> SYS_ROLES = Collections.unmodifiableList(new ArrayList<String>());
041
042    private String origUserName;
043
044    private int hash;
045
046    public SystemPrincipal(String origUserName) {
047        this.origUserName = origUserName == null ? LoginComponent.SYSTEM_USERNAME : origUserName;
048        computeHash();
049    }
050
051    private void computeHash() {
052        if (origUserName != null) {
053            hash = (LoginComponent.SYSTEM_USERNAME + "-" + origUserName).hashCode();
054        } else {
055            hash = LoginComponent.SYSTEM_USERNAME.hashCode();
056        }
057    }
058
059    @Override
060    public boolean equals(Object other) {
061        if (other instanceof SystemPrincipal) {
062            if (!LoginComponent.SYSTEM_USERNAME.equals(((Principal) other).getName())) {
063                return false;
064            }
065            if (origUserName == null) {
066                return ((SystemPrincipal) other).origUserName == null;
067            } else {
068                return origUserName.equals(((SystemPrincipal) other).origUserName);
069            }
070        } else {
071            return false;
072        }
073    }
074
075    @Override
076    public int hashCode() {
077        return hash;
078    }
079
080    public String getCompany() {
081        return "Nuxeo";
082    }
083
084    @Override
085    public String getEmail() {
086        return null;
087    }
088
089    @Override
090    public void setEmail(String email) {
091
092    }
093
094    public String getFirstName() {
095        return "System";
096    }
097
098    public String getLastName() {
099        return "System";
100    }
101
102    public String getName() {
103        return LoginComponent.SYSTEM_USERNAME;
104    }
105
106    public List<String> getGroups() {
107        return SYS_GROUPS;
108    }
109
110    public List<String> getAllGroups() {
111        return SYS_GROUPS;
112    }
113
114    public List<String> getRoles() {
115        return SYS_ROLES;
116    }
117
118    public String getPassword() {
119        if (SYS_PASSWORD == null) {
120            return null;
121        }
122        return new String(SYS_PASSWORD);
123    }
124
125    public String getPrincipalId() {
126        return "";
127    }
128
129    public String getOriginatingUser() {
130        return origUserName;
131    }
132
133    public void setOriginatingUser(String originatingUser) {
134        origUserName = originatingUser;
135        computeHash();
136    }
137
138    @Override
139    public String getActingUser() {
140        return getOriginatingUser() == null ? getName() : getOriginatingUser();
141    }
142
143    public DocumentModel getModel() {
144        return null;
145    }
146
147    public void setCompany(String company) {
148    }
149
150    public void setFirstName(String firstName) {
151    }
152
153    public void setLastName(String lastName) {
154    }
155
156    public void setName(String userName) {
157    }
158
159    public void setGroups(List<String> groups) {
160    }
161
162    public void setRoles(List<String> roles) {
163    }
164
165    public void setPassword(String password) {
166    }
167
168    public void setPrincipalId(String principalId) {
169    }
170
171    public void setModel(DocumentModel model) {
172    }
173
174    public boolean isMemberOf(String group) {
175        return SYS_GROUPS.contains(group);
176    }
177
178    @Override
179    public String toString() {
180        return getName();
181    }
182
183    public boolean isAdministrator() {
184        return true;
185    }
186
187    @Override
188    public String getTenantId() {
189        return null;
190    }
191
192    public boolean isAnonymous() {
193        return false;
194    }
195
196}