1 17 package org.alfresco.web.bean.users; 18 19 import java.text.MessageFormat ; 20 import java.util.ArrayList ; 21 import java.util.Collections ; 22 import java.util.List ; 23 import java.util.Map ; 24 25 import javax.faces.context.FacesContext; 26 import javax.faces.event.ActionEvent; 27 import javax.transaction.UserTransaction ; 28 29 import org.alfresco.model.ContentModel; 30 import org.alfresco.repo.security.authentication.AuthenticationException; 31 import org.alfresco.service.cmr.repository.InvalidNodeRefException; 32 import org.alfresco.service.cmr.repository.NodeRef; 33 import org.alfresco.service.cmr.repository.NodeService; 34 import org.alfresco.service.cmr.search.SearchParameters; 35 import org.alfresco.service.cmr.search.SearchService; 36 import org.alfresco.service.cmr.security.AuthenticationService; 37 import org.alfresco.service.cmr.security.PersonService; 38 import org.alfresco.web.app.Application; 39 import org.alfresco.web.app.context.IContextListener; 40 import org.alfresco.web.app.context.UIContextService; 41 import org.alfresco.web.bean.LoginBean; 42 import org.alfresco.web.bean.repository.MapNode; 43 import org.alfresco.web.bean.repository.Node; 44 import org.alfresco.web.bean.repository.Repository; 45 import org.alfresco.web.ui.common.Utils; 46 import org.alfresco.web.ui.common.component.UIActionLink; 47 import org.alfresco.web.ui.common.component.data.UIRichList; 48 import org.apache.log4j.Logger; 49 50 53 public class UsersBean implements IContextListener 54 { 55 private static Logger logger = Logger.getLogger(UsersBean.class); 56 57 public static final String ERROR_PASSWORD_MATCH = "error_password_match"; 58 private static final String ERROR_DELETE = "error_delete_user"; 59 private static final String ERROR_USER_DELETE = "error_delete_user_object"; 60 61 private static final String DEFAULT_OUTCOME = "manageUsers"; 62 63 64 protected NodeService nodeService; 65 66 67 protected SearchService searchService; 68 69 70 protected AuthenticationService authenticationService; 71 72 73 protected PersonService personService; 74 75 76 protected UIRichList usersRichList; 77 78 79 private Node person = null; 80 81 private List <Node> users = Collections.<Node>emptyList(); 82 83 private String password = null; 84 private String confirm = null; 85 private String searchCriteria = null; 86 87 88 91 94 public UsersBean() 95 { 96 UIContextService.getInstance(FacesContext.getCurrentInstance()).registerBean(this); 97 } 98 99 100 103 106 public void setNodeService(NodeService nodeService) 107 { 108 this.nodeService = nodeService; 109 } 110 111 114 public void setSearchService(SearchService searchService) 115 { 116 this.searchService = searchService; 117 } 118 119 122 public void setAuthenticationService(AuthenticationService authenticationService) 123 { 124 this.authenticationService = authenticationService; 125 } 126 127 130 public void setPersonService(PersonService personService) 131 { 132 this.personService = personService; 133 } 134 135 138 public UIRichList getUsersRichList() 139 { 140 return this.usersRichList; 141 } 142 143 146 public void setUsersRichList(UIRichList usersRichList) 147 { 148 this.usersRichList = usersRichList; 149 } 150 151 154 public List <Node> getUsers() 155 { 156 if (this.users == null) 157 { 158 search(); 159 } 160 161 return this.users; 162 } 163 164 167 public String getSearchCriteria() 168 { 169 return searchCriteria; 170 } 171 172 175 public void setSearchCriteria(String searchCriteria) 176 { 177 this.searchCriteria = searchCriteria; 178 } 179 180 183 public String getConfirm() 184 { 185 return this.confirm; 186 } 187 188 191 public void setConfirm(String confirm) 192 { 193 this.confirm = confirm; 194 } 195 196 199 public String getPassword() 200 { 201 return this.password; 202 } 203 204 207 public void setPassword(String password) 208 { 209 this.password = password; 210 } 211 212 215 public Node getPerson() 216 { 217 return this.person; 218 } 219 220 223 public void setPerson(Node person) 224 { 225 this.person = person; 226 } 227 228 234 public void setupUserAction(ActionEvent event) 235 { 236 UIActionLink link = (UIActionLink) event.getComponent(); 237 Map <String , String > params = link.getParameterMap(); 238 String id = params.get("id"); 239 if (id != null && id.length() != 0) 240 { 241 if (logger.isDebugEnabled()) 242 logger.debug("Setup for action, setting current Person to: " + id); 243 244 try 245 { 246 NodeRef ref = new NodeRef(Repository.getStoreRef(), id); 248 Node node = new Node(ref); 249 250 setPerson(node); 252 253 contextUpdated(); 256 } 257 catch (InvalidNodeRefException refErr) 258 { 259 Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext 260 .getCurrentInstance(), Repository.ERROR_NODEREF), new Object [] { id })); 261 } 262 } 263 else 264 { 265 setPerson(null); 266 } 267 } 268 269 272 public String deleteOK() 273 { 274 UserTransaction tx = null; 275 276 try 277 { 278 FacesContext context = FacesContext.getCurrentInstance(); 279 tx = Repository.getUserTransaction(context); 280 tx.begin(); 281 282 String userName = (String )getPerson().getProperties().get("userName"); 283 284 Map session = context.getExternalContext().getSessionMap(); 286 if (session.get(LoginBean.LOGIN_EXTERNAL_AUTH) == null) 287 { 288 try 290 { 291 authenticationService.deleteAuthentication(userName); 292 } 293 catch (AuthenticationException authErr) 294 { 295 Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), ERROR_USER_DELETE)); 296 } 297 } 298 299 this.personService.deletePerson(userName); 301 302 tx.commit(); 304 305 search(); 307 } 308 catch (Throwable e) 309 { 310 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 312 Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext 313 .getCurrentInstance(), ERROR_DELETE), e.getMessage()), e); 314 } 315 316 return DEFAULT_OUTCOME; 317 } 318 319 322 public String changePasswordOK() 323 { 324 String outcome = DEFAULT_OUTCOME; 325 326 if (this.password != null && this.confirm != null && this.password.equals(this.confirm)) 327 { 328 try 329 { 330 String userName = (String )this.person.getProperties().get(ContentModel.PROP_USERNAME); 331 this.authenticationService.setAuthentication(userName, this.password.toCharArray()); 332 } 333 catch (Exception e) 334 { 335 outcome = null; 336 Utils.addErrorMessage(MessageFormat.format(Application.getMessage(FacesContext 337 .getCurrentInstance(), Repository.ERROR_GENERIC), e.getMessage()), e); 338 } 339 } 340 else 341 { 342 outcome = null; 343 Utils.addErrorMessage(Application.getMessage(FacesContext.getCurrentInstance(), 344 ERROR_PASSWORD_MATCH)); 345 } 346 347 return outcome; 348 } 349 350 355 public String search() 356 { 357 this.usersRichList.setValue(null); 358 359 if (this.searchCriteria == null || this.searchCriteria.length() == 0) 360 { 361 this.users = Collections.<Node>emptyList(); 362 } 363 else 364 { 365 FacesContext context = FacesContext.getCurrentInstance(); 366 UserTransaction tx = null; 367 368 try 369 { 370 tx = Repository.getUserTransaction(context, true); 371 tx.begin(); 372 373 String query = "( TYPE:\"{http://www.alfresco.org/model/content/1.0}person\") AND " + 375 "((@\\{http\\://www.alfresco.org/model/content/1.0\\}firstName:" + this.searchCriteria + 376 "*) OR (@\\{http\\://www.alfresco.org/model/content/1.0\\}lastName:" + this.searchCriteria + 377 "*) OR (@\\{http\\://www.alfresco.org/model/content/1.0\\}userName:" + this.searchCriteria + 378 "*)))"; 379 380 if (logger.isDebugEnabled()) 381 logger.debug("Query: " + query); 382 383 SearchParameters params = new SearchParameters(); 385 params.setLanguage(SearchService.LANGUAGE_LUCENE); 386 params.addStore(Repository.getStoreRef()); 387 params.setQuery(query); 388 389 List <NodeRef> people = this.searchService.query(params).getNodeRefs(); 390 391 if (logger.isDebugEnabled()) 392 logger.debug("Found " + people.size() + " users"); 393 394 this.users = new ArrayList <Node>(people.size()); 395 396 for (NodeRef nodeRef : people) 397 { 398 MapNode node = new MapNode(nodeRef); 400 401 Map <String , Object > props = node.getProperties(); 405 props.put("fullName", ((String )props.get("firstName")) + ' ' + ((String )props.get("lastName"))); 406 NodeRef homeFolderNodeRef = (NodeRef)props.get("homeFolder"); 407 if (homeFolderNodeRef != null) 408 { 409 props.put("homeSpace", homeFolderNodeRef); 410 } 411 412 this.users.add(node); 413 } 414 415 tx.commit(); 417 } 418 catch (InvalidNodeRefException refErr) 419 { 420 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 421 context, Repository.ERROR_NODEREF), new Object [] {"root"}) ); 422 this.users = Collections.<Node>emptyList(); 423 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 424 } 425 catch (Exception err) 426 { 427 Utils.addErrorMessage(MessageFormat.format(Application.getMessage( 428 context, Repository.ERROR_GENERIC), err.getMessage()), err ); 429 this.users = Collections.<Node>emptyList(); 430 try { if (tx != null) {tx.rollback();} } catch (Exception tex) {} 431 } 432 } 433 434 return null; 436 } 437 438 443 public String showAll() 444 { 445 this.usersRichList.setValue(null); 446 447 this.users = Repository.getUsers(FacesContext.getCurrentInstance(), 448 this.nodeService, this.searchService); 449 450 return null; 452 } 453 454 455 458 461 public void contextUpdated() 462 { 463 if (this.usersRichList != null) 464 { 465 this.usersRichList.setValue(null); 466 this.users = null; 467 } 468 } 469 } 470 | Popular Tags |