Search in sources :

Example 41 with CurrentUser

use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.

the class ThreadLocalRequestContext method module.

public static Module module() {
    return new AbstractModule() {

        @Override
        protected void configure() {
            bind(ThreadLocalRequestContext.class);
            bind(RequestContext.class).annotatedWith(Names.named(FALLBACK)).to(FallbackRequestContext.class);
        }

        @Provides
        RequestContext provideRequestContext(@Named(FALLBACK) RequestContext fallback) {
            return MoreObjects.firstNonNull(local.get(), fallback);
        }

        @Provides
        CurrentUser provideCurrentUser(RequestContext ctx) {
            return ctx.getUser();
        }

        @Provides
        IdentifiedUser provideCurrentUser(CurrentUser user) {
            if (user.isIdentifiedUser()) {
                return user.asIdentifiedUser();
            }
            throw new ProvisionException(NotSignedInException.MESSAGE, new NotSignedInException());
        }

        @Provides
        ReviewDb provideReviewDb(RequestContext ctx) {
            return ctx.getReviewDbProvider().get();
        }
    };
}
Also used : Named(com.google.inject.name.Named) ProvisionException(com.google.inject.ProvisionException) CurrentUser(com.google.gerrit.server.CurrentUser) NotSignedInException(com.google.gerrit.common.errors.NotSignedInException) AbstractModule(com.google.inject.AbstractModule)

Example 42 with CurrentUser

use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.

the class PRED_current_user_2 method createUser.

public Term createUser(Prolog engine, Term key) {
    if (!(key instanceof StructureTerm) || key.arity() != 1 || !((StructureTerm) key).functor().equals(user)) {
        throw new IllegalTypeException(this, 1, "user(int)", key);
    }
    Term idTerm = key.arg(0);
    CurrentUser user;
    if (idTerm instanceof IntegerTerm) {
        Map<Account.Id, IdentifiedUser> cache = StoredValues.USERS.get(engine);
        Account.Id accountId = new Account.Id(((IntegerTerm) idTerm).intValue());
        user = cache.get(accountId);
        if (user == null) {
            IdentifiedUser.GenericFactory userFactory = userFactory(engine);
            IdentifiedUser who = userFactory.create(accountId);
            cache.put(accountId, who);
            user = who;
        }
    } else if (idTerm.equals(anonymous)) {
        user = StoredValues.ANONYMOUS_USER.get(engine);
    } else {
        throw new IllegalTypeException(this, 1, "user(int)", key);
    }
    return new JavaObjectTerm(user);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) IllegalTypeException(com.googlecode.prolog_cafe.exceptions.IllegalTypeException) CurrentUser(com.google.gerrit.server.CurrentUser) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) Term(com.googlecode.prolog_cafe.lang.Term) IntegerTerm(com.googlecode.prolog_cafe.lang.IntegerTerm) StructureTerm(com.googlecode.prolog_cafe.lang.StructureTerm) JavaObjectTerm(com.googlecode.prolog_cafe.lang.JavaObjectTerm) SymbolTerm(com.googlecode.prolog_cafe.lang.SymbolTerm) VariableTerm(com.googlecode.prolog_cafe.lang.VariableTerm) IdentifiedUser(com.google.gerrit.server.IdentifiedUser)

Example 43 with CurrentUser

use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.

the class AccountsCollection method parseIdOnBehalfOf.

private IdentifiedUser parseIdOnBehalfOf(@Nullable CurrentUser caller, String id) throws AuthException, OrmException {
    if (id.equals("self")) {
        CurrentUser user = self.get();
        if (user.isIdentifiedUser()) {
            return user.asIdentifiedUser();
        } else if (user instanceof AnonymousUser) {
            throw new AuthException("Authentication required");
        } else {
            return null;
        }
    }
    Account match = resolver.find(db.get(), id);
    if (match == null) {
        return null;
    }
    CurrentUser realUser = caller != null ? caller.getRealUser() : null;
    return userFactory.runAs(null, match.getId(), realUser);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) CurrentUser(com.google.gerrit.server.CurrentUser) AuthException(com.google.gerrit.extensions.restapi.AuthException) AnonymousUser(com.google.gerrit.server.AnonymousUser)

Example 44 with CurrentUser

use of com.google.gerrit.server.CurrentUser in project gerrit by GerritCodeReview.

the class ChangeQueryBuilder method watchedby.

@Operator
public Predicate<ChangeData> watchedby(String who) throws QueryParseException, OrmException {
    Set<Account.Id> m = parseAccount(who);
    List<IsWatchedByPredicate> p = Lists.newArrayListWithCapacity(m.size());
    Account.Id callerId;
    try {
        CurrentUser caller = args.self.get();
        callerId = caller.isIdentifiedUser() ? caller.getAccountId() : null;
    } catch (ProvisionException e) {
        callerId = null;
    }
    for (Account.Id id : m) {
        // Each child IsWatchedByPredicate includes a visibility filter for the
        // corresponding user, to ensure that predicate subtree only returns
        // changes visible to that user. The exception is if one of the users is
        // the caller of this method, in which case visibility is already being
        // checked at the top level.
        p.add(new IsWatchedByPredicate(args.asUser(id), !id.equals(callerId)));
    }
    return Predicate.or(p);
}
Also used : Account(com.google.gerrit.reviewdb.client.Account) ProvisionException(com.google.inject.ProvisionException) CurrentUser(com.google.gerrit.server.CurrentUser)

Aggregations

CurrentUser (com.google.gerrit.server.CurrentUser)44 Account (com.google.gerrit.reviewdb.client.Account)14 AuthException (com.google.gerrit.extensions.restapi.AuthException)11 Provider (com.google.inject.Provider)8 IdentifiedUser (com.google.gerrit.server.IdentifiedUser)7 Change (com.google.gerrit.reviewdb.client.Change)6 PatchSet (com.google.gerrit.reviewdb.client.PatchSet)6 Project (com.google.gerrit.reviewdb.client.Project)6 RequestContext (com.google.gerrit.server.util.RequestContext)6 ThreadLocalRequestContext (com.google.gerrit.server.util.ThreadLocalRequestContext)6 InMemoryModule (com.google.gerrit.testutil.InMemoryModule)6 OrmException (com.google.gwtorm.server.OrmException)6 ResourceNotFoundException (com.google.gerrit.extensions.restapi.ResourceNotFoundException)5 ReviewDb (com.google.gerrit.reviewdb.server.ReviewDb)5 PermissionBackendException (com.google.gerrit.server.permissions.PermissionBackendException)5 HashMap (java.util.HashMap)5 LabelType (com.google.gerrit.common.data.LabelType)4 LifecycleManager (com.google.gerrit.lifecycle.LifecycleManager)4 PermissionBackend (com.google.gerrit.server.permissions.PermissionBackend)4 ChangeControl (com.google.gerrit.server.project.ChangeControl)4