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 <[email protected]> 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:[email protected]">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 (origUserName == null) { 065 return ((SystemPrincipal) other).origUserName == null; 066 } else { 067 return origUserName.equals(((SystemPrincipal) other).origUserName); 068 } 069 } else { 070 return false; 071 } 072 } 073 074 @Override 075 public int hashCode() { 076 return hash; 077 } 078 079 public String getCompany() { 080 return "Nuxeo"; 081 } 082 083 @Override 084 public String getEmail() { 085 return null; 086 } 087 088 @Override 089 public void setEmail(String email) { 090 091 } 092 093 public String getFirstName() { 094 return "System"; 095 } 096 097 public String getLastName() { 098 return "System"; 099 } 100 101 public String getName() { 102 return LoginComponent.SYSTEM_USERNAME; 103 } 104 105 public List<String> getGroups() { 106 return SYS_GROUPS; 107 } 108 109 public List<String> getAllGroups() { 110 return SYS_GROUPS; 111 } 112 113 public List<String> getRoles() { 114 return SYS_ROLES; 115 } 116 117 public String getPassword() { 118 if (SYS_PASSWORD == null) { 119 return null; 120 } 121 return new String(SYS_PASSWORD); 122 } 123 124 public String getPrincipalId() { 125 return ""; 126 } 127 128 public String getOriginatingUser() { 129 return origUserName; 130 } 131 132 public void setOriginatingUser(String originatingUser) { 133 origUserName = originatingUser; 134 computeHash(); 135 } 136 137 @Override 138 public String getActingUser() { 139 return getOriginatingUser() == null ? getName() : getOriginatingUser(); 140 } 141 142 public DocumentModel getModel() { 143 return null; 144 } 145 146 public void setCompany(String company) { 147 } 148 149 public void setFirstName(String firstName) { 150 } 151 152 public void setLastName(String lastName) { 153 } 154 155 public void setName(String userName) { 156 } 157 158 public void setGroups(List<String> groups) { 159 } 160 161 public void setRoles(List<String> roles) { 162 } 163 164 public void setPassword(String password) { 165 } 166 167 public void setPrincipalId(String principalId) { 168 } 169 170 public void setModel(DocumentModel model) { 171 } 172 173 public boolean isMemberOf(String group) { 174 return SYS_GROUPS.contains(group); 175 } 176 177 @Override 178 public String toString() { 179 return getName(); 180 } 181 182 public boolean isAdministrator() { 183 return true; 184 } 185 186 @Override 187 public String getTenantId() { 188 return null; 189 } 190 191 public boolean isAnonymous() { 192 return false; 193 } 194 195 @Override 196 public boolean isTransient() { 197 return false; 198 } 199}