/** * Saves a user. * * @param user the user to save * @param currentUser the user performing the save operation */ public void saveUser(User user, User currentUser) throws IOException { Assert.notNull(user); Assert.notNull(currentUser); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); Map<String, Object> userMap = new HashMap<String, Object>(); userMap.put("loginName", user.getLoginName()); // $NON-NLS-1$ userMap.put("password", user.getPassword()); // $NON-NLS-1$ userMap.put("email", user.getEmail()); // $NON-NLS-1$ userMap.put("disabled", Boolean.valueOf(user.isDisabled())); // $NON-NLS-1$ if (!user.getOpenIds().isEmpty()) { userMap.put("openIds", user.getOpenIds()); // $NON-NLS-1$ } Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gson.toJson(userMap); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File workingFile = new File(workingDir, user.getLoginName() + USER_SUFFIX); FileUtils.write(workingFile, json, Charsets.UTF_8); Git git = Git.wrap(repo.r()); git.add().addFilepattern(user.getLoginName() + USER_SUFFIX).call(); PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage(user.getLoginName()).call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Util.closeQuietly(repo); } }
/** * Saves a role. * * @param role the role to save * @param currentUser the user performing the save operation */ public void saveRole(Role role, User currentUser) throws IOException { Assert.notNull(role); Assert.notNull(currentUser); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); Map<String, Object> roleMap = new HashMap<String, Object>(); roleMap.put("name", role.getName()); // $NON-NLS-1$ Set<String> permissions = Sets.newHashSet(); for (Permission permission : role.getPermissions()) { permissions.add(permission.name()); } roleMap.put("permissions", permissions); // $NON-NLS-1$ Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gson.toJson(roleMap); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File workingFile = new File(workingDir, role.getName() + ROLE_SUFFIX); FileUtils.write(workingFile, json, Charsets.UTF_8); Git git = Git.wrap(repo.r()); git.add().addFilepattern(role.getName() + ROLE_SUFFIX).call(); PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage(role.getName()).call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Util.closeQuietly(repo); } }
@Test public void getMarkdown() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); Page page1 = Page.fromText("title", UUID.randomUUID().toString()); // $NON-NLS-1$ pageStore.savePage(PROJECT, BRANCH_1, "home", page1, null, USER); // $NON-NLS-1$ RevCommit commit1 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$ Page page2 = Page.fromText("title", UUID.randomUUID().toString()); // $NON-NLS-1$ pageStore.savePage(PROJECT, BRANCH_1, "home", page2, null, USER); // $NON-NLS-1$ RevCommit commit2 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$ Page page3 = Page.fromText("title", UUID.randomUUID().toString()); // $NON-NLS-1$ pageStore.savePage(PROJECT, BRANCH_1, "home", page3, null, USER); // $NON-NLS-1$ RevCommit commit3 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$ Map<String, String> result = pageStore.getMarkdown( PROJECT, BRANCH_1, "home", //$NON-NLS-1$ Sets.newHashSet( "latest", "previous", commit2.getName(), commit1.getName())); // $NON-NLS-1$ //$NON-NLS-2$ assertEquals(commit3.getName(), result.get("latest")); // $NON-NLS-1$ assertEquals(commit2.getName(), result.get("previous")); // $NON-NLS-1$ assertEquals(((PageTextData) page2.getData()).getText(), result.get(commit2.getName())); assertEquals(((PageTextData) page1.getData()).getText(), result.get(commit1.getName())); }
private void saveUserAuthorities( String loginName, Set<RoleGrantedAuthority> authorities, ILockedRepository repo, User currentUser, boolean commit) throws IOException, GitAPIException { Map<String, Set<String>> authoritiesMap = new HashMap<String, Set<String>>(); for (RoleGrantedAuthority rga : authorities) { GrantedAuthorityTarget target = rga.getTarget(); String targetStr = target.getType().name() + ":" + target.getTargetId(); // $NON-NLS-1$ Set<String> roleNames = authoritiesMap.get(targetStr); if (roleNames == null) { roleNames = Sets.newHashSet(); authoritiesMap.put(targetStr, roleNames); } roleNames.add(rga.getRoleName()); } Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); String json = gson.toJson(authoritiesMap); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File workingFile = new File(workingDir, loginName + AUTHORITIES_SUFFIX); FileUtils.write(workingFile, json, Charsets.UTF_8); Git git = Git.wrap(repo.r()); git.add().addFilepattern(loginName + AUTHORITIES_SUFFIX).call(); if (commit) { PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit().setAuthor(ident).setCommitter(ident).setMessage(loginName).call(); } }
public void renameRole(String roleName, String newRoleName, User currentUser) throws IOException { Assert.hasLength(roleName); Assert.hasLength(newRoleName); Assert.notNull(currentUser); // check that role exists by trying to load it getRole(roleName); // check that new role does not exist by trying to load it try { getRole(newRoleName); throw new IllegalArgumentException("role already exists: " + newRoleName); // $NON-NLS-1$ } catch (RoleNotFoundException e) { // okay } log.info("renaming role: {} -> {}", roleName, newRoleName); // $NON-NLS-1$ ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File file = new File(workingDir, roleName + ROLE_SUFFIX); File newFile = new File(workingDir, newRoleName + ROLE_SUFFIX); FileUtils.copyFile(file, newFile); Git git = Git.wrap(repo.r()); git.rm().addFilepattern(roleName + ROLE_SUFFIX).call(); git.add().addFilepattern(newRoleName + ROLE_SUFFIX).call(); List<String> users = listUsers(repo); users.add(ANONYMOUS_USER_LOGIN_NAME); for (String user : users) { List<RoleGrantedAuthority> authorities = getUserAuthorities(user, repo); Set<RoleGrantedAuthority> newAuthorities = Sets.newHashSet(); for (Iterator<RoleGrantedAuthority> iter = authorities.iterator(); iter.hasNext(); ) { RoleGrantedAuthority rga = iter.next(); if (rga.getRoleName().equals(roleName)) { RoleGrantedAuthority newRga = new RoleGrantedAuthority(rga.getTarget(), newRoleName); newAuthorities.add(newRga); iter.remove(); } } if (!newAuthorities.isEmpty()) { authorities.addAll(newAuthorities); saveUserAuthorities(user, Sets.newHashSet(authorities), repo, currentUser, false); } } PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit() .setAuthor(ident) .setCommitter(ident) .setMessage("rename role " + roleName + " to " + newRoleName) // $NON-NLS-1$ //$NON-NLS-2$ .call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Util.closeQuietly(repo); } }
@Test public void deleteAttachment() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); saveRandomPage(BRANCH_1, PAGE); Page attachment = Page.fromData(new byte[] {1, 2, 3}, "application/octet-stream"); // $NON-NLS-1$ pageStore.saveAttachment(PROJECT, BRANCH_1, PAGE, "test.dat", attachment, USER); // $NON-NLS-1$ assertFalse(pageStore.listPageAttachments(PROJECT, BRANCH_1, PAGE).isEmpty()); File pageFile = new File( new File(new File(RepositoryUtil.getWorkingDir(repo.r()), "attachments"), PAGE), "test.dat.page"); //$NON-NLS-1$ //$NON-NLS-2$ File metaFile = new File( new File(new File(RepositoryUtil.getWorkingDir(repo.r()), "attachments"), PAGE), "test.dat.meta"); //$NON-NLS-1$ //$NON-NLS-2$ assertTrue(pageFile.exists()); assertTrue(metaFile.exists()); pageStore.deleteAttachment(PROJECT, BRANCH_1, PAGE, "test.dat", USER); // $NON-NLS-1$ assertTrue(pageStore.listPageAttachments(PROJECT, BRANCH_1, PAGE).isEmpty()); assertFalse(pageFile.exists()); assertFalse(metaFile.exists()); assertClean(repo.r()); }
/** * Returns the role that has the specified name. * * @throws RoleNotFoundException when the role could not be found */ public Role getRole(String roleName) throws IOException { Assert.hasLength(roleName); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); String json = BlobUtils.getHeadContent(repo.r(), roleName + ROLE_SUFFIX); if (json == null) { throw new RoleNotFoundException(roleName); } Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); Map<String, Object> roleMap = gson.fromJson(json, new TypeToken<Map<String, Object>>() {}.getType()); @SuppressWarnings("unchecked") Collection<String> permissions = (Collection<String>) roleMap.get("permissions"); // $NON-NLS-1$ EnumSet<Permission> rolePermissions = EnumSet.noneOf(Permission.class); for (String permission : permissions) { rolePermissions.add(Permission.valueOf(permission)); } Role role = new Role(roleName, rolePermissions); return role; } finally { Util.closeQuietly(repo); } }
@Test public void savePageWithSameBaseCommitAndConflictingChanges() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); Page page = Page.fromText("title", "a\nb\nc\nd\ne\nf\ng\nh\ni\nj\n"); // $NON-NLS-1$ //$NON-NLS-2$ pageStore.savePage(PROJECT, BRANCH_1, PAGE, page, null, USER); String baseCommit = pageStore.getPageMetadata(PROJECT, BRANCH_1, PAGE).getCommit(); page = Page.fromText("title", "a\nbbb\nc\nd\ne\nf\ng\nh\ni\nj\n"); // $NON-NLS-1$ //$NON-NLS-2$ pageStore.savePage(PROJECT, BRANCH_1, PAGE, page, baseCommit, USER); page = Page.fromText("title", "a\nxxx\nc\nd\ne\nf\ng\nh\ni\nj\n"); // $NON-NLS-1$ //$NON-NLS-2$ MergeConflict conflict = pageStore.savePage(PROJECT, BRANCH_1, PAGE, page, baseCommit, USER); Page result = pageStore.getPage(PROJECT, BRANCH_1, PAGE, true); assertNotNull(conflict); assertEquals( "a\nbbb\nc\nd\ne\nf\ng\nh\ni\nj\n", ((PageTextData) result.getData()).getText()); // $NON-NLS-1$ assertEquals( "a\n<<<<<<< OURS\nbbb\n=======\nxxx\n>>>>>>> THEIRS\nc\nd\ne\nf\ng\nh\ni\nj\n", conflict.getText()); // $NON-NLS-1$ assertClean(repo.r()); }
/** * Returns the user that has an OpenID whose real ID is equal to the specified OpenID. * * @throws UserNotFoundException when the user could not be found */ public User getUserByOpenId(String openId) throws IOException { ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); FileFilter filter = new FileFilter() { @Override public boolean accept(File file) { return file.isFile() && file.getName().endsWith(USER_SUFFIX); } }; for (File file : workingDir.listFiles(filter)) { String loginName = StringUtils.substringBeforeLast(file.getName(), USER_SUFFIX); String json = FileUtils.readFileToString(file, Charsets.UTF_8); User user = getUser(loginName, json); for (OpenId id : user.getOpenIds()) { if (id.getRealId().equals(openId)) { return user; } } } throw new OpenIdNotFoundException(openId); } finally { Util.closeQuietly(repo); } }
@Test public void saveAndGetAttachment() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); saveRandomPage(BRANCH_1, "foo/bar/baz"); // $NON-NLS-1$ Page attachment = Page.fromData(new byte[] {1, 2, 3}, "application/octet-stream"); // $NON-NLS-1$ pageStore.saveAttachment( PROJECT, BRANCH_1, "foo/bar/baz", "test.dat", attachment, USER); //$NON-NLS-1$ //$NON-NLS-2$ Page result = pageStore.getAttachment( PROJECT, BRANCH_1, "foo/bar/baz", "test.dat"); // $NON-NLS-1$ //$NON-NLS-2$ assertTrue(ArrayUtils.isEquals(attachment.getData(), result.getData())); assertEquals(attachment.getContentType(), result.getContentType()); assertClean(repo.r()); }
/** Returns all known role names. */ public List<String> listRoles() throws IOException { ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); FileFilter filter = new FileFilter() { @Override public boolean accept(File file) { return file.isFile() && file.getName().endsWith(ROLE_SUFFIX); } }; List<File> files = Lists.newArrayList(workingDir.listFiles(filter)); Function<File, String> function = new Function<File, String>() { @Override public String apply(File file) { return StringUtils.substringBeforeLast(file.getName(), ROLE_SUFFIX); } }; List<String> users = Lists.newArrayList(Lists.transform(files, function)); Collections.sort(users); return users; } finally { Util.closeQuietly(repo); } }
private void deleteFromAllAuthorities( Predicate<RoleGrantedAuthority> predicate, String commitMessage, User currentUser) throws IOException, GitAPIException { ILockedRepository repo = null; try { List<String> users = listUsers(); users.add(ANONYMOUS_USER_LOGIN_NAME); repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); boolean anyChanged = false; for (String loginName : users) { Set<RoleGrantedAuthority> authorities = Sets.newHashSet(getUserAuthorities(loginName, repo)); Set<RoleGrantedAuthority> newAuthorities = Sets.newHashSet(Sets.filter(authorities, predicate)); if (!newAuthorities.equals(authorities)) { saveUserAuthorities(loginName, newAuthorities, repo, currentUser, false); anyChanged = true; } } if (anyChanged) { PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); Git.wrap(repo.r()) .commit() .setAuthor(ident) .setCommitter(ident) .setMessage(commitMessage) .call(); } } finally { Util.closeQuietly(repo); } }
public void renameUser(String loginName, String newLoginName, User currentUser) throws IOException { Assert.hasLength(loginName); Assert.hasLength(newLoginName); Assert.notNull(currentUser); // check that user exists by trying to load it getUser(loginName); // check that new user does not exist by trying to load it try { getUser(newLoginName); throw new IllegalArgumentException("user already exists: " + newLoginName); // $NON-NLS-1$ } catch (UserNotFoundException e) { // okay } ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); File workingDir = RepositoryUtil.getWorkingDir(repo.r()); File file = new File(workingDir, loginName + USER_SUFFIX); File newFile = new File(workingDir, newLoginName + USER_SUFFIX); FileUtils.copyFile(file, newFile); file = new File(workingDir, loginName + AUTHORITIES_SUFFIX); newFile = new File(workingDir, newLoginName + AUTHORITIES_SUFFIX); FileUtils.copyFile(file, newFile); Git git = Git.wrap(repo.r()); git.rm().addFilepattern(loginName + USER_SUFFIX).call(); git.rm().addFilepattern(loginName + AUTHORITIES_SUFFIX).call(); git.add().addFilepattern(newLoginName + USER_SUFFIX).call(); git.add().addFilepattern(newLoginName + AUTHORITIES_SUFFIX).call(); PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit() .setAuthor(ident) .setCommitter(ident) .setMessage( "rename user " + loginName + " to " + newLoginName) // $NON-NLS-1$ //$NON-NLS-2$ .call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Util.closeQuietly(repo); } }
@Test public void deletePage() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); saveRandomPage(BRANCH_1, "home"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/foo"); // $NON-NLS-1$ saveRandomAttachment(BRANCH_1, "home/foo", "test.txt"); // $NON-NLS-1$ //$NON-NLS-2$ saveRandomPage(BRANCH_1, "home/foo/bar"); // $NON-NLS-1$ File pageFile = new File( new File(new File(RepositoryUtil.getWorkingDir(repo.r()), "pages"), "home"), "foo.page"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ File metaFile = new File( new File(new File(RepositoryUtil.getWorkingDir(repo.r()), "pages"), "home"), "foo.meta"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ File subPagesDir = new File( new File(new File(RepositoryUtil.getWorkingDir(repo.r()), "pages"), "home"), "foo"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ File attachmentsDir = new File( new File(new File(RepositoryUtil.getWorkingDir(repo.r()), "attachments"), "home"), "foo"); //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ assertTrue(pageFile.isFile()); assertTrue(metaFile.isFile()); assertTrue(subPagesDir.isDirectory()); assertTrue(attachmentsDir.isDirectory()); pageStore.deletePage(PROJECT, BRANCH_1, "home/foo", USER); // $NON-NLS-1$ List<String> result = pageStore.listChildPagePaths(PROJECT, BRANCH_1, "home"); // $NON-NLS-1$ assertTrue(result.isEmpty()); assertFalse(pageFile.exists()); assertFalse(metaFile.exists()); assertFalse(subPagesDir.exists()); assertFalse(attachmentsDir.exists()); assertClean(repo.r()); }
@Test public void listPageVersions() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); saveRandomPage(BRANCH_1, "home"); // $NON-NLS-1$ RevCommit commit1 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/foo"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home"); // $NON-NLS-1$ RevCommit commit2 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/bar"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home"); // $NON-NLS-1$ RevCommit commit3 = CommitUtils.getLastCommit(repo.r(), "pages/home.page"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/baz"); // $NON-NLS-1$ List<PageVersion> versions = pageStore.listPageVersions(PROJECT, BRANCH_1, "home"); // $NON-NLS-1$ assertEquals(3, versions.size()); assertPageVersion(commit3, versions.get(0)); assertPageVersion(commit2, versions.get(1)); assertPageVersion(commit1, versions.get(2)); }
/** * Returns the user that has the specified login name. * * @throws UserNotFoundException when the user could not be found */ public User getUser(String loginName) throws IOException { Assert.hasLength(loginName); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); String json = BlobUtils.getHeadContent(repo.r(), loginName + USER_SUFFIX); if (json == null) { throw new UserNotFoundException(loginName); } return getUser(loginName, json); } finally { Util.closeQuietly(repo); } }
@Test public void getPageMetadata() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); saveRandomPage(BRANCH_1, PAGE); File file = new File( new File(RepositoryUtil.getWorkingDir(repo.r()), "pages"), PAGE + ".page"); // $NON-NLS-1$ //$NON-NLS-2$ long size = file.length(); PageMetadata metadata = pageStore.getPageMetadata(PROJECT, BRANCH_1, PAGE); assertEquals(USER.getLoginName(), metadata.getLastEditedBy()); assertSecondsAgo(metadata.getLastEdited(), 5); assertEquals(size, metadata.getSize()); }
@Test public void savePageWithViewRestrictionRole() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); Page page = Page.fromText("title", "text"); // $NON-NLS-1$ //$NON-NLS-2$ page.setViewRestrictionRole("viewRole"); // $NON-NLS-1$ pageStore.savePage(PROJECT, BRANCH_1, "home/foo", page, null, USER); // $NON-NLS-1$ Page result = pageStore.getPage(PROJECT, BRANCH_1, "home/foo", true); // $NON-NLS-1$ assertEquals(page.getTitle(), result.getTitle()); assertEquals( ((PageTextData) page.getData()).getText(), ((PageTextData) result.getData()).getText()); assertEquals(page.getContentType(), result.getContentType()); assertEquals(page.getViewRestrictionRole(), result.getViewRestrictionRole()); assertEquals("home", result.getParentPagePath()); // $NON-NLS-1$ assertClean(repo.r()); }
public void deleteRole(String roleName, User currentUser) throws IOException { Assert.hasLength(roleName); Assert.notNull(currentUser); // check that role exists by trying to load it getRole(roleName); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); Git git = Git.wrap(repo.r()); git.rm().addFilepattern(roleName + ROLE_SUFFIX).call(); // remove role from all users List<String> users = listUsers(repo); users.add(ANONYMOUS_USER_LOGIN_NAME); for (String user : users) { List<RoleGrantedAuthority> authorities = getUserAuthorities(user, repo); boolean changed = false; for (Iterator<RoleGrantedAuthority> iter = authorities.iterator(); iter.hasNext(); ) { RoleGrantedAuthority rga = iter.next(); if (rga.getRoleName().equals(roleName)) { iter.remove(); changed = true; } } if (changed) { saveUserAuthorities(user, Sets.newHashSet(authorities), repo, currentUser, false); } } PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit() .setAuthor(ident) .setCommitter(ident) .setMessage("delete role " + roleName) // $NON-NLS-1$ .call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Util.closeQuietly(repo); } }
@Test public void relocatePage() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); saveRandomPage(BRANCH_1, "home"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/foo"); // $NON-NLS-1$ Page page = saveRandomPage(BRANCH_1, "home/foo/bar"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/foo/bar/quuux"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/foo/quux"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/baz"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/baz/bar"); // $NON-NLS-1$ saveRandomPage(BRANCH_1, "home/baz/qux"); // $NON-NLS-1$ Page attachment = saveRandomAttachment(BRANCH_1, "home/foo/bar", "test.txt"); // $NON-NLS-1$ //$NON-NLS-2$ saveRandomAttachment(BRANCH_1, "home/baz/bar", "test.txt"); // $NON-NLS-1$ //$NON-NLS-2$ pageStore.relocatePage( PROJECT, BRANCH_1, "home/foo/bar", "home/baz", USER); // $NON-NLS-1$ //$NON-NLS-2$ assertEquals( Sets.newHashSet("home/foo/quux"), // $NON-NLS-1$ Sets.newHashSet( pageStore.listChildPagePaths(PROJECT, BRANCH_1, "home/foo"))); // $NON-NLS-1$ assertEquals( Sets.newHashSet("home/baz/bar", "home/baz/qux"), // $NON-NLS-1$ //$NON-NLS-2$ Sets.newHashSet( pageStore.listChildPagePaths(PROJECT, BRANCH_1, "home/baz"))); // $NON-NLS-1$ assertEquals( Sets.newHashSet("home/baz/bar/quuux"), // $NON-NLS-1$ Sets.newHashSet( pageStore.listChildPagePaths(PROJECT, BRANCH_1, "home/baz/bar"))); // $NON-NLS-1$ assertEquals( page.getData(), pageStore.getPage(PROJECT, BRANCH_1, "home/baz/bar", true).getData()); // $NON-NLS-1$ assertEquals( attachment.getData(), pageStore .getAttachment(PROJECT, BRANCH_1, "home/baz/bar", "test.txt") .getData()); //$NON-NLS-1$ //$NON-NLS-2$ assertClean(repo.r()); }
@Test public void saveAndGetPage() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); Page page = saveRandomPage(BRANCH_1, "home/foo"); // $NON-NLS-1$ Page result = pageStore.getPage(PROJECT, BRANCH_1, "home/foo", true); // $NON-NLS-1$ assertEquals(page.getTitle(), result.getTitle()); assertEquals( ((PageTextData) page.getData()).getText(), ((PageTextData) result.getData()).getText()); assertEquals(page.getContentType(), result.getContentType()); assertEquals("home", result.getParentPagePath()); // $NON-NLS-1$ assertNull(result.getViewRestrictionRole()); verify(eventBus).post(new PageChangedEvent(PROJECT, BRANCH_1, "home/foo")); // $NON-NLS-1$ assertClean(repo.r()); }
@Test public void restorePageVersion() throws IOException, GitAPIException { register(globalRepoManager.createProjectCentralRepository(PROJECT, USER)); ILockedRepository repo = globalRepoManager.createProjectBranchRepository(PROJECT, BRANCH_1, null); register(repo); Page page = Page.fromText("old", "old"); // $NON-NLS-1$ //$NON-NLS-2$ pageStore.savePage(PROJECT, BRANCH_1, PAGE, page, null, USER); page = Page.fromText("new", "new"); // $NON-NLS-1$ //$NON-NLS-2$ pageStore.savePage(PROJECT, BRANCH_1, PAGE, page, null, USER); List<PageVersion> versions = pageStore.listPageVersions(PROJECT, BRANCH_1, PAGE); pageStore.restorePageVersion(PROJECT, BRANCH_1, PAGE, versions.get(1).getCommitName(), USER); Page result = pageStore.getPage(PROJECT, BRANCH_1, PAGE, true); assertEquals("old", ((PageTextData) result.getData()).getText()); // $NON-NLS-1$ versions = pageStore.listPageVersions(PROJECT, BRANCH_1, PAGE); assertEquals(3, versions.size()); assertClean(repo.r()); }
private List<String> listUsers(ILockedRepository repo) { File workingDir = RepositoryUtil.getWorkingDir(repo.r()); FileFilter filter = new FileFilter() { @Override public boolean accept(File file) { return file.isFile() && file.getName().endsWith(USER_SUFFIX); } }; List<File> files = Lists.newArrayList(workingDir.listFiles(filter)); Function<File, String> function = new Function<File, String>() { @Override public String apply(File file) { return StringUtils.substringBeforeLast(file.getName(), USER_SUFFIX); } }; List<String> users = Lists.newArrayList(Lists.transform(files, function)); Collections.sort(users); return users; }
public void deleteUser(String loginName, User currentUser) throws IOException { Assert.hasLength(loginName); Assert.notNull(currentUser); ILockedRepository repo = null; try { repo = globalRepositoryManager.getProjectCentralRepository(REPOSITORY_NAME, false); Git git = Git.wrap(repo.r()); git.rm().addFilepattern(loginName + USER_SUFFIX).call(); git.rm().addFilepattern(loginName + AUTHORITIES_SUFFIX).call(); PersonIdent ident = new PersonIdent(currentUser.getLoginName(), currentUser.getEmail()); git.commit() .setAuthor(ident) .setCommitter(ident) .setMessage("delete user " + loginName) // $NON-NLS-1$ .call(); } catch (GitAPIException e) { throw new IOException(e); } finally { Util.closeQuietly(repo); } }
private List<RoleGrantedAuthority> getUserAuthorities(String loginName, ILockedRepository repo) { String json = BlobUtils.getHeadContent(repo.r(), loginName + AUTHORITIES_SUFFIX); if (json == null) { throw new UserNotFoundException(loginName); } Gson gson = new GsonBuilder().enableComplexMapKeySerialization().create(); Map<String, Set<String>> authoritiesMap = gson.fromJson(json, new TypeToken<Map<String, Set<String>>>() {}.getType()); List<RoleGrantedAuthority> authorities = Lists.newArrayList(); for (Map.Entry<String, Set<String>> entry : authoritiesMap.entrySet()) { String targetStr = entry.getKey(); Type type = Type.valueOf(StringUtils.substringBefore(targetStr, ":")); // $NON-NLS-1$ String targetId = StringUtils.substringAfter(targetStr, ":"); // $NON-NLS-1$ for (String roleName : entry.getValue()) { authorities.add( new RoleGrantedAuthority(new GrantedAuthorityTarget(targetId, type), roleName)); } } Collections.sort(authorities, new RoleGrantedAuthorityComparator()); return authorities; }