Ajout méthode CRUD pour postgres
continuous-integration/drone/push Build is passing Details

adminDb
Corentin RICHARD 3 years ago
parent 648e2bbd24
commit b5e90789ca

@ -0,0 +1,94 @@
import 'dart:convert';
import 'dart:io';
import 'package:postgres/postgres.dart';
class AccountsToPostgres{
final connection = PostgreSQLConnection("localhost", 5432, 'passworld',username: 'pass',password: '1p2a3s4s5');
AccountsToPostgres(){
initConnection();
}
void initConnection()async{
await connection.open().then((value){
print("PostgreSQL connection opened");
});
}
@override
void create(String id,String hash,String salt,List<String> twoFaStr, File passwordFile ) async {
List<int> passwordBlob = utf8.encode( await passwordFile.readAsString(encoding: utf8));
connection.query("INSERT INTO \"Account\" VALUES(@id,@hash,@salt,@twofa,@passwords)",substitutionValues: {
"id" : id,
"hash" : hash,
"salt" : salt,
"twofa" : twoFaStr,
"passwords" : passwordBlob
});
}
@override
Future<String> selectHashById(String id) async {
List<List<dynamic>> results = await connection.query("SELECT hash FROM \"Account\" WHERE id=@identifiant",substitutionValues: {
"identifiant" : id
});
connection.close();
return results[0][0];
}
@override
void updatePass(String identifiant,String hash,String salt) async {
if(selectHashById(identifiant)==null){
return;
}else{
await connection.query("UPDATE \"Account\" SET hash=@h, salt=@s WHERE id=@identifiant",substitutionValues: {
"identifiant" : identifiant,
"h" : hash,
"s" : salt
});
}
}
@override
void updateFilePass(String identifiant, File passwordFile) async{
List<int> passwordBlob = utf8.encode( await passwordFile.readAsString(encoding: utf8));
if(selectHashById(identifiant)==null){
return;
}else{
await connection.query("UPDATE \"Account\" SET passwords=@p WHERE id=@identifiant",substitutionValues: {
"identifiant" : identifiant,
"p" : passwordBlob
});
}
}
@override
void updateTwoFa(String identifiant,List<String> tfa) async {
List<String> twoFaStr = List.empty(growable: true);
if(selectHashById(identifiant)==null){
return;
}else{
await connection.query("UPDATE \"Account\" SET twofa=@tfa WHERE id=@identifiant",substitutionValues: {
"identifiant" : identifiant,
"tfa" : tfa
});
}
}
@override
void DeleteById(String id) async{
await connection.query("DELETE FROM \"Account\" WHERE id=@identifiant",substitutionValues: {
"identifiant" : id
});
}
}

@ -36,6 +36,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.1"
buffer:
dependency: transitive
description:
name: buffer
url: "https://pub.dartlang.org"
source: hosted
version: "1.1.1"
collection:
dependency: transitive
description:
@ -183,6 +190,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.8.2"
pedantic:
dependency: transitive
description:
name: pedantic
url: "https://pub.dartlang.org"
source: hosted
version: "1.11.1"
pool:
dependency: transitive
description:
@ -190,6 +204,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.5.1"
postgres:
dependency: "direct main"
description:
name: postgres
url: "https://pub.dartlang.org"
source: hosted
version: "2.5.2"
pub_semver:
dependency: transitive
description:
@ -197,6 +218,20 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "2.1.3"
sasl_scram:
dependency: transitive
description:
name: sasl_scram
url: "https://pub.dartlang.org"
source: hosted
version: "0.1.1"
saslprep:
dependency: transitive
description:
name: saslprep
url: "https://pub.dartlang.org"
source: hosted
version: "1.0.2"
shelf:
dependency: "direct main"
description:
@ -309,6 +344,13 @@ packages:
url: "https://pub.dartlang.org"
source: hosted
version: "1.3.1"
unorm_dart:
dependency: transitive
description:
name: unorm_dart
url: "https://pub.dartlang.org"
source: hosted
version: "0.2.0"
vm_service:
dependency: transitive
description:

@ -9,6 +9,7 @@ environment:
dependencies:
args: ^2.0.0
path: ^1.8.2
postgres: ^2.5.2
shelf: ^1.1.0
shelf_router: ^1.0.0

Loading…
Cancel
Save