Skip to content

Commit

Permalink
Moderation subject: user:<email> (#7699)
Browse files Browse the repository at this point in the history
  • Loading branch information
isoos committed May 6, 2024
1 parent 0835ed5 commit 5efb7d1
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 5 deletions.
22 changes: 18 additions & 4 deletions app/lib/admin/models.dart
Original file line number Diff line number Diff line change
Expand Up @@ -98,30 +98,36 @@ class ModerationSubject {
/// The kind of moderation as described by [ModerationSubjectKind], one of:
/// - package,
/// - package-version,
/// - publisher.
/// - publisher,
/// - user.
final String kind;

/// The local name part of the subject, may be a composite, one of:
/// - <package>,
/// - <package>/<version>,
/// - <publisherId>.
/// - <publisherId>,
/// - <email>.
final String localName;

/// The package name of the subject (if not a publisher).
/// The package name of the subject (if not a publisher, or user).
final String? package;

/// The package version of the subject (if the version was specified).
final String? version;

/// The publisher id (if not a package or package/version).
/// The publisher id (if not a package or package/version, or user).
final String? publisherId;

/// The email address of the user (if not a package or publisher).
final String? email;

ModerationSubject._({
required this.kind,
required this.localName,
this.package,
this.version,
this.publisherId,
this.email,
});

factory ModerationSubject.package(String package, [String? version]) {
Expand Down Expand Up @@ -183,6 +189,13 @@ class ModerationSubject {
localName: publisherId,
publisherId: publisherId,
);
case ModerationSubjectKind.user:
final email = parts[1];
return ModerationSubject._(
kind: kind,
localName: email,
email: email,
);
default:
return null;
}
Expand All @@ -195,4 +208,5 @@ class ModerationSubjectKind {
static const package = 'package';
static const packageVersion = 'package-version';
static const publisher = 'publisher';
static const user = 'user';
}
12 changes: 11 additions & 1 deletion app/lib/frontend/handlers/report.dart
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,6 @@ Future<shelf.Response> reportPageHandler(shelf.Request request) async {
Future<void> _verifySubject(ModerationSubject? subject) async {
final package = subject?.package;
final version = subject?.version;
final publisherId = subject?.publisherId;
if (package != null) {
final p = await packageBackend.lookupPackage(package);
if (p == null) {
Expand All @@ -67,12 +66,23 @@ Future<void> _verifySubject(ModerationSubject? subject) async {
}
}
}

final publisherId = subject?.publisherId;
if (publisherId != null) {
final p = await publisherBackend.getPublisher(publisherId);
if (p == null) {
throw NotFoundException('Publisher "$publisherId" does not exist.');
}
}

final email = subject?.email;
if (email != null) {
InvalidInputException.check(
isValidEmail(email), '"$email" is not a valid email.');

// NOTE: We are not going to lookup and reject the requests based on the
// email address, as it would leak the existence of user accounts.
}
}

/// Handles POST /api/report
Expand Down
13 changes: 13 additions & 0 deletions app/test/admin/models_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ void main() {
expect(ms.package, 'x');
expect(ms.version, isNull);
expect(ms.publisherId, isNull);
expect(ms.email, isNull);
});

test('package version', () {
Expand All @@ -30,6 +31,7 @@ void main() {
expect(ms.package, 'x');
expect(ms.version, '1.0.0');
expect(ms.publisherId, isNull);
expect(ms.email, isNull);
});

test('publisher', () {
Expand All @@ -39,6 +41,17 @@ void main() {
expect(ms.package, isNull);
expect(ms.version, isNull);
expect(ms.publisherId, 'example.com');
expect(ms.email, isNull);
});

test('user', () {
final ms = ModerationSubject.tryParse('user:a@example.com');
expect(ms!.kind, ModerationSubjectKind.user);
expect(ms.localName, 'a@example.com');
expect(ms.package, isNull);
expect(ms.version, isNull);
expect(ms.publisherId, isNull);
expect(ms.email, 'a@example.com');
});
});
}

0 comments on commit 5efb7d1

Please sign in to comment.