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