Mercurial > hg > LGDataverses
comparison src/main/java/edu/harvard/iq/dataverse/ManagePermissionsPage.java @ 10:a50cf11e5178
Rewrite LGDataverse completely upgrading to dataverse4.0
| author | Zoe Hong <zhong@mpiwg-berlin.mpg.de> |
|---|---|
| date | Tue, 08 Sep 2015 17:00:21 +0200 |
| parents | |
| children |
comparison
equal
deleted
inserted
replaced
| 9:5926d6419569 | 10:a50cf11e5178 |
|---|---|
| 1 package edu.harvard.iq.dataverse; | |
| 2 | |
| 3 import edu.harvard.iq.dataverse.authorization.AuthenticationServiceBean; | |
| 4 import edu.harvard.iq.dataverse.authorization.DataverseRole; | |
| 5 import edu.harvard.iq.dataverse.authorization.Permission; | |
| 6 import edu.harvard.iq.dataverse.authorization.RoleAssignee; | |
| 7 import edu.harvard.iq.dataverse.authorization.RoleAssigneeDisplayInfo; | |
| 8 import edu.harvard.iq.dataverse.authorization.groups.Group; | |
| 9 import edu.harvard.iq.dataverse.authorization.groups.GroupException; | |
| 10 import edu.harvard.iq.dataverse.authorization.groups.GroupServiceBean; | |
| 11 import edu.harvard.iq.dataverse.authorization.groups.impl.builtin.AuthenticatedUsers; | |
| 12 import edu.harvard.iq.dataverse.authorization.groups.impl.explicit.ExplicitGroup; | |
| 13 import edu.harvard.iq.dataverse.authorization.groups.impl.explicit.ExplicitGroupServiceBean; | |
| 14 import edu.harvard.iq.dataverse.authorization.users.AuthenticatedUser; | |
| 15 import edu.harvard.iq.dataverse.engine.command.exception.CommandException; | |
| 16 import edu.harvard.iq.dataverse.engine.command.exception.PermissionException; | |
| 17 import edu.harvard.iq.dataverse.engine.command.impl.AssignRoleCommand; | |
| 18 import edu.harvard.iq.dataverse.engine.command.impl.CreateExplicitGroupCommand; | |
| 19 import edu.harvard.iq.dataverse.engine.command.impl.CreateRoleCommand; | |
| 20 import edu.harvard.iq.dataverse.engine.command.impl.RevokeRoleCommand; | |
| 21 import edu.harvard.iq.dataverse.engine.command.impl.UpdateDataverseDefaultContributorRoleCommand; | |
| 22 import edu.harvard.iq.dataverse.util.JsfHelper; | |
| 23 import static edu.harvard.iq.dataverse.util.JsfHelper.JH; | |
| 24 import edu.harvard.iq.dataverse.util.StringUtil; | |
| 25 import java.util.ArrayList; | |
| 26 import java.util.Arrays; | |
| 27 import java.util.Collections; | |
| 28 import java.util.LinkedList; | |
| 29 import java.util.List; | |
| 30 import java.util.Set; | |
| 31 import java.util.logging.Level; | |
| 32 import java.util.logging.Logger; | |
| 33 import java.util.regex.Pattern; | |
| 34 import javax.ejb.EJB; | |
| 35 import javax.faces.application.FacesMessage; | |
| 36 import javax.faces.component.UIComponent; | |
| 37 import javax.faces.component.UIInput; | |
| 38 import javax.faces.context.FacesContext; | |
| 39 import javax.faces.event.ActionEvent; | |
| 40 import javax.faces.view.ViewScoped; | |
| 41 import javax.inject.Inject; | |
| 42 import javax.inject.Named; | |
| 43 import javax.persistence.EntityManager; | |
| 44 import javax.persistence.PersistenceContext; | |
| 45 import org.apache.commons.lang.StringUtils; | |
| 46 | |
| 47 /** | |
| 48 * | |
| 49 * @author gdurand | |
| 50 */ | |
| 51 @ViewScoped | |
| 52 @Named | |
| 53 public class ManagePermissionsPage implements java.io.Serializable { | |
| 54 | |
| 55 private static final Logger logger = Logger.getLogger(ManagePermissionsPage.class.getCanonicalName()); | |
| 56 | |
| 57 @EJB | |
| 58 DvObjectServiceBean dvObjectService; | |
| 59 @EJB | |
| 60 DataverseRoleServiceBean roleService; | |
| 61 @EJB | |
| 62 RoleAssigneeServiceBean roleAssigneeService; | |
| 63 @EJB | |
| 64 PermissionServiceBean permissionService; | |
| 65 @EJB | |
| 66 AuthenticationServiceBean authenticationService; | |
| 67 @EJB | |
| 68 GroupServiceBean groupService; | |
| 69 @EJB | |
| 70 EjbDataverseEngine commandEngine; | |
| 71 | |
| 72 | |
| 73 @PersistenceContext(unitName = "VDCNet-ejbPU") | |
| 74 EntityManager em; | |
| 75 | |
| 76 @Inject | |
| 77 DataverseSession session; | |
| 78 | |
| 79 DvObject dvObject = new Dataverse(); // by default we use a Dataverse, but this will be overridden in init by the findById | |
| 80 | |
| 81 public DvObject getDvObject() { | |
| 82 return dvObject; | |
| 83 } | |
| 84 | |
| 85 public void setDvObject(DvObject dvObject) { | |
| 86 this.dvObject = dvObject; | |
| 87 /*if (dvObject instanceof DvObjectContainer) { | |
| 88 inheritAssignments = !((DvObjectContainer) dvObject).isPermissionRoot(); | |
| 89 }*/ | |
| 90 } | |
| 91 | |
| 92 public String init() { | |
| 93 //@todo deal with any kind of dvObject | |
| 94 if (dvObject.getId() != null) { | |
| 95 dvObject = dvObjectService.findDvObject(dvObject.getId()); | |
| 96 } | |
| 97 | |
| 98 // check if dvObject exists and user has permission | |
| 99 if (dvObject == null) { | |
| 100 return "/404.xhtml"; | |
| 101 } | |
| 102 | |
| 103 // for dataFiles, check the perms on its owning dataset | |
| 104 DvObject checkPermissionsdvObject = dvObject instanceof DataFile ? dvObject.getOwner() : dvObject; | |
| 105 if (!permissionService.on(checkPermissionsdvObject).has(checkPermissionsdvObject instanceof Dataverse ? Permission.ManageDataversePermissions : Permission.ManageDatasetPermissions)) { | |
| 106 return "/loginpage.xhtml" + DataverseHeaderFragment.getRedirectPage(); | |
| 107 } | |
| 108 | |
| 109 // initialize the configure settings | |
| 110 if (dvObject instanceof Dataverse) { | |
| 111 initAccessSettings(); | |
| 112 } | |
| 113 return ""; | |
| 114 } | |
| 115 | |
| 116 /* | |
| 117 main page - role assignment table | |
| 118 */ | |
| 119 | |
| 120 // used by remove Role Assignment | |
| 121 private RoleAssignment selectedRoleAssignment; | |
| 122 | |
| 123 public RoleAssignment getSelectedRoleAssignment() { | |
| 124 return selectedRoleAssignment; | |
| 125 } | |
| 126 | |
| 127 public void setSelectedRoleAssignment(RoleAssignment selectedRoleAssignment) { | |
| 128 this.selectedRoleAssignment = selectedRoleAssignment; | |
| 129 } | |
| 130 | |
| 131 public List<RoleAssignmentRow> getRoleAssignments() { | |
| 132 List<RoleAssignmentRow> raList = null; | |
| 133 if (dvObject != null && dvObject.getId() != null) { | |
| 134 Set<RoleAssignment> ras = roleService.rolesAssignments(dvObject); | |
| 135 raList = new ArrayList<>(ras.size()); | |
| 136 for (RoleAssignment roleAssignment : ras) { | |
| 137 // for files, only show role assignments which can download | |
| 138 if (!(dvObject instanceof DataFile) || roleAssignment.getRole().permissions().contains(Permission.DownloadFile)) { | |
| 139 RoleAssignee roleAssignee = roleAssigneeService.getRoleAssignee(roleAssignment.getAssigneeIdentifier()); | |
| 140 if (roleAssignee != null) { | |
| 141 raList.add(new RoleAssignmentRow(roleAssignment, roleAssignee.getDisplayInfo())); | |
| 142 } else { | |
| 143 logger.info("Could not find role assignee based on role assignment id " + roleAssignment.getId()); | |
| 144 } | |
| 145 } | |
| 146 } | |
| 147 } | |
| 148 return raList; | |
| 149 } | |
| 150 | |
| 151 public void removeRoleAssignment() { | |
| 152 revokeRole(selectedRoleAssignment); | |
| 153 | |
| 154 if (dvObject instanceof Dataverse) { | |
| 155 initAccessSettings(); // in case the revoke was for the AuthenticatedUsers group | |
| 156 } | |
| 157 | |
| 158 showAssignmentMessages(); | |
| 159 } | |
| 160 | |
| 161 // internal method used by removeRoleAssignment and saveConfiguration | |
| 162 private void revokeRole(RoleAssignment ra) { | |
| 163 try { | |
| 164 commandEngine.submit(new RevokeRoleCommand(ra, session.getUser())); | |
| 165 JsfHelper.addSuccessMessage(ra.getRole().getName() + " role for " + roleAssigneeService.getRoleAssignee(ra.getAssigneeIdentifier()).getDisplayInfo().getTitle() + " was removed."); | |
| 166 } catch (PermissionException ex) { | |
| 167 JH.addMessage(FacesMessage.SEVERITY_ERROR, "The role assignment was not able to be removed.", "Permissions " + ex.getRequiredPermissions().toString() + " missing."); | |
| 168 } catch (CommandException ex) { | |
| 169 JH.addMessage(FacesMessage.SEVERITY_FATAL, "The role assignment could not be removed."); | |
| 170 logger.log(Level.SEVERE, "Error removing role assignment: " + ex.getMessage(), ex); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 /* | |
| 175 main page - roles table | |
| 176 */ | |
| 177 | |
| 178 public List<DataverseRole> getRoles() { | |
| 179 if (dvObject != null && dvObject.getId() != null) { | |
| 180 return roleService.findByOwnerId(dvObject.getId()); | |
| 181 } | |
| 182 return new ArrayList(); | |
| 183 } | |
| 184 | |
| 185 public void createNewRole(ActionEvent e) { | |
| 186 setRole(new DataverseRole()); | |
| 187 role.setOwner(dvObject); | |
| 188 } | |
| 189 | |
| 190 public void cloneRole(String roleId) { | |
| 191 DataverseRole clonedRole = new DataverseRole(); | |
| 192 clonedRole.setOwner(dvObject); | |
| 193 | |
| 194 DataverseRole originalRole = roleService.find(Long.parseLong(roleId)); | |
| 195 clonedRole.addPermissions(originalRole.permissions()); | |
| 196 setRole(clonedRole); | |
| 197 } | |
| 198 | |
| 199 public void editRole(String roleId) { | |
| 200 setRole(roleService.find(Long.parseLong(roleId))); | |
| 201 } | |
| 202 | |
| 203 /* | |
| 204 ============================================================================ | |
| 205 edit configuration dialog // only for dataverse version of page | |
| 206 ============================================================================ | |
| 207 */ | |
| 208 | |
| 209 private String authenticatedUsersContributorRoleAlias = null; | |
| 210 private String defaultContributorRoleAlias = DataverseRole.EDITOR; | |
| 211 | |
| 212 public String getAuthenticatedUsersContributorRoleAlias() { | |
| 213 return authenticatedUsersContributorRoleAlias; | |
| 214 } | |
| 215 | |
| 216 public void setAuthenticatedUsersContributorRoleAlias(String authenticatedUsersContributorRoleAlias) { | |
| 217 this.authenticatedUsersContributorRoleAlias = authenticatedUsersContributorRoleAlias; | |
| 218 } | |
| 219 | |
| 220 public String getDefaultContributorRoleAlias() { | |
| 221 return defaultContributorRoleAlias; | |
| 222 } | |
| 223 | |
| 224 public void setDefaultContributorRoleAlias(String defaultContributorRoleAlias) { | |
| 225 this.defaultContributorRoleAlias = defaultContributorRoleAlias; | |
| 226 } | |
| 227 | |
| 228 public void initAccessSettings() { | |
| 229 if (dvObject instanceof Dataverse) { | |
| 230 authenticatedUsersContributorRoleAlias = ""; | |
| 231 | |
| 232 List<RoleAssignment> aUsersRoleAssignments = roleService.directRoleAssignments(AuthenticatedUsers.get(), dvObject); | |
| 233 for (RoleAssignment roleAssignment : aUsersRoleAssignments) { | |
| 234 String roleAlias = roleAssignment.getRole().getAlias(); | |
| 235 authenticatedUsersContributorRoleAlias = roleAlias; | |
| 236 break; | |
| 237 // @todo handle case where more than one role has been assigned to the AutenticatedUsers group! | |
| 238 } | |
| 239 | |
| 240 defaultContributorRoleAlias = ((Dataverse) dvObject).getDefaultContributorRole().getAlias(); | |
| 241 } | |
| 242 } | |
| 243 | |
| 244 | |
| 245 public void saveConfiguration(ActionEvent e) { | |
| 246 // Set role (if any) for authenticatedUsers | |
| 247 DataverseRole roleToAssign = null; | |
| 248 List<String> contributorRoles = Arrays.asList(DataverseRole.FULL_CONTRIBUTOR, DataverseRole.DV_CONTRIBUTOR, DataverseRole.DS_CONTRIBUTOR); | |
| 249 | |
| 250 if (!StringUtil.isEmpty(authenticatedUsersContributorRoleAlias)) { | |
| 251 roleToAssign = roleService.findBuiltinRoleByAlias(authenticatedUsersContributorRoleAlias); | |
| 252 } | |
| 253 | |
| 254 // then, check current contributor role | |
| 255 List<RoleAssignment> aUsersRoleAssignments = roleService.directRoleAssignments(AuthenticatedUsers.get(), dvObject); | |
| 256 for (RoleAssignment roleAssignment : aUsersRoleAssignments) { | |
| 257 DataverseRole currentRole = roleAssignment.getRole(); | |
| 258 if (contributorRoles.contains(currentRole.getAlias())) { | |
| 259 if (currentRole.equals(roleToAssign)) { | |
| 260 roleToAssign = null; // found the role, so no need to assign | |
| 261 } else { | |
| 262 revokeRole(roleAssignment); | |
| 263 } | |
| 264 } | |
| 265 } | |
| 266 // finally, assign role, if new | |
| 267 if (roleToAssign != null) { | |
| 268 assignRole(AuthenticatedUsers.get(), roleToAssign); | |
| 269 } | |
| 270 | |
| 271 // set dataverse default contributor role | |
| 272 if (dvObject instanceof Dataverse) { | |
| 273 Dataverse dv = (Dataverse) dvObject; | |
| 274 DataverseRole defaultRole = roleService.findBuiltinRoleByAlias(defaultContributorRoleAlias); | |
| 275 if (!defaultRole.equals(dv.getDefaultContributorRole())) { | |
| 276 try { | |
| 277 commandEngine.submit(new UpdateDataverseDefaultContributorRoleCommand(defaultRole, session.getUser(), dv)); | |
| 278 JsfHelper.addSuccessMessage("The default permissions for this dataverse have been updated."); | |
| 279 } catch (PermissionException ex) { | |
| 280 JH.addMessage(FacesMessage.SEVERITY_ERROR, "Cannot assign default permissions.", "Permissions " + ex.getRequiredPermissions().toString() + " missing."); | |
| 281 } catch (CommandException ex) { | |
| 282 JH.addMessage(FacesMessage.SEVERITY_FATAL, "Cannot assign default permissions."); | |
| 283 logger.log(Level.SEVERE, "Error assigning default permissions: " + ex.getMessage(), ex); | |
| 284 } | |
| 285 } | |
| 286 } | |
| 287 showConfigureMessages(); | |
| 288 } | |
| 289 | |
| 290 /* | |
| 291 ============================================================================ | |
| 292 assign roles dialog | |
| 293 ============================================================================ | |
| 294 */ | |
| 295 private List<RoleAssignee> roleAssignSelectedRoleAssignees; | |
| 296 private Long selectedRoleId; | |
| 297 | |
| 298 public List<RoleAssignee> getRoleAssignSelectedRoleAssignees() { | |
| 299 return roleAssignSelectedRoleAssignees; | |
| 300 } | |
| 301 | |
| 302 public void setRoleAssignSelectedRoleAssignees(List<RoleAssignee> selectedRoleAssignees) { | |
| 303 this.roleAssignSelectedRoleAssignees = selectedRoleAssignees; | |
| 304 } | |
| 305 | |
| 306 public Long getSelectedRoleId() { | |
| 307 return selectedRoleId; | |
| 308 } | |
| 309 | |
| 310 public void setSelectedRoleId(Long selectedRoleId) { | |
| 311 this.selectedRoleId = selectedRoleId; | |
| 312 } | |
| 313 | |
| 314 public void initAssigneeDialog(ActionEvent ae) { | |
| 315 roleAssignSelectedRoleAssignees = new LinkedList<>(); | |
| 316 selectedRoleId = null; | |
| 317 showNoMessages(); | |
| 318 } | |
| 319 | |
| 320 public List<RoleAssignee> completeRoleAssignee( String query ) { | |
| 321 List<RoleAssignee> roleAssigneeList = new ArrayList<>(); | |
| 322 // TODO push this to the authentication and group services. Below code retrieves all the users. | |
| 323 for (AuthenticatedUser au : authenticationService.findAllAuthenticatedUsers()) { | |
| 324 roleAssigneeList.add(au); | |
| 325 } | |
| 326 for ( Group g : groupService.findGlobalGroups() ) { | |
| 327 roleAssigneeList.add( g ); | |
| 328 } | |
| 329 roleAssigneeList.addAll( explicitGroupSvc.findAvailableFor(dvObject) ); | |
| 330 | |
| 331 List<RoleAssignee> filteredList = new LinkedList(); | |
| 332 for (RoleAssignee ra : roleAssigneeList) { | |
| 333 // @todo unsure if containsIgnore case will work for all locales | |
| 334 // @todo maybe add some solr/lucene style searching, did-you-mean style? | |
| 335 if (StringUtils.containsIgnoreCase(ra.getDisplayInfo().getTitle(), query) && | |
| 336 (roleAssignSelectedRoleAssignees == null || !roleAssignSelectedRoleAssignees.contains(ra))) { | |
| 337 filteredList.add(ra); | |
| 338 } | |
| 339 } | |
| 340 return filteredList; | |
| 341 } | |
| 342 | |
| 343 public List<DataverseRole> getAvailableRoles() { | |
| 344 List<DataverseRole> roles = new LinkedList<>(); | |
| 345 if (dvObject != null && dvObject.getId() != null) { | |
| 346 | |
| 347 if (dvObject instanceof Dataverse) { | |
| 348 roles.addAll(roleService.availableRoles(dvObject.getId())); | |
| 349 | |
| 350 } else if (dvObject instanceof Dataset) { | |
| 351 // don't show roles that only have Dataverse level permissions | |
| 352 // current the available roles for a dataset are gotten from its parent | |
| 353 for (DataverseRole role : roleService.availableRoles(dvObject.getOwner().getId())) { | |
| 354 for (Permission permission : role.permissions()) { | |
| 355 if (permission.appliesTo(Dataset.class) || permission.appliesTo(DataFile.class)) { | |
| 356 roles.add(role); | |
| 357 break; | |
| 358 } | |
| 359 } | |
| 360 } | |
| 361 | |
| 362 } else if (dvObject instanceof DataFile) { | |
| 363 roles.add(roleService.findBuiltinRoleByAlias(DataverseRole.FILE_DOWNLOADER)); | |
| 364 } | |
| 365 | |
| 366 Collections.sort(roles, DataverseRole.CMP_BY_NAME); | |
| 367 } | |
| 368 return roles; | |
| 369 } | |
| 370 | |
| 371 public DataverseRole getAssignedRole() { | |
| 372 if (selectedRoleId != null) { | |
| 373 return roleService.find(selectedRoleId); | |
| 374 } | |
| 375 return null; | |
| 376 } | |
| 377 | |
| 378 public void assignRole(ActionEvent evt) { | |
| 379 logger.info("Got to assignRole"); | |
| 380 List<RoleAssignee> selectedRoleAssigneesList = getRoleAssignSelectedRoleAssignees(); | |
| 381 if ( selectedRoleAssigneesList == null ) { | |
| 382 logger.info("** SELECTED role asignees is null"); | |
| 383 selectedRoleAssigneesList = new LinkedList<>(); | |
| 384 } | |
| 385 for (RoleAssignee roleAssignee : selectedRoleAssigneesList) { | |
| 386 assignRole(roleAssignee, roleService.find(selectedRoleId)); | |
| 387 } | |
| 388 } | |
| 389 | |
| 390 private void assignRole(RoleAssignee ra, DataverseRole r) { | |
| 391 try { | |
| 392 commandEngine.submit(new AssignRoleCommand(ra, r, dvObject, session.getUser())); | |
| 393 JsfHelper.addSuccessMessage(r.getName() + " role assigned to " + ra.getDisplayInfo().getTitle() + " for " + dvObject.getDisplayName() + "."); | |
| 394 } catch (PermissionException ex) { | |
| 395 JH.addMessage(FacesMessage.SEVERITY_ERROR, "The role was not able to be assigned.", "Permissions " + ex.getRequiredPermissions().toString() + " missing."); | |
| 396 } catch (CommandException ex) { | |
| 397 JH.addMessage(FacesMessage.SEVERITY_FATAL, "The role was not able to be assigned."); | |
| 398 logger.log(Level.SEVERE, "Error assiging role: " + ex.getMessage(), ex); | |
| 399 } | |
| 400 | |
| 401 showAssignmentMessages(); | |
| 402 } | |
| 403 | |
| 404 /* | |
| 405 ============================================================================ | |
| 406 edit role dialog | |
| 407 ============================================================================ | |
| 408 */ | |
| 409 private DataverseRole role = new DataverseRole(); | |
| 410 private List<String> selectedPermissions; | |
| 411 | |
| 412 public DataverseRole getRole() { | |
| 413 return role; | |
| 414 } | |
| 415 | |
| 416 public void setRole(DataverseRole role) { | |
| 417 this.role = role; | |
| 418 selectedPermissions = new LinkedList<>(); | |
| 419 if (role != null) { | |
| 420 for (Permission p : role.permissions()) { | |
| 421 selectedPermissions.add(p.name()); | |
| 422 } | |
| 423 } | |
| 424 } | |
| 425 | |
| 426 public List<String> getSelectedPermissions() { | |
| 427 return selectedPermissions; | |
| 428 } | |
| 429 | |
| 430 public void setSelectedPermissions(List<String> selectedPermissions) { | |
| 431 this.selectedPermissions = selectedPermissions; | |
| 432 } | |
| 433 | |
| 434 public List<Permission> getPermissions() { | |
| 435 return Arrays.asList(Permission.values()); | |
| 436 } | |
| 437 | |
| 438 public void updateRole(ActionEvent e) { | |
| 439 // @todo currently only works for Dataverse since CreateRoleCommand only takes a dataverse | |
| 440 // we need to decide if we want roles at the dataset level or not | |
| 441 if (dvObject instanceof Dataverse) { | |
| 442 role.clearPermissions(); | |
| 443 for (String pmsnStr : getSelectedPermissions()) { | |
| 444 role.addPermission(Permission.valueOf(pmsnStr)); | |
| 445 } | |
| 446 try { | |
| 447 String roleState = role.getId() != null ? "updated" : "created"; | |
| 448 setRole(commandEngine.submit(new CreateRoleCommand(role, session.getUser(), (Dataverse) role.getOwner()))); | |
| 449 JsfHelper.addSuccessMessage("The role was " + roleState + ". To assign it to a user and/or group, click on the Assign Roles to Users/Groups button in the Users/Groups section of this page."); | |
| 450 } catch (PermissionException ex) { | |
| 451 JH.addMessage(FacesMessage.SEVERITY_ERROR, "The role was not able to be saved.", "Permissions " + ex.getRequiredPermissions().toString() + " missing."); | |
| 452 } catch (CommandException ex) { | |
| 453 JH.addMessage(FacesMessage.SEVERITY_FATAL, "The role was not able to be saved."); | |
| 454 logger.log(Level.SEVERE, "Error saving role: " + ex.getMessage(), ex); | |
| 455 } | |
| 456 } | |
| 457 showRoleMessages(); | |
| 458 } | |
| 459 | |
| 460 | |
| 461 /* | |
| 462 ============================================================================ | |
| 463 Explicit Group dialogs | |
| 464 ============================================================================ | |
| 465 */ | |
| 466 | |
| 467 String explicitGroupIdentifier = ""; | |
| 468 String explicitGroupName = ""; | |
| 469 String newExplicitGroupDescription = ""; | |
| 470 UIInput explicitGroupIdentifierField; | |
| 471 | |
| 472 @EJB | |
| 473 ExplicitGroupServiceBean explicitGroupSvc; | |
| 474 | |
| 475 List<RoleAssignee> newExplicitGroupRoleAssignees = new LinkedList<>(); | |
| 476 | |
| 477 public void initExplicitGroupDialog(ActionEvent ae) { | |
| 478 showNoMessages(); | |
| 479 setExplicitGroupName(""); | |
| 480 setExplicitGroupIdentifier(""); | |
| 481 setNewExplicitGroupDescription(""); | |
| 482 setNewExplicitGroupRoleAssignees(new LinkedList<RoleAssignee>()); | |
| 483 FacesContext context = FacesContext.getCurrentInstance(); | |
| 484 | |
| 485 } | |
| 486 | |
| 487 public void saveExplicitGroup(ActionEvent ae) { | |
| 488 | |
| 489 ExplicitGroup eg = explicitGroupSvc.getProvider().makeGroup(); | |
| 490 eg.setDisplayName( getExplicitGroupName() ); | |
| 491 eg.setGroupAliasInOwner( getExplicitGroupIdentifier() ); | |
| 492 eg.setDescription( getNewExplicitGroupDescription() ); | |
| 493 | |
| 494 if ( getNewExplicitGroupRoleAssignees()!= null ) { | |
| 495 try { | |
| 496 for ( RoleAssignee ra : getNewExplicitGroupRoleAssignees() ) { | |
| 497 eg.add( ra ); | |
| 498 } | |
| 499 } catch ( GroupException ge ) { | |
| 500 JsfHelper.JH.addMessage(FacesMessage.SEVERITY_ERROR, | |
| 501 "Group Creation failed.", | |
| 502 ge.getMessage()); | |
| 503 return; | |
| 504 } | |
| 505 } | |
| 506 try { | |
| 507 logger.info( "Attempting to create group " + eg.getGroupAliasInOwner() ); // TODO MBS remove | |
| 508 eg = commandEngine.submit( new CreateExplicitGroupCommand(session.getUser(), (Dataverse) getDvObject(), eg)); | |
| 509 JsfHelper.addSuccessMessage("Succesfully created group " + eg.getDisplayName()); | |
| 510 | |
| 511 } catch ( CreateExplicitGroupCommand.GroupAliasExistsException gaee ) { | |
| 512 logger.info( "Got me then message " + gaee.getMessage() ); // TODO MBS remove | |
| 513 explicitGroupIdentifierField.setValid( false ); | |
| 514 FacesContext.getCurrentInstance().addMessage(explicitGroupIdentifierField.getClientId(), | |
| 515 new FacesMessage( FacesMessage.SEVERITY_ERROR, gaee.getMessage(), null)); | |
| 516 | |
| 517 } catch (CommandException ex) { | |
| 518 logger.log(Level.WARNING, "Group creation failed", ex); | |
| 519 JsfHelper.JH.addMessage(FacesMessage.SEVERITY_ERROR, | |
| 520 "Group Creation failed.", | |
| 521 ex.getMessage()); | |
| 522 } catch (Exception ex) { | |
| 523 JH.addMessage(FacesMessage.SEVERITY_FATAL, "The role was not able to be saved."); | |
| 524 logger.log(Level.SEVERE, "Error saving role: " + ex.getMessage(), ex); | |
| 525 } | |
| 526 showAssignmentMessages(); | |
| 527 } | |
| 528 | |
| 529 public void setExplicitGroupName(String explicitGroupFriendlyName) { | |
| 530 this.explicitGroupName = explicitGroupFriendlyName; | |
| 531 } | |
| 532 | |
| 533 public String getExplicitGroupName() { | |
| 534 return explicitGroupName; | |
| 535 } | |
| 536 | |
| 537 public void setExplicitGroupIdentifier(String explicitGroupName) { | |
| 538 this.explicitGroupIdentifier = explicitGroupName; | |
| 539 } | |
| 540 | |
| 541 public String getExplicitGroupIdentifier() { | |
| 542 return explicitGroupIdentifier; | |
| 543 } | |
| 544 | |
| 545 public UIInput getExplicitGroupIdentifierField() { | |
| 546 return explicitGroupIdentifierField; | |
| 547 } | |
| 548 | |
| 549 public void setExplicitGroupIdentifierField(UIInput explicitGroupIdentifierField) { | |
| 550 this.explicitGroupIdentifierField = explicitGroupIdentifierField; | |
| 551 } | |
| 552 | |
| 553 public void validateGroupIdentifier(FacesContext context, UIComponent toValidate, Object rawValue) { | |
| 554 String value = (String) rawValue; | |
| 555 UIInput input = (UIInput) toValidate; | |
| 556 input.setValid(true); // Optimistic approach | |
| 557 | |
| 558 if ( context.getExternalContext().getRequestParameterMap().get("DO_GROUP_VALIDATION") != null | |
| 559 && !StringUtils.isEmpty(value) ) { | |
| 560 | |
| 561 // cheap test - regex | |
| 562 if (! Pattern.matches("^[a-zA-Z0-9\\_\\-]+$", value) ) { | |
| 563 input.setValid(false); | |
| 564 context.addMessage(toValidate.getClientId(), | |
| 565 new FacesMessage(FacesMessage.SEVERITY_ERROR, "", JH.localize("dataverse.permissions.explicitGroupEditDialog.groupIdentifier.invalid"))); | |
| 566 | |
| 567 } else if ( explicitGroupSvc.findInOwner(getDvObject().getId(), value) != null ) { | |
| 568 // Ok, see that the alias is not taken | |
| 569 input.setValid(false); | |
| 570 context.addMessage(toValidate.getClientId(), | |
| 571 new FacesMessage(FacesMessage.SEVERITY_ERROR, "", JH.localize("dataverse.permissions.explicitGroupEditDialog.groupIdentifier.taken"))); | |
| 572 } | |
| 573 } | |
| 574 } | |
| 575 | |
| 576 public void setNewExplicitGroupRoleAssignees(List<RoleAssignee> newExplicitGroupRoleAssignees) { | |
| 577 this.newExplicitGroupRoleAssignees = newExplicitGroupRoleAssignees; | |
| 578 } | |
| 579 | |
| 580 public List<RoleAssignee> getNewExplicitGroupRoleAssignees() { | |
| 581 return newExplicitGroupRoleAssignees; | |
| 582 } | |
| 583 | |
| 584 public String getNewExplicitGroupDescription() { | |
| 585 return newExplicitGroupDescription; | |
| 586 } | |
| 587 | |
| 588 public void setNewExplicitGroupDescription(String newExplicitGroupDescription) { | |
| 589 this.newExplicitGroupDescription = newExplicitGroupDescription; | |
| 590 } | |
| 591 | |
| 592 /* | |
| 593 ============================================================================ | |
| 594 Internal methods | |
| 595 ============================================================================ | |
| 596 */ | |
| 597 | |
| 598 boolean renderConfigureMessages = false; | |
| 599 boolean renderAssignmentMessages = false; | |
| 600 boolean renderRoleMessages = false; | |
| 601 | |
| 602 private void showNoMessages() { | |
| 603 renderConfigureMessages = false; | |
| 604 renderAssignmentMessages = false; | |
| 605 renderRoleMessages = false; | |
| 606 } | |
| 607 | |
| 608 private void showConfigureMessages() { | |
| 609 renderConfigureMessages = true; | |
| 610 renderAssignmentMessages = false; | |
| 611 renderRoleMessages = false; | |
| 612 } | |
| 613 | |
| 614 private void showAssignmentMessages() { | |
| 615 renderConfigureMessages = false; | |
| 616 renderAssignmentMessages = true; | |
| 617 renderRoleMessages = false; | |
| 618 } | |
| 619 | |
| 620 private void showRoleMessages() { | |
| 621 renderConfigureMessages = false; | |
| 622 renderAssignmentMessages = false; | |
| 623 renderRoleMessages = true; | |
| 624 } | |
| 625 | |
| 626 public Boolean getRenderConfigureMessages() { | |
| 627 return renderConfigureMessages; | |
| 628 } | |
| 629 | |
| 630 public void setRenderConfigureMessages(Boolean renderConfigureMessages) { | |
| 631 this.renderConfigureMessages = renderConfigureMessages; | |
| 632 } | |
| 633 | |
| 634 public Boolean getRenderAssignmentMessages() { | |
| 635 return renderAssignmentMessages; | |
| 636 } | |
| 637 | |
| 638 public void setRenderAssignmentMessages(Boolean renderAssignmentMessages) { | |
| 639 this.renderAssignmentMessages = renderAssignmentMessages; | |
| 640 } | |
| 641 | |
| 642 public Boolean getRenderRoleMessages() { | |
| 643 return renderRoleMessages; | |
| 644 } | |
| 645 | |
| 646 public void setRenderRoleMessages(Boolean renderRoleMessages) { | |
| 647 this.renderRoleMessages = renderRoleMessages; | |
| 648 } | |
| 649 | |
| 650 // inner class used for display of role assignments | |
| 651 public static class RoleAssignmentRow { | |
| 652 | |
| 653 private final RoleAssigneeDisplayInfo assigneeDisplayInfo; | |
| 654 private final RoleAssignment ra; | |
| 655 | |
| 656 public RoleAssignmentRow(RoleAssignment anRa, RoleAssigneeDisplayInfo disInf) { | |
| 657 ra = anRa; | |
| 658 assigneeDisplayInfo = disInf; | |
| 659 } | |
| 660 | |
| 661 public RoleAssignment getRoleAssignment() { | |
| 662 return ra; | |
| 663 } | |
| 664 | |
| 665 public RoleAssigneeDisplayInfo getAssigneeDisplayInfo() { | |
| 666 return assigneeDisplayInfo; | |
| 667 } | |
| 668 | |
| 669 public DataverseRole getRole() { | |
| 670 return ra.getRole(); | |
| 671 } | |
| 672 | |
| 673 public String getRoleName() { | |
| 674 return getRole().getName(); | |
| 675 } | |
| 676 | |
| 677 | |
| 678 public DvObject getDefinitionPoint() { | |
| 679 return ra.getDefinitionPoint(); | |
| 680 } | |
| 681 | |
| 682 public String getAssignedDvName() { | |
| 683 return ra.getDefinitionPoint().getDisplayName(); | |
| 684 } | |
| 685 | |
| 686 public Long getId() { | |
| 687 return ra.getId(); | |
| 688 } | |
| 689 | |
| 690 } | |
| 691 } |
