-
-
Save soraef/fd3a7ae09a07f51ca33bc9a07aa30f1b to your computer and use it in GitHub Desktop.
FirestoreとDartでRole管理
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import 'package:firebase_core/firebase_core.dart'; | |
import 'package:cloud_firestore/cloud_firestore.dart'; | |
import 'dart:math' as math; | |
void main() async { | |
await Firebase.initializeApp( | |
options: const FirebaseOptions( | |
apiKey: "", | |
authDomain: "", | |
projectId: "", | |
storageBucket: "", | |
messagingSenderId: "", | |
appId: "", | |
measurementId: ""), | |
); | |
final roomRole = Role(id: "1", name: "room1", roles: { | |
"user1": ["write"], | |
"user2": ["read"], | |
"user3": ["write"], | |
}); | |
final roomRole2 = Role(id: "2", name: "room2", roles: { | |
"user1": ["read"], | |
"user2": ["write"], | |
"user3": ["read", "write"], | |
"user4": ["read", "write"], | |
}); | |
print("> room1 に write 権限があるのは以下のユーザーです"); | |
print(roomRole.userIds("write")); | |
await save(roomRole); | |
await save(roomRole2); | |
await printRole("user1", "read"); | |
print("> room1 に user1 の read 権限を付与します\n"); | |
await addRole(roleId: roomRole.id, userId: "user1", roles: ["read"]); | |
await printRole("user1", "read"); | |
print("> room1 から user1 の read 権限を剥奪します\n"); | |
await removeRole(roleId: roomRole.id, userId: "user1", roles: ["read"]); | |
await printRole("user1", "read"); | |
} | |
// 実行例 | |
// > room1 に write 権限があるのは以下のユーザーです | |
// (user1, user3) | |
// > user1 が read 権限を持つのは以下の部屋です | |
// (room2) | |
// > room1 に user1 の read 権限を付与します | |
// > user1 が read 権限を持つのは以下の部屋です | |
// (room1, room2) | |
// > room1 から user1 の read 権限を剥奪します | |
// > user1 が read 権限を持つのは以下の部屋です | |
// (room2) | |
Future<void> printRole(String userId, String role) async { | |
print("> $userId が $role 権限を持つのは以下の部屋です"); | |
final names = await findRoom(userId, role); | |
print(names); | |
print(""); | |
} | |
Future<void> save(Role role) async { | |
await FirebaseFirestore.instance | |
.collection("room_roles") | |
.doc(role.id) | |
.set(role.toJson()); | |
} | |
Future<Iterable<String>> findRoom(String userId, String role) async { | |
final query = FirebaseFirestore.instance | |
.collection("room_roles") | |
.where("roles.$userId", arrayContains: role); | |
final snapshot = await query.get(); | |
return snapshot.docs.map((e) => e.data()["name"]); | |
} | |
Future<void> addRole({ | |
required String roleId, | |
required String userId, | |
required List<String> roles, | |
}) async { | |
await FirebaseFirestore.instance | |
.collection("room_roles") | |
.doc(roleId) | |
.update({"roles.$userId": FieldValue.arrayUnion(roles)}); | |
} | |
Future<void> removeRole({ | |
required String roleId, | |
required String userId, | |
required List<String> roles, | |
}) async { | |
await FirebaseFirestore.instance | |
.collection("room_roles") | |
.doc(roleId) | |
.update({"roles.$userId": FieldValue.arrayRemove(roles)}); | |
} | |
class Role { | |
final String id; | |
final String name; | |
final Map<String, List<String>> roles; | |
Role({ | |
required this.id, | |
required this.name, | |
required this.roles, | |
}); | |
Map<String, dynamic> toJson() { | |
return { | |
"id": id, | |
"roles": roles, | |
"name": name, | |
}; | |
} | |
Iterable<String> userIds(String role) { | |
return roles.entries.where((r) => r.value.contains(role)).map((r) => r.key); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment