From 2a3cac1d9a9e6ebdbec7d735e5ee359cee871770 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Mat=C3=ADas=20Irland?= Date: Wed, 18 Dec 2024 16:15:27 -0300 Subject: [PATCH] fix: Database access and DataStorage rules (#68) --- lib/core/interfaces/db_interface.dart | 4 +-- lib/core/repository/player_repository.dart | 10 ++---- lib/core/services/firestore_db.dart | 42 ++++++++++------------ lib/core/source/user_remote_source.dart | 13 +++---- pubspec.yaml | 1 - 5 files changed, 26 insertions(+), 44 deletions(-) diff --git a/lib/core/interfaces/db_interface.dart b/lib/core/interfaces/db_interface.dart index 57ef322..2703c76 100644 --- a/lib/core/interfaces/db_interface.dart +++ b/lib/core/interfaces/db_interface.dart @@ -2,10 +2,9 @@ abstract interface class DbInterface { Future insert({ required String id, required T data, - required String createdBy, }); - Future?> getAllData(String createdBy); + Future?> getAllData(); Future getData(String id); @@ -14,7 +13,6 @@ abstract interface class DbInterface { Future update({ required String id, required T data, - required String createdBy, }); Future close(); diff --git a/lib/core/repository/player_repository.dart b/lib/core/repository/player_repository.dart index 9c810e6..2bbe9b1 100644 --- a/lib/core/repository/player_repository.dart +++ b/lib/core/repository/player_repository.dart @@ -10,13 +10,13 @@ class PlayerRepository { final UserRemoteSource _userRemoteSource; final AuthLocalSource _authLocalSource; - final Stock?> _store; + final Stock?> _store; final defaultId = 'default'; PlayerRepository(this._userRemoteSource, this._authLocalSource) : _store = Stock( fetcher: Fetcher.ofFuture( - (createdBy) => _userRemoteSource.getAllUsers(createdBy), + (_) => _userRemoteSource.getAllUsers(), ), ); @@ -24,7 +24,7 @@ class PlayerRepository { final userTokenStream = _authLocalSource.getUserToken(); return userTokenStream.switchMap( (createdBy) => _store - .stream(createdBy ?? defaultId) + .stream(null) .where((event) => event.isData) .map((event) => event.requireData()), ); @@ -32,24 +32,20 @@ class PlayerRepository { Future> setPlayer(Player player) => Result.fromFuture(() async { - final userId = await _authLocalSource.getUserToken().first; await _userRemoteSource.createUser( player.email, player, - userId ?? defaultId, ); await _authLocalSource.saveCurrentPlayer(player); return player; }); Future> updatePoints(int points) => Result.fromFuture(() async { - final userId = await _authLocalSource.getUserToken().first; final player = (await getCurrentPlayer().first)!.copyWith(points: points); await _userRemoteSource.updateUser( player.email, player, - userId ?? defaultId, ); await _authLocalSource.saveCurrentPlayer(player); }); diff --git a/lib/core/services/firestore_db.dart b/lib/core/services/firestore_db.dart index 4e6dcea..278d0c7 100644 --- a/lib/core/services/firestore_db.dart +++ b/lib/core/services/firestore_db.dart @@ -1,32 +1,38 @@ import 'package:cloud_firestore/cloud_firestore.dart'; +import 'package:firebase_auth/firebase_auth.dart'; import 'package:simon_ai/core/interfaces/db_interface.dart'; interface class FirestoreRankingDb implements DbInterface> { final FirebaseFirestore _firestore = FirebaseFirestore.instance; + final FirebaseAuth _auth = FirebaseAuth.instance; final String collection; final String subCollection; + final defaultId = 'default'; FirestoreRankingDb({ required this.collection, required this.subCollection, }); + CollectionReference> _getCollectionReference() { + final user = _auth.currentUser; + return _firestore + .collection(collection) + .doc(user?.uid ?? defaultId) + .collection(subCollection); + } + @override Future close() async => _firestore.terminate(); @override - Future delete(String id) => - _firestore.collection(collection).doc(id).delete(); + Future delete(String id) => _getCollectionReference().doc(id).delete(); @override - Future>> getAllData(String createdBy) async { - final snapshot = await _firestore - .collection(collection) - .doc(createdBy) - .collection(subCollection) - .get(); + Future>> getAllData() async { + final snapshot = await _getCollectionReference().get(); return snapshot.docs.map((doc) => doc.data()).toList(); } @@ -34,31 +40,19 @@ interface class FirestoreRankingDb Future insert({ required String id, required Map data, - required String createdBy, }) => - _firestore - .collection(collection) - .doc(createdBy) - .collection(subCollection) - .doc(id) - .set(data); + _getCollectionReference().doc(id).set(data); @override Future update({ required String id, required Map data, - required String createdBy, }) => - _firestore - .collection(collection) - .doc(createdBy) - .collection(subCollection) - .doc(id) - .update(data); + _getCollectionReference().doc(id).update(data); @override - Future?> getData(String id) async { - final snapshot = await _firestore.collection(collection).doc(id).get(); + Future?> getData(String userId) async { + final snapshot = await _getCollectionReference().doc(userId).get(); if (snapshot.exists) { return snapshot.data(); } diff --git a/lib/core/source/user_remote_source.dart b/lib/core/source/user_remote_source.dart index dd2a875..4afcf43 100644 --- a/lib/core/source/user_remote_source.dart +++ b/lib/core/source/user_remote_source.dart @@ -7,11 +7,10 @@ class UserRemoteSource { UserRemoteSource(); - Future createUser(String id, Player data, String createdBy) async { + Future createUser(String id, Player data) async { await _firestoreDb.insert( id: id, data: data.toJson(), - createdBy: createdBy, ); } @@ -19,21 +18,17 @@ class UserRemoteSource { .getData(id) .then((value) => value == null ? null : Player.fromJson(value)); - Future updateUser(String id, Player data, String createdBy) => - _firestoreDb.update( + Future updateUser(String id, Player data) => _firestoreDb.update( id: id, data: data.toJson(), - createdBy: createdBy, ); Future deleteUser(String id) async { await _firestoreDb.delete(id); } - Future> getAllUsers(String createdBy) async => - (await _firestoreDb.getAllData(createdBy)) - .map((e) => Player.fromJson(e)) - .toList(); + Future> getAllUsers() async => + (await _firestoreDb.getAllData()).map((e) => Player.fromJson(e)).toList(); Future close() => _firestoreDb.close(); } diff --git a/pubspec.yaml b/pubspec.yaml index ba90f14..2997b3b 100644 --- a/pubspec.yaml +++ b/pubspec.yaml @@ -89,7 +89,6 @@ flutter: generate: true uses-material-design: true assets: - - assets/images/ - assets/models/ - assets/audio/ - environments/