use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.
the class VisibleRefFilter method filter.
public Map<String, Ref> filter(Map<String, Ref> refs, boolean filterTagsSeparately) {
if (projectCtl.getProjectState().isAllUsers()) {
refs = addUsersSelfSymref(refs);
}
if (projectCtl.allRefsAreVisible(ImmutableSet.of(REFS_CONFIG))) {
return fastHideRefsMetaConfig(refs);
}
Account.Id userId;
boolean viewMetadata;
if (projectCtl.getUser().isIdentifiedUser()) {
IdentifiedUser user = projectCtl.getUser().asIdentifiedUser();
userId = user.getAccountId();
viewMetadata = user.getCapabilities().canAccessDatabase();
userEditPrefix = RefNames.refsEditPrefix(userId);
} else {
userId = null;
viewMetadata = false;
}
Map<String, Ref> result = new HashMap<>();
List<Ref> deferredTags = new ArrayList<>();
for (Ref ref : refs.values()) {
String name = ref.getName();
Change.Id changeId;
Account.Id accountId;
if (name.startsWith(REFS_CACHE_AUTOMERGE) || (!showMetadata && isMetadata(projectCtl, name))) {
continue;
} else if (RefNames.isRefsEdit(name)) {
// Edits are visible only to the owning user, if change is visible.
if (viewMetadata || visibleEdit(name)) {
result.put(name, ref);
}
} else if ((changeId = Change.Id.fromRef(name)) != null) {
// Change ref is visible only if the change is visible.
if (viewMetadata || visible(changeId)) {
result.put(name, ref);
}
} else if ((accountId = Account.Id.fromRef(name)) != null) {
// Account ref is visible only to corresponding account.
if (viewMetadata || (accountId.equals(userId) && projectCtl.controlForRef(name).isVisible())) {
result.put(name, ref);
}
} else if (isTag(ref)) {
// If its a tag, consider it later.
if (ref.getObjectId() != null) {
deferredTags.add(ref);
}
} else if (name.startsWith(RefNames.REFS_SEQUENCES)) {
// Sequences are internal database implementation details.
if (viewMetadata) {
result.put(name, ref);
}
} else if (projectCtl.getProjectState().isAllUsers() && name.equals(RefNames.REFS_EXTERNAL_IDS)) {
// The notes branch with the external IDs of all users must not be exposed to normal users.
if (viewMetadata) {
result.put(name, ref);
}
} else if (projectCtl.controlForRef(ref.getLeaf().getName()).isVisible()) {
// Use the leaf to lookup the control data. If the reference is
// symbolic we want the control around the final target. If its
// not symbolic then getLeaf() is a no-op returning ref itself.
result.put(name, ref);
}
}
//
if (!deferredTags.isEmpty() && (!result.isEmpty() || filterTagsSeparately)) {
TagMatcher tags = tagCache.get(projectName).matcher(tagCache, db, filterTagsSeparately ? filter(db.getAllRefs()).values() : result.values());
for (Ref tag : deferredTags) {
if (tags.isReachable(tag)) {
result.put(tag.getName(), tag);
}
}
}
return result;
}
use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.
the class PutDescription method apply.
@Override
public Response<String> apply(ProjectResource resource, DescriptionInput input) throws AuthException, ResourceConflictException, ResourceNotFoundException, IOException {
if (input == null) {
// Delete would set description to null.
input = new DescriptionInput();
}
ProjectControl ctl = resource.getControl();
IdentifiedUser user = ctl.getUser().asIdentifiedUser();
if (!ctl.isOwner()) {
throw new AuthException("not project owner");
}
try (MetaDataUpdate md = updateFactory.create(resource.getNameKey())) {
ProjectConfig config = ProjectConfig.read(md);
Project project = config.getProject();
project.setDescription(Strings.emptyToNull(input.description));
String msg = MoreObjects.firstNonNull(Strings.emptyToNull(input.commitMessage), "Updated description.\n");
if (!msg.endsWith("\n")) {
msg += "\n";
}
md.setAuthor(user);
md.setMessage(msg);
config.commit(md);
cache.evict(ctl.getProject());
md.getRepository().setGitwebDescription(project.getDescription());
return Strings.isNullOrEmpty(project.getDescription()) ? Response.<String>none() : Response.ok(project.getDescription());
} catch (RepositoryNotFoundException notFound) {
throw new ResourceNotFoundException(resource.getName());
} catch (ConfigInvalidException e) {
throw new ResourceConflictException(String.format("invalid project.config: %s", e.getMessage()));
}
}
use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.
the class Schema_150_to_151_Test method setUp.
@Before
public void setUp() throws Exception {
Injector injector = Guice.createInjector(new InMemoryModule());
injector.injectMembers(this);
lifecycle = new LifecycleManager();
lifecycle.add(injector);
lifecycle.start();
try (ReviewDb underlyingDb = inMemoryDatabase.getDatabase().open()) {
schemaCreator.create(underlyingDb);
}
db = schemaFactory.open();
Account.Id userId = accountManager.authenticate(AuthRequest.forUser("user")).getAccountId();
IdentifiedUser user = userFactory.create(userId);
requestContext.setContext(new RequestContext() {
@Override
public CurrentUser getUser() {
return user;
}
@Override
public Provider<ReviewDb> getReviewDbProvider() {
return Providers.of(db);
}
});
}
use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.
the class AbstractQueryChangesTest method insert.
protected Change insert(TestRepository<Repo> repo, ChangeInserter ins, @Nullable Account.Id owner, Timestamp createdOn) throws Exception {
Project.NameKey project = new Project.NameKey(repo.getRepository().getDescription().getRepositoryName());
Account.Id ownerId = owner != null ? owner : userId;
IdentifiedUser user = userFactory.create(ownerId);
try (BatchUpdate bu = updateFactory.create(db, project, user, createdOn)) {
bu.insertChange(ins);
bu.execute();
return ins.getChange();
}
}
use of com.google.gerrit.server.IdentifiedUser in project gerrit by GerritCodeReview.
the class ChangeNotesTest method patchLineCommentNotesFormatWeirdUser.
@Test
public void patchLineCommentNotesFormatWeirdUser() throws Exception {
Account account = new Account(new Account.Id(3), TimeUtil.nowTs());
account.setFullName("Weird\n<User>\n");
account.setPreferredEmail(" we\r\nird@ex>ample<.com");
accountCache.put(account);
IdentifiedUser user = userFactory.create(account.getId());
Change c = newChange();
ChangeUpdate update = newUpdate(c, user);
String uuid = "uuid";
CommentRange range = new CommentRange(1, 1, 2, 1);
Timestamp time = TimeUtil.nowTs();
PatchSet.Id psId = c.currentPatchSetId();
Comment comment = newComment(psId, "file1", uuid, range, range.getEndLine(), user, null, time, "comment", (short) 1, "abcd1234abcd1234abcd1234abcd1234abcd1234", false);
update.setPatchSetId(psId);
update.putComment(Status.PUBLISHED, comment);
update.commit();
ChangeNotes notes = newNotes(c);
try (RevWalk walk = new RevWalk(repo)) {
ArrayList<Note> notesInTree = Lists.newArrayList(notes.revisionNoteMap.noteMap.iterator());
Note note = Iterables.getOnlyElement(notesInTree);
byte[] bytes = walk.getObjectReader().open(note.getData(), Constants.OBJ_BLOB).getBytes();
String noteString = new String(bytes, UTF_8);
String timeStr = ChangeNoteUtil.formatTime(serverIdent, time);
if (!testJson()) {
assertThat(noteString).isEqualTo("Revision: abcd1234abcd1234abcd1234abcd1234abcd1234\n" + "Patch-set: 1\n" + "File: file1\n" + "\n" + "1:1-2:1\n" + timeStr + "\n" + "Author: WeirdUser <3@gerrit>\n" + "Unresolved: false\n" + "UUID: uuid\n" + "Bytes: 7\n" + "comment\n" + "\n");
}
}
assertThat(notes.getComments()).isEqualTo(ImmutableListMultimap.of(new RevId(comment.revId), comment));
}
Aggregations