How to use the dbutils.User.fromId function in DBUtils

To help you get started, we’ve selected a few DBUtils examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github jensl / critic / src / reviewing / utils.py View on Github external
def generateMailsForAssignmentsTransaction(db, transaction_id):
    cursor = db.cursor()
    cursor.execute("SELECT review, assigner, note FROM reviewassignmentstransactions WHERE id=%s", (transaction_id,))

    review_id, assigner_id, note = cursor.fetchone()

    review = dbutils.Review.fromId(db, review_id)
    assigner = dbutils.User.fromId(db, assigner_id)

    cursor.execute("""SELECT uid, path, type, created
                        FROM reviewfilterchanges
                       WHERE transaction=%s""",
                   (transaction_id,))

    by_user = {}

    for reviewer_id, path, filter_type, created in cursor:
        added_filters, removed_filters, unassigned, assigned = by_user.setdefault(reviewer_id, ([], [], [], []))
        if created: added_filters.append((filter_type, path or "/"))
        else: removed_filters.append((filter_type, path or "/"))

    cursor.execute("""SELECT reviewassignmentchanges.uid, reviewassignmentchanges.assigned, reviewfiles.file, SUM(reviewfiles.deleted), SUM(reviewfiles.inserted)
                        FROM reviewfiles
                        JOIN reviewassignmentchanges ON (reviewassignmentchanges.file=reviewfiles.id)
github jensl / critic / src / operation / registeruser.py View on Github external
message="Invalid external authentication state.")

            cursor.execute("""SELECT id, uid, email
                                FROM externalusers
                               WHERE provider=%s
                                 AND account=%s""",
                           (external["provider"], external["account"]))

            # Note: the token validation above implicitly checks that there's a
            # matching row in the 'externalusers' table.
            external_user_id, existing_user_id, external_email = cursor.fetchone()

            # Check that we don't already have a Critic user associated with
            # this external user.
            if existing_user_id is not None:
                existing_user = dbutils.User.fromId(db, existing_user_id)
                return OperationResult(
                    message=("There is already a Critic user ('%s') connected "
                             "to the %s '%s'" % (existing_user.name,
                                                 provider.getTitle(),
                                                 external["account"])))

            if email == external_email:
                verify_email_address = provider.configuration["verify_email_addresses"]

            # Reset 'email' column in 'externalusers': we only need it to detect
            # if the user changed the email address in the "Create user" form.
            # Also reset the 'token' column, which serves no further purpose
            # beyond this point.
            with db.updating_cursor("externalusers") as cursor:
                cursor.execute("""UPDATE externalusers
                                     SET email=NULL,
github jensl / critic / src / operation / typechecker.py View on Github external
def __call__(self, value, context):
        super(UserId, self).__call__(value, context)
        return dbutils.User.fromId(context.db, value)
github jensl / critic / src / auth / databases / ldapdb.py View on Github external
def authenticate(self, db, fields):
        import ldap
        import ldap.filter

        cached_data = self.cache.get(fields)
        if cached_data is not None:
            cached_user_id, cached_authentication_labels = cached_data
            try:
                user = dbutils.User.fromId(db, cached_user_id)
            except dbutils.InvalidUserId:
                pass
            else:
                db.setUser(user, cached_authentication_labels)
                return

        connection = self.__startConnection()

        search_base = (self.configuration["search_base"]
                       % escaped(fields, ldap.dn.escape_dn_chars))
        search_filter = (self.configuration["search_filter"]
                         % escaped(fields, ldap.filter.escape_filter_chars))

        attributes = [self.configuration["username_attribute"]]

        if self.configuration["create_user"]:
github jensl / critic / src / extensions / role / filterhook.py View on Github external
FROM extensionfilterhookevents AS events
                        JOIN extensionhookfilters AS filters ON (filters.id=events.filter)
                       WHERE events.id=%s""",
                   (event_id,))

    # Note:
    # - filter_user_id / filter_user represent the user whose filter was
    #   triggered.
    # - user_id /user represent the user that added commits and thereby
    #   triggered the filter.

    (extension_id, filter_user_id, filter_path,
     filterhook_name, review_id, user_id, filter_data) = cursor.fetchone()

    extension = Extension.fromId(db, extension_id)
    filter_user = dbutils.User.fromId(db, filter_user_id)

    installed_sha1, _ = extension.getInstalledVersion(db, filter_user)

    if installed_sha1 is False:
        # Invalid event (user doesn't have extension installed); do nothing.
        # The event will be deleted by the caller.
        return

    manifest = extension.getManifest(sha1=installed_sha1)

    for role in manifest.roles:
        if isinstance(role, FilterHookRole) and role.name == filterhook_name:
            break
    else:
        # Invalid event (installed version of extension doesn't have the named
        # filter hook role); do nothing.  The event will be deleted by the
github jensl / critic / src / reviewing / utils.py View on Github external
FROM reviewfiles
                        JOIN reviewassignmentchanges ON (reviewassignmentchanges.file=reviewfiles.id)
                       WHERE reviewassignmentchanges.transaction=%s
                    GROUP BY reviewassignmentchanges.uid, reviewassignmentchanges.assigned, reviewfiles.file""",
                   (transaction_id,))

    for reviewer_id, was_assigned, file_id, deleted, inserted in cursor:
        added_filters, removed_filters, unassigned, assigned = by_user.setdefault(reviewer_id, (None, None, [], []))

        if was_assigned: assigned.append((file_id, deleted, inserted))
        else: unassigned.append((file_id, deleted, inserted))

    pending_mails = []

    for reviewer_id, (added_filters, removed_filters, unassigned, assigned) in by_user.items():
        reviewer = dbutils.User.fromId(db, reviewer_id)
        if assigner != reviewer:
            pending_mails.extend(mail.sendAssignmentsChanged(db, assigner, reviewer, review, added_filters, removed_filters, unassigned, assigned))

    return pending_mails
github jensl / critic / src / reviewing / utils.py View on Github external
def applyFilters(db, user, review, globalfilters=False, parentfilters=False):
    new_reviewers, new_watchers = queryFilters(db, user, review, globalfilters, parentfilters)

    pending_mails = []
    cursor = db.cursor()

    for user_id in new_reviewers:
        new_reviewer = dbutils.User.fromId(db, user_id)

        cursor.execute("""SELECT reviewfiles.file, SUM(reviewfiles.deleted), SUM(reviewfiles.inserted)
                            FROM reviewfiles
                            JOIN reviewuserfiles ON (reviewuserfiles.file=reviewfiles.id)
                           WHERE reviewfiles.review=%s
                             AND reviewuserfiles.uid=%s
                        GROUP BY reviewfiles.file""",
                       (review.id, user_id))

        pending_mails.extend(mail.sendFiltersApplied(
                db, user, new_reviewer, review, globalfilters, parentfilters, cursor.fetchall()))

    for user_id in new_watchers:
        new_watcher = dbutils.User.fromId(db, user_id)
        pending_mails.extend(mail.sendFiltersApplied(
                db, user, new_watcher, review, globalfilters, parentfilters, None))