Guest User

Untitled

a guest
Aug 7th, 2018
131
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.09 KB | None | 0 0
  1. @RunWith(classOf[JUnitRunner])
  2. class JRSuperUserSpec extends WordSpec with ShouldMatchers with LoggingTrait{
  3. val superuser = "admin"
  4. var password = admin
  5.  
  6. val tmpFolder = new File("tmp" + File.separator + UUID.randomUUID.toString)
  7. tmpFolder.mkdirs
  8.  
  9. def inSession[T](repository:TransientRepository,username:String,password:String)(block: JackrabbitSession => T) : T = {
  10. val session = repository.login(new SimpleCredentials(username,password.toCharArray)).asInstanceOf[JackrabbitSession]
  11. try{
  12. block(session)
  13. }finally{
  14. session.logout
  15. }
  16. }
  17. val repository = new TransientRepository(new File("src/test/resources/repository.xml"),tmpFolder)
  18.  
  19. "The super user" when {
  20. "managing groups" should {
  21. "be able to create the administrators group and give its members full rights" in{
  22. inSession(repository,superuser,password){session =>
  23. val um = session.getUserManager
  24. val acm = session.getAccessControlManager
  25. val group = um.createGroup(SecurityConstants.ADMINISTRATORS_NAME)
  26.  
  27. val policies = acm.getPolicies(session.getRootNode.getPath)
  28.  
  29. policies.foreach { policy =>
  30. policy match{
  31. case acl : AccessControlList =>
  32. acl.addAccessControlEntry(group.getPrincipal,Array( acm.privilegeFromName(Privilege.JCR_ALL)))
  33. acm.setPolicy(session.getRootNode.getPath,policy)
  34. case _ => fail("Unable to give rights to the administrators group")
  35. }
  36. }
  37. session.save
  38. }
  39. }
  40.  
  41. "be able to create a new administrator. The new admin can also create users" in {
  42. inSession(repository,superuser,password){session =>
  43. val um = session.getUserManager
  44. val group = um.getAuthorizable(SecurityConstants.ADMINISTRATORS_NAME).asInstanceOf[Group]
  45.  
  46. val newadmin = "newadmin"
  47. val user = um.createUser(newadmin,newadmin)
  48. group.addMember(user)
  49. inSession(repository,newadmin,newadmin){ usersession =>
  50. usersession.getUserManager.createUser("foo","bar")
  51. }
  52. }
  53. }
  54. }
  55. }
  56. }
Add Comment
Please, Sign In to add comment