PageRenderTime 63ms CodeModel.GetById 39ms RepoModel.GetById 1ms app.codeStats 0ms

/root/projects/remote-api/source/java/org/alfresco/rest/api/impl/PeopleImpl.java

https://github.com/deas/alfresco
Java | 358 lines | 265 code | 38 blank | 55 comment | 23 complexity | a5ef7ea8a60ff68acad4f75cc6a8bac6 MD5 | raw file
  1. /*
  2. * Copyright (C) 2005-2012 Alfresco Software Limited.
  3. *
  4. * This file is part of Alfresco
  5. *
  6. * Alfresco is free software: you can redistribute it and/or modify
  7. * it under the terms of the GNU Lesser General Public License as published by
  8. * the Free Software Foundation, either version 3 of the License, or
  9. * (at your option) any later version.
  10. *
  11. * Alfresco is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. * GNU Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  18. */
  19. /*
  20. * Copyright (C) 2005-2012 Alfresco Software Limited.
  21. *
  22. * This file is part of Alfresco
  23. *
  24. * Alfresco is free software: you can redistribute it and/or modify
  25. * it under the terms of the GNU Lesser General Public License as published by
  26. * the Free Software Foundation, either version 3 of the License, or
  27. * (at your option) any later version.
  28. *
  29. * Alfresco is distributed in the hope that it will be useful,
  30. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  31. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  32. * GNU Lesser General Public License for more details.
  33. *
  34. * You should have received a copy of the GNU Lesser General Public License
  35. * along with Alfresco. If not, see <http://www.gnu.org/licenses/>.
  36. */
  37. package org.alfresco.rest.api.impl;
  38. import java.io.Serializable;
  39. import java.util.HashMap;
  40. import java.util.List;
  41. import java.util.Map;
  42. import org.alfresco.model.ContentModel;
  43. import org.alfresco.repo.security.authentication.AuthenticationUtil;
  44. import org.alfresco.repo.security.authentication.AuthenticationUtil.RunAsWork;
  45. import org.alfresco.rest.api.Nodes;
  46. import org.alfresco.rest.api.People;
  47. import org.alfresco.rest.api.Sites;
  48. import org.alfresco.rest.api.model.Company;
  49. import org.alfresco.rest.api.model.Person;
  50. import org.alfresco.rest.framework.core.exceptions.EntityNotFoundException;
  51. import org.alfresco.service.cmr.repository.AssociationRef;
  52. import org.alfresco.service.cmr.repository.ContentData;
  53. import org.alfresco.service.cmr.repository.ContentReader;
  54. import org.alfresco.service.cmr.repository.ContentService;
  55. import org.alfresco.service.cmr.repository.NodeRef;
  56. import org.alfresco.service.cmr.repository.NodeService;
  57. import org.alfresco.service.cmr.security.AuthenticationService;
  58. import org.alfresco.service.cmr.security.NoSuchPersonException;
  59. import org.alfresco.service.cmr.security.PersonService;
  60. import org.alfresco.service.cmr.site.SiteService;
  61. import org.alfresco.service.cmr.thumbnail.ThumbnailService;
  62. import org.alfresco.service.cmr.usage.ContentUsageService;
  63. import org.alfresco.service.namespace.QName;
  64. /**
  65. * Centralises access to people services and maps between representations.
  66. *
  67. * @author steveglover
  68. * @since publicapi1.0
  69. */
  70. public class PeopleImpl implements People
  71. {
  72. protected Nodes nodes;
  73. protected Sites sites;
  74. protected SiteService siteService;
  75. protected NodeService nodeService;
  76. protected PersonService personService;
  77. protected AuthenticationService authenticationService;
  78. protected ContentUsageService contentUsageService;
  79. protected ContentService contentService;
  80. protected ThumbnailService thumbnailService;
  81. public void setSites(Sites sites)
  82. {
  83. this.sites = sites;
  84. }
  85. public void setSiteService(SiteService siteService)
  86. {
  87. this.siteService = siteService;
  88. }
  89. public void setNodes(Nodes nodes)
  90. {
  91. this.nodes = nodes;
  92. }
  93. public void setNodeService(NodeService nodeService)
  94. {
  95. this.nodeService = nodeService;
  96. }
  97. public void setPersonService(PersonService personService)
  98. {
  99. this.personService = personService;
  100. }
  101. public void setAuthenticationService(AuthenticationService authenticationService)
  102. {
  103. this.authenticationService = authenticationService;
  104. }
  105. public void setContentUsageService(ContentUsageService contentUsageService)
  106. {
  107. this.contentUsageService = contentUsageService;
  108. }
  109. public void setContentService(ContentService contentService)
  110. {
  111. this.contentService = contentService;
  112. }
  113. public void setThumbnailService(ThumbnailService thumbnailService)
  114. {
  115. this.thumbnailService = thumbnailService;
  116. }
  117. public String validatePerson(String personId)
  118. {
  119. return validatePerson(personId, false);
  120. }
  121. public String validatePerson(String personId, boolean validateIsCurrentUser)
  122. {
  123. if(personId.equalsIgnoreCase(DEFAULT_USER))
  124. {
  125. personId = AuthenticationUtil.getFullyAuthenticatedUser();
  126. }
  127. personId = personService.getUserIdentifier(personId);
  128. if(personId == null)
  129. {
  130. // "User " + personId + " does not exist"
  131. throw new EntityNotFoundException("personId is null.");
  132. }
  133. if(validateIsCurrentUser)
  134. {
  135. String currentUserId = AuthenticationUtil.getFullyAuthenticatedUser();
  136. if(!currentUserId.equalsIgnoreCase(personId))
  137. {
  138. throw new EntityNotFoundException(personId);
  139. }
  140. }
  141. return personId;
  142. }
  143. protected void processPersonProperties(String userName, final Map<QName, Serializable> nodeProps)
  144. {
  145. if(!contentUsageService.getEnabled())
  146. {
  147. // quota used will always be 0 in this case so remove from the person properties
  148. nodeProps.remove(ContentModel.PROP_SIZE_QUOTA);
  149. nodeProps.remove(ContentModel.PROP_SIZE_CURRENT);
  150. }
  151. final ContentData personDescription = (ContentData)nodeProps.get(ContentModel.PROP_PERSONDESC);
  152. if(personDescription != null)
  153. {
  154. nodeProps.remove(ContentModel.PROP_PERSONDESC);
  155. AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
  156. {
  157. @Override
  158. public Void doWork() throws Exception
  159. {
  160. ContentReader reader = contentService.getRawReader(personDescription.getContentUrl());
  161. if(reader != null && reader.exists())
  162. {
  163. String description = reader.getContentString();
  164. nodeProps.put(Person.PROP_PERSON_DESCRIPTION, description);
  165. }
  166. return null;
  167. }
  168. });
  169. }
  170. }
  171. public boolean hasAvatar(NodeRef personNodeRef)
  172. {
  173. if(personNodeRef != null)
  174. {
  175. List<AssociationRef> avatorAssocs = nodeService.getTargetAssocs(personNodeRef, ContentModel.ASSOC_AVATAR);
  176. return(avatorAssocs.size() > 0);
  177. }
  178. else
  179. {
  180. return false;
  181. }
  182. }
  183. public NodeRef getAvatar(String personId)
  184. {
  185. NodeRef avatar = null;
  186. personId = validatePerson(personId);
  187. NodeRef personNode = personService.getPerson(personId);
  188. if(personNode != null)
  189. {
  190. List<AssociationRef> avatorAssocs = nodeService.getTargetAssocs(personNode, ContentModel.ASSOC_AVATAR);
  191. if(avatorAssocs.size() > 0)
  192. {
  193. AssociationRef ref = avatorAssocs.get(0);
  194. NodeRef thumbnailNodeRef = thumbnailService.getThumbnailByName(ref.getTargetRef(), ContentModel.PROP_CONTENT, "avatar");
  195. if(thumbnailNodeRef != null)
  196. {
  197. avatar = thumbnailNodeRef;
  198. }
  199. else
  200. {
  201. throw new EntityNotFoundException("avatar");
  202. }
  203. }
  204. else
  205. {
  206. throw new EntityNotFoundException("avatar");
  207. }
  208. }
  209. else
  210. {
  211. throw new EntityNotFoundException(personId);
  212. }
  213. return avatar;
  214. }
  215. /**
  216. *
  217. * @throws NoSuchPersonException if personId does not exist
  218. */
  219. public Person getPerson(String personId)
  220. {
  221. Person person = null;
  222. personId = validatePerson(personId);
  223. NodeRef personNode = personService.getPerson(personId, false);
  224. if (personNode != null)
  225. {
  226. Map<QName, Serializable> nodeProps = nodeService.getProperties(personNode);
  227. processPersonProperties(personId, nodeProps);
  228. // TODO this needs to be run as admin but should we do this here?
  229. final String pId = personId;
  230. Boolean enabled = AuthenticationUtil.runAsSystem(new RunAsWork<Boolean>()
  231. {
  232. public Boolean doWork() throws Exception
  233. {
  234. return authenticationService.getAuthenticationEnabled(pId);
  235. }
  236. });
  237. person = new Person(personNode, nodeProps, enabled);
  238. // get avatar information
  239. if(hasAvatar(personNode))
  240. {
  241. try
  242. {
  243. NodeRef avatar = getAvatar(personId);
  244. person.setAvatarId(avatar);
  245. }
  246. catch(EntityNotFoundException e)
  247. {
  248. // shouldn't happen, but ok
  249. }
  250. }
  251. }
  252. else
  253. {
  254. throw new EntityNotFoundException(personId);
  255. }
  256. return person;
  257. }
  258. private void addToMap(Map<QName, Serializable> properties, QName name, Serializable value)
  259. {
  260. // if(name != null && value != null)
  261. // {
  262. properties.put(name, value);
  263. // }
  264. }
  265. public Person updatePerson(String personId, final Person person)
  266. {
  267. personId = validatePerson(personId);
  268. final Map<QName, Serializable> properties = new HashMap<QName, Serializable>();
  269. // addToMap(properties, ContentModel.PROP_USERNAME, person.getUserName());
  270. addToMap(properties, ContentModel.PROP_FIRSTNAME, person.getFirstName());
  271. addToMap(properties, ContentModel.PROP_LASTNAME, person.getLastName());
  272. addToMap(properties, ContentModel.PROP_JOBTITLE, person.getJobTitle());
  273. addToMap(properties, ContentModel.PROP_LOCATION, person.getLocation());
  274. addToMap(properties, ContentModel.PROP_TELEPHONE, person.getTelephone());
  275. addToMap(properties, ContentModel.PROP_MOBILE, person.getMobile());
  276. addToMap(properties, ContentModel.PROP_EMAIL, person.getEmail());
  277. Company company = person.getCompany();
  278. if(company != null)
  279. {
  280. addToMap(properties, ContentModel.PROP_ORGANIZATION, company.getOrganization());
  281. addToMap(properties, ContentModel.PROP_COMPANYADDRESS1, company.getAddress1());
  282. addToMap(properties, ContentModel.PROP_COMPANYADDRESS2, company.getAddress2());
  283. addToMap(properties, ContentModel.PROP_COMPANYADDRESS3, company.getAddress3());
  284. addToMap(properties, ContentModel.PROP_COMPANYPOSTCODE, company.getPostcode());
  285. addToMap(properties, ContentModel.PROP_COMPANYTELEPHONE, company.getTelephone());
  286. addToMap(properties, ContentModel.PROP_COMPANYFAX, company.getFax());
  287. addToMap(properties, ContentModel.PROP_COMPANYEMAIL, company.getEmail());
  288. }
  289. else
  290. {
  291. addToMap(properties, ContentModel.PROP_ORGANIZATION, null);
  292. addToMap(properties, ContentModel.PROP_COMPANYADDRESS1, null);
  293. addToMap(properties, ContentModel.PROP_COMPANYADDRESS2, null);
  294. addToMap(properties, ContentModel.PROP_COMPANYADDRESS3, null);
  295. addToMap(properties, ContentModel.PROP_COMPANYPOSTCODE, null);
  296. addToMap(properties, ContentModel.PROP_COMPANYTELEPHONE, null);
  297. addToMap(properties, ContentModel.PROP_COMPANYFAX, null);
  298. addToMap(properties, ContentModel.PROP_COMPANYEMAIL, null);
  299. }
  300. addToMap(properties, ContentModel.PROP_SKYPE, person.getSkypeId());
  301. addToMap(properties, ContentModel.PROP_INSTANTMSG, person.getInstantMessageId());
  302. addToMap(properties, ContentModel.PROP_USER_STATUS, person.getUserStatus());
  303. addToMap(properties, ContentModel.PROP_USER_STATUS_TIME, person.getStatusUpdatedAt());
  304. addToMap(properties, ContentModel.PROP_GOOGLEUSERNAME, person.getGoogleId());
  305. addToMap(properties, ContentModel.PROP_SIZE_QUOTA, person.getQuota());
  306. addToMap(properties, ContentModel.PROP_SIZE_CURRENT, person.getQuotaUsed());
  307. addToMap(properties, ContentModel.PROP_DESCRIPTION, person.getDescription());
  308. final String pId = personId;
  309. AuthenticationUtil.runAsSystem(new RunAsWork<Void>()
  310. {
  311. @Override
  312. public Void doWork() throws Exception
  313. {
  314. personService.setPersonProperties(pId, properties, false);
  315. return null;
  316. }
  317. });
  318. return getPerson(personId);
  319. }
  320. }