Search in sources :

Example 1 with ParameterizedString

use of com.google.gerrit.common.data.ParameterizedString in project gerrit by GerritCodeReview.

the class LdapGroupBackend method suggestLdap.

private Set<GroupReference> suggestLdap(String name) {
    if (name.isEmpty()) {
        return Collections.emptySet();
    }
    Set<GroupReference> out = Sets.newTreeSet(GROUP_REF_NAME_COMPARATOR);
    try {
        DirContext ctx = helper.open();
        try {
            // Do exact lookups until there are at least 3 characters.
            name = Rdn.escapeValue(name) + ((name.length() >= 3) ? "*" : "");
            LdapSchema schema = helper.getSchema(ctx);
            ParameterizedString filter = ParameterizedString.asis(schema.groupPattern.replace(GROUPNAME, name).toString());
            Set<String> returnAttrs = new HashSet<>(schema.groupName.getParameterNames());
            Map<String, String> params = Collections.emptyMap();
            for (String groupBase : schema.groupBases) {
                LdapQuery query = new LdapQuery(groupBase, schema.groupScope, filter, returnAttrs);
                for (LdapQuery.Result res : query.query(ctx, params)) {
                    out.add(groupReference(schema.groupName, res));
                }
            }
        } finally {
            try {
                ctx.close();
            } catch (NamingException e) {
                log.warn("Cannot close LDAP query handle", e);
            }
        }
    } catch (NamingException | LoginException e) {
        log.warn("Cannot query LDAP for groups matching requested name", e);
    }
    return out;
}
Also used : DirContext(javax.naming.directory.DirContext) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) LdapSchema(com.google.gerrit.server.auth.ldap.Helper.LdapSchema) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) GroupReference(com.google.gerrit.common.data.GroupReference) HashSet(java.util.HashSet)

Example 2 with ParameterizedString

use of com.google.gerrit.common.data.ParameterizedString in project gerrit by GerritCodeReview.

the class LdapGroupBackend method suggestLdap.

private Set<GroupReference> suggestLdap(String name) {
    if (name.isEmpty()) {
        return Collections.emptySet();
    }
    Set<GroupReference> out = Sets.newTreeSet(GROUP_REF_NAME_COMPARATOR);
    try {
        DirContext ctx = helper.open();
        try {
            // Do exact lookups until there are at least 3 characters.
            name = Rdn.escapeValue(name) + ((name.length() >= 3) ? "*" : "");
            LdapSchema schema = helper.getSchema(ctx);
            ParameterizedString filter = ParameterizedString.asis(schema.groupPattern.replace(GROUPNAME, name).toString());
            Set<String> returnAttrs = new HashSet<>(schema.groupName.getParameterNames());
            Map<String, String> params = Collections.emptyMap();
            for (String groupBase : schema.groupBases) {
                LdapQuery query = new LdapQuery(groupBase, schema.groupScope, filter, returnAttrs);
                for (LdapQuery.Result res : query.query(ctx, params, helper.getGroupSearchLatencyTimer())) {
                    out.add(groupReference(schema.groupName, res));
                }
            }
        } finally {
            helper.close(ctx);
        }
    } catch (IOException | NamingException | LoginException e) {
        logger.atWarning().withCause(e).log("Cannot query LDAP for groups matching requested name");
    }
    return out;
}
Also used : DirContext(javax.naming.directory.DirContext) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) IOException(java.io.IOException) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) LdapSchema(com.google.gerrit.auth.ldap.Helper.LdapSchema) LoginException(javax.security.auth.login.LoginException) NamingException(javax.naming.NamingException) GroupReference(com.google.gerrit.entities.GroupReference) HashSet(java.util.HashSet)

Example 3 with ParameterizedString

use of com.google.gerrit.common.data.ParameterizedString in project gerrit by GerritCodeReview.

the class LdapRealm method apply.

static String apply(ParameterizedString p, LdapQuery.Result m) throws NamingException {
    if (p == null) {
        return null;
    }
    final Map<String, String> values = new HashMap<>();
    for (String name : m.attributes()) {
        values.put(name, m.get(name));
    }
    String r = p.replace(values).trim();
    return r.isEmpty() ? null : r;
}
Also used : HashMap(java.util.HashMap) ParameterizedString(com.google.gerrit.common.data.ParameterizedString)

Example 4 with ParameterizedString

use of com.google.gerrit.common.data.ParameterizedString in project gerrit by GerritCodeReview.

the class LdapRealm method apply.

static String apply(ParameterizedString p, LdapQuery.Result m) throws NamingException {
    if (p == null) {
        return null;
    }
    final Map<String, String> values = new HashMap<>();
    for (final String name : m.attributes()) {
        values.put(name, m.get(name));
    }
    String r = p.replace(values);
    return r.isEmpty() ? null : r;
}
Also used : HashMap(java.util.HashMap) ParameterizedString(com.google.gerrit.common.data.ParameterizedString)

Example 5 with ParameterizedString

use of com.google.gerrit.common.data.ParameterizedString in project gerrit by GerritCodeReview.

the class Submit method getDescription.

@Override
public UiAction.Description getDescription(RevisionResource resource) {
    Change change = resource.getChange();
    String topic = change.getTopic();
    ReviewDb db = dbProvider.get();
    ChangeData cd = changeDataFactory.create(db, resource.getControl());
    boolean visible;
    try {
        visible = change.getStatus().isOpen() && resource.isCurrent() && !resource.getPatchSet().isDraft() && resource.permissions().test(ChangePermission.SUBMIT);
        MergeOp.checkSubmitRule(cd);
    } catch (ResourceConflictException e) {
        visible = false;
    } catch (PermissionBackendException e) {
        log.error("Error checking if change is submittable", e);
        throw new OrmRuntimeException("Could not check submit permission", e);
    } catch (OrmException e) {
        log.error("Error checking if change is submittable", e);
        throw new OrmRuntimeException("Could not determine problems for the change", e);
    }
    if (!visible) {
        return new UiAction.Description().setLabel("").setTitle("").setVisible(false);
    }
    ChangeSet cs;
    try {
        cs = mergeSuperSet.get().completeChangeSet(db, cd.change(), resource.getControl().getUser());
    } catch (OrmException | IOException e) {
        throw new OrmRuntimeException("Could not determine complete set of changes to be submitted", e);
    }
    int topicSize = 0;
    if (!Strings.isNullOrEmpty(topic)) {
        topicSize = getChangesByTopic(topic).size();
    }
    boolean treatWithTopic = submitWholeTopic && !Strings.isNullOrEmpty(topic) && topicSize > 1;
    String submitProblems = problemsForSubmittingChangeset(cd, cs, resource.getUser());
    Boolean enabled;
    try {
        // Recheck mergeability rather than using value stored in the index,
        // which may be stale.
        // TODO(dborowitz): This is ugly; consider providing a way to not read
        // stored fields from the index in the first place.
        // cd.setMergeable(null);
        // That was done in unmergeableChanges which was called by
        // problemsForSubmittingChangeset, so now it is safe to read from
        // the cache, as it yields the same result.
        enabled = cd.isMergeable();
    } catch (OrmException e) {
        throw new OrmRuntimeException("Could not determine mergeability", e);
    }
    if (submitProblems != null) {
        return new UiAction.Description().setLabel(treatWithTopic ? submitTopicLabel : (cs.size() > 1) ? labelWithParents : label).setTitle(submitProblems).setVisible(true).setEnabled(false);
    }
    if (treatWithTopic) {
        Map<String, String> params = ImmutableMap.of("topicSize", String.valueOf(topicSize), "submitSize", String.valueOf(cs.size()));
        return new UiAction.Description().setLabel(submitTopicLabel).setTitle(Strings.emptyToNull(submitTopicTooltip.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
    }
    RevId revId = resource.getPatchSet().getRevision();
    Map<String, String> params = ImmutableMap.of("patchSet", String.valueOf(resource.getPatchSet().getPatchSetId()), "branch", change.getDest().getShortName(), "commit", ObjectId.fromString(revId.get()).abbreviate(7).name(), "submitSize", String.valueOf(cs.size()));
    ParameterizedString tp = cs.size() > 1 ? titlePatternWithAncestors : titlePattern;
    return new UiAction.Description().setLabel(cs.size() > 1 ? labelWithParents : label).setTitle(Strings.emptyToNull(tp.replace(params))).setVisible(true).setEnabled(Boolean.TRUE.equals(enabled));
}
Also used : OrmRuntimeException(com.google.gwtorm.server.OrmRuntimeException) PermissionBackendException(com.google.gerrit.server.permissions.PermissionBackendException) Change(com.google.gerrit.reviewdb.client.Change) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) IOException(java.io.IOException) UiAction(com.google.gerrit.extensions.webui.UiAction) ChangeData(com.google.gerrit.server.query.change.ChangeData) RevId(com.google.gerrit.reviewdb.client.RevId) ParameterizedString(com.google.gerrit.common.data.ParameterizedString) ResourceConflictException(com.google.gerrit.extensions.restapi.ResourceConflictException) OrmException(com.google.gwtorm.server.OrmException) ChangeSet(com.google.gerrit.server.git.ChangeSet) ReviewDb(com.google.gerrit.reviewdb.server.ReviewDb)

Aggregations

ParameterizedString (com.google.gerrit.common.data.ParameterizedString)7 ResourceConflictException (com.google.gerrit.extensions.restapi.ResourceConflictException)2 ChangeData (com.google.gerrit.server.query.change.ChangeData)2 IOException (java.io.IOException)2 HashMap (java.util.HashMap)2 HashSet (java.util.HashSet)2 NamingException (javax.naming.NamingException)2 DirContext (javax.naming.directory.DirContext)2 LoginException (javax.security.auth.login.LoginException)2 LdapSchema (com.google.gerrit.auth.ldap.Helper.LdapSchema)1 GroupReference (com.google.gerrit.common.data.GroupReference)1 Change (com.google.gerrit.entities.Change)1 GroupReference (com.google.gerrit.entities.GroupReference)1 UiAction (com.google.gerrit.extensions.webui.UiAction)1 Change (com.google.gerrit.reviewdb.client.Change)1 RevId (com.google.gerrit.reviewdb.client.RevId)1 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)1 LdapSchema (com.google.gerrit.server.auth.ldap.Helper.LdapSchema)1 ChangeSet (com.google.gerrit.server.git.ChangeSet)1 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)1